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
@@ -1,299 +1,37 @@
1
1
  import { useEffect, useRef, type KeyboardEvent, type PointerEvent } from 'react'
2
- import { boxRing, clamp01, lerp, nearest, polyPath, seg, softRing, type Pt } from './liquidPath'
3
- import { morphEase, motionFor } from './liquidMatter'
4
- import { motionMs, prefersReducedMotion } from './motion'
2
+ import { createSoftPress, type SoftLook } from './motion/softPress'
5
3
 
6
- // Soft press (Button): while someone presses, hovers or lets go, an SVG draws the button's own shape
7
- // with a soft edge. The edge dents where the finger is (the middle, for a key) and follows it, a mouse
8
- // near the edge pulls it out a little, and a let-go sends one small wave round the edge. A wet spot
9
- // spreads under the press and fades after. The label never moves. At rest the SVG is gone and the
10
- // button's CSS shape is back, exact.
11
-
12
- const NS = 'http://www.w3.org/2000/svg'
13
- let ids = 0
14
-
15
- export type SoftLook = { layer: string; fill: string; ring: string; wet: string }
16
-
17
- type Ring = Array<[number, number, number, number]>
18
-
19
- type Live = {
20
- el: HTMLElement
21
- svg: SVGSVGElement
22
- fill: SVGPathElement
23
- ring: SVGPathElement
24
- clip: SVGPathElement
25
- wet: SVGCircleElement
26
- outer: Ring
27
- inner: Ring
28
- w: number
29
- h: number
30
- top: number
31
- bottom: number
32
- meet: number
33
- }
34
-
35
- const fine = () => window.matchMedia('(hover: hover) and (pointer: fine)').matches
4
+ export type { SoftLook } from './motion/softPress'
36
5
 
6
+ /** React's hold on createSoftPress (motion/softPress.ts): returns the button's event handlers. */
37
7
  export function useSoftPress(look: SoftLook, inert: boolean, keys: string[]) {
38
- const s = useRef({
39
- live: null as Live | null,
40
- dent: 0,
41
- press: null as null | { into: boolean; from: number; t0: number; ms: number; wave: boolean },
42
- held: false,
43
- byKey: false,
44
- at: [0, 0] as Pt,
45
- aim: [0, 0] as Pt,
46
- pull: 0,
47
- pullAim: 0,
48
- to: [0, 0] as Pt,
49
- toAim: [0, 0] as Pt,
50
- wave: null as null | { t0: number; ms: number; from: number[] },
51
- wet: null as null | { t0: number; ms: number; fade: { t0: number; ms: number } | null },
52
- last: 0,
53
- raf: 0,
54
- off: () => {},
55
- }).current
56
-
57
- // The shape for the button's size now. Runs again when the size changes while the layer is up, such
58
- // as a label that changes on click.
59
- const fit = (L: Pick<Live, 'el' | 'svg'>) => {
60
- const { el, svg } = L
61
- const w = el.offsetWidth
62
- const h = el.offsetHeight
63
- const css = getComputedStyle(el)
64
- const line = parseFloat(css.borderTopWidth) || 0
65
- const r = Math.min(h / 2, parseFloat(css.borderTopLeftRadius) || 0)
66
- const outer = boxRing({ cx: w / 2, cy: h / 2, hw: w / 2, hh: h / 2, r }, 1)
67
- const inner = boxRing({ cx: w / 2, cy: h / 2, hw: w / 2 - line, hh: h / 2 - line, r: Math.max(0, r - line) }, 1)
68
- svg.setAttribute('width', String(w))
69
- svg.setAttribute('height', String(h))
70
- let len = 0
71
- outer.forEach((p, i) => (len += Math.hypot(outer[(i + 1) % outer.length][0] - p[0], outer[(i + 1) % outer.length][1] - p[1])))
72
- return { outer, inner, w, h, top: nearest(outer, w / 2, 0), bottom: nearest(outer, w / 2, h), meet: len / 2 }
73
- }
74
-
75
- const build = (el: HTMLElement): Live => {
76
- const svg = document.createElementNS(NS, 'svg')
77
- svg.setAttribute('class', look.layer)
78
- svg.setAttribute('aria-hidden', 'true')
79
- svg.setAttribute('data-gooee-liquid', '')
80
- const id = `gooee-soft-${++ids}`
81
- const defs = svg.appendChild(document.createElementNS(NS, 'defs'))
82
- const grad = defs.appendChild(document.createElementNS(NS, 'radialGradient'))
83
- grad.id = `${id}-wet`
84
- for (const [at, o] of [['0', '0.9'], ['0.5', '0.6'], ['1', '0']]) {
85
- const stop = grad.appendChild(document.createElementNS(NS, 'stop'))
86
- stop.setAttribute('offset', at)
87
- stop.setAttribute('stop-opacity', o)
88
- }
89
- const clipEl = defs.appendChild(document.createElementNS(NS, 'clipPath'))
90
- clipEl.id = `${id}-clip`
91
- const clip = clipEl.appendChild(document.createElementNS(NS, 'path'))
92
- const fill = svg.appendChild(document.createElementNS(NS, 'path'))
93
- fill.setAttribute('class', look.fill)
94
- const ring = svg.appendChild(document.createElementNS(NS, 'path'))
95
- ring.setAttribute('class', look.ring)
96
- ring.setAttribute('fill-rule', 'evenodd')
97
- const wet = svg.appendChild(document.createElementNS(NS, 'circle'))
98
- wet.setAttribute('class', look.wet)
99
- wet.setAttribute('fill', `url(#${id}-wet)`)
100
- wet.setAttribute('clip-path', `url(#${id}-clip)`)
101
- wet.setAttribute('r', '0')
102
- el.prepend(svg)
103
- el.dataset.soft = ''
104
- return { el, svg, fill, ring, clip, wet, ...fit({ el, svg }) }
105
- }
106
-
107
- const teardown = () => {
108
- cancelAnimationFrame(s.raf)
109
- s.raf = 0
110
- s.off()
111
- s.off = () => {}
112
- if (s.live) {
113
- const el = s.live.el
114
- s.live.svg.remove()
115
- // The button's own paint comes back at once, with no fade, so the hand-over does not show.
116
- const own = el.style.transition
117
- el.style.transition = 'none'
118
- delete el.dataset.soft
119
- delete el.dataset.held
120
- void getComputedStyle(el).backgroundColor
121
- el.style.transition = own
122
- }
123
- s.live = null
124
- s.dent = 0
125
- s.press = null
126
- s.held = false
127
- s.pull = 0
128
- s.pullAim = 0
129
- s.wave = null
130
- s.wet = null
131
- }
132
-
133
- const frame = (now: number) => {
134
- s.raf = 0
135
- const L = s.live
136
- if (!L) return
137
- if (L.el.offsetWidth !== L.w || L.el.offsetHeight !== L.h) Object.assign(L, fit(L))
138
- const dt = Math.max(0, now - s.last)
139
- s.last = now
140
- const e = morphEase()
141
- const still = prefersReducedMotion()
142
- const fast = motionMs('fast') || 1
143
- const follow = still ? 1 : 1 - Math.exp(-dt / (fast / 3.5))
144
- s.at = [s.at[0] + (s.aim[0] - s.at[0]) * follow, s.at[1] + (s.aim[1] - s.at[1]) * follow]
145
- const pullTo = s.held || s.press || s.wave ? 0 : s.pullAim
146
- s.pull = still ? 0 : s.pull + (pullTo - s.pull) * (1 - Math.exp(-dt / (fast / 1.5)))
147
- if (pullTo === 0 && s.pull < 0.01) s.pull = 0
148
- const ease = still ? 1 : 1 - Math.exp(-dt / (fast / 2.3))
149
- s.to = [s.to[0] + (s.toAim[0] - s.to[0]) * ease, s.to[1] + (s.toAim[1] - s.to[1]) * ease]
150
- const depth = parseFloat(getComputedStyle(L.el).getPropertyValue('--gooee-press-depth')) || 0
151
- const p = s.press
152
- if (p) {
153
- const k = p.ms ? clamp01((now - p.t0) / p.ms) : 1
154
- if (p.into) s.dent = lerp(p.from, depth, e(k))
155
- else s.dent = p.wave ? p.from * (1 - e(seg(k, 0, 0.45))) : 0
156
- if (!p.into && k >= 1) {
157
- s.press = null
158
- s.dent = 0
159
- }
160
- }
161
- let wave = 0
162
- let run = 0
163
- if (s.wave) {
164
- const k = s.wave.ms ? clamp01((now - s.wave.t0) / s.wave.ms) : 1
165
- if (k >= 1) s.wave = null
166
- else {
167
- // It slows near the far side and fades out on the morph curve in its second half.
168
- wave = Math.sin((Math.PI / 2) * clamp01(k / 0.12)) * (1 - e(seg(k, 0.55, 1)))
169
- run = (1 - (1 - k) ** 2) * (L.meet / s.wave.from.length)
170
- }
171
- }
172
- let wetOn = false
173
- if (s.wet) {
174
- const r = lerp(L.h * 0.21, L.h, s.wet.ms ? e(clamp01((now - s.wet.t0) / s.wet.ms)) : 1)
175
- const f = s.wet.fade
176
- const o = f ? (f.ms ? 1 - e(clamp01((now - f.t0) / f.ms)) : 0) : 1
177
- if (o <= 0.002) s.wet = null
178
- else {
179
- wetOn = true
180
- L.wet.setAttribute('cx', s.at[0].toFixed(2))
181
- L.wet.setAttribute('cy', s.at[1].toFixed(2))
182
- L.wet.setAttribute('r', r.toFixed(2))
183
- L.wet.setAttribute('opacity', o.toFixed(3))
184
- }
185
- }
186
- L.wet.style.display = wetOn ? '' : 'none'
187
- const shape = { dent: s.dent, at: s.at, pull: s.pull, to: s.to, wave, run, from: s.wave?.from ?? [] }
188
- if (!s.dent && !s.pull && !wave && !wetOn && !s.held && !s.press && !s.pullAim) {
189
- teardown()
190
- return
191
- }
192
- const outer = polyPath(softRing(L.outer, L.h, shape))
193
- L.fill.setAttribute('d', outer)
194
- L.clip.setAttribute('d', outer)
195
- L.ring.setAttribute('d', outer + polyPath(softRing(L.inner, L.h, shape)))
196
- s.raf = requestAnimationFrame(frame)
197
- }
198
-
199
- const wake = (el: HTMLElement) => {
200
- if (!s.live) {
201
- s.live = build(el)
202
- s.at = [...s.aim]
203
- s.to = [...s.toAim]
204
- s.last = performance.now()
205
- }
206
- if (!s.raf) s.raf = requestAnimationFrame(frame)
207
- }
208
-
209
- const local = (el: HTMLElement, x: number, y: number): Pt => {
210
- const r = el.getBoundingClientRect()
211
- return [x - r.left, y - r.top]
212
- }
213
-
214
- const press = (el: HTMLElement, at: Pt, byKey: boolean) => {
215
- s.aim = at
216
- if (!s.held) s.at = [...at]
217
- s.held = true
218
- s.byKey = byKey
219
- s.wave = null
220
- el.dataset.held = ''
221
- wake(el)
222
- const L = s.live!
223
- s.at = [...at]
224
- s.press = { into: true, from: s.dent, t0: performance.now(), ms: motionFor('fast'), wave: false }
225
- s.wet = { t0: performance.now(), ms: motionFor('slower'), fade: null }
226
- // The wet spot darkens a light fill and lightens a dark one.
227
- const rgb = getComputedStyle(L.fill).fill.match(/[\d.]+/g)?.map(Number) ?? [255, 255, 255, 0]
228
- const light = (rgb[3] ?? 1) < 0.1 || 0.2126 * rgb[0] + 0.7152 * rgb[1] + 0.0722 * rgb[2] > 140
229
- L.svg.dataset.wet = light ? 'dark' : 'light'
230
- }
231
-
232
- const release = () => {
233
- if (!s.held || !s.live) return
234
- s.held = false
235
- delete s.live.el.dataset.held
236
- s.off()
237
- s.off = () => {}
238
- const ms = motionFor('slow')
239
- s.press = { into: false, from: s.dent, t0: performance.now(), ms, wave: ms > 0 }
240
- 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])] }
241
- if (s.wet) s.wet.fade = { t0: performance.now(), ms: motionFor('slow') }
242
- wake(s.live.el)
243
- }
8
+ const props = useRef({ look, inert, keys })
9
+ props.current = { look, inert, keys }
10
+ const soft = useRef<ReturnType<typeof createSoftPress>>(null)
11
+ soft.current ??= createSoftPress({
12
+ get look() {
13
+ return props.current.look
14
+ },
15
+ get keys() {
16
+ return props.current.keys
17
+ },
18
+ get inert() {
19
+ return props.current.inert
20
+ },
21
+ })
22
+ const p = soft.current
244
23
 
245
- useEffect(() => teardown, [])
24
+ useEffect(() => p.destroy, [p])
246
25
  useEffect(() => {
247
- if (inert) teardown()
248
- }, [inert])
26
+ if (inert) p.stop()
27
+ }, [inert, p])
249
28
 
250
29
  return {
251
- onPointerDown: (e: PointerEvent<HTMLElement>) => {
252
- if (inert || e.button !== 0) return
253
- const el = e.currentTarget
254
- press(el, local(el, e.clientX, e.clientY), false)
255
- const move = (m: globalThis.PointerEvent) => {
256
- if (m.pointerId === e.pointerId) s.aim = local(el, m.clientX, m.clientY)
257
- }
258
- const up = (m: globalThis.PointerEvent) => {
259
- if (m.pointerId === e.pointerId) release()
260
- }
261
- window.addEventListener('pointermove', move)
262
- window.addEventListener('pointerup', up)
263
- window.addEventListener('pointercancel', up)
264
- s.off = () => {
265
- window.removeEventListener('pointermove', move)
266
- window.removeEventListener('pointerup', up)
267
- window.removeEventListener('pointercancel', up)
268
- }
269
- },
270
- onPointerMove: (e: PointerEvent<HTMLElement>) => {
271
- if (inert || e.pointerType !== 'mouse' || !fine() || prefersReducedMotion() || s.held) return
272
- const el = e.currentTarget
273
- const [x, y] = local(el, e.clientX, e.clientY)
274
- const w = el.offsetWidth
275
- const h = el.offsetHeight
276
- // Strongest at the edge, less toward the middle.
277
- const edge = Math.min(x, w - x, y, h - y)
278
- s.pullAim = Math.exp(-(edge * edge) / (2 * (h * 0.58) ** 2))
279
- const ring = s.live?.outer ?? boxRing({ cx: w / 2, cy: h / 2, hw: w / 2, hh: h / 2, r: h / 2 }, 1)
280
- const p = ring[nearest(ring, x, y)]
281
- s.toAim = [p[0], p[1]]
282
- wake(el)
283
- },
284
- onPointerLeave: () => {
285
- s.pullAim = 0
286
- },
287
- onKeyDown: (e: KeyboardEvent<HTMLElement>) => {
288
- if (inert || e.repeat || !keys.includes(e.key)) return
289
- const el = e.currentTarget
290
- press(el, [el.offsetWidth / 2, el.offsetHeight / 2], true)
291
- },
292
- onKeyUp: (e: KeyboardEvent<HTMLElement>) => {
293
- if (keys.includes(e.key) && s.byKey) release()
294
- },
295
- onBlur: () => {
296
- if (s.byKey) release()
297
- },
30
+ onPointerDown: (e: PointerEvent<HTMLElement>) => p.down(e),
31
+ onPointerMove: (e: PointerEvent<HTMLElement>) => p.hover(e),
32
+ onPointerLeave: p.leave,
33
+ onKeyDown: (e: KeyboardEvent<HTMLElement>) => p.keyDown(e),
34
+ onKeyUp: (e: KeyboardEvent<HTMLElement>) => p.keyUp(e),
35
+ onBlur: p.blur,
298
36
  }
299
37
  }
@@ -1,63 +1,5 @@
1
1
  import { flushSync } from 'react-dom'
2
- import { easing, motionMs, prefersReducedMotion } from './motion'
2
+ import { themeReveal as reveal } from './motion/themeReveal'
3
3
 
4
- // The new theme grows from the spot you pressed: the page is snapshot as it is, and the new page opens
5
- // over it in a circle from the button's centre to the farthest corner, at an even pace. The old snapshot
6
- // stays still under it, and no element plays its own colour change meanwhile, so nothing flips piece by
7
- // piece. A second press finishes the running one first. Without View Transitions the page fades in once;
8
- // with reduced motion it changes at once.
9
-
10
- let running: ViewTransition | null = null
11
-
12
- // The radius whose circle covers `share` of the screen, by halving, counted on a coarse grid.
13
- function radiusFor(share: number, x: number, y: number, reach: number) {
14
- if (share >= 1) return reach
15
- const cols = 48, rows = 32
16
- const covered = (r: number) => {
17
- let n = 0
18
- for (let j = 0; j < rows; j++) for (let i = 0; i < cols; i++) if (Math.hypot(((i + 0.5) * innerWidth) / cols - x, ((j + 0.5) * innerHeight) / rows - y) <= r) n++
19
- return n / (cols * rows)
20
- }
21
- let lo = 0, hi = reach
22
- for (let k = 0; k < 18; k++) {
23
- const m = (lo + hi) / 2
24
- if (covered(m) < share) lo = m
25
- else hi = m
26
- }
27
- return hi
28
- }
29
-
30
- export function themeReveal(origin: Element | null, apply: () => void) {
31
- const root = document.documentElement
32
- const ms = motionMs('reveal')
33
- if (prefersReducedMotion() || !ms) return flushSync(apply)
34
- running?.skipTransition()
35
- root.dataset.gooeeThemeSwitch = ''
36
- const done = () => delete root.dataset.gooeeThemeSwitch
37
- if (typeof document.startViewTransition !== 'function') {
38
- flushSync(apply)
39
- document.body.animate([{ opacity: 0.4 }, { opacity: 1 }], { duration: ms * 0.6, easing: easing('morph') }).finished.then(done, done)
40
- return
41
- }
42
- const r = origin?.getBoundingClientRect()
43
- const x = r ? r.left + r.width / 2 : innerWidth / 2
44
- const y = r ? r.top + r.height / 2 : 0
45
- const reach = Math.hypot(Math.max(x, innerWidth - x), Math.max(y, innerHeight - y))
46
- const vt = document.startViewTransition(() => flushSync(apply))
47
- running = vt
48
- vt.ready.then(
49
- // The share of the screen covered grows at an even, calm pace (ease out): a circle's area grows with
50
- // the radius squared, so each step's radius is solved for its share; linear in between.
51
- () =>
52
- root.animate(
53
- { clipPath: Array.from({ length: 21 }, (_, i) => `circle(${radiusFor(1 - (1 - i / 20) ** 2, x, y, reach)}px at ${x}px ${y}px)`) },
54
- { duration: ms, easing: 'linear', pseudoElement: '::view-transition-new(root)' },
55
- ),
56
- () => {},
57
- )
58
- vt.finished.finally(() => {
59
- if (running !== vt) return
60
- running = null
61
- done()
62
- })
63
- }
4
+ /** React's hold on themeReveal (motion/themeReveal.ts): the new theme is put in the page at once with flushSync. */
5
+ export const themeReveal = (origin: Element | null, apply: () => void) => reveal(origin, () => flushSync(apply))
@@ -1,9 +1,7 @@
1
1
  import { useCallback, useLayoutEffect, useState, type CSSProperties, type RefObject } from 'react'
2
+ import { floatingStyle, follow, place, type Align, type Place, type Side } from './motion/floating'
2
3
 
3
- export type Side = 'top' | 'bottom' | 'left' | 'right'
4
- export type Align = 'start' | 'center' | 'end'
5
-
6
- const MARGIN = 8
4
+ export type { Align, Side } from './motion/floating'
7
5
 
8
6
  type Options = {
9
7
  open: boolean
@@ -13,45 +11,20 @@ type Options = {
13
11
  matchWidth?: boolean
14
12
  }
15
13
 
16
- // Before the first measure the layer is see-through, not hidden, so focus can still move into it.
14
+ /** React's hold on place and follow (motion/floating.ts). */
17
15
  export function useFloating(
18
16
  anchorRef: RefObject<HTMLElement | null>,
19
17
  floatingRef: RefObject<HTMLElement | null>,
20
18
  { open, side = 'bottom', align = 'center', offset = 8, matchWidth = false }: Options,
21
19
  ) {
22
- const [pos, setPos] = useState<{ top: number; left: number; side: Side; width?: number } | null>(null)
20
+ const [pos, setPos] = useState<Place | null>(null)
23
21
  // Measured since this open began.
24
22
  const [fresh, setFresh] = useState(false)
25
23
 
26
24
  const update = useCallback(() => {
27
- const a = anchorRef.current
28
- const f = floatingRef.current
29
- if (!a || !f) return
30
- const r = a.getBoundingClientRect()
31
- const width = matchWidth ? r.width : undefined
32
- const fw = width ?? f.offsetWidth
33
- const fh = f.offsetHeight
34
- const vw = window.innerWidth
35
- const vh = window.innerHeight
36
-
37
- let s = side
38
- if (s === 'bottom' && r.bottom + offset + fh > vh - MARGIN && r.top - offset - fh > MARGIN) s = 'top'
39
- else if (s === 'top' && r.top - offset - fh < MARGIN && r.bottom + offset + fh < vh - MARGIN) s = 'bottom'
40
- else if (s === 'right' && r.right + offset + fw > vw - MARGIN && r.left - offset - fw > MARGIN) s = 'left'
41
- else if (s === 'left' && r.left - offset - fw < MARGIN && r.right + offset + fw < vw - MARGIN) s = 'right'
42
-
43
- let top = 0
44
- let left = 0
45
- if (s === 'bottom' || s === 'top') {
46
- top = s === 'bottom' ? r.bottom + offset : r.top - offset - fh
47
- left = align === 'start' ? r.left : align === 'end' ? r.right - fw : r.left + r.width / 2 - fw / 2
48
- } else {
49
- left = s === 'right' ? r.right + offset : r.left - offset - fw
50
- top = align === 'start' ? r.top : align === 'end' ? r.bottom - fh : r.top + r.height / 2 - fh / 2
51
- }
52
- left = Math.min(Math.max(MARGIN, left), Math.max(MARGIN, vw - fw - MARGIN))
53
- top = Math.min(Math.max(MARGIN, top), Math.max(MARGIN, vh - fh - MARGIN))
54
- setPos({ top, left, side: s, width })
25
+ const at = place(anchorRef.current, floatingRef.current, { side, align, offset, matchWidth })
26
+ if (!at) return
27
+ setPos(at)
55
28
  setFresh(true)
56
29
  }, [anchorRef, floatingRef, side, align, offset, matchWidth])
57
30
 
@@ -62,23 +35,10 @@ export function useFloating(
62
35
  setFresh(false)
63
36
  return
64
37
  }
65
- update()
66
- const f = floatingRef.current
67
- const ro = new ResizeObserver(update)
68
- if (f) ro.observe(f)
69
- if (anchorRef.current) ro.observe(anchorRef.current)
70
- window.addEventListener('scroll', update, true)
71
- window.addEventListener('resize', update)
72
- return () => {
73
- ro.disconnect()
74
- window.removeEventListener('scroll', update, true)
75
- window.removeEventListener('resize', update)
76
- }
38
+ return follow(anchorRef.current, floatingRef.current, update)
77
39
  }, [open, update, floatingRef, anchorRef])
78
40
 
79
- const style: CSSProperties = pos
80
- ? { position: 'fixed', top: pos.top, left: pos.left, width: pos.width }
81
- : { position: 'fixed', top: 0, left: 0, opacity: 0, pointerEvents: 'none' }
41
+ const style = floatingStyle(pos) as CSSProperties
82
42
 
83
43
  // ready: the layer is in its place, so a part can measure it there (for its opening animation).
84
44
  return { style, side: pos?.side ?? side, update, ready: pos !== null && fresh }
@@ -1,150 +1,45 @@
1
1
  import { useEffect, useRef, useState, type KeyboardEvent, type PointerEvent } from 'react'
2
- import { createScrub, type ScrubOptions } from './feel'
3
- import { morphCurve, readMotionMs } from './motion'
2
+ import { createScrubber, type ScrubberOptions } from './motion/scrubber'
4
3
 
5
- // The React side of feel.ts for Dial, RulerScrubber and PickerWheel: pointer, keys and wheel in, the
6
- // drawn position out. The motion itself lives in feel.ts.
4
+ // React's hold on createScrubber (motion/scrubber.ts) for Dial, RulerScrubber and PickerWheel.
7
5
 
8
- type Last = { x: number; y: number }
9
-
10
- export type UseScrubOptions = Pick<ScrubOptions, 'min' | 'max' | 'edge' | 'land' | 'coast' | 'maxSpeed'> & {
11
- /** The value from props: a change from outside glides there. */
12
- value: number
13
- onSettle: (value: number) => void
14
- /** A new resting value came under the hand while it moves. */
15
- onMark?: (value: number) => void
16
- /** How far a pointer move turns the value, in its units. */
17
- move: (e: PointerEvent<HTMLElement>, last: Last, el: HTMLElement) => number
18
- /** The value a key goes to, or null to leave the key alone. */
19
- key: (key: string, target: number) => number | null
20
- /** The value a plain tap goes to, or null to land where it is. */
21
- tap?: (e: PointerEvent<HTMLElement>, at: number) => number | null
22
- /** The value a wheel turn goes to, or null to let the page scroll. */
23
- wheel?: (e: WheelEvent, target: number, focused: boolean) => number | null
24
- /** A short buzz on each new value under the hand, where the phone can (Android). */
25
- haptics?: boolean
26
- }
27
-
28
- type Press = { id: number; sx: number; sy: number; x: number; y: number; moved: boolean }
29
-
30
- // A press is not a drag until it moves this far, so a tap stays a tap.
31
- const SLOP = 4
6
+ export type UseScrubOptions = Omit<ScrubberOptions<PointerEvent<HTMLElement>>, 'onFrame' | 'onMoving'>
32
7
 
33
8
  export function useScrub(o: UseScrubOptions) {
34
- const opt = useRef(o)
35
- opt.current = o
36
9
  const [at, setAt] = useState(o.value)
37
10
  const [moving, setMoving] = useState(false)
38
- const press = useRef<Press | null>(null)
39
11
  const ref = useRef<HTMLDivElement>(null)
40
- const scrub = useRef<ReturnType<typeof createScrub> | null>(null)
41
- if (!scrub.current) {
42
- scrub.current = createScrub({
43
- value: o.value,
44
- land: (p) => opt.current.land(p),
45
- duration: () => readMotionMs('morph-out'),
46
- curve: morphCurve,
47
- onFrame: setAt,
48
- onMoving: setMoving,
49
- onMark: (v) => {
50
- opt.current.onMark?.(v)
51
- if (opt.current.haptics) navigator.vibrate?.(5)
52
- },
53
- // Only a new value is news: a tap, Home at the start, or a drag back to where it was says nothing.
54
- onSettle: (v) => v !== opt.current.value && opt.current.onSettle(v),
55
- })
56
- }
12
+ const scrub = useRef<ReturnType<typeof createScrubber<PointerEvent<HTMLElement>>>>(null)
13
+ const options = { ...o, onFrame: setAt, onMoving: setMoving }
14
+ scrub.current ??= createScrubber(options)
57
15
  const s = scrub.current
58
- s.update({ min: o.min, max: o.max, edge: o.edge, coast: o.coast, maxSpeed: o.maxSpeed })
16
+ s.update(options)
59
17
 
60
18
  // A value set from outside (a controlled prop, a reset) glides there. So does one the parent kept
61
19
  // or changed when it came to rest.
62
- useEffect(() => {
63
- if (press.current || s.moving || o.value === s.target) return
64
- s.go(o.value)
65
- }, [o.value, s, moving])
20
+ useEffect(() => s.sync(o.value), [o.value, s, moving])
66
21
 
67
22
  const wheels = !!o.wheel
68
-
69
23
  useEffect(() => {
70
24
  const el = ref.current
71
25
  if (!el || !wheels) return
72
- const onWheel = (e: WheelEvent) => {
73
- const t = opt.current.wheel?.(e, s.target, document.activeElement === el) ?? null
74
- if (t === null) return
75
- e.preventDefault()
76
- if (t !== s.target) s.go(t)
77
- }
78
- el.addEventListener('wheel', onWheel, { passive: false })
79
- return () => el.removeEventListener('wheel', onWheel)
26
+ return s.wheelOn(el)
80
27
  }, [s, wheels])
81
- useEffect(() => () => s.stop(), [s])
82
-
83
- const onPointerDown = (e: PointerEvent<HTMLElement>) => {
84
- if (e.pointerType === 'mouse' && e.button !== 0) return
85
- try {
86
- e.currentTarget.setPointerCapture(e.pointerId)
87
- } catch {
88
- // A pointer the browser no longer knows (a synthetic one) cannot be captured, and needs no capture.
89
- }
90
- s.grab(e.timeStamp)
91
- press.current = { id: e.pointerId, sx: e.clientX, sy: e.clientY, x: e.clientX, y: e.clientY, moved: false }
92
- }
93
- const onPointerMove = (e: PointerEvent<HTMLElement>) => {
94
- const p = press.current
95
- if (!p || p.id !== e.pointerId) return
96
- if (!p.moved) {
97
- if (Math.hypot(e.clientX - p.sx, e.clientY - p.sy) < SLOP) return
98
- p.moved = true
99
- }
100
- const delta = opt.current.move(e, p, e.currentTarget)
101
- p.x = e.clientX
102
- p.y = e.clientY
103
- s.drag(delta, e.timeStamp)
104
- }
105
- const onPointerUp = (e: PointerEvent<HTMLElement>) => {
106
- const p = press.current
107
- if (!p || p.id !== e.pointerId) return
108
- press.current = null
109
- if (e.type === 'pointercancel') return s.cancel()
110
- if (p.moved) return s.release(e.timeStamp)
111
- const t = opt.current.tap?.(e, s.at)
112
- if (t === null || t === undefined) s.release(e.timeStamp)
113
- else s.go(t)
114
- }
115
- const onKeyDown = (e: KeyboardEvent<HTMLElement>) => {
116
- if (e.altKey || e.ctrlKey || e.metaKey) return
117
- const t = opt.current.key(e.key, s.target)
118
- if (t === null) return
119
- e.preventDefault()
120
- s.go(t)
121
- }
28
+ useEffect(() => () => s.destroy(), [s])
122
29
 
123
30
  return {
124
31
  ref,
125
32
  /** Where it is drawn now. */
126
33
  at,
127
34
  moving,
128
- go: (to: number) => s.go(to),
129
- place: (to: number) => s.place(to),
130
- target: () => s.target,
131
- handlers: { onPointerDown, onPointerMove, onPointerUp, onPointerCancel: onPointerUp, onKeyDown },
35
+ go: s.go,
36
+ place: s.place,
37
+ target: s.target,
38
+ handlers: { onPointerDown: s.down, onPointerMove: s.move, onPointerUp: s.up, onPointerCancel: s.up, onKeyDown: (e: KeyboardEvent<HTMLElement>) => s.keyDown(e) },
132
39
  }
133
40
  }
134
41
 
135
- /** A colour between two tokens by the pull, so ink fills in as a mark is drawn in. */
136
- export const ink = (p: number, from = 'var(--gooee-color-fg-muted)', to = 'var(--gooee-color-fg)') => `color-mix(in srgb, ${to} ${Math.round(p * 100)}%, ${from})`
137
-
138
- /** A number token read from `el`, such as a px size or a share. */
139
- export const tokenNumber = (el: Element | null, name: string) => (el ? parseFloat(getComputedStyle(el).getPropertyValue(name)) || 0 : 0)
140
-
141
- /** A wheel turn's sideways part in px: a sideways swipe, or Shift and the wheel. Null when it is mostly up and down, so the page scrolls. */
142
- export function sideways(e: WheelEvent) {
143
- const line = e.deltaMode === 1 ? 40 : 1
144
- const dx = (e.shiftKey && !e.deltaX ? e.deltaY : e.deltaX) * line
145
- const dy = e.shiftKey ? 0 : e.deltaY * line
146
- return Math.abs(dx) > Math.abs(dy) ? dx : null
147
- }
42
+ export { ink, sideways, tokenNumber } from './motion/scrubLook'
148
43
 
149
44
  /** Calls `fn` each time `value` changes (by `key`), but not for the value it starts with. */
150
45
  export function useOnChange<T>(value: T, fn: ((value: T) => void) | undefined, key = String(value)) {
@@ -0,0 +1,18 @@
1
+ import { Comment, Fragment, cloneVNode, type Ref, type VNode } from 'vue'
2
+
3
+ /** The first real node in a slot, looking through fragments and past comments. */
4
+ export function firstNode(nodes?: VNode[]): VNode | undefined {
5
+ for (const n of nodes ?? []) {
6
+ if (n.type === Comment) continue
7
+ const inner = n.type === Fragment ? firstNode(n.children as VNode[]) : n
8
+ if (inner) return inner
9
+ }
10
+ }
11
+
12
+ /** A ref callback that stores the element. A component's ref is its instance, so its element is its $el. */
13
+ export const elementRef = (target: Ref<HTMLElement | null>) => (el: unknown) => (target.value = el instanceof Element ? (el as HTMLElement) : ((el as { $el?: HTMLElement } | null)?.$el ?? null))
14
+
15
+ /** The slot's own element as the trigger, given the part's ref, ARIA and events, like React's cloneElement. */
16
+ export function cloneTrigger(node: VNode | undefined, ref: (el: unknown) => void, props: Record<string, unknown>) {
17
+ return node ? cloneVNode(node, { ref, ...props }, true) : null
18
+ }