gooee 0.48.0 → 0.51.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 (185) hide show
  1. package/package.json +4 -2
  2. package/scripts/add.js +28 -6
  3. package/src/components/Accordion/Accordion.vue +62 -0
  4. package/src/components/Accordion/AccordionItem.vue +45 -0
  5. package/src/components/Accordion/accordionContext.ts +6 -0
  6. package/src/components/Alert/Alert.tsx +7 -27
  7. package/src/components/Alert/Alert.vue +59 -0
  8. package/src/components/AlertDialog/AlertDialog.vue +126 -0
  9. package/src/components/AnimatedNumber/AnimatedNumber.vue +36 -0
  10. package/src/components/AspectRatio/AspectRatio.vue +17 -0
  11. package/src/components/Avatar/Avatar.vue +56 -0
  12. package/src/components/AvatarGroup/AvatarGroup.vue +42 -0
  13. package/src/components/Badge/Badge.vue +32 -0
  14. package/src/components/Breadcrumb/Breadcrumb.vue +58 -0
  15. package/src/components/Button/Button.vue +130 -0
  16. package/src/components/ButtonGroup/ButtonGroup.tsx +4 -21
  17. package/src/components/ButtonGroup/ButtonGroup.vue +33 -0
  18. package/src/components/Card/Card.vue +34 -0
  19. package/src/components/Center/Center.vue +32 -0
  20. package/src/components/Checkbox/Checkbox.tsx +8 -25
  21. package/src/components/Checkbox/Checkbox.vue +108 -0
  22. package/src/components/CheckboxGroup/CheckboxGroup.vue +76 -0
  23. package/src/components/Cluster/Cluster.vue +34 -0
  24. package/src/components/CodeBlock/CodeBlock.vue +45 -0
  25. package/src/components/Collapsible/Collapsible.vue +59 -0
  26. package/src/components/Container/Container.vue +22 -0
  27. package/src/components/CopyButton/CopyButton.tsx +3 -49
  28. package/src/components/CopyButton/CopyButton.vue +116 -0
  29. package/src/components/Dial/Dial.tsx +12 -120
  30. package/src/components/Dial/Dial.vue +228 -0
  31. package/src/components/Dialog/Dialog.vue +139 -0
  32. package/src/components/Divider/Divider.vue +27 -0
  33. package/src/components/DropdownMenu/DropdownMenu.tsx +11 -46
  34. package/src/components/DropdownMenu/DropdownMenu.vue +185 -0
  35. package/src/components/EmptyState/EmptyState.vue +33 -0
  36. package/src/components/Fab/Fab.tsx +4 -14
  37. package/src/components/Fab/Fab.vue +115 -0
  38. package/src/components/Fieldset/Fieldset.vue +42 -0
  39. package/src/components/FormField/FormField.tsx +2 -17
  40. package/src/components/FormField/FormField.vue +64 -0
  41. package/src/components/Grid/Grid.vue +43 -0
  42. package/src/components/HoldToConfirm/HoldToConfirm.tsx +13 -142
  43. package/src/components/HoldToConfirm/HoldToConfirm.vue +281 -0
  44. package/src/components/IconButton/IconButton.vue +25 -0
  45. package/src/components/Input/Input.vue +112 -0
  46. package/src/components/InputGroup/InputGroup.vue +102 -0
  47. package/src/components/Kbd/Kbd.tsx +2 -12
  48. package/src/components/Kbd/Kbd.vue +21 -0
  49. package/src/components/Link/Link.vue +38 -0
  50. package/src/components/LivePill/LivePill.tsx +12 -40
  51. package/src/components/LivePill/LivePill.vue +152 -0
  52. package/src/components/Meter/Meter.vue +88 -0
  53. package/src/components/MorphButton/MorphButton.tsx +11 -67
  54. package/src/components/MorphButton/MorphButton.vue +120 -0
  55. package/src/components/NumberInput/NumberInput.tsx +11 -48
  56. package/src/components/NumberInput/NumberInput.vue +184 -0
  57. package/src/components/Pagination/Pagination.tsx +2 -15
  58. package/src/components/Pagination/Pagination.vue +87 -0
  59. package/src/components/Pagination/range.ts +15 -0
  60. package/src/components/PasswordInput/PasswordInput.vue +53 -0
  61. package/src/components/PickerWheel/DateWheel.vue +48 -0
  62. package/src/components/PickerWheel/PickerWheel.tsx +15 -129
  63. package/src/components/PickerWheel/PickerWheel.vue +77 -0
  64. package/src/components/PickerWheel/TimeWheel.vue +50 -0
  65. package/src/components/PickerWheel/WheelColumn.vue +70 -0
  66. package/src/components/Popover/Popover.vue +108 -0
  67. package/src/components/ProgressBar/ProgressBar.vue +48 -0
  68. package/src/components/Prose/Prose.vue +18 -0
  69. package/src/components/Radio/Radio.vue +49 -0
  70. package/src/components/Radio/RadioGroup.vue +69 -0
  71. package/src/components/Retime/Retime.tsx +7 -40
  72. package/src/components/Retime/Retime.vue +64 -0
  73. package/src/components/RulerScrubber/RulerScrubber.tsx +9 -38
  74. package/src/components/RulerScrubber/RulerScrubber.vue +166 -0
  75. package/src/components/ScrollArea/ScrollArea.tsx +17 -129
  76. package/src/components/ScrollArea/ScrollArea.vue +58 -0
  77. package/src/components/SearchInput/SearchInput.tsx +2 -15
  78. package/src/components/SearchInput/SearchInput.vue +70 -0
  79. package/src/components/SegmentedControl/SegmentedControl.tsx +7 -51
  80. package/src/components/SegmentedControl/SegmentedControl.vue +111 -0
  81. package/src/components/Select/Select.tsx +16 -47
  82. package/src/components/Select/Select.vue +236 -0
  83. package/src/components/Sheet/Sheet.tsx +52 -231
  84. package/src/components/Sheet/Sheet.vue +249 -0
  85. package/src/components/Skeleton/Skeleton.vue +38 -0
  86. package/src/components/Slider/Slider.tsx +25 -84
  87. package/src/components/Slider/Slider.vue +113 -0
  88. package/src/components/Spinner/Spinner.vue +30 -0
  89. package/src/components/Split/Split.vue +34 -0
  90. package/src/components/SplitFlap/SplitFlap.tsx +11 -175
  91. package/src/components/SplitFlap/SplitFlap.vue +52 -0
  92. package/src/components/Stack/Stack.vue +22 -0
  93. package/src/components/SwipeDeck/SwipeDeck.tsx +30 -194
  94. package/src/components/SwipeDeck/SwipeDeck.vue +168 -0
  95. package/src/components/Switch/Switch.tsx +6 -75
  96. package/src/components/Switch/Switch.vue +66 -0
  97. package/src/components/Table/Table.module.css +12 -3
  98. package/src/components/Table/Table.tsx +8 -59
  99. package/src/components/Table/Table.vue +274 -0
  100. package/src/components/TableOfContents/TableOfContents.vue +90 -0
  101. package/src/components/Tabs/Tab.vue +37 -0
  102. package/src/components/Tabs/TabList.vue +47 -0
  103. package/src/components/Tabs/TabPanel.vue +26 -0
  104. package/src/components/Tabs/Tabs.module.css +2 -2
  105. package/src/components/Tabs/Tabs.tsx +11 -84
  106. package/src/components/Tabs/Tabs.vue +33 -0
  107. package/src/components/Tabs/tabsContext.ts +15 -0
  108. package/src/components/Tag/Tag.tsx +6 -26
  109. package/src/components/Tag/Tag.vue +63 -0
  110. package/src/components/Textarea/Textarea.tsx +2 -22
  111. package/src/components/Textarea/Textarea.vue +120 -0
  112. package/src/components/ThemeToggle/ThemeToggle.tsx +10 -84
  113. package/src/components/ThemeToggle/ThemeToggle.vue +90 -0
  114. package/src/components/Toast/Toast.tsx +34 -264
  115. package/src/components/Toast/Toast.vue +138 -0
  116. package/src/components/Toast/ToastItem.vue +108 -0
  117. package/src/components/Toggle/Toggle.vue +52 -0
  118. package/src/components/Tooltip/Tooltip.tsx +17 -48
  119. package/src/components/Tooltip/Tooltip.vue +95 -0
  120. package/src/components/VisuallyHidden/VisuallyHidden.vue +17 -0
  121. package/src/internal/FieldMessage.vue +41 -0
  122. package/src/internal/Roll.vue +20 -0
  123. package/src/internal/fling.ts +29 -106
  124. package/src/internal/focus.ts +4 -79
  125. package/src/internal/icons.module.css +39 -27
  126. package/src/internal/layers.ts +7 -61
  127. package/src/internal/liquid.ts +26 -213
  128. package/src/internal/liquidMatter.ts +9 -181
  129. package/src/internal/liquidMore.ts +6 -669
  130. package/src/internal/motion/alert.ts +17 -0
  131. package/src/internal/motion/buttonGroup.ts +21 -0
  132. package/src/internal/motion/checkbox.ts +39 -0
  133. package/src/internal/motion/collapseOut.ts +26 -0
  134. package/src/internal/motion/copyButton.ts +48 -0
  135. package/src/internal/motion/dial.ts +137 -0
  136. package/src/internal/motion/fab.ts +14 -0
  137. package/src/internal/motion/fling.ts +147 -0
  138. package/src/internal/motion/floating.ts +58 -0
  139. package/src/internal/motion/focus.ts +88 -0
  140. package/src/internal/motion/formField.ts +19 -0
  141. package/src/internal/motion/holdToConfirm.ts +147 -0
  142. package/src/internal/motion/index.ts +20 -0
  143. package/src/internal/motion/keys.ts +42 -0
  144. package/src/internal/motion/layers.ts +78 -0
  145. package/src/internal/motion/liquid.ts +241 -0
  146. package/src/internal/motion/liquidMatter.ts +198 -0
  147. package/src/internal/motion/liquidMore.ts +676 -0
  148. package/src/internal/motion/listbox.ts +69 -0
  149. package/src/internal/motion/livePill.ts +71 -0
  150. package/src/internal/motion/menuKeys.ts +58 -0
  151. package/src/internal/motion/morphButton.ts +71 -0
  152. package/src/internal/motion/numberInput.ts +57 -0
  153. package/src/internal/motion/pickerWheel.ts +166 -0
  154. package/src/internal/motion/retime.ts +45 -0
  155. package/src/internal/motion/ruler.ts +57 -0
  156. package/src/internal/motion/scrollArea.ts +133 -0
  157. package/src/internal/motion/scrollFrame.ts +152 -0
  158. package/src/internal/motion/scrubLook.ts +15 -0
  159. package/src/internal/motion/scrubber.ts +154 -0
  160. package/src/internal/motion/segmented.ts +63 -0
  161. package/src/internal/motion/sheetDetents.ts +271 -0
  162. package/src/internal/motion/slider.ts +108 -0
  163. package/src/internal/motion/softPress.ts +334 -0
  164. package/src/internal/motion/splitFlap.ts +205 -0
  165. package/src/internal/motion/swipeDeck.ts +236 -0
  166. package/src/internal/motion/switchThumb.ts +83 -0
  167. package/src/internal/motion/table.ts +77 -0
  168. package/src/internal/motion/tabs.ts +105 -0
  169. package/src/internal/motion/textarea.ts +22 -0
  170. package/src/internal/motion/themeIcon.ts +91 -0
  171. package/src/internal/motion/themeReveal.ts +71 -0
  172. package/src/internal/motion/toast.ts +291 -0
  173. package/src/internal/motion/tooltip.ts +58 -0
  174. package/src/internal/scrollFrame.ts +4 -145
  175. package/src/internal/softPress.ts +27 -289
  176. package/src/internal/themeReveal.ts +3 -61
  177. package/src/internal/useFloating.ts +9 -49
  178. package/src/internal/useScrub.ts +15 -120
  179. package/src/internal/vue/trigger.ts +18 -0
  180. package/src/internal/vue/useFloating.ts +38 -0
  181. package/src/internal/vue/usePresence.ts +21 -0
  182. package/src/internal/vue/useScrub.ts +40 -0
  183. package/src/internal/vueIcons.ts +134 -0
  184. package/src/tokens.css +5 -0
  185. package/src/internal/relativeTime.ts +0 -26
@@ -0,0 +1,334 @@
1
+ import { boxRing, clamp01, lerp, nearest, polyPath, seg, softRing, type Pt } from '../liquidPath'
2
+ import { morphEase, motionFor } from './liquidMatter'
3
+ import { motionMs, prefersReducedMotion } from '../motion'
4
+
5
+ // Soft press (Button): while someone presses, hovers or lets go, an SVG draws the button's own shape
6
+ // with a soft edge. The edge dents where the finger is (the middle, for a key) and follows it, a mouse
7
+ // near the edge pulls it out a little, and a let-go sends one small wave round the edge. A wet spot
8
+ // spreads under the press and fades after. The label never moves. At rest the SVG is gone and the
9
+ // button's CSS shape is back, exact.
10
+
11
+ const NS = 'http://www.w3.org/2000/svg'
12
+ let ids = 0
13
+
14
+ export type SoftLook = { layer: string; fill: string; ring: string; wet: string }
15
+
16
+ type Ring = Array<[number, number, number, number]>
17
+
18
+ type Live = {
19
+ el: HTMLElement
20
+ svg: SVGSVGElement
21
+ fill: SVGPathElement
22
+ ring: SVGPathElement
23
+ clip: SVGPathElement
24
+ wet: SVGCircleElement
25
+ outer: Ring
26
+ inner: Ring
27
+ w: number
28
+ h: number
29
+ top: number
30
+ bottom: number
31
+ meet: number
32
+ }
33
+
34
+ const fine = () => window.matchMedia('(hover: hover) and (pointer: fine)').matches
35
+
36
+ export type SoftPressOptions = {
37
+ /** Class names for the layer and its parts. */
38
+ look: SoftLook
39
+ /** Keys that press it, such as Enter and Space. */
40
+ keys: string[]
41
+ /** True while it must not respond, such as when disabled or loading. */
42
+ inert?: boolean
43
+ }
44
+
45
+ type PointerLike = Pick<PointerEvent, 'button' | 'clientX' | 'clientY' | 'pointerId' | 'pointerType'> & { currentTarget: EventTarget | null }
46
+ type KeyLike = Pick<KeyboardEvent, 'key' | 'repeat'> & { currentTarget: EventTarget | null }
47
+
48
+ /**
49
+ * Soft press for one button. Options are read at each event, so they can change. Feed it the button's
50
+ * events, or call `listen` on the button. Call `destroy` when the button goes, and `stop` when it turns inert.
51
+ */
52
+ export function createSoftPress(o: SoftPressOptions) {
53
+ const s = ({
54
+ live: null as Live | null,
55
+ dent: 0,
56
+ press: null as null | { into: boolean; from: number; t0: number; ms: number; wave: boolean },
57
+ held: false,
58
+ byKey: false,
59
+ at: [0, 0] as Pt,
60
+ aim: [0, 0] as Pt,
61
+ pull: 0,
62
+ pullAim: 0,
63
+ to: [0, 0] as Pt,
64
+ toAim: [0, 0] as Pt,
65
+ wave: null as null | { t0: number; ms: number; from: number[] },
66
+ wet: null as null | { t0: number; ms: number; fade: { t0: number; ms: number } | null },
67
+ last: 0,
68
+ raf: 0,
69
+ off: () => {},
70
+ })
71
+
72
+ // The shape for the button's size now. Runs again when the size changes while the layer is up, such
73
+ // as a label that changes on click.
74
+ const fit = (L: Pick<Live, 'el' | 'svg'>) => {
75
+ const { el, svg } = L
76
+ const w = el.offsetWidth
77
+ const h = el.offsetHeight
78
+ const css = getComputedStyle(el)
79
+ const line = parseFloat(css.borderTopWidth) || 0
80
+ const r = Math.min(h / 2, parseFloat(css.borderTopLeftRadius) || 0)
81
+ const outer = boxRing({ cx: w / 2, cy: h / 2, hw: w / 2, hh: h / 2, r }, 1)
82
+ const inner = boxRing({ cx: w / 2, cy: h / 2, hw: w / 2 - line, hh: h / 2 - line, r: Math.max(0, r - line) }, 1)
83
+ svg.setAttribute('width', String(w))
84
+ svg.setAttribute('height', String(h))
85
+ let len = 0
86
+ outer.forEach((p, i) => (len += Math.hypot(outer[(i + 1) % outer.length][0] - p[0], outer[(i + 1) % outer.length][1] - p[1])))
87
+ return { outer, inner, w, h, top: nearest(outer, w / 2, 0), bottom: nearest(outer, w / 2, h), meet: len / 2 }
88
+ }
89
+
90
+ const build = (el: HTMLElement): Live => {
91
+ const svg = document.createElementNS(NS, 'svg')
92
+ svg.setAttribute('class', o.look.layer)
93
+ svg.setAttribute('aria-hidden', 'true')
94
+ svg.setAttribute('data-gooee-liquid', '')
95
+ const id = `gooee-soft-${++ids}`
96
+ const defs = svg.appendChild(document.createElementNS(NS, 'defs'))
97
+ const grad = defs.appendChild(document.createElementNS(NS, 'radialGradient'))
98
+ grad.id = `${id}-wet`
99
+ for (const [at, o] of [['0', '0.9'], ['0.5', '0.6'], ['1', '0']]) {
100
+ const stop = grad.appendChild(document.createElementNS(NS, 'stop'))
101
+ stop.setAttribute('offset', at)
102
+ stop.setAttribute('stop-opacity', o)
103
+ }
104
+ const clipEl = defs.appendChild(document.createElementNS(NS, 'clipPath'))
105
+ clipEl.id = `${id}-clip`
106
+ const clip = clipEl.appendChild(document.createElementNS(NS, 'path'))
107
+ const fill = svg.appendChild(document.createElementNS(NS, 'path'))
108
+ fill.setAttribute('class', o.look.fill)
109
+ const ring = svg.appendChild(document.createElementNS(NS, 'path'))
110
+ ring.setAttribute('class', o.look.ring)
111
+ ring.setAttribute('fill-rule', 'evenodd')
112
+ const wet = svg.appendChild(document.createElementNS(NS, 'circle'))
113
+ wet.setAttribute('class', o.look.wet)
114
+ wet.setAttribute('fill', `url(#${id}-wet)`)
115
+ wet.setAttribute('clip-path', `url(#${id}-clip)`)
116
+ wet.setAttribute('r', '0')
117
+ el.prepend(svg)
118
+ el.dataset.soft = ''
119
+ return { el, svg, fill, ring, clip, wet, ...fit({ el, svg }) }
120
+ }
121
+
122
+ const teardown = () => {
123
+ cancelAnimationFrame(s.raf)
124
+ s.raf = 0
125
+ s.off()
126
+ s.off = () => {}
127
+ if (s.live) {
128
+ const el = s.live.el
129
+ s.live.svg.remove()
130
+ // The button's own paint comes back at once, with no fade, so the hand-over does not show.
131
+ const own = el.style.transition
132
+ el.style.transition = 'none'
133
+ delete el.dataset.soft
134
+ delete el.dataset.held
135
+ void getComputedStyle(el).backgroundColor
136
+ el.style.transition = own
137
+ }
138
+ s.live = null
139
+ s.dent = 0
140
+ s.press = null
141
+ s.held = false
142
+ s.pull = 0
143
+ s.pullAim = 0
144
+ s.wave = null
145
+ s.wet = null
146
+ }
147
+
148
+ const frame = (now: number) => {
149
+ s.raf = 0
150
+ const L = s.live
151
+ if (!L) return
152
+ if (L.el.offsetWidth !== L.w || L.el.offsetHeight !== L.h) Object.assign(L, fit(L))
153
+ const dt = Math.max(0, now - s.last)
154
+ s.last = now
155
+ const e = morphEase()
156
+ const still = prefersReducedMotion()
157
+ const fast = motionMs('fast') || 1
158
+ const follow = still ? 1 : 1 - Math.exp(-dt / (fast / 3.5))
159
+ s.at = [s.at[0] + (s.aim[0] - s.at[0]) * follow, s.at[1] + (s.aim[1] - s.at[1]) * follow]
160
+ const pullTo = s.held || s.press || s.wave ? 0 : s.pullAim
161
+ s.pull = still ? 0 : s.pull + (pullTo - s.pull) * (1 - Math.exp(-dt / (fast / 1.5)))
162
+ if (pullTo === 0 && s.pull < 0.01) s.pull = 0
163
+ const ease = still ? 1 : 1 - Math.exp(-dt / (fast / 2.3))
164
+ s.to = [s.to[0] + (s.toAim[0] - s.to[0]) * ease, s.to[1] + (s.toAim[1] - s.to[1]) * ease]
165
+ const depth = parseFloat(getComputedStyle(L.el).getPropertyValue('--gooee-press-depth')) || 0
166
+ const p = s.press
167
+ if (p) {
168
+ const k = p.ms ? clamp01((now - p.t0) / p.ms) : 1
169
+ if (p.into) s.dent = lerp(p.from, depth, e(k))
170
+ else s.dent = p.wave ? p.from * (1 - e(seg(k, 0, 0.45))) : 0
171
+ if (!p.into && k >= 1) {
172
+ s.press = null
173
+ s.dent = 0
174
+ }
175
+ }
176
+ let wave = 0
177
+ let run = 0
178
+ if (s.wave) {
179
+ const k = s.wave.ms ? clamp01((now - s.wave.t0) / s.wave.ms) : 1
180
+ if (k >= 1) s.wave = null
181
+ else {
182
+ // It slows near the far side and fades out on the morph curve in its second half.
183
+ wave = Math.sin((Math.PI / 2) * clamp01(k / 0.12)) * (1 - e(seg(k, 0.55, 1)))
184
+ run = (1 - (1 - k) ** 2) * (L.meet / s.wave.from.length)
185
+ }
186
+ }
187
+ let wetOn = false
188
+ if (s.wet) {
189
+ const r = lerp(L.h * 0.21, L.h, s.wet.ms ? e(clamp01((now - s.wet.t0) / s.wet.ms)) : 1)
190
+ const f = s.wet.fade
191
+ const o = f ? (f.ms ? 1 - e(clamp01((now - f.t0) / f.ms)) : 0) : 1
192
+ if (o <= 0.002) s.wet = null
193
+ else {
194
+ wetOn = true
195
+ L.wet.setAttribute('cx', s.at[0].toFixed(2))
196
+ L.wet.setAttribute('cy', s.at[1].toFixed(2))
197
+ L.wet.setAttribute('r', r.toFixed(2))
198
+ L.wet.setAttribute('opacity', o.toFixed(3))
199
+ }
200
+ }
201
+ L.wet.style.display = wetOn ? '' : 'none'
202
+ const shape = { dent: s.dent, at: s.at, pull: s.pull, to: s.to, wave, run, from: s.wave?.from ?? [] }
203
+ if (!s.dent && !s.pull && !wave && !wetOn && !s.held && !s.press && !s.pullAim) {
204
+ teardown()
205
+ return
206
+ }
207
+ const outer = polyPath(softRing(L.outer, L.h, shape))
208
+ L.fill.setAttribute('d', outer)
209
+ L.clip.setAttribute('d', outer)
210
+ L.ring.setAttribute('d', outer + polyPath(softRing(L.inner, L.h, shape)))
211
+ s.raf = requestAnimationFrame(frame)
212
+ }
213
+
214
+ const wake = (el: HTMLElement) => {
215
+ if (!s.live) {
216
+ s.live = build(el)
217
+ s.at = [...s.aim]
218
+ s.to = [...s.toAim]
219
+ s.last = performance.now()
220
+ }
221
+ if (!s.raf) s.raf = requestAnimationFrame(frame)
222
+ }
223
+
224
+ const local = (el: HTMLElement, x: number, y: number): Pt => {
225
+ const r = el.getBoundingClientRect()
226
+ return [x - r.left, y - r.top]
227
+ }
228
+
229
+ const press = (el: HTMLElement, at: Pt, byKey: boolean) => {
230
+ s.aim = at
231
+ if (!s.held) s.at = [...at]
232
+ s.held = true
233
+ s.byKey = byKey
234
+ s.wave = null
235
+ el.dataset.held = ''
236
+ wake(el)
237
+ const L = s.live!
238
+ s.at = [...at]
239
+ s.press = { into: true, from: s.dent, t0: performance.now(), ms: motionFor('fast'), wave: false }
240
+ s.wet = { t0: performance.now(), ms: motionFor('slower'), fade: null }
241
+ // The wet spot darkens a light fill and lightens a dark one.
242
+ const rgb = getComputedStyle(L.fill).fill.match(/[\d.]+/g)?.map(Number) ?? [255, 255, 255, 0]
243
+ const light = (rgb[3] ?? 1) < 0.1 || 0.2126 * rgb[0] + 0.7152 * rgb[1] + 0.0722 * rgb[2] > 140
244
+ L.svg.dataset.wet = light ? 'dark' : 'light'
245
+ }
246
+
247
+ const release = () => {
248
+ if (!s.held || !s.live) return
249
+ s.held = false
250
+ delete s.live.el.dataset.held
251
+ s.off()
252
+ s.off = () => {}
253
+ const ms = motionFor('slow')
254
+ s.press = { into: false, from: s.dent, t0: performance.now(), ms, wave: ms > 0 }
255
+ if (ms) s.wave = { t0: performance.now(), ms, from: s.byKey ? [s.live.top, s.live.bottom] : [nearest(s.live.outer, s.at[0], s.at[1])] }
256
+ if (s.wet) s.wet.fade = { t0: performance.now(), ms: motionFor('slow') }
257
+ wake(s.live.el)
258
+ }
259
+
260
+ const down = (e: PointerLike) => {
261
+ if (o.inert || e.button !== 0) return
262
+ const el = e.currentTarget as HTMLElement
263
+ press(el, local(el, e.clientX, e.clientY), false)
264
+ const move = (m: PointerEvent) => {
265
+ if (m.pointerId === e.pointerId) s.aim = local(el, m.clientX, m.clientY)
266
+ }
267
+ const up = (m: PointerEvent) => {
268
+ if (m.pointerId === e.pointerId) release()
269
+ }
270
+ window.addEventListener('pointermove', move)
271
+ window.addEventListener('pointerup', up)
272
+ window.addEventListener('pointercancel', up)
273
+ s.off = () => {
274
+ window.removeEventListener('pointermove', move)
275
+ window.removeEventListener('pointerup', up)
276
+ window.removeEventListener('pointercancel', up)
277
+ }
278
+ }
279
+ const hover = (e: PointerLike) => {
280
+ if (o.inert || e.pointerType !== 'mouse' || !fine() || prefersReducedMotion() || s.held) return
281
+ const el = e.currentTarget as HTMLElement
282
+ const [x, y] = local(el, e.clientX, e.clientY)
283
+ const w = el.offsetWidth
284
+ const h = el.offsetHeight
285
+ // Strongest at the edge, less toward the middle.
286
+ const edge = Math.min(x, w - x, y, h - y)
287
+ s.pullAim = Math.exp(-(edge * edge) / (2 * (h * 0.58) ** 2))
288
+ const ring = s.live?.outer ?? boxRing({ cx: w / 2, cy: h / 2, hw: w / 2, hh: h / 2, r: h / 2 }, 1)
289
+ const p = ring[nearest(ring, x, y)]
290
+ s.toAim = [p[0], p[1]]
291
+ wake(el)
292
+ }
293
+ const leave = () => {
294
+ s.pullAim = 0
295
+ }
296
+ const keyDown = (e: KeyLike) => {
297
+ if (o.inert || e.repeat || !o.keys.includes(e.key)) return
298
+ const el = e.currentTarget as HTMLElement
299
+ press(el, [el.offsetWidth / 2, el.offsetHeight / 2], true)
300
+ }
301
+ const keyUp = (e: KeyLike) => {
302
+ if (o.keys.includes(e.key) && s.byKey) release()
303
+ }
304
+ const blur = () => {
305
+ if (s.byKey) release()
306
+ }
307
+
308
+ return {
309
+ down,
310
+ hover,
311
+ leave,
312
+ keyDown,
313
+ keyUp,
314
+ blur,
315
+ /** Ends any press at once and removes the drawing. */
316
+ stop: teardown,
317
+ destroy: teardown,
318
+ /** Listens on the button. Returns a function that stops listening and removes the drawing. */
319
+ listen(button: HTMLElement) {
320
+ const ac = new AbortController()
321
+ const opts = { signal: ac.signal }
322
+ button.addEventListener('pointerdown', down, opts)
323
+ button.addEventListener('pointermove', hover, opts)
324
+ button.addEventListener('pointerleave', leave, opts)
325
+ button.addEventListener('keydown', keyDown, opts)
326
+ button.addEventListener('keyup', keyUp, opts)
327
+ button.addEventListener('blur', blur, opts)
328
+ return () => {
329
+ ac.abort()
330
+ teardown()
331
+ }
332
+ },
333
+ }
334
+ }
@@ -0,0 +1,205 @@
1
+ import { easing, readMotionMs } from '../motion'
2
+
3
+ // SplitFlap's board with no framework: each cell runs forward through its drum to the new letter, and
4
+ // the board's edge grows or folds when the text gets longer or shorter.
5
+
6
+ /** The class names the board draws with (SplitFlap.module.css). */
7
+ export type FlapLook = { cell: string; top: string; bottom: string; glyph: string; flap: string; front: string; back: string }
8
+
9
+ /** The drum of a real board: a blank, letters, digits and a few marks. */
10
+ export const SPLIT_FLAP_CHARS = ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789:.,-+%/'
11
+
12
+ // A fold of about 90ms, fast steps of 40ms, and the last three slow down. A cell passes 8 letters at
13
+ // most, and the cells start one after another within 350ms, so a whole change ends inside a second.
14
+ const FOLD = 90
15
+ const FAST = 40
16
+ const TAIL = [70, 100, 140]
17
+ const MAX_STEPS = 8
18
+ const STAGGER = 35
19
+ const CASCADE_CAP = 350
20
+ // The fast motion token at its base, so Retime scales the board with everything else.
21
+ const FAST_TOKEN = 140
22
+
23
+ type Cell = { root: HTMLSpanElement; top: HTMLSpanElement; bottom: HTMLSpanElement; shown: string }
24
+
25
+ /** The letters a cell passes on its way from `from` to `to`, at most MAX_STEPS, ending on `to`. */
26
+ function route(from: string, to: string, drum: string) {
27
+ const a = drum.indexOf(from)
28
+ const b = drum.indexOf(to)
29
+ if (a < 0 || b < 0) return from === to ? [] : [to]
30
+ const n = Math.min((b - a + drum.length) % drum.length, MAX_STEPS)
31
+ return Array.from({ length: n }, (_, i) => drum[(b - n + 1 + i + drum.length) % drum.length])
32
+ }
33
+
34
+ /** The wait before each step: fast in the middle, slow for the last three. */
35
+ const gaps = (n: number) => Array.from({ length: n }, (_, i) => (n - 1 - i < TAIL.length ? TAIL[TAIL.length - n + i] : FAST))
36
+
37
+ const NBSP = ' '
38
+ const shownChar = (ch: string) => (ch === ' ' ? NBSP : ch)
39
+ const escape = (ch: string) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;' })[ch] ?? ch
40
+
41
+ const halfHtml = (look: FlapLook, cls: string, ch: string) => `<span class="${cls}"><span class="${look.glyph}">${escape(shownChar(ch))}</span></span>`
42
+ const cellHtml = (look: FlapLook, ch: string) => `<span class="${look.cell}">${halfHtml(look, look.top, ch)}${halfHtml(look, look.bottom, ch)}</span>`
43
+
44
+ /** The first cells as markup, so a server page shows the board before any script runs. */
45
+ export const flapCellsHtml = (look: FlapLook, target: string) => [...target].map((ch) => cellHtml(look, ch)).join('')
46
+
47
+ const fromHtml = (html: string) => {
48
+ const t = document.createElement('template')
49
+ t.innerHTML = html
50
+ return t.content.firstElementChild as HTMLSpanElement
51
+ }
52
+
53
+ const makeCell = (look: FlapLook, ch: string): Cell => {
54
+ const root = fromHtml(cellHtml(look, ch))
55
+ return { root, top: root.children[0] as HTMLSpanElement, bottom: root.children[1] as HTMLSpanElement, shown: ch }
56
+ }
57
+
58
+ const setHalf = (h: Element, ch: string) => {
59
+ ;(h.firstElementChild as HTMLElement).textContent = shownChar(ch)
60
+ }
61
+
62
+ /**
63
+ * Runs a board whose cells are already in `b` and show `first`. `show` flips them to new text and calls
64
+ * `onDone` once every cell has landed; a call in the middle of a run goes straight on to the newest text.
65
+ */
66
+ export function createSplitFlap(b: HTMLElement, look: FlapLook, first: string) {
67
+ let cells: Cell[] = [...b.children].map((root, i) => ({
68
+ root: root as HTMLSpanElement,
69
+ top: root.children[0] as HTMLSpanElement,
70
+ bottom: root.children[1] as HTMLSpanElement,
71
+ shown: [...first][i] ?? ' ',
72
+ }))
73
+ let timers: number[] = []
74
+ const anims = new Set<Animation>()
75
+ let run = 0
76
+ let shrinkEnd: (() => void) | null = null
77
+
78
+ const stopAll = () => {
79
+ timers.forEach(clearTimeout)
80
+ timers = []
81
+ anims.forEach((a) => a.finish())
82
+ anims.clear()
83
+ shrinkEnd?.()
84
+ // Caught mid-run, each cell carries on from the letter it was showing.
85
+ for (const c of cells) setHalf(c.bottom, c.shown)
86
+ }
87
+
88
+ const show = (target: string, chars: string, onDone: () => void) => {
89
+ const id = ++run
90
+ const live = () => id === run
91
+ const running = timers.length > 0 || anims.size > 0
92
+ stopAll()
93
+ const done = () => {
94
+ if (!live()) return
95
+ timers = []
96
+ onDone()
97
+ }
98
+ const list = cells
99
+ if (list.map((c) => c.shown).join('') === target) {
100
+ if (running) done()
101
+ return
102
+ }
103
+
104
+ const scale = readMotionMs('fast') / FAST_TOKEN
105
+ // Reduced motion: the new text at once, with no flips and no morph.
106
+ if (!scale) {
107
+ cells = [...target].map((ch) => makeCell(look, ch))
108
+ b.replaceChildren(...cells.map((c) => c.root))
109
+ done()
110
+ return
111
+ }
112
+
113
+ const n0 = list.length
114
+ const n1 = target.length
115
+ const radius = getComputedStyle(b).borderRadius || '0px'
116
+ const grow = { duration: readMotionMs('morph'), easing: easing('morph') }
117
+ const shrink = { duration: readMotionMs('morph-out'), easing: easing('morph') }
118
+ let left = 0
119
+ const landed = () => {
120
+ if (--left === 0) done()
121
+ }
122
+
123
+ // Longer: the new cells are laid out at once, and the board's edge grows over them.
124
+ if (n1 > n0) {
125
+ const before = b.offsetWidth
126
+ for (let i = n0; i < n1; i++) {
127
+ const c = makeCell(look, ' ')
128
+ list.push(c)
129
+ b.append(c.root)
130
+ }
131
+ const wider = b.offsetWidth - before
132
+ const a = b.animate({ clipPath: [`inset(0 ${wider}px 0 0 round ${radius})`, `inset(0 0 0 0 round ${radius})`] }, grow)
133
+ anims.add(a)
134
+ a.finished.then(
135
+ () => anims.delete(a),
136
+ () => {},
137
+ )
138
+ }
139
+
140
+ // Shorter: the board's edge folds over the extra cells while the rest flip, then they go.
141
+ if (n1 < n0) {
142
+ const keep = list.slice(0, n1)
143
+ const box = b.getBoundingClientRect()
144
+ const edge = keep.length ? keep[keep.length - 1].root.getBoundingClientRect().right : box.left
145
+ const a = b.animate({ clipPath: [`inset(0 0 0 0 round ${radius})`, `inset(0 ${box.right - edge}px 0 0 round ${radius})`] }, { ...shrink, fill: 'forwards' })
146
+ left++
147
+ const end = () => {
148
+ shrinkEnd = null
149
+ list.slice(n1).forEach((c) => c.root.remove())
150
+ cells = keep
151
+ a.cancel()
152
+ if (live()) landed()
153
+ }
154
+ shrinkEnd = end
155
+ a.finished.then(
156
+ () => shrinkEnd === end && end(),
157
+ () => {},
158
+ )
159
+ }
160
+
161
+ const goal = [...target]
162
+ const changed = goal.map((ch, i) => (list[i].shown !== ch ? i : -1)).filter((i) => i >= 0)
163
+ const stagger = Math.min(STAGGER, CASCADE_CAP / Math.max(1, changed.length))
164
+ left += changed.length
165
+ if (!left) return done()
166
+
167
+ const flip = (cell: Cell, next: string, last: boolean, fold: number) => {
168
+ const prev = cell.shown
169
+ cell.shown = next
170
+ setHalf(cell.top, next)
171
+ // The falling flap: the old letter's top half in front, the new letter's bottom half behind.
172
+ const flap = fromHtml(`<span class="${look.flap}">${halfHtml(look, look.front, prev)}${halfHtml(look, look.back, next)}</span>`)
173
+ cell.root.append(flap)
174
+ const a = flap.animate([{ transform: 'rotateX(0deg)' }, { transform: 'rotateX(-180deg)' }], { duration: fold, easing: easing('morph') })
175
+ anims.add(a)
176
+ const end = () => {
177
+ anims.delete(a)
178
+ setHalf(cell.bottom, next)
179
+ flap.remove()
180
+ if (last && live()) landed()
181
+ }
182
+ a.finished.then(end, end)
183
+ }
184
+
185
+ changed.forEach((i, order) => {
186
+ const cell = list[i]
187
+ const path = route(cell.shown, goal[i], chars)
188
+ const wait = gaps(path.length)
189
+ let t = order * stagger
190
+ path.forEach((next, k) => {
191
+ timers.push(window.setTimeout(() => flip(cell, next, k === path.length - 1, Math.max(FOLD, wait[k]) * scale), t * scale))
192
+ t += wait[k]
193
+ })
194
+ })
195
+ }
196
+
197
+ // Gone: the run is over, so a cancelled flip ends without calling onDone.
198
+ const destroy = () => {
199
+ run++
200
+ timers.forEach(clearTimeout)
201
+ anims.forEach((a) => a.cancel())
202
+ }
203
+
204
+ return { show, destroy }
205
+ }