@bloxwap/hig 0.7.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (272) hide show
  1. package/README.md +2 -2
  2. package/chart/README.md +13 -2
  3. package/chart/dist/altimeter-burst.d.ts +4 -0
  4. package/chart/dist/altimeter-burst.js +43 -0
  5. package/chart/dist/altimeter-chart.d.ts +52 -0
  6. package/chart/dist/altimeter-chart.js +275 -0
  7. package/chart/dist/altimeter-model.d.ts +43 -0
  8. package/chart/dist/altimeter-model.js +148 -0
  9. package/chart/dist/altimeter-targets.d.ts +33 -0
  10. package/chart/dist/altimeter-targets.js +88 -0
  11. package/chart/dist/bitcoin-price.d.ts +8 -0
  12. package/chart/dist/bitcoin-price.js +41 -0
  13. package/chart/dist/champions-league.d.ts +19 -0
  14. package/chart/dist/champions-league.js +104 -0
  15. package/chart/dist/index.d.ts +6 -0
  16. package/chart/dist/index.js +5 -0
  17. package/chart/dist/outcome-chart.d.ts +37 -0
  18. package/chart/dist/outcome-chart.js +212 -0
  19. package/chart/dist/outcome-model.d.ts +43 -0
  20. package/chart/dist/outcome-model.js +71 -0
  21. package/chart/dist/plus-grid.d.ts +6 -0
  22. package/chart/dist/plus-grid.js +29 -0
  23. package/contracts/README.md +1 -1
  24. package/contracts/dist/avatar.d.ts +68 -0
  25. package/contracts/dist/avatar.js +93 -0
  26. package/contracts/dist/confetti.d.ts +174 -0
  27. package/contracts/dist/confetti.js +138 -0
  28. package/contracts/dist/decimal.d.ts +7 -0
  29. package/contracts/dist/decimal.js +35 -0
  30. package/contracts/dist/index.d.ts +29 -2
  31. package/contracts/dist/index.js +123 -4
  32. package/contracts/dist/invariants.d.ts +17 -0
  33. package/contracts/dist/invariants.js +24 -0
  34. package/contracts/dist/login.d.ts +34 -0
  35. package/contracts/dist/login.js +43 -0
  36. package/contracts/dist/messages.d.ts +57 -3
  37. package/contracts/dist/messages.js +20 -2
  38. package/contracts/dist/number-flow.d.ts +90 -0
  39. package/contracts/dist/number-flow.js +150 -0
  40. package/contracts/dist/qr.d.ts +151 -0
  41. package/contracts/dist/qr.js +581 -0
  42. package/contracts/dist/toast.d.ts +168 -0
  43. package/contracts/dist/toast.js +171 -0
  44. package/feedback/README.md +1 -1
  45. package/feedback/dist/contract.js +1 -1
  46. package/feedback/dist/react.js +2 -4
  47. package/native/README.md +6 -1
  48. package/native/dist/account-block.d.ts +10 -1
  49. package/native/dist/account-block.js +17 -7
  50. package/native/dist/animated-number-model.d.ts +41 -1
  51. package/native/dist/animated-number-model.js +201 -23
  52. package/native/dist/animated-number.d.ts +39 -1
  53. package/native/dist/animated-number.js +164 -17
  54. package/native/dist/announce.d.ts +2 -0
  55. package/native/dist/announce.js +10 -0
  56. package/native/dist/asset-balance-row.d.ts +3 -1
  57. package/native/dist/asset-balance-row.js +15 -4
  58. package/native/dist/assets-block.d.ts +115 -0
  59. package/native/dist/assets-block.js +87 -0
  60. package/native/dist/async-result.d.ts +2 -0
  61. package/native/dist/async-result.js +11 -0
  62. package/native/dist/avatar-frame.d.ts +29 -0
  63. package/native/dist/avatar-frame.js +56 -0
  64. package/native/dist/avatar.d.ts +21 -0
  65. package/native/dist/avatar.js +31 -0
  66. package/native/dist/bar-mark.d.ts +2 -0
  67. package/native/dist/bar-mark.js +4 -2
  68. package/native/dist/bar-pnl.js +2 -1
  69. package/native/dist/blocks.d.ts +9 -2
  70. package/native/dist/blocks.js +8 -1
  71. package/native/dist/brand.js +2 -1
  72. package/native/dist/button.d.ts +7 -2
  73. package/native/dist/button.js +13 -3
  74. package/native/dist/checkbox.js +2 -1
  75. package/native/dist/confetti.d.ts +33 -0
  76. package/native/dist/confetti.js +143 -0
  77. package/native/dist/confirm-block.d.ts +98 -0
  78. package/native/dist/confirm-block.js +48 -0
  79. package/native/dist/data.d.ts +1 -1
  80. package/native/dist/data.js +5 -1
  81. package/native/dist/deposit-block.d.ts +19 -0
  82. package/native/dist/deposit-block.js +17 -0
  83. package/native/dist/explainer-block.d.ts +91 -0
  84. package/native/dist/explainer-block.js +34 -0
  85. package/native/dist/field.d.ts +5 -1
  86. package/native/dist/field.js +27 -6
  87. package/native/dist/gesture-sheet-model.d.ts +50 -0
  88. package/native/dist/gesture-sheet-model.js +105 -0
  89. package/native/dist/gesture-sheet-next.d.ts +82 -0
  90. package/native/dist/gesture-sheet-next.js +315 -0
  91. package/native/dist/gesture-sheet.d.ts +5 -1
  92. package/native/dist/gesture-sheet.js +13 -6
  93. package/native/dist/icons.d.ts +9 -6
  94. package/native/dist/icons.js +25 -24
  95. package/native/dist/language-block.d.ts +10 -1
  96. package/native/dist/language-block.js +11 -4
  97. package/native/dist/list-group.d.ts +3 -1
  98. package/native/dist/list-group.js +26 -13
  99. package/native/dist/login-block.d.ts +4 -0
  100. package/native/dist/login-block.js +22 -0
  101. package/native/dist/manage-position-block.d.ts +8 -1
  102. package/native/dist/manage-position-block.js +7 -2
  103. package/native/dist/market-row.js +19 -8
  104. package/native/dist/navigation.js +5 -3
  105. package/native/dist/number-format.d.ts +6 -0
  106. package/native/dist/number-format.js +9 -0
  107. package/native/dist/pickers.d.ts +8 -0
  108. package/native/dist/pickers.js +3 -3
  109. package/native/dist/position-block.d.ts +5 -5
  110. package/native/dist/preset-group.d.ts +31 -0
  111. package/native/dist/preset-group.js +39 -0
  112. package/native/dist/preset-picker-block.d.ts +107 -0
  113. package/native/dist/preset-picker-block.js +43 -0
  114. package/native/dist/profile-block.d.ts +10 -1
  115. package/native/dist/profile-block.js +8 -3
  116. package/native/dist/provider.d.ts +5 -2
  117. package/native/dist/provider.js +27 -5
  118. package/native/dist/qr-code.d.ts +33 -0
  119. package/native/dist/qr-code.js +29 -0
  120. package/native/dist/radio-group.d.ts +19 -7
  121. package/native/dist/radio-group.js +39 -10
  122. package/native/dist/reward-block.d.ts +10 -7
  123. package/native/dist/reward-block.js +9 -4
  124. package/native/dist/screen-header.d.ts +13 -3
  125. package/native/dist/screen-header.js +12 -4
  126. package/native/dist/select.js +4 -2
  127. package/native/dist/selection.d.ts +2 -1
  128. package/native/dist/selection.js +1 -0
  129. package/native/dist/setting-switch.d.ts +1 -1
  130. package/native/dist/setting-switch.js +7 -3
  131. package/native/dist/slider.js +5 -1
  132. package/native/dist/sound-haptics-block.js +1 -1
  133. package/native/dist/status.d.ts +41 -9
  134. package/native/dist/status.js +34 -17
  135. package/native/dist/sticky-header.js +9 -1
  136. package/native/dist/swap-amount-card.d.ts +57 -0
  137. package/native/dist/swap-amount-card.js +31 -0
  138. package/native/dist/{swap-panel-block.d.ts → swap-block.d.ts} +18 -8
  139. package/native/dist/{swap-panel-block.js → swap-block.js} +63 -35
  140. package/native/dist/swap-parts.d.ts +3 -10
  141. package/native/dist/swap-parts.js +22 -24
  142. package/native/dist/swap-shared.d.ts +2 -2
  143. package/native/dist/swap-shared.js +10 -6
  144. package/native/dist/swap-sheet.d.ts +3 -3
  145. package/native/dist/swap-sheet.js +3 -3
  146. package/native/dist/swap-views.js +1 -1
  147. package/native/dist/target-block.d.ts +7 -1
  148. package/native/dist/target-block.js +13 -6
  149. package/native/dist/toast.d.ts +47 -0
  150. package/native/dist/toast.js +386 -0
  151. package/native/dist/token-action-block.d.ts +11 -2
  152. package/native/dist/token-action-block.js +10 -4
  153. package/native/dist/trade-history-list-block.js +7 -4
  154. package/native/dist/wallet-address-core.js +6 -3
  155. package/native-fonts/README.md +1 -1
  156. package/og-fonts/README.md +1 -1
  157. package/package.json +155 -11
  158. package/react/README.md +3 -1
  159. package/react/dist/animated-number-flow.d.ts +72 -0
  160. package/react/dist/animated-number-flow.js +316 -0
  161. package/react/dist/animated-number-model.d.ts +18 -0
  162. package/react/dist/animated-number-model.js +75 -5
  163. package/react/dist/animated-number.d.ts +45 -1
  164. package/react/dist/animated-number.js +137 -6
  165. package/react/dist/asset-balance-row.d.ts +3 -1
  166. package/react/dist/asset-balance-row.js +10 -4
  167. package/react/dist/async-result.d.ts +2 -0
  168. package/react/dist/async-result.js +11 -0
  169. package/react/dist/avatar.d.ts +25 -0
  170. package/react/dist/avatar.js +50 -0
  171. package/react/dist/blocks/account-block.d.ts +5 -1
  172. package/react/dist/blocks/account-block.js +11 -6
  173. package/react/dist/blocks/assets-block.d.ts +106 -0
  174. package/react/dist/blocks/assets-block.js +135 -0
  175. package/react/dist/blocks/confirm-block.d.ts +91 -0
  176. package/react/dist/blocks/confirm-block.js +31 -0
  177. package/react/dist/blocks/deposit-block.d.ts +19 -0
  178. package/react/dist/blocks/deposit-block.js +15 -0
  179. package/react/dist/blocks/explainer-block.d.ts +80 -0
  180. package/react/dist/blocks/explainer-block.js +20 -0
  181. package/react/dist/blocks/install-command.js +3 -1
  182. package/react/dist/blocks/language-block.d.ts +5 -1
  183. package/react/dist/blocks/language-block.js +4 -3
  184. package/react/dist/blocks/login-block.d.ts +4 -0
  185. package/react/dist/blocks/login-block.js +14 -0
  186. package/react/dist/blocks/manage-position-block.d.ts +3 -1
  187. package/react/dist/blocks/manage-position-block.js +2 -2
  188. package/react/dist/blocks/market-row.js +8 -3
  189. package/react/dist/blocks/notifications-block.d.ts +3 -1
  190. package/react/dist/blocks/notifications-block.js +2 -2
  191. package/react/dist/blocks/pickers.d.ts +9 -0
  192. package/react/dist/blocks/pickers.js +4 -2
  193. package/react/dist/blocks/position-block.d.ts +3 -3
  194. package/react/dist/blocks/preset-picker-block.d.ts +85 -0
  195. package/react/dist/blocks/preset-picker-block.js +19 -0
  196. package/react/dist/blocks/profile-block.d.ts +5 -1
  197. package/react/dist/blocks/profile-block.js +12 -13
  198. package/react/dist/blocks/reward-block.d.ts +13 -8
  199. package/react/dist/blocks/reward-block.js +7 -4
  200. package/react/dist/blocks/screen-header.d.ts +10 -2
  201. package/react/dist/blocks/screen-header.js +7 -4
  202. package/react/dist/blocks/setting-switch.js +6 -3
  203. package/react/dist/blocks/signals-block.d.ts +3 -1
  204. package/react/dist/blocks/signals-block.js +4 -3
  205. package/react/dist/blocks/sound-haptics-block.d.ts +3 -1
  206. package/react/dist/blocks/sound-haptics-block.js +2 -2
  207. package/react/dist/blocks/sticky-header.d.ts +14 -4
  208. package/react/dist/blocks/sticky-header.js +68 -8
  209. package/react/dist/blocks/swap/settings.js +2 -1
  210. package/react/dist/blocks/swap/shared.d.ts +22 -22
  211. package/react/dist/blocks/swap/shared.js +30 -28
  212. package/react/dist/blocks/{swap-panel-block.d.ts → swap-block.d.ts} +19 -8
  213. package/react/dist/blocks/swap-block.js +215 -0
  214. package/react/dist/blocks/swap-sheet.d.ts +3 -3
  215. package/react/dist/blocks/swap-sheet.js +3 -3
  216. package/react/dist/blocks/target-block.d.ts +7 -1
  217. package/react/dist/blocks/target-block.js +10 -4
  218. package/react/dist/blocks/token-action-block.d.ts +6 -2
  219. package/react/dist/blocks/token-action-block.js +3 -2
  220. package/react/dist/blocks/trade-history-detail-block.d.ts +3 -1
  221. package/react/dist/blocks/trade-history-detail-block.js +2 -2
  222. package/react/dist/blocks/trade-history-list-block.d.ts +3 -1
  223. package/react/dist/blocks/trade-history-list-block.js +7 -4
  224. package/react/dist/blocks.d.ts +8 -2
  225. package/react/dist/blocks.js +7 -1
  226. package/react/dist/brand.js +2 -1
  227. package/react/dist/button.d.ts +9 -3
  228. package/react/dist/button.js +8 -3
  229. package/react/dist/confetti.d.ts +27 -0
  230. package/react/dist/confetti.js +240 -0
  231. package/react/dist/data.js +8 -1
  232. package/react/dist/field.d.ts +6 -1
  233. package/react/dist/field.js +18 -9
  234. package/react/dist/gesture-sheet.d.ts +15 -1
  235. package/react/dist/gesture-sheet.js +53 -8
  236. package/react/dist/list-group.js +11 -7
  237. package/react/dist/navigation.js +11 -3
  238. package/react/dist/overlay.d.ts +1 -4
  239. package/react/dist/overlay.js +1 -3
  240. package/react/dist/popover.d.ts +30 -0
  241. package/react/dist/popover.js +34 -0
  242. package/react/dist/preset-group.d.ts +30 -0
  243. package/react/dist/preset-group.js +13 -0
  244. package/react/dist/provider.d.ts +2 -2
  245. package/react/dist/provider.js +23 -6
  246. package/react/dist/qr-code.d.ts +32 -0
  247. package/react/dist/qr-code.js +28 -0
  248. package/react/dist/select.js +10 -3
  249. package/react/dist/selection.d.ts +13 -5
  250. package/react/dist/selection.js +13 -8
  251. package/react/dist/slider.js +5 -1
  252. package/react/dist/status.d.ts +14 -10
  253. package/react/dist/status.js +17 -9
  254. package/react/dist/styles.css +260 -165
  255. package/react/dist/toast.d.ts +19 -12
  256. package/react/dist/toast.js +29 -7
  257. package/react/dist/wallet-address-core.d.ts +8 -4
  258. package/react/dist/wallet-address-core.js +10 -5
  259. package/tokens/README.md +2 -2
  260. package/tokens/assets/onramps/apple-pay.svg +33 -0
  261. package/tokens/assets/onramps/bank.svg +98 -0
  262. package/tokens/assets/onramps/blink.svg +37 -0
  263. package/tokens/assets/onramps/cashapp.svg +33 -0
  264. package/tokens/assets/onramps/crypto.svg +121 -0
  265. package/tokens/assets/onramps/exchange.svg +105 -0
  266. package/tokens/dist/deposit-icons.d.ts +1 -0
  267. package/tokens/dist/deposit-icons.js +28 -0
  268. package/tokens/dist/index.d.ts +16 -0
  269. package/tokens/dist/index.js +16 -0
  270. package/tokens/dist/manifest.json +8 -0
  271. package/tokens/dist/tokens.css +6 -0
  272. package/react/dist/blocks/swap-panel-block.js +0 -178
@@ -0,0 +1,316 @@
1
+ import { numberFlowEasingCSS, numberFlowGlyph } from '@bloxwap/hig/contracts/number-flow';
2
+ import { numberFlowAnchorSide } from './animated-number-model.js';
3
+ export function numberFlowWebMotion(timings) {
4
+ const web = ({ duration, easing }) => ({ duration, easing: numberFlowEasingCSS(easing), fallback: numberFlowEasingCSS(easing, false) });
5
+ return { spin: web(timings.spin), transform: web(timings.transform), opacity: web(timings.opacity) };
6
+ }
7
+ const part = '[data-hig-flow-key]', drawnDigit = '.hig-number-flow-digit';
8
+ // A browser without `linear()` throws on the first animation that asks for it; from then on every animation takes the fallback.
9
+ let sampledEasing = true;
10
+ // Rolls and slides are added to a transform that already holds the resting position. A browser from before 2023 ignores
11
+ // `composite: 'add'` and would replace that transform, rolling the wrong glyphs through the window, so there nothing moves and
12
+ // every value is drawn at once. (No `KeyframeEffect` at all is a test double for `animate`, which is taken at its word.)
13
+ const additive = () => typeof KeyframeEffect === 'undefined' || 'composite' in KeyframeEffect.prototype;
14
+ // A turn or a slide that is alone on its element does not need to be added to anything: it is the same motion written as a
15
+ // plain animation from where the element is to where it rests. That matters in Chromium, which keeps additive animations on
16
+ // the main thread (a style recalculation every frame) and hands plain ones to the compositor, where a busy page cannot make
17
+ // them stutter. When a second movement joins it, the first is swapped for its additive twin on the same clock, which takes a
18
+ // start time that can be set; where an animation has none (a test double), every movement is additive from the start.
19
+ const retimable = () => typeof Animation !== 'undefined' && 'startTime' in Animation.prototype;
20
+ // One commit can change many figures, and each reads where its parts are before it moves them. Taken figure by figure, every
21
+ // read that follows another figure's writes lays the page out again: forty figures, forty layouts. So a figure that is about to
22
+ // change says so before the commit (`expect`), what it would write is held, and when the last of them has read (`done`)
23
+ // everything held is written in the order it was asked for: one layout for the commit, however many figures changed. A figure
24
+ // that never reports back costs only the wait, since a microtask (still before the next paint) writes what is held.
25
+ let expected = 0, scheduled = false, held = [];
26
+ function release() { expected = 0; scheduled = false; const writes = held; held = []; for (const write of writes)
27
+ write(); }
28
+ export const numberFlowCommit = {
29
+ /** A figure will read and write in the commit that is starting. */
30
+ expect() { expected++; },
31
+ /** That figure has read all it needs: the last one to say so writes for all of them. */
32
+ done() { if (expected > 0 && --expected === 0)
33
+ release(); }
34
+ };
35
+ /**
36
+ * NumberFlow's motion over elements React drew. React owns the final text of every part; this adds what moves, and removes it
37
+ * when it lands, so nothing here survives into a render:
38
+ * - a window over a turning place and, inside it, a strip of glyphs that rolls through every digit between the old and the new;
39
+ * - a sideways slide for each part whose position changed (first, last, invert, play), so a figure that gains or loses a
40
+ * place closes up instead of jumping;
41
+ * - a fade for parts that arrive, and a fading copy of each part that left;
42
+ * - a clip on the digits (first digit to last, with the separators between) that travels from their old edges to their new
43
+ * ones, so a place arriving or leaving passes under the edge rather than over the sign or the text beside it.
44
+ * A roll or a slide that joins another in flight on the same element is an additive animation on `transform`, so a new value
45
+ * joins the motion instead of restarting it. After the commit that starts it there is no script and no layout per frame.
46
+ * Chromium does not hand an additive animation to its compositor, though: it runs on the main thread, one style recalculation
47
+ * a frame for the parts that move (55 for one 900 ms roll against 6 for a plain one), so a movement that is alone on its
48
+ * element, which is most of them, is written as a plain animation instead (see `retimable`). Fades animate `opacity`, and
49
+ * the clip animates `clip-path` only while the figure changes width.
50
+ */
51
+ export class NumberFlowEngine {
52
+ root = null;
53
+ before = null;
54
+ extent = null;
55
+ clip = null;
56
+ reels = new Map();
57
+ slides = new Map();
58
+ running = new Set();
59
+ fades = new Map();
60
+ ghosts = 0;
61
+ epoch = 0;
62
+ /** The element holding the parts. Fading copies are drawn in the empty layers React left in it: one inside the digits, clipped with them, and one for the signs and affixes around them. */
63
+ attach(root) { this.root = root; }
64
+ /** Whether anything is still moving. */
65
+ get busy() { return this.running.size > 0 || this.reels.size > 0; }
66
+ /** First: where every part is now, read before the commit that changes them. */
67
+ measure() {
68
+ const root = this.root;
69
+ if (!root) {
70
+ this.before = null;
71
+ return;
72
+ }
73
+ const before = new Map(), core = root.querySelector('.hig-number-flow-digits')?.getBoundingClientRect();
74
+ this.extent = core ? { left: core.left, right: core.right } : null;
75
+ for (const element of root.querySelectorAll(part)) {
76
+ const rect = element.getBoundingClientRect(), shown = element.querySelector(drawnDigit) ?? element;
77
+ before.set(element.dataset.higFlowKey, { element, left: rect.left, top: rect.top, opacity: this.fades.has(element) ? Number(getComputedStyle(element).opacity) || 0 : 1, text: shown.textContent ?? '' });
78
+ }
79
+ this.before = before;
80
+ }
81
+ /** Last, invert, play: slide every part from where it was, fade in what arrived and fade out a copy of what left. Does nothing without a `measure` since the last call. */
82
+ flip(motion) {
83
+ const root = this.root, before = this.before, extent = this.extent;
84
+ this.before = null;
85
+ this.extent = null;
86
+ if (!root || !before || !additive())
87
+ return;
88
+ const core = root.querySelector('.hig-number-flow-digits'), layers = { digits: core?.querySelector(':scope > .hig-number-flow-ghosts') ?? null, around: root.querySelector(':scope > .hig-number-flow-ghosts') };
89
+ // Read everything first, then write, so the commit costs one layout.
90
+ const now = Array.from(root.querySelectorAll(part)), keys = now.map(element => element.dataset.higFlowKey), present = new Set(keys), rects = now.map(element => element.getBoundingClientRect()), edges = core?.getBoundingClientRect(), origins = { digits: layers.digits?.getBoundingClientRect(), around: layers.around?.getBoundingClientRect() };
91
+ const shift = new Map();
92
+ now.forEach((_, index) => { const prior = before.get(keys[index]); if (prior && !keys[index].startsWith('ghost:'))
93
+ shift.set(keys[index], prior.left - rects[index].left); });
94
+ // Everything from here on writes.
95
+ this.write(() => {
96
+ // A part with no place of its own in both readings travels with its nearest neighbour that has one, on the side it is anchored to.
97
+ const travel = (order, index, side) => { for (const direction of [side, -side])
98
+ for (let at = index + direction; at >= 0 && at < order.length; at += direction) {
99
+ const dx = shift.get(order[at]);
100
+ if (dx !== undefined)
101
+ return dx;
102
+ } return 0; };
103
+ const slide = (element, dx) => {
104
+ if (Math.abs(dx) <= 0.01)
105
+ return;
106
+ const frames = [`translateX(${dx}px)`, 'translateX(0px)'], flight = this.join(element, this.slides.get(element) ?? []), alone = !flight.length && retimable();
107
+ const move = { delta: dx, frames, animation: null, timing: motion.transform, alone, landed: () => { const rest = (this.slides.get(element) ?? []).filter(other => other !== move); if (rest.length)
108
+ this.slides.set(element, rest);
109
+ else
110
+ this.slides.delete(element); } };
111
+ // A part rests where the layout puts it, so alone its slide is these same keyframes without the addition.
112
+ move.animation = this.play(element, { transform: frames }, motion.transform, alone ? {} : { composite: 'add' }, move.landed);
113
+ if (move.animation)
114
+ flight.push(move);
115
+ if (flight.length)
116
+ this.slides.set(element, flight);
117
+ else
118
+ this.slides.delete(element);
119
+ };
120
+ now.forEach((element, index) => {
121
+ const key = keys[index], prior = before.get(key);
122
+ if (prior) {
123
+ slide(element, prior.left - rects[index].left);
124
+ return;
125
+ }
126
+ slide(element, travel(keys, index, numberFlowAnchorSide(key)));
127
+ const fade = this.play(element, { opacity: [0, 1] }, motion.opacity, {}, () => this.fades.delete(element));
128
+ if (fade)
129
+ this.fades.set(element, fade);
130
+ });
131
+ const order = Array.from(before.keys());
132
+ order.forEach((key, index) => {
133
+ const prior = before.get(key);
134
+ if (present.has(key))
135
+ return;
136
+ const reel = this.reels.get(prior.element);
137
+ if (reel)
138
+ this.settle(prior.element, reel);
139
+ const kind = /^(?:digit|sep):/.test(key) ? 'digits' : 'around', layer = layers[kind], origin = origins[kind];
140
+ if (key.startsWith('ghost:') || !prior.text || !layer || !origin)
141
+ return;
142
+ const dx = travel(order, index, numberFlowAnchorSide(key)), ghost = document.createElement('span');
143
+ ghost.className = `${prior.element.className} hig-number-flow-ghost`;
144
+ ghost.dataset.higFlowKey = `ghost:${++this.ghosts}`;
145
+ ghost.textContent = prior.text;
146
+ ghost.style.left = `${prior.left - origin.left - dx}px`;
147
+ ghost.style.top = `${prior.top - origin.top}px`;
148
+ layer.appendChild(ghost);
149
+ slide(ghost, dx);
150
+ if (!this.play(ghost, { opacity: [prior.opacity, 0] }, motion.opacity, { fill: 'forwards' }, () => ghost.remove()))
151
+ ghost.remove();
152
+ });
153
+ // The digits' clip starts at their old edges and travels to their new ones with the parts.
154
+ if (core && edges && extent) {
155
+ const left = extent.left - edges.left, right = edges.right - extent.right;
156
+ if (Math.abs(left) > 0.01 || Math.abs(right) > 0.01) {
157
+ if (this.clip)
158
+ this.stop(this.clip);
159
+ this.clip = this.play(core, { clipPath: [`inset(-1em ${right}px -1em ${left}px)`, 'inset(-1em 0px -1em 0px)'] }, motion.transform, {}, () => { this.clip = null; });
160
+ }
161
+ }
162
+ });
163
+ }
164
+ /** Turn the places whose digits changed. `glyphs` are the ten digits of the reading's numbering system, zero first. */
165
+ spin(spins, glyphs, timing) {
166
+ const root = this.root;
167
+ if (!root || !spins.size || !additive())
168
+ return;
169
+ this.write(() => {
170
+ for (const place of root.querySelectorAll('[data-hig-flow-digit]')) {
171
+ const spin = spins.get(place.dataset.higFlowKey);
172
+ if (!spin || !spin.delta)
173
+ continue;
174
+ let reel = this.reels.get(place);
175
+ if (reel && reel.length !== spin.length) {
176
+ this.settle(place, reel);
177
+ reel = undefined;
178
+ }
179
+ if (!reel) {
180
+ const frame = document.createElement('span'), strip = document.createElement('span');
181
+ frame.className = 'hig-number-flow-window';
182
+ strip.className = 'hig-number-flow-strip';
183
+ frame.appendChild(strip);
184
+ place.appendChild(frame);
185
+ place.dataset.spinning = '';
186
+ reel = { frame, strip, origin: spin.from, target: spin.from, length: spin.length, glyphs: new Map(), active: [] };
187
+ this.reels.set(place, reel);
188
+ }
189
+ const turning = reel, target = turning.target + spin.delta;
190
+ // A glyph is put on its row with a transform, the unit the strip travels in: WebKit resolves a transform's percentages against
191
+ // the box snapped to device pixels and `top` against the unsnapped one, so rows placed with `top` drift from the strip's
192
+ // travel by up to half a device pixel a glyph, and the digit would jump by that much when the reel is taken away.
193
+ // The strip holds the glyphs between wherever the place can be drawn right now (its target less what is still in flight) and
194
+ // its new target. A place that keeps turning the same way faster than it settles would grow without bound, so past four
195
+ // turns of the reel the turns in flight land at once and this one starts from the last target.
196
+ let low = turning.target, high = turning.target;
197
+ for (const turn of turning.active) {
198
+ if (turn.delta > 0)
199
+ low -= turn.delta;
200
+ else
201
+ high -= turn.delta;
202
+ }
203
+ low = Math.min(low, target);
204
+ high = Math.max(high, target);
205
+ if (high - low > turning.length * 4) {
206
+ for (const turn of turning.active)
207
+ this.stop(turn.animation);
208
+ turning.active = [];
209
+ low = Math.min(turning.target, target);
210
+ high = Math.max(turning.target, target);
211
+ }
212
+ for (const [position, glyph] of turning.glyphs)
213
+ if (position < low || position > high) {
214
+ glyph.remove();
215
+ turning.glyphs.delete(position);
216
+ }
217
+ for (let position = low; position <= high; position++)
218
+ if (!turning.glyphs.has(position)) {
219
+ const glyph = document.createElement('span');
220
+ glyph.className = 'hig-number-flow-glyph';
221
+ glyph.textContent = glyphs[numberFlowGlyph(position, turning.length)] ?? '';
222
+ glyph.style.transform = `translateY(${(position - turning.origin) * 100}%)`;
223
+ turning.strip.appendChild(glyph);
224
+ turning.glyphs.set(position, glyph);
225
+ }
226
+ turning.target = target;
227
+ turning.strip.style.transform = `translateY(${-(target - turning.origin) * 100}%)`;
228
+ turning.active = this.join(turning.strip, turning.active);
229
+ const rest = -(target - turning.origin) * 100, frames = [`translateY(${spin.delta * 100}%)`, 'translateY(0%)'], alone = !turning.active.length && retimable();
230
+ const turn = { delta: spin.delta, frames, animation: null, timing, alone, landed: () => { turning.active = turning.active.filter(other => other !== turn); if (!turning.active.length && this.reels.get(place) === turning)
231
+ this.settle(place, turning); } };
232
+ // Alone, the turn is written from where the strip is to where it rests; in company it is added to the strip's resting place.
233
+ turn.animation = alone ? this.play(turning.strip, { transform: [`translateY(${rest + spin.delta * 100}%)`, `translateY(${rest}%)`] }, timing, {}, turn.landed) : this.play(turning.strip, { transform: frames }, timing, { composite: 'add' }, turn.landed);
234
+ if (turn.animation)
235
+ turning.active.push(turn);
236
+ else if (!turning.active.length)
237
+ this.settle(place, turning);
238
+ }
239
+ });
240
+ }
241
+ /** Stop everything where it is going: the parts are left exactly as React drew them. */
242
+ cancel() {
243
+ this.before = null;
244
+ this.extent = null;
245
+ this.clip = null;
246
+ this.epoch++;
247
+ this.slides.clear();
248
+ for (const [place, reel] of Array.from(this.reels))
249
+ this.settle(place, reel);
250
+ for (const animation of Array.from(this.running))
251
+ this.stop(animation);
252
+ this.fades.clear();
253
+ for (const layer of this.root?.querySelectorAll('.hig-number-flow-ghosts') ?? [])
254
+ layer.replaceChildren();
255
+ }
256
+ /** Writes now, or with the rest of the commit when other figures are still to read. A `cancel` in between drops it. */
257
+ write(change) {
258
+ if (expected <= 0) {
259
+ change();
260
+ return;
261
+ }
262
+ const epoch = this.epoch;
263
+ held.push(() => { if (this.epoch === epoch)
264
+ change(); });
265
+ if (!scheduled) {
266
+ scheduled = true;
267
+ void Promise.resolve().then(release);
268
+ }
269
+ }
270
+ /** Makes room on an element for another movement: one in flight alone becomes its additive twin, where it is and on the same clock. Returns what is still in flight. */
271
+ join(element, flight) {
272
+ return flight.filter(first => {
273
+ if (!first.alone)
274
+ return true;
275
+ const twin = this.play(element, { transform: first.frames }, first.timing, { composite: 'add' }, first.landed);
276
+ if (twin) {
277
+ const started = first.animation.startTime, elapsed = first.animation.currentTime;
278
+ if (started !== null)
279
+ twin.startTime = started;
280
+ else if (elapsed !== null)
281
+ twin.currentTime = elapsed;
282
+ }
283
+ this.stop(first.animation);
284
+ first.alone = false;
285
+ if (twin)
286
+ first.animation = twin;
287
+ return !!twin;
288
+ });
289
+ }
290
+ stop(animation) { this.running.delete(animation); animation.onfinish = null; animation.cancel(); }
291
+ settle(place, reel) { for (const turn of reel.active)
292
+ this.stop(turn.animation); reel.active = []; reel.frame.remove(); delete place.dataset.spinning; this.reels.delete(place); }
293
+ play(element, keyframes, timing, options, done) {
294
+ // No duration is no motion: the part is simply where it ends.
295
+ if (timing.duration <= 0 || typeof element.animate !== 'function')
296
+ return null;
297
+ let animation;
298
+ try {
299
+ animation = element.animate(keyframes, { ...options, duration: timing.duration, easing: sampledEasing ? timing.easing : timing.fallback });
300
+ }
301
+ catch {
302
+ if (!sampledEasing)
303
+ return null;
304
+ sampledEasing = false;
305
+ try {
306
+ animation = element.animate(keyframes, { ...options, duration: timing.duration, easing: timing.fallback });
307
+ }
308
+ catch {
309
+ return null;
310
+ }
311
+ }
312
+ this.running.add(animation);
313
+ animation.onfinish = () => { this.running.delete(animation); done?.(); };
314
+ return animation;
315
+ }
316
+ }
@@ -1,3 +1,4 @@
1
+ import type { NumberFlowPlace } from '@bloxwap/hig/contracts/number-flow';
1
2
  export interface AnimatedNumberToken {
2
3
  key: string;
3
4
  text: string;
@@ -10,6 +11,8 @@ export interface AnimatedNumberModel {
10
11
  reading: string;
11
12
  tokens: readonly AnimatedNumberToken[];
12
13
  animatable: boolean;
14
+ /** The ten digit glyphs of the reading's numbering system, zero first, when it moves place by place. A reel rolls through these. */
15
+ digits?: readonly string[];
13
16
  }
14
17
  export interface AnimatedNumberTransitionToken extends AnimatedNumberToken {
15
18
  oldText?: string;
@@ -24,3 +27,18 @@ export declare function createAnimatedNumberModel(value: number | null | undefin
24
27
  */
25
28
  export declare function createFormattedAnimatedNumberModel(text: string, locale: string, animate: boolean): AnimatedNumberModel;
26
29
  export declare function createAnimatedNumberTransitions(previous: AnimatedNumberModel | null, next: AnimatedNumberModel): readonly AnimatedNumberTransitionToken[];
30
+ /** The digits of a model as NumberFlow places: the number's own digits carry their place, an exponent's carry none. */
31
+ export declare function numberFlowPlaces(model: AnimatedNumberModel): NumberFlowPlace[];
32
+ /**
33
+ * A stable key for every token, in order, so a part keeps its element while the figure around it changes. Digits keep their
34
+ * place key. Text before the first digit is keyed outward from the number (`pre:0` sits against it), text after the last
35
+ * digit likewise (`post:0`), and text between digits by the digit that follows it (`sep:number:2` is the separator before
36
+ * the hundreds, `sep:number:-1` the decimal mark), so a separator stays itself when a place is added in front of it.
37
+ */
38
+ export declare function numberFlowTokenKeys(tokens: readonly AnimatedNumberToken[]): string[];
39
+ /**
40
+ * Which way a part looks for the neighbour it travels with when the figure changes width: `1` towards the parts after it
41
+ * (integer places, their separators and what stands before the number, which are anchored at the decimal mark's left), `-1`
42
+ * towards the parts before it (the decimal mark, fraction places and what follows the number).
43
+ */
44
+ export declare function numberFlowAnchorSide(key: string): -1 | 1;
@@ -5,6 +5,24 @@ function digitsFor(formatter) {
5
5
  const glyphs = Array.from({ length: 10 }, (_, digit) => { const text = digitFormatter.format(digit); const parts = digitFormatter.formatToParts(digit); return parts.length === 1 && parts[0].type === 'integer' && parts[0].value === text ? text : ''; });
6
6
  return new Set(glyphs).size === 10 && glyphs.every(Boolean) ? glyphs : null;
7
7
  }
8
+ const keptFormats = new Map();
9
+ let keptFormatsFor;
10
+ function formatFor(locale, options) {
11
+ if (keptFormatsFor !== Intl.NumberFormat) {
12
+ keptFormats.clear();
13
+ keptFormatsFor = Intl.NumberFormat;
14
+ }
15
+ const key = `${locale}\u0000${options ? JSON.stringify(options) : ''}`;
16
+ let kept = keptFormats.get(key);
17
+ if (!kept) {
18
+ if (keptFormats.size >= 64)
19
+ keptFormats.clear();
20
+ kept = { formatter: new Intl.NumberFormat(locale, options) };
21
+ keptFormats.set(key, kept);
22
+ }
23
+ return kept;
24
+ }
25
+ const glyphsFor = (kept) => kept.glyphs === undefined ? kept.glyphs = digitsFor(kept.formatter) : kept.glyphs;
8
26
  function decodeDigits(text, glyphs) {
9
27
  const digits = [];
10
28
  let offset = 0;
@@ -23,13 +41,13 @@ function decodeDigits(text, glyphs) {
23
41
  export function createAnimatedNumberModel(value, locale, options) {
24
42
  if (value == null || !Number.isFinite(value))
25
43
  return staticModel('—');
26
- const formatter = new Intl.NumberFormat(locale, options), reading = formatter.format(value);
44
+ const kept = formatFor(locale, options), formatter = kept.formatter, reading = formatter.format(value);
27
45
  if (Number.isInteger(value) && !Number.isSafeInteger(value))
28
46
  return staticModel(reading);
29
47
  const parts = formatter.formatToParts(value);
30
48
  if (parts.map(part => part.value).join('') !== reading)
31
49
  return staticModel(reading);
32
- const glyphs = digitsFor(formatter);
50
+ const glyphs = glyphsFor(kept);
33
51
  if (!glyphs)
34
52
  return staticModel(reading);
35
53
  const decoded = parts.map(part => part.type === 'integer' || part.type === 'fraction' || part.type === 'exponentInteger' ? decodeDigits(part.value, glyphs) : null);
@@ -57,7 +75,7 @@ export function createAnimatedNumberModel(value, locale, options) {
57
75
  tokens.push({ key: `digit:${section}:${place}`, text: digit.text, digit: true, value: digit.value, place, section });
58
76
  }
59
77
  }
60
- return tokens.some(token => token.digit) ? { reading, tokens, animatable: true } : staticModel(reading);
78
+ return tokens.some(token => token.digit) ? { reading, tokens, animatable: true, digits: glyphs } : staticModel(reading);
61
79
  }
62
80
  /**
63
81
  * A model for host-formatted text. Without `animate` it is the text as one static token. With it, every digit of the text
@@ -70,7 +88,7 @@ export function createFormattedAnimatedNumberModel(text, locale, animate) {
70
88
  const ascii = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
71
89
  let local = null;
72
90
  try {
73
- local = digitsFor(new Intl.NumberFormat(locale));
91
+ local = glyphsFor(formatFor(locale));
74
92
  }
75
93
  catch {
76
94
  local = null;
@@ -90,7 +108,7 @@ export function createFormattedAnimatedNumberModel(text, locale, animate) {
90
108
  return;
91
109
  } flush(); const place = count - ++seen; tokens.push({ key: `digit:number:${place}`, text: character, digit: true, value, place, section: 'number' }); });
92
110
  flush();
93
- return { reading: text, tokens, animatable: true };
111
+ return { reading: text, tokens, animatable: true, digits: local && !values.some((value, index) => value >= 0 && ascii[value] === characters[index]) ? local : ascii };
94
112
  }
95
113
  export function createAnimatedNumberTransitions(previous, next) {
96
114
  if (!previous?.animatable || !next.animatable)
@@ -123,3 +141,55 @@ export function createAnimatedNumberTransitions(previous, next) {
123
141
  }
124
142
  return result;
125
143
  }
144
+ /** The digits of a model as NumberFlow places: the number's own digits carry their place, an exponent's carry none. */
145
+ export function numberFlowPlaces(model) {
146
+ return model.tokens.filter(token => token.digit).map(token => ({ key: token.key, digit: token.value ?? 0, place: token.section === 'number' ? token.place ?? null : null }));
147
+ }
148
+ /**
149
+ * A stable key for every token, in order, so a part keeps its element while the figure around it changes. Digits keep their
150
+ * place key. Text before the first digit is keyed outward from the number (`pre:0` sits against it), text after the last
151
+ * digit likewise (`post:0`), and text between digits by the digit that follows it (`sep:number:2` is the separator before
152
+ * the hundreds, `sep:number:-1` the decimal mark), so a separator stays itself when a place is added in front of it.
153
+ */
154
+ export function numberFlowTokenKeys(tokens) {
155
+ const first = tokens.findIndex(token => token.digit);
156
+ if (first < 0)
157
+ return tokens.map((_, index) => `pre:${tokens.length - 1 - index}`);
158
+ let last = tokens.length - 1;
159
+ while (!tokens[last].digit)
160
+ last--;
161
+ const keys = [], used = new Map();
162
+ for (let index = 0; index < tokens.length; index++) {
163
+ const token = tokens[index];
164
+ if (token.digit) {
165
+ keys.push(token.key);
166
+ continue;
167
+ }
168
+ if (index < first) {
169
+ keys.push(`pre:${first - 1 - index}`);
170
+ continue;
171
+ }
172
+ if (index > last) {
173
+ keys.push(`post:${index - last - 1}`);
174
+ continue;
175
+ }
176
+ let next = index + 1;
177
+ while (!tokens[next].digit)
178
+ next++;
179
+ const base = `sep:${tokens[next].section ?? 'number'}:${tokens[next].place ?? 0}`, count = used.get(base) ?? 0;
180
+ used.set(base, count + 1);
181
+ keys.push(count ? `${base}:${count}` : base);
182
+ }
183
+ return keys;
184
+ }
185
+ /**
186
+ * Which way a part looks for the neighbour it travels with when the figure changes width: `1` towards the parts after it
187
+ * (integer places, their separators and what stands before the number, which are anchored at the decimal mark's left), `-1`
188
+ * towards the parts before it (the decimal mark, fraction places and what follows the number).
189
+ */
190
+ export function numberFlowAnchorSide(key) {
191
+ if (key.startsWith('pre:') || key === 'affix:prefix')
192
+ return 1;
193
+ const match = /^(?:digit|sep):number:(-?\d+)/.exec(key);
194
+ return match && Number(match[1]) >= 0 ? 1 : -1;
195
+ }
@@ -1,12 +1,56 @@
1
1
  import React from 'react';
2
2
  import { type NumberProps } from './data.js';
3
+ import { type NumberFlowDigits, type NumberFlowTiming } from '@bloxwap/hig/contracts/number-flow';
3
4
  export type AnimatedNumberTrend = -1 | 0 | 1 | ((oldValue: number, newValue: number) => -1 | 0 | 1);
5
+ export type { NumberFlowDigits, NumberFlowEasing, NumberFlowTiming } from '@bloxwap/hig/contracts/number-flow';
4
6
  export interface AnimatedNumberProps extends NumberProps {
7
+ /** How long a change takes, in milliseconds. Sliding: 180 by default, at most 600. Flowing: sets the roll and the sideways slide (900 by default) and fades take half of it; the three timings below override it. Zero or less draws the new value at once. */
5
8
  duration?: number;
9
+ /** Which way digits move: `1` up, `-1` down, `0` each digit its own way, or a function of the old and new value. Defaults to the direction the value moved. */
6
10
  trend?: AnimatedNumberTrend;
11
+ /**
12
+ * How a change is drawn. `'flow'` is NumberFlow's motion: every changed digit rolls through the digits between its old and new
13
+ * value, a figure that gains or loses a place slides closed while the place fades, and a new value joins the motion in flight.
14
+ * `'slide'` is the short settle: the old digit leaves by one line as the new one arrives, and a newer value restarts it.
15
+ * `NumberFlow` flows and `AnimatedNumberReadout` slides unless told otherwise. A reading that cannot be laid out place by place
16
+ * (right-to-left letters or bidi controls in the number itself) slides.
17
+ */
18
+ motion?: 'flow' | 'slide';
19
+ /** `false` draws every new value at once, with no motion, as reduced motion does. Use it for typed echoes and until first data arrives. Default `true`. */
20
+ animated?: boolean;
21
+ /** Flowing only: the roll of a changed digit. Defaults to `transformTiming`. */
22
+ spinTiming?: NumberFlowTiming;
23
+ /** Flowing only: the sideways slide when the figure changes width, and the roll unless `spinTiming` is set. Default 900 ms on NumberFlow's curve. */
24
+ transformTiming?: NumberFlowTiming;
25
+ /** Flowing only: the fade of a part that arrives or leaves. Default 450 ms ease-out. */
26
+ opacityTiming?: NumberFlowTiming;
27
+ /** Flowing only: a limit for an integer place, keyed by place (0 is the ones). `{1:{max:5}}` makes the tens of a seconds counter wrap from 5 to 0. */
28
+ digits?: NumberFlowDigits;
29
+ /** Flowing only, with a trend: every place below the highest one that changed turns once all the way round even when its digit is the same, so the figure reads as counting through the values between. */
30
+ continuous?: boolean;
31
+ /** Flowing only: keeps the moving parts on their own compositor layers between changes. For a figure that changes many times a second; it costs memory, so leave it off elsewhere. */
32
+ willChange?: boolean;
33
+ /** Class names for the readout's root, for the host's type size, weight and colour. */
34
+ className?: string;
7
35
  /** Host-formatted text for a finite `value`, drawn in place of the Intl reading and read the same way, for a host whose number table the browser's Intl does not match. Never parse it back. Empty text falls back to Intl. Unknown values still read as the dash. */
8
36
  formattedValue?: string;
9
37
  /** With `formattedValue`, moves the host's text place by place: each digit rolls in place, counted from the end, and the rest stays as written. Without it `formattedValue` is drawn static. */
10
38
  animateFormatted?: boolean;
11
39
  }
12
- export declare function AnimatedNumberReadout({ duration, trend, options: requested, formattedValue, animateFormatted, ...rest }: AnimatedNumberProps): React.JSX.Element;
40
+ /**
41
+ * A number that moves place by place when its value changes. It slides by default; `motion='flow'` (or `NumberFlow`, which is
42
+ * this component flowing by default) gives it NumberFlow's motion. Either way the reading assistive technology gets is one
43
+ * static text, and reduced motion draws each new value at once.
44
+ */
45
+ export declare function AnimatedNumberReadout({ motion, ...props }: AnimatedNumberProps): React.JSX.Element;
46
+ /** `AnimatedNumberReadout` with NumberFlow's motion by default: the same component and the same props, under the name the product uses for it. */
47
+ export declare function NumberFlow({ motion, ...props }: AnimatedNumberProps): React.JSX.Element;
48
+ export type NumberFlowProps = AnimatedNumberProps;
49
+ /**
50
+ * Moves the flowing figures inside it together: when one changes width, its neighbours in the group slide to their new
51
+ * positions instead of jumping, as the parts of a single figure do. For a row of figures that read as one, such as the units
52
+ * of a countdown. Text between the figures is not moved. On native it draws its children and nothing else.
53
+ */
54
+ export declare function NumberFlowGroup({ children }: {
55
+ children?: React.ReactNode;
56
+ }): React.JSX.Element;