@fabricator-ui/react-native 0.0.0-stage → 0.10.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 (46) hide show
  1. package/README.md +8 -2
  2. package/dist/LICENSE.md +25 -0
  3. package/dist/NOTICE.md +24 -0
  4. package/dist/lib/data-variants.ts +23 -0
  5. package/dist/lib/density-scale.ts +208 -0
  6. package/dist/lib/fabricator/css-object.ts +2 -0
  7. package/dist/lib/fabricator/density-scale.ts +210 -0
  8. package/dist/lib/fabricator/density-variables.ts +37 -0
  9. package/dist/lib/fabricator/motion-speeds.ts +566 -0
  10. package/dist/lib/fabricator/radius.ts +240 -0
  11. package/dist/lib/feedback.ts +124 -0
  12. package/dist/lib/field-frame.ts +53 -0
  13. package/dist/lib/field-model.ts +112 -0
  14. package/dist/lib/fluid-hover-model.ts +142 -0
  15. package/dist/lib/fluid-hover.tsx +788 -0
  16. package/dist/lib/icon-stroke.ts +38 -0
  17. package/dist/lib/motion-tokens.ts +97 -0
  18. package/dist/lib/motion.ts +292 -0
  19. package/dist/lib/overlay-model.ts +205 -0
  20. package/dist/lib/press-wash.tsx +73 -0
  21. package/dist/lib/render.tsx +53 -0
  22. package/dist/lib/select-model.ts +87 -0
  23. package/dist/lib/sheet-model.ts +101 -0
  24. package/dist/lib/sheet-surface.tsx +429 -0
  25. package/dist/lib/sound-recipes.ts +449 -0
  26. package/dist/lib/spring-model.ts +214 -0
  27. package/dist/lib/surface-color.tsx +27 -0
  28. package/dist/lib/text-children.ts +43 -0
  29. package/dist/styles.css +610 -0
  30. package/dist/ui/badge.tsx +108 -0
  31. package/dist/ui/button.tsx +285 -0
  32. package/dist/ui/card.tsx +365 -0
  33. package/dist/ui/checkbox.tsx +230 -0
  34. package/dist/ui/collapsible.tsx +194 -0
  35. package/dist/ui/density.tsx +79 -0
  36. package/dist/ui/dialog.tsx +232 -0
  37. package/dist/ui/dropdown-menu.tsx +687 -0
  38. package/dist/ui/fabricator-root.tsx +68 -0
  39. package/dist/ui/input.tsx +63 -0
  40. package/dist/ui/label.tsx +22 -0
  41. package/dist/ui/select.tsx +616 -0
  42. package/dist/ui/switch.tsx +177 -0
  43. package/dist/ui/tabs.tsx +272 -0
  44. package/dist/ui/text.tsx +125 -0
  45. package/dist/ui/textarea.tsx +65 -0
  46. package/package.json +54 -4
@@ -0,0 +1,449 @@
1
+ /*
2
+ * The recipes behind Fabricator's interface sounds, as plain data: no React, no
3
+ * Web Audio. The website synthesizes them live; React Native renders them to
4
+ * files (lib/sound-synth). Change a sound here and both follow.
5
+ */
6
+
7
+ export type SoundName =
8
+ // Actions
9
+ | "tap"
10
+ | "press"
11
+ | "thud"
12
+ | "tick"
13
+ | "copy"
14
+ // Selection
15
+ | "check"
16
+ | "uncheck"
17
+ | "pick"
18
+ | "toggle-on"
19
+ | "toggle-off"
20
+ | "latch"
21
+ | "unlatch"
22
+ // Navigation
23
+ | "note"
24
+ | "page"
25
+ | "whisk"
26
+ // Surfaces
27
+ | "pop"
28
+ | "dismiss"
29
+ | "open"
30
+ | "close"
31
+ | "alert"
32
+ | "slide-in"
33
+ | "slide-out"
34
+ | "expand"
35
+ | "collapse"
36
+ // Notifications
37
+ | "notify"
38
+ | "success"
39
+ | "warning"
40
+ | "error"
41
+ // Movement
42
+ | "notch"
43
+ | "grab"
44
+ | "drop"
45
+ // Feedback
46
+ | "deny"
47
+
48
+ export type SoundCategory =
49
+ | "Actions"
50
+ | "Selection"
51
+ | "Navigation"
52
+ | "Surfaces"
53
+ | "Notifications"
54
+ | "Movement"
55
+ | "Feedback"
56
+
57
+ /**
58
+ * One layer of a sound. Times are in milliseconds from the start of the sound.
59
+ * An oscillator partial plays `wave` at `freq`; a noise partial plays white
60
+ * noise through a band-pass centred on `freq`.
61
+ */
62
+ export type SoundPartial = {
63
+ wave: OscillatorType | "noise"
64
+ freq: number
65
+ /** Glide to this frequency over the partial's length. */
66
+ to?: number
67
+ at?: number
68
+ length: number
69
+ attack?: number
70
+ /** Peak level, before the master volume. */
71
+ gain: number
72
+ /** Low-pass cutoff for oscillators; rounds off square and triangle edges. */
73
+ lowpass?: number
74
+ /** Band-pass width for noise (higher is narrower). */
75
+ q?: number
76
+ }
77
+
78
+ export type SoundDefinition = {
79
+ name: SoundName
80
+ category: SoundCategory
81
+ description: string
82
+ /** Where the sound plays by default. */
83
+ usedBy: string
84
+ partials: SoundPartial[]
85
+ }
86
+
87
+ // The roles follow Bencho's sound library (https://bencho.dev/sounds): one
88
+ // sound per kind of moment, quiet and short. The sounds themselves are ours.
89
+ // prettier-ignore
90
+ export const SOUND_DEFINITIONS: SoundDefinition[] = [
91
+ // --- Actions: wood -------------------------------------------------------
92
+ {
93
+ name: "tap",
94
+ category: "Actions",
95
+ description: "A light knock of wood, for an everyday press.",
96
+ usedBy: "Outline, secondary and ghost buttons",
97
+ partials: [
98
+ { wave: "triangle", freq: 280, to: 250, length: 45, attack: 2, gain: 0.22, lowpass: 1800 },
99
+ { wave: "noise", freq: 3200, length: 8, attack: 1, gain: 0.25, q: 1.2 },
100
+ ],
101
+ },
102
+ {
103
+ name: "press",
104
+ category: "Actions",
105
+ description: "A fuller, lower knock with an octave above, for the main action.",
106
+ usedBy: "Primary buttons",
107
+ partials: [
108
+ { wave: "triangle", freq: 196, to: 175, length: 80, attack: 3, gain: 0.221, lowpass: 1400 },
109
+ { wave: "sine", freq: 392, to: 370, length: 40, attack: 2, gain: 0.068 },
110
+ { wave: "noise", freq: 2400, length: 10, attack: 1, gain: 0.255, q: 1 },
111
+ ],
112
+ },
113
+ {
114
+ name: "thud",
115
+ category: "Actions",
116
+ description: "A heavy, damped drop, for an action that can't be taken back.",
117
+ usedBy: "Destructive buttons",
118
+ partials: [
119
+ { wave: "sine", freq: 155, to: 98, length: 140, attack: 3, gain: 0.221, lowpass: 700 },
120
+ { wave: "triangle", freq: 310, to: 250, length: 30, attack: 2, gain: 0.065, lowpass: 1200 },
121
+ { wave: "noise", freq: 900, length: 25, attack: 1, gain: 0.227, q: 0.8 },
122
+ ],
123
+ },
124
+ {
125
+ name: "tick",
126
+ category: "Actions",
127
+ description: "A tiny, glassy tick, light enough for every row and link.",
128
+ usedBy: "Menu items, links, sidebar and navigation items, link buttons",
129
+ partials: [
130
+ { wave: "sine", freq: 1180, to: 1100, length: 22, attack: 1, gain: 0.07 },
131
+ { wave: "triangle", freq: 590, length: 16, attack: 1, gain: 0.06, lowpass: 2600 },
132
+ ],
133
+ },
134
+ {
135
+ name: "copy",
136
+ category: "Actions",
137
+ description: "Two bright blips a fifth apart: it's on the clipboard.",
138
+ usedBy: 'Copy buttons (data-sound="copy")',
139
+ partials: [
140
+ { wave: "sine", freq: 1047, length: 45, attack: 2, gain: 0.102 },
141
+ { wave: "sine", freq: 1568, at: 55, length: 70, attack: 2, gain: 0.085 },
142
+ ],
143
+ },
144
+
145
+ // --- Selection: a mechanism -----------------------------------------------
146
+ {
147
+ name: "check",
148
+ category: "Selection",
149
+ description: "Two clicks stepping up, the way a check mark is drawn.",
150
+ usedBy: "Checkboxes, menu checkboxes",
151
+ partials: [
152
+ { wave: "square", freq: 392, length: 20, attack: 1, gain: 0.06, lowpass: 2400 },
153
+ { wave: "square", freq: 587, at: 30, length: 45, attack: 1, gain: 0.07, lowpass: 2600 },
154
+ { wave: "sine", freq: 1174, at: 30, length: 60, attack: 2, gain: 0.03 },
155
+ ],
156
+ },
157
+ {
158
+ name: "uncheck",
159
+ category: "Selection",
160
+ description: "One soft click sliding down, for a box being cleared.",
161
+ usedBy: "Checkboxes, menu checkboxes",
162
+ partials: [
163
+ { wave: "square", freq: 330, to: 280, length: 30, attack: 1, gain: 0.07, lowpass: 1800 },
164
+ ],
165
+ },
166
+ {
167
+ name: "pick",
168
+ category: "Selection",
169
+ description: "A click that rings on, for one choice taken from a set.",
170
+ usedBy: "Radios, select and combobox options, command items, calendar days",
171
+ partials: [
172
+ { wave: "square", freq: 440, length: 18, attack: 1, gain: 0.033, lowpass: 2400 },
173
+ { wave: "sine", freq: 880, at: 4, length: 110, attack: 4, gain: 0.065 },
174
+ ],
175
+ },
176
+ // The switch pair is Bencho's liquid toggle ("Tick" and "Hush"), with the
177
+ // gains scaled to sit at Bencho's level under our master volume.
178
+ {
179
+ name: "toggle-on",
180
+ category: "Selection",
181
+ description: "A soft, round drop as the thumb settles on.",
182
+ usedBy: "Switches",
183
+ partials: [
184
+ { wave: "sine", freq: 196, to: 130, length: 150, attack: 4, gain: 0.26, lowpass: 900 },
185
+ ],
186
+ },
187
+ {
188
+ name: "toggle-off",
189
+ category: "Selection",
190
+ description: "A quieter, shorter sigh as it lets go.",
191
+ usedBy: "Switches",
192
+ partials: [
193
+ { wave: "sine", freq: 330, to: 247, length: 88, attack: 8, gain: 0.11, lowpass: 720 },
194
+ ],
195
+ },
196
+ {
197
+ name: "latch",
198
+ category: "Selection",
199
+ description: "A key pressing in, then catching: click, clack.",
200
+ usedBy: "Toggles, toggle group items",
201
+ partials: [
202
+ { wave: "noise", freq: 1500, length: 12, attack: 1, gain: 0.3, q: 1.5 },
203
+ { wave: "triangle", freq: 247, length: 40, attack: 2, gain: 0.18, lowpass: 1500 },
204
+ { wave: "noise", freq: 2200, at: 32, length: 10, attack: 1, gain: 0.22, q: 1.5 },
205
+ { wave: "square", freq: 494, at: 32, length: 22, attack: 1, gain: 0.045, lowpass: 2000 },
206
+ ],
207
+ },
208
+ {
209
+ name: "unlatch",
210
+ category: "Selection",
211
+ description: "The key springing back up: one lower click that lifts.",
212
+ usedBy: "Toggles, toggle group items",
213
+ partials: [
214
+ { wave: "noise", freq: 1100, length: 12, attack: 1, gain: 0.3, q: 1.5 },
215
+ { wave: "triangle", freq: 196, to: 233, length: 50, attack: 2, gain: 0.2, lowpass: 1100 },
216
+ ],
217
+ },
218
+
219
+ // --- Navigation -----------------------------------------------------------
220
+ {
221
+ name: "note",
222
+ category: "Navigation",
223
+ description: "A small bell per tab: each position rings the next note of a chord.",
224
+ usedBy: "Tabs",
225
+ partials: [
226
+ { wave: "sine", freq: 294, to: 292, length: 220, attack: 6, gain: 0.11 },
227
+ { wave: "sine", freq: 588, length: 120, attack: 4, gain: 0.028 },
228
+ { wave: "triangle", freq: 882, length: 40, attack: 2, gain: 0.017, lowpass: 2000 },
229
+ ],
230
+ },
231
+ {
232
+ name: "page",
233
+ category: "Navigation",
234
+ description: "A paper flick, for turning to another page or month.",
235
+ usedBy: "Pagination, calendar month buttons",
236
+ partials: [
237
+ { wave: "noise", freq: 2600, to: 1400, length: 60, attack: 3, gain: 0.255, q: 0.9 },
238
+ { wave: "triangle", freq: 330, to: 300, length: 35, attack: 2, gain: 0.068, lowpass: 1600 },
239
+ ],
240
+ },
241
+ {
242
+ name: "whisk",
243
+ category: "Navigation",
244
+ description: "A soft rush of air, for content sliding past.",
245
+ usedBy: "Carousel previous and next",
246
+ partials: [
247
+ { wave: "noise", freq: 500, to: 1800, length: 150, attack: 40, gain: 0.28, q: 0.7 },
248
+ { wave: "sine", freq: 165, to: 220, length: 140, attack: 20, gain: 0.1 },
249
+ ],
250
+ },
251
+
252
+ // --- Surfaces -------------------------------------------------------------
253
+ {
254
+ name: "pop",
255
+ category: "Surfaces",
256
+ description: "A quick rising bubble, for a small surface appearing.",
257
+ usedBy: "Menus, popovers, select and combobox lists, hover cards",
258
+ partials: [
259
+ { wave: "sine", freq: 520, to: 820, length: 40, attack: 2, gain: 0.14 },
260
+ { wave: "triangle", freq: 1040, length: 14, attack: 1, gain: 0.03, lowpass: 3000 },
261
+ ],
262
+ },
263
+ {
264
+ name: "dismiss",
265
+ category: "Surfaces",
266
+ description: "The bubble falling back, quieter.",
267
+ usedBy: "Menus, popovers, select and combobox lists, hover cards",
268
+ partials: [{ wave: "sine", freq: 700, to: 460, length: 45, attack: 2, gain: 0.1 }],
269
+ },
270
+ {
271
+ name: "open",
272
+ category: "Surfaces",
273
+ description: "Two warm notes a fifth apart blooming in, for a dialog.",
274
+ usedBy: "Dialogs, command palettes, Search",
275
+ partials: [
276
+ { wave: "sine", freq: 220, to: 218, length: 340, attack: 14, gain: 0.144, lowpass: 1400 },
277
+ { wave: "sine", freq: 330, to: 328, at: 40, length: 300, attack: 14, gain: 0.096 },
278
+ { wave: "sine", freq: 660, at: 40, length: 120, attack: 10, gain: 0.016 },
279
+ ],
280
+ },
281
+ {
282
+ name: "close",
283
+ category: "Surfaces",
284
+ description: "The same fifth stepping down and away.",
285
+ usedBy: "Dialogs, alert dialogs, command palettes, Search",
286
+ partials: [
287
+ { wave: "sine", freq: 330, to: 320, length: 120, attack: 6, gain: 0.12 },
288
+ { wave: "sine", freq: 220, to: 200, at: 45, length: 160, attack: 6, gain: 0.15 },
289
+ ],
290
+ },
291
+ {
292
+ name: "alert",
293
+ category: "Surfaces",
294
+ description: "A minor third with a knock in front: this one needs an answer.",
295
+ usedBy: "Alert dialogs",
296
+ partials: [
297
+ { wave: "sine", freq: 220, length: 380, attack: 10, gain: 0.144 },
298
+ { wave: "sine", freq: 262, at: 60, length: 340, attack: 10, gain: 0.104 },
299
+ { wave: "triangle", freq: 440, length: 60, attack: 2, gain: 0.032, lowpass: 1600 },
300
+ ],
301
+ },
302
+ {
303
+ name: "slide-in",
304
+ category: "Surfaces",
305
+ description: "Air rising under a low note, for a panel sliding in.",
306
+ usedBy: "Sheets and drawers opening",
307
+ partials: [
308
+ { wave: "noise", freq: 350, to: 1500, length: 200, attack: 70, gain: 0.3, q: 0.6 },
309
+ { wave: "sine", freq: 131, to: 175, length: 200, attack: 50, gain: 0.12 },
310
+ ],
311
+ },
312
+ {
313
+ name: "slide-out",
314
+ category: "Surfaces",
315
+ description: "The air falling away again.",
316
+ usedBy: "Sheets and drawers closing",
317
+ partials: [
318
+ { wave: "noise", freq: 1500, to: 350, length: 170, attack: 30, gain: 0.221, q: 0.6 },
319
+ { wave: "sine", freq: 175, to: 131, length: 160, attack: 20, gain: 0.085 },
320
+ ],
321
+ },
322
+ {
323
+ name: "expand",
324
+ category: "Surfaces",
325
+ description: "A short upward unfold, for content opening in place.",
326
+ usedBy: "Accordions, collapsibles, <details>",
327
+ partials: [
328
+ { wave: "triangle", freq: 294, to: 392, length: 90, attack: 8, gain: 0.16, lowpass: 1500 },
329
+ { wave: "noise", freq: 2000, length: 20, attack: 2, gain: 0.1, q: 1 },
330
+ ],
331
+ },
332
+ {
333
+ name: "collapse",
334
+ category: "Surfaces",
335
+ description: "The unfold in reverse, a little quieter.",
336
+ usedBy: "Accordions, collapsibles, <details>",
337
+ partials: [
338
+ { wave: "triangle", freq: 392, to: 294, length: 75, attack: 6, gain: 0.13, lowpass: 1300 },
339
+ ],
340
+ },
341
+
342
+ // --- Notifications: chimes ------------------------------------------------
343
+ {
344
+ name: "notify",
345
+ category: "Notifications",
346
+ description: "Two bell tones a fifth apart, for news arriving.",
347
+ usedBy: "Toasts (default and info)",
348
+ partials: [
349
+ { wave: "sine", freq: 392, length: 260, attack: 6, gain: 0.109 },
350
+ { wave: "sine", freq: 1176, length: 90, attack: 3, gain: 0.017 },
351
+ { wave: "sine", freq: 587, at: 90, length: 320, attack: 6, gain: 0.095 },
352
+ { wave: "sine", freq: 1761, at: 90, length: 100, attack: 3, gain: 0.014 },
353
+ ],
354
+ },
355
+ {
356
+ name: "success",
357
+ category: "Notifications",
358
+ description: "A quick rising major chord, for something done.",
359
+ usedBy: "Success toasts",
360
+ partials: [
361
+ { wave: "sine", freq: 523, length: 140, attack: 4, gain: 0.082 },
362
+ { wave: "sine", freq: 659, at: 60, length: 150, attack: 4, gain: 0.082 },
363
+ { wave: "sine", freq: 784, at: 120, length: 300, attack: 4, gain: 0.088 },
364
+ { wave: "sine", freq: 1568, at: 120, length: 120, attack: 3, gain: 0.014 },
365
+ ],
366
+ },
367
+ {
368
+ name: "warning",
369
+ category: "Notifications",
370
+ description: "Two even, reedy pulses on one note: look at this.",
371
+ usedBy: "Warning toasts",
372
+ partials: [
373
+ { wave: "triangle", freq: 440, length: 90, attack: 4, gain: 0.16, lowpass: 1800 },
374
+ { wave: "triangle", freq: 440, at: 130, length: 120, attack: 4, gain: 0.16, lowpass: 1800 },
375
+ ],
376
+ },
377
+ {
378
+ name: "error",
379
+ category: "Notifications",
380
+ description: "Two low, buzzing notes falling a third: something failed.",
381
+ usedBy: "Error toasts",
382
+ partials: [
383
+ { wave: "triangle", freq: 247, length: 110, attack: 4, gain: 0.2, lowpass: 1000 },
384
+ { wave: "square", freq: 247, length: 60, attack: 2, gain: 0.03, lowpass: 900 },
385
+ { wave: "triangle", freq: 196, at: 120, length: 200, attack: 4, gain: 0.22, lowpass: 900 },
386
+ ],
387
+ },
388
+
389
+ // --- Movement -------------------------------------------------------------
390
+ {
391
+ name: "notch",
392
+ category: "Movement",
393
+ description: "A tiny detent that follows the value, light enough to repeat along a drag.",
394
+ usedBy: "Sliders",
395
+ partials: [
396
+ { wave: "triangle", freq: 640, length: 16, attack: 1, gain: 0.09, lowpass: 2400 },
397
+ ],
398
+ },
399
+ {
400
+ name: "grab",
401
+ category: "Movement",
402
+ description: "A low catch, for picking something up.",
403
+ usedBy: "Resizable handles",
404
+ partials: [
405
+ { wave: "noise", freq: 1200, length: 18, attack: 1, gain: 0.325, q: 1.2 },
406
+ { wave: "triangle", freq: 175, length: 40, attack: 2, gain: 0.208, lowpass: 1000 },
407
+ ],
408
+ },
409
+ {
410
+ name: "drop",
411
+ category: "Movement",
412
+ description: "A soft landing, for letting it go.",
413
+ usedBy: "Resizable handles",
414
+ partials: [
415
+ { wave: "sine", freq: 165, to: 123, length: 90, attack: 2, gain: 0.2, lowpass: 900 },
416
+ { wave: "noise", freq: 700, length: 25, attack: 1, gain: 0.2, q: 0.9 },
417
+ ],
418
+ },
419
+
420
+ // --- Feedback -------------------------------------------------------------
421
+ {
422
+ name: "deny",
423
+ category: "Feedback",
424
+ description: 'Two low knocks, "uh-uh": short enough not to scold.',
425
+ usedBy: "Disabled controls",
426
+ partials: [
427
+ { wave: "triangle", freq: 185, to: 175, length: 70, attack: 2, gain: 0.18, lowpass: 900 },
428
+ { wave: "triangle", freq: 165, to: 150, at: 95, length: 110, attack: 2, gain: 0.18, lowpass: 800 },
429
+ ],
430
+ },
431
+ ]
432
+
433
+ /** The level of the mix under the master volume. */
434
+ export const SOUND_MASTER_LEVEL = 0.45
435
+
436
+ /** The master volume sounds start at, 0 to 1. */
437
+ export const DEFAULT_SOUND_VOLUME = 0.6
438
+
439
+ /**
440
+ * Chord tones for `note`, as `pitch` multipliers of its 294Hz: G, B, D, G, B,
441
+ * D. Tabs pass their position, so a row of tabs plays a rising arpeggio from
442
+ * first to last.
443
+ */
444
+ export const NOTE_STEPS = [196, 247, 294, 392, 494, 587].map((hz) => hz / 294)
445
+
446
+ /** The `pitch` for the item at `index` in a row of navigation items. */
447
+ export function noteStep(index: number) {
448
+ return NOTE_STEPS[Math.max(0, index) % NOTE_STEPS.length]
449
+ }
@@ -0,0 +1,214 @@
1
+ // Springs: a value pulled to its target by stiffness and slowed by damping
2
+ // (mass 1), stepped with semi-implicit Euler in small substeps. Retargeting
3
+ // keeps the current velocity, so a second click bends the motion instead of
4
+ // restarting it. The motion tokens carry the presets as "stiffness damping"
5
+ // (`--motion-spring-lead: 620 36`); React Native passes the same two numbers
6
+ // to Reanimated's `withSpring`.
7
+
8
+ export type SpringConfig = { stiffness: number; damping: number }
9
+
10
+ export type SpringState = { value: number; velocity: number }
11
+
12
+ // A dropped frame or a tab that slept is stepped as one short frame.
13
+ const MAX_ELAPSED = 1 / 30
14
+ const MAX_SUBSTEP = 1 / 240
15
+ const REST_DISTANCE = 0.05
16
+ const REST_VELOCITY = 0.5
17
+
18
+ export function stepSpring(
19
+ state: SpringState,
20
+ target: number,
21
+ { stiffness, damping }: SpringConfig,
22
+ elapsedSeconds: number
23
+ ): SpringState {
24
+ "worklet"
25
+ const elapsed = Math.min(Math.max(elapsedSeconds, 0), MAX_ELAPSED)
26
+ const steps = Math.max(1, Math.ceil(elapsed / MAX_SUBSTEP))
27
+ const step = elapsed / steps
28
+ let { value, velocity } = state
29
+ for (let index = 0; index < steps; index++) {
30
+ velocity += (-stiffness * (value - target) - damping * velocity) * step
31
+ value += velocity * step
32
+ }
33
+ return { value, velocity }
34
+ }
35
+
36
+ export function isSpringAtRest(state: SpringState, target: number) {
37
+ "worklet"
38
+ return (
39
+ Math.abs(state.value - target) < REST_DISTANCE &&
40
+ Math.abs(state.velocity) < REST_VELOCITY
41
+ )
42
+ }
43
+
44
+ /**
45
+ * The same spring played `timeScale` times slower (2 is twice as long): the
46
+ * damping ratio, and so the overshoot, stays the same.
47
+ */
48
+ export function retimeSpring(
49
+ { stiffness, damping }: SpringConfig,
50
+ timeScale: number
51
+ ): SpringConfig {
52
+ return {
53
+ stiffness: stiffness / timeScale ** 2,
54
+ damping: damping / timeScale,
55
+ }
56
+ }
57
+
58
+ /** The same stiffness, critically damped: it arrives without overshooting. */
59
+ export function getCriticalSpring({ stiffness }: SpringConfig): SpringConfig {
60
+ return { stiffness, damping: 2 * Math.sqrt(stiffness) }
61
+ }
62
+
63
+ const SAMPLE_STEP = 1 / 240
64
+ const SAMPLE_REST = 0.005
65
+
66
+ /**
67
+ * A spring released from 0 towards 1, for CSS `linear()`: how long it takes
68
+ * to settle within 0.5 %, in whole milliseconds, and `count + 1` evenly
69
+ * spaced values over that time, ending exactly on 1.
70
+ */
71
+ export function sampleSpring(config: SpringConfig, count = 40) {
72
+ let state: SpringState = { value: 0, velocity: 0 }
73
+ const trace = [0]
74
+ let settledAt = 0
75
+ for (let step = 1; step <= 4 / SAMPLE_STEP; step++) {
76
+ state = stepSpring(state, 1, config, SAMPLE_STEP)
77
+ trace.push(state.value)
78
+ if (Math.abs(state.value - 1) > SAMPLE_REST) settledAt = step
79
+ }
80
+ const last = Math.max(settledAt, 1)
81
+ const samples = Array.from({ length: count + 1 }, (_, index) => {
82
+ if (index === count) return 1
83
+ // `last` is a step the trace holds; past it the spring rests on 1.
84
+ const value = trace[Math.round((index / count) * last)] ?? 1
85
+ return Math.round(value * 1000) / 1000
86
+ })
87
+ return { duration: Math.round(last * SAMPLE_STEP * 1000), samples }
88
+ }
89
+
90
+ /** A "stiffness damping" token, or null when it isn't one. */
91
+ export function parseSpring(token: string): SpringConfig | null {
92
+ const parts = token.trim().split(/\s+/)
93
+ if (parts.length !== 2) return null
94
+ const stiffness = Number(parts[0])
95
+ const damping = Number(parts[1])
96
+ if (!(stiffness > 0) || !(damping >= 0)) return null
97
+ return { stiffness, damping }
98
+ }
99
+
100
+ /**
101
+ * A spring token as the page sets it, stretched by `--motion-scale`; null
102
+ * when motion is off (scale 0) or the token is missing, so the caller jumps
103
+ * straight to the target.
104
+ */
105
+ export function resolveSpring(
106
+ token: string,
107
+ motionScale: string
108
+ ): SpringConfig | null {
109
+ const scale = motionScale.trim() === "" ? 1 : Number(motionScale)
110
+ if (!(scale > 0)) return null
111
+ const spring = parseSpring(token)
112
+ if (!spring) return null
113
+ return scale === 1 ? spring : retimeSpring(spring, scale)
114
+ }
115
+
116
+ /**
117
+ * A spring's CSS form (`--motion-spring-<name>-duration` and
118
+ * `--motion-ease-<name>-spring`) as a Web Animations timing, stretched by
119
+ * `--motion-scale`; null when motion is off or the tokens are missing.
120
+ */
121
+ export function resolveSpringTransition(
122
+ durationToken: string,
123
+ easingToken: string,
124
+ motionScale: string
125
+ ) {
126
+ const scale = motionScale.trim() === "" ? 1 : Number(motionScale)
127
+ const duration = Number.parseFloat(durationToken)
128
+ const easing = easingToken.trim()
129
+ if (!(scale > 0) || !(duration > 0) || easing === "") return null
130
+ return { duration: duration * scale, easing }
131
+ }
132
+
133
+ export type EdgeRect = {
134
+ left: number
135
+ top: number
136
+ right: number
137
+ bottom: number
138
+ }
139
+
140
+ type Edge = keyof EdgeRect
141
+
142
+ const EDGES: Edge[] = ["left", "top", "right", "bottom"]
143
+
144
+ export type RectMotion = Record<Edge, SpringState>
145
+
146
+ export type RectEdgeSprings = Record<Edge, SpringConfig>
147
+
148
+ export function createRectMotion(rect: EdgeRect): RectMotion {
149
+ return {
150
+ left: { value: rect.left, velocity: 0 },
151
+ top: { value: rect.top, velocity: 0 },
152
+ right: { value: rect.right, velocity: 0 },
153
+ bottom: { value: rect.bottom, velocity: 0 },
154
+ }
155
+ }
156
+
157
+ /**
158
+ * Which spring drives each edge on the way from one rect to another: the edge
159
+ * facing the move is stiff (`lead`) and the one behind it soft (`trail`), so
160
+ * a highlight stretches toward its target and gathers when it lands. An axis
161
+ * that only resizes moves both edges on `lead`.
162
+ */
163
+ export function getRectEdgeSprings(
164
+ from: EdgeRect,
165
+ to: EdgeRect,
166
+ lead: SpringConfig,
167
+ trail: SpringConfig
168
+ ): RectEdgeSprings {
169
+ "worklet"
170
+ const alongX = to.left + to.right - (from.left + from.right)
171
+ const alongY = to.top + to.bottom - (from.top + from.bottom)
172
+ return {
173
+ left: alongX > 0 ? trail : lead,
174
+ right: alongX < 0 ? trail : lead,
175
+ top: alongY > 0 ? trail : lead,
176
+ bottom: alongY < 0 ? trail : lead,
177
+ }
178
+ }
179
+
180
+ export function stepRectMotion(
181
+ motion: RectMotion,
182
+ target: EdgeRect,
183
+ springs: RectEdgeSprings,
184
+ elapsedSeconds: number
185
+ ): RectMotion {
186
+ const next = { ...motion }
187
+ for (const edge of EDGES) {
188
+ next[edge] = stepSpring(
189
+ motion[edge],
190
+ target[edge],
191
+ springs[edge],
192
+ elapsedSeconds
193
+ )
194
+ }
195
+ return next
196
+ }
197
+
198
+ export function isRectMotionAtRest(motion: RectMotion, target: EdgeRect) {
199
+ return EDGES.every((edge) => isSpringAtRest(motion[edge], target[edge]))
200
+ }
201
+
202
+ /**
203
+ * The box to draw, in whole hundredths of a pixel. A trailing edge that
204
+ * overshoots past the leading one draws nothing rather than a negative box.
205
+ */
206
+ export function readRectMotion(motion: RectMotion) {
207
+ const round = (value: number) => Math.round(value * 100) / 100
208
+ return {
209
+ left: round(motion.left.value),
210
+ top: round(motion.top.value),
211
+ width: round(Math.max(0, motion.right.value - motion.left.value)),
212
+ height: round(Math.max(0, motion.bottom.value - motion.top.value)),
213
+ }
214
+ }
@@ -0,0 +1,27 @@
1
+ import * as React from "react"
2
+
3
+ // The colour of the surface below, for what paints a fade over its content
4
+ // (React Native has no mask): a ScrollArea in a Card fades to the card, not
5
+ // to the page. The website gets the same from `--current-surface`.
6
+
7
+ const SurfaceColorContext = React.createContext<string | undefined>(undefined)
8
+
9
+ function SurfaceColorProvider({
10
+ color,
11
+ children,
12
+ }: {
13
+ /** The surface's colour; `undefined` keeps the one around it. */
14
+ color: string | undefined
15
+ children: React.ReactNode
16
+ }) {
17
+ const outer = React.use(SurfaceColorContext)
18
+ return (
19
+ <SurfaceColorContext value={color ?? outer}>{children}</SurfaceColorContext>
20
+ )
21
+ }
22
+
23
+ function useSurfaceColor() {
24
+ return React.use(SurfaceColorContext)
25
+ }
26
+
27
+ export { SurfaceColorProvider, useSurfaceColor }