gooee 0.0.0-stage → 0.41.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 (247) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +99 -2
  3. package/package.json +36 -4
  4. package/scripts/add.js +533 -0
  5. package/scripts/pro-components.json +82 -0
  6. package/src/base.css +39 -0
  7. package/src/components/Accordion/Accordion.module.css +90 -0
  8. package/src/components/Accordion/Accordion.tsx +125 -0
  9. package/src/components/Accordion/index.ts +1 -0
  10. package/src/components/Alert/Alert.module.css +121 -0
  11. package/src/components/Alert/Alert.tsx +90 -0
  12. package/src/components/Alert/index.ts +1 -0
  13. package/src/components/AlertDialog/AlertDialog.module.css +2 -0
  14. package/src/components/AlertDialog/AlertDialog.tsx +108 -0
  15. package/src/components/AlertDialog/index.ts +1 -0
  16. package/src/components/AnimatedNumber/AnimatedNumber.module.css +48 -0
  17. package/src/components/AnimatedNumber/AnimatedNumber.tsx +52 -0
  18. package/src/components/AnimatedNumber/index.ts +1 -0
  19. package/src/components/AspectRatio/AspectRatio.module.css +15 -0
  20. package/src/components/AspectRatio/AspectRatio.tsx +12 -0
  21. package/src/components/AspectRatio/index.ts +1 -0
  22. package/src/components/Avatar/Avatar.module.css +62 -0
  23. package/src/components/Avatar/Avatar.tsx +55 -0
  24. package/src/components/Avatar/index.ts +3 -0
  25. package/src/components/AvatarGroup/AvatarGroup.module.css +53 -0
  26. package/src/components/AvatarGroup/AvatarGroup.tsx +43 -0
  27. package/src/components/AvatarGroup/index.ts +1 -0
  28. package/src/components/Badge/Badge.module.css +69 -0
  29. package/src/components/Badge/Badge.tsx +25 -0
  30. package/src/components/Badge/index.ts +1 -0
  31. package/src/components/Breadcrumb/Breadcrumb.module.css +104 -0
  32. package/src/components/Breadcrumb/Breadcrumb.tsx +76 -0
  33. package/src/components/Breadcrumb/index.ts +1 -0
  34. package/src/components/Button/Button.module.css +191 -0
  35. package/src/components/Button/Button.tsx +148 -0
  36. package/src/components/Button/index.ts +1 -0
  37. package/src/components/ButtonGroup/ButtonGroup.module.css +59 -0
  38. package/src/components/ButtonGroup/ButtonGroup.tsx +45 -0
  39. package/src/components/ButtonGroup/index.ts +1 -0
  40. package/src/components/Card/Card.module.css +82 -0
  41. package/src/components/Card/Card.tsx +34 -0
  42. package/src/components/Card/index.ts +1 -0
  43. package/src/components/Center/Center.module.css +15 -0
  44. package/src/components/Center/Center.tsx +28 -0
  45. package/src/components/Center/index.ts +1 -0
  46. package/src/components/Checkbox/Checkbox.module.css +120 -0
  47. package/src/components/Checkbox/Checkbox.tsx +133 -0
  48. package/src/components/Checkbox/index.ts +1 -0
  49. package/src/components/CheckboxGroup/CheckboxGroup.module.css +30 -0
  50. package/src/components/CheckboxGroup/CheckboxGroup.tsx +88 -0
  51. package/src/components/CheckboxGroup/index.ts +1 -0
  52. package/src/components/Cluster/Cluster.module.css +25 -0
  53. package/src/components/Cluster/Cluster.tsx +26 -0
  54. package/src/components/Cluster/index.ts +1 -0
  55. package/src/components/CodeBlock/CodeBlock.module.css +74 -0
  56. package/src/components/CodeBlock/CodeBlock.tsx +53 -0
  57. package/src/components/CodeBlock/highlight.ts +73 -0
  58. package/src/components/CodeBlock/index.ts +1 -0
  59. package/src/components/Collapsible/Collapsible.module.css +75 -0
  60. package/src/components/Collapsible/Collapsible.tsx +71 -0
  61. package/src/components/Collapsible/index.ts +1 -0
  62. package/src/components/Container/Container.module.css +14 -0
  63. package/src/components/Container/Container.tsx +19 -0
  64. package/src/components/Container/index.ts +1 -0
  65. package/src/components/CopyButton/CopyButton.module.css +125 -0
  66. package/src/components/CopyButton/CopyButton.tsx +160 -0
  67. package/src/components/CopyButton/index.ts +1 -0
  68. package/src/components/Dial/Dial.module.css +344 -0
  69. package/src/components/Dial/Dial.tsx +332 -0
  70. package/src/components/Dial/index.ts +1 -0
  71. package/src/components/Dialog/Dialog.module.css +190 -0
  72. package/src/components/Dialog/Dialog.tsx +100 -0
  73. package/src/components/Dialog/index.ts +1 -0
  74. package/src/components/Divider/Divider.module.css +39 -0
  75. package/src/components/Divider/Divider.tsx +34 -0
  76. package/src/components/Divider/index.ts +1 -0
  77. package/src/components/DropdownMenu/DropdownMenu.module.css +134 -0
  78. package/src/components/DropdownMenu/DropdownMenu.tsx +215 -0
  79. package/src/components/DropdownMenu/index.ts +1 -0
  80. package/src/components/EmptyState/EmptyState.module.css +66 -0
  81. package/src/components/EmptyState/EmptyState.tsx +38 -0
  82. package/src/components/EmptyState/index.ts +1 -0
  83. package/src/components/Fab/Fab.module.css +176 -0
  84. package/src/components/Fab/Fab.tsx +117 -0
  85. package/src/components/Fab/index.ts +1 -0
  86. package/src/components/Fieldset/Fieldset.module.css +34 -0
  87. package/src/components/Fieldset/Fieldset.tsx +41 -0
  88. package/src/components/Fieldset/index.ts +1 -0
  89. package/src/components/FormField/FormField.module.css +62 -0
  90. package/src/components/FormField/FormField.tsx +91 -0
  91. package/src/components/FormField/index.ts +2 -0
  92. package/src/components/Grid/Grid.module.css +39 -0
  93. package/src/components/Grid/Grid.tsx +35 -0
  94. package/src/components/Grid/index.ts +1 -0
  95. package/src/components/HoldToConfirm/HoldToConfirm.module.css +175 -0
  96. package/src/components/HoldToConfirm/HoldToConfirm.tsx +334 -0
  97. package/src/components/HoldToConfirm/index.ts +1 -0
  98. package/src/components/IconButton/IconButton.module.css +78 -0
  99. package/src/components/IconButton/IconButton.tsx +35 -0
  100. package/src/components/IconButton/index.ts +1 -0
  101. package/src/components/Input/Input.module.css +157 -0
  102. package/src/components/Input/Input.tsx +133 -0
  103. package/src/components/Input/index.ts +1 -0
  104. package/src/components/InputGroup/InputGroup.module.css +146 -0
  105. package/src/components/InputGroup/InputGroup.tsx +90 -0
  106. package/src/components/InputGroup/index.ts +1 -0
  107. package/src/components/Kbd/Kbd.module.css +28 -0
  108. package/src/components/Kbd/Kbd.tsx +38 -0
  109. package/src/components/Kbd/index.ts +1 -0
  110. package/src/components/Link/Link.module.css +65 -0
  111. package/src/components/Link/Link.tsx +41 -0
  112. package/src/components/Link/index.ts +1 -0
  113. package/src/components/LivePill/LivePill.module.css +207 -0
  114. package/src/components/LivePill/LivePill.tsx +182 -0
  115. package/src/components/LivePill/index.ts +1 -0
  116. package/src/components/Meter/Meter.module.css +91 -0
  117. package/src/components/Meter/Meter.tsx +109 -0
  118. package/src/components/Meter/index.ts +1 -0
  119. package/src/components/MorphButton/MorphButton.module.css +152 -0
  120. package/src/components/MorphButton/MorphButton.tsx +203 -0
  121. package/src/components/MorphButton/index.ts +1 -0
  122. package/src/components/NumberInput/NumberInput.module.css +104 -0
  123. package/src/components/NumberInput/NumberInput.tsx +252 -0
  124. package/src/components/NumberInput/index.ts +2 -0
  125. package/src/components/Pagination/Pagination.module.css +88 -0
  126. package/src/components/Pagination/Pagination.tsx +126 -0
  127. package/src/components/Pagination/index.ts +1 -0
  128. package/src/components/PasswordInput/PasswordInput.module.css +64 -0
  129. package/src/components/PasswordInput/PasswordInput.tsx +63 -0
  130. package/src/components/PasswordInput/index.ts +2 -0
  131. package/src/components/PickerWheel/PickerWheel.module.css +123 -0
  132. package/src/components/PickerWheel/PickerWheel.tsx +326 -0
  133. package/src/components/PickerWheel/index.ts +1 -0
  134. package/src/components/Popover/Popover.module.css +78 -0
  135. package/src/components/Popover/Popover.tsx +117 -0
  136. package/src/components/Popover/index.ts +1 -0
  137. package/src/components/ProgressBar/ProgressBar.module.css +61 -0
  138. package/src/components/ProgressBar/ProgressBar.tsx +48 -0
  139. package/src/components/ProgressBar/index.ts +1 -0
  140. package/src/components/Prose/Prose.module.css +115 -0
  141. package/src/components/Prose/Prose.tsx +14 -0
  142. package/src/components/Prose/index.ts +1 -0
  143. package/src/components/Radio/Radio.module.css +106 -0
  144. package/src/components/Radio/Radio.tsx +115 -0
  145. package/src/components/Radio/index.ts +1 -0
  146. package/src/components/Retime/Retime.tsx +116 -0
  147. package/src/components/Retime/index.ts +1 -0
  148. package/src/components/RulerScrubber/RulerScrubber.module.css +146 -0
  149. package/src/components/RulerScrubber/RulerScrubber.tsx +209 -0
  150. package/src/components/RulerScrubber/index.ts +1 -0
  151. package/src/components/ScrollArea/ScrollArea.module.css +72 -0
  152. package/src/components/ScrollArea/ScrollArea.tsx +172 -0
  153. package/src/components/ScrollArea/index.ts +1 -0
  154. package/src/components/SearchInput/SearchInput.module.css +53 -0
  155. package/src/components/SearchInput/SearchInput.tsx +104 -0
  156. package/src/components/SearchInput/index.ts +2 -0
  157. package/src/components/SegmentedControl/SegmentedControl.module.css +150 -0
  158. package/src/components/SegmentedControl/SegmentedControl.tsx +169 -0
  159. package/src/components/SegmentedControl/index.ts +1 -0
  160. package/src/components/Select/Select.module.css +204 -0
  161. package/src/components/Select/Select.tsx +254 -0
  162. package/src/components/Select/index.ts +1 -0
  163. package/src/components/Sheet/Sheet.module.css +228 -0
  164. package/src/components/Sheet/Sheet.tsx +449 -0
  165. package/src/components/Sheet/index.ts +1 -0
  166. package/src/components/Skeleton/Skeleton.module.css +56 -0
  167. package/src/components/Skeleton/Skeleton.tsx +37 -0
  168. package/src/components/Skeleton/index.ts +1 -0
  169. package/src/components/Slider/Slider.module.css +155 -0
  170. package/src/components/Slider/Slider.tsx +200 -0
  171. package/src/components/Slider/index.ts +1 -0
  172. package/src/components/Spinner/Spinner.module.css +50 -0
  173. package/src/components/Spinner/Spinner.tsx +29 -0
  174. package/src/components/Spinner/index.ts +1 -0
  175. package/src/components/Split/Split.module.css +29 -0
  176. package/src/components/Split/Split.tsx +33 -0
  177. package/src/components/Split/index.ts +1 -0
  178. package/src/components/SplitFlap/SplitFlap.module.css +107 -0
  179. package/src/components/SplitFlap/SplitFlap.tsx +225 -0
  180. package/src/components/SplitFlap/index.ts +1 -0
  181. package/src/components/Stack/Stack.module.css +17 -0
  182. package/src/components/Stack/Stack.tsx +19 -0
  183. package/src/components/Stack/index.ts +1 -0
  184. package/src/components/SwipeDeck/SwipeDeck.module.css +250 -0
  185. package/src/components/SwipeDeck/SwipeDeck.tsx +350 -0
  186. package/src/components/SwipeDeck/index.ts +1 -0
  187. package/src/components/Switch/Switch.module.css +97 -0
  188. package/src/components/Switch/Switch.tsx +147 -0
  189. package/src/components/Switch/index.ts +1 -0
  190. package/src/components/Table/Table.module.css +369 -0
  191. package/src/components/Table/Table.tsx +368 -0
  192. package/src/components/Table/index.ts +1 -0
  193. package/src/components/TableOfContents/TableOfContents.module.css +107 -0
  194. package/src/components/TableOfContents/TableOfContents.tsx +93 -0
  195. package/src/components/TableOfContents/index.ts +1 -0
  196. package/src/components/Tabs/Tabs.module.css +101 -0
  197. package/src/components/Tabs/Tabs.tsx +190 -0
  198. package/src/components/Tabs/index.ts +1 -0
  199. package/src/components/Tag/Tag.module.css +120 -0
  200. package/src/components/Tag/Tag.tsx +109 -0
  201. package/src/components/Tag/index.ts +1 -0
  202. package/src/components/Textarea/Textarea.module.css +79 -0
  203. package/src/components/Textarea/Textarea.tsx +130 -0
  204. package/src/components/Textarea/index.ts +1 -0
  205. package/src/components/ThemeToggle/ThemeToggle.module.css +21 -0
  206. package/src/components/ThemeToggle/ThemeToggle.tsx +69 -0
  207. package/src/components/ThemeToggle/index.ts +1 -0
  208. package/src/components/Toast/Toast.module.css +202 -0
  209. package/src/components/Toast/Toast.tsx +473 -0
  210. package/src/components/Toast/index.ts +1 -0
  211. package/src/components/Toggle/Toggle.module.css +94 -0
  212. package/src/components/Toggle/Toggle.tsx +50 -0
  213. package/src/components/Toggle/index.ts +1 -0
  214. package/src/components/Tooltip/Tooltip.module.css +34 -0
  215. package/src/components/Tooltip/Tooltip.tsx +154 -0
  216. package/src/components/Tooltip/index.ts +1 -0
  217. package/src/components/VisuallyHidden/VisuallyHidden.module.css +13 -0
  218. package/src/components/VisuallyHidden/VisuallyHidden.tsx +12 -0
  219. package/src/components/VisuallyHidden/index.ts +1 -0
  220. package/src/internal/FieldMessage.module.css +49 -0
  221. package/src/internal/FieldMessage.tsx +44 -0
  222. package/src/internal/Portal.tsx +7 -0
  223. package/src/internal/Roll.module.css +31 -0
  224. package/src/internal/Roll.tsx +20 -0
  225. package/src/internal/cx.ts +3 -0
  226. package/src/internal/feel.ts +355 -0
  227. package/src/internal/fling.ts +118 -0
  228. package/src/internal/focus.ts +94 -0
  229. package/src/internal/icons.module.css +62 -0
  230. package/src/internal/icons.tsx +511 -0
  231. package/src/internal/keyboard.ts +62 -0
  232. package/src/internal/layers.ts +85 -0
  233. package/src/internal/liquid.ts +248 -0
  234. package/src/internal/liquidMatter.ts +168 -0
  235. package/src/internal/liquidMore.ts +686 -0
  236. package/src/internal/liquidPath.ts +519 -0
  237. package/src/internal/mergeRefs.ts +10 -0
  238. package/src/internal/motion.ts +37 -0
  239. package/src/internal/relativeTime.ts +26 -0
  240. package/src/internal/scrollFrame.ts +157 -0
  241. package/src/internal/softPress.ts +299 -0
  242. package/src/internal/space.ts +6 -0
  243. package/src/internal/useControllable.ts +21 -0
  244. package/src/internal/useFloating.ts +85 -0
  245. package/src/internal/usePresence.ts +15 -0
  246. package/src/internal/useScrub.ts +160 -0
  247. package/src/tokens.css +727 -0
@@ -0,0 +1,686 @@
1
+ import { useEffect, type RefObject } from 'react'
2
+ import { boxPath, boxRing, clamp01, lerp, polyPath, sdBox, seg, trace, type Box, type Pt, type Sdf } from './liquidPath'
3
+ import { frames, layer, morphEase, motionFor } from './liquidMatter'
4
+
5
+ // Liquid matter for Checkbox (a cup that fills), the fields (an edge that flows round), CopyButton (a
6
+ // drop that fills the icon and turns into the check) and the menus (a bead that follows the pointer).
7
+ // Same contract as liquidMatter.ts: the SVG exists only while the part moves, and its last frame is
8
+ // the part's own CSS shape.
9
+
10
+ const NS = 'http://www.w3.org/2000/svg'
11
+ let ids = 0
12
+
13
+ function clipOf(svg: SVGSVGElement) {
14
+ const defs = svg.insertBefore(document.createElementNS(NS, 'defs'), svg.firstChild)
15
+ const clip = defs.appendChild(document.createElementNS(NS, 'clipPath'))
16
+ clip.id = `gooee-liquid-clip-${++ids}`
17
+ const path = clip.appendChild(document.createElementNS(NS, 'path'))
18
+ return { path, url: `url(#${clip.id})` }
19
+ }
20
+
21
+ // ---------- cup ----------
22
+
23
+ /** A container's inner walls, floor and rim, and how much the surface tips and curves. */
24
+ export type Cup = { x0: number; x1: number; top: number; bot: number; tilt: number; men: number }
25
+
26
+ /**
27
+ * The surface at fill `f` (0 on the floor, 1 over the rim), poured from side `s` (-1 left, 1 right,
28
+ * 0 evenly from below): high on the poured side at first, then it rises and levels, with a meniscus.
29
+ */
30
+ export function cupLevel(f: number, s: number, cup: Cup) {
31
+ const cx = (cup.x0 + cup.x1) / 2
32
+ const hw = (cup.x1 - cup.x0) / 2
33
+ const T = cup.tilt * (1 - f) * (1 - f)
34
+ const M = cup.men * (0.4 + 0.6 * (1 - f))
35
+ const L = lerp(cup.bot + cup.tilt * Math.abs(s) + cup.men, cup.top - 1, f)
36
+ return (x: number) => {
37
+ const q = (x - cx) / hw
38
+ return L - s * T * q - M * q * q
39
+ }
40
+ }
41
+
42
+ /** The liquid under `y` from x0 to x1, closed along `floor`. */
43
+ export function cupPath(y: (x: number) => number, x0: number, x1: number, floor: number) {
44
+ const pts: Pt[] = []
45
+ for (let x = x0; x <= x1 + 1e-6; x += 0.5) pts.push([x, y(x)])
46
+ pts.push([x1, floor], [x0, floor])
47
+ return polyPath(pts)
48
+ }
49
+
50
+ const cupAttr = (c: Cup) => `${c.x0} ${c.x1} ${c.top} ${c.bot}`
51
+
52
+ // ---------- Checkbox ----------
53
+
54
+ export type Pour = { u: number; side: number; done: boolean; stop: () => void }
55
+
56
+ /**
57
+ * Checkbox: ink pours into `box` from `side` (the side tapped; 0 fills evenly from below), rises with
58
+ * a meniscus and levels, then `mark` draws. Emptying runs it back. While it pours the box carries
59
+ * data-pouring, so its CSS fill stands down. A pour caught half way turns from where it is.
60
+ */
61
+ export function pour(box: HTMLElement, mark: SVGPathElement | null, to: 0 | 1, side: number, cls: string, prev: Pour | null): Pour | null {
62
+ const live = prev && !prev.done ? (prev as Pour & { svg?: SVGSVGElement; halt?: () => void }) : null
63
+ const from = live ? live.u : 1 - to
64
+ live?.halt?.()
65
+ const ms = motionFor('slow') * Math.abs(to - from)
66
+ if (!ms) {
67
+ live?.stop()
68
+ return null
69
+ }
70
+ // A run caught half way may hand over to the other mark (tick or dash): the old one rests.
71
+ box.querySelectorAll<SVGElement>('[data-mark]').forEach((m) => m !== mark && m.style.removeProperty('stroke-dashoffset'))
72
+ const W = box.offsetWidth
73
+ const bw = box.clientLeft
74
+ const look = getComputedStyle(box)
75
+ const r = parseFloat(look.borderTopLeftRadius) || 0
76
+ const fill = box.querySelector<HTMLElement>('[data-pour-fill]')
77
+ const outer = boxPath({ cx: W / 2, cy: W / 2, hw: W / 2, hh: W / 2, r })
78
+ const inner = W - 2 * bw
79
+ const cup: Cup = { x0: bw, x1: W - bw, top: bw, bot: W - bw, tilt: inner / 3, men: inner / 9 }
80
+ let svg = live?.svg
81
+ let liquid: SVGPathElement
82
+ let clipUrl: string
83
+ if (svg) {
84
+ liquid = svg.querySelector('path[data-measure]')!
85
+ clipUrl = liquid.getAttribute('clip-path') ?? ''
86
+ } else {
87
+ const l = layer(box, cls, box.firstChild)
88
+ svg = l.svg
89
+ liquid = l.paths[0]
90
+ const clip = clipOf(svg)
91
+ clip.path.setAttribute('d', outer)
92
+ clipUrl = clip.url
93
+ svg.setAttribute('width', `${W}`)
94
+ svg.setAttribute('height', `${W}`)
95
+ svg.style.left = svg.style.top = `${-bw}px`
96
+ liquid.style.fill = fill ? getComputedStyle(fill).backgroundColor : 'currentColor'
97
+ liquid.setAttribute('data-measure', 'surface')
98
+ liquid.setAttribute('data-cup', cupAttr(cup))
99
+ }
100
+ box.dataset.pouring = ''
101
+ const e = morphEase()
102
+ const s = live ? live.side : side
103
+ const layerSvg = svg
104
+ const run = {
105
+ u: from,
106
+ side: s,
107
+ done: false,
108
+ svg: layerSvg,
109
+ halt: () => {},
110
+ stop: () => {
111
+ run.done = true
112
+ run.halt()
113
+ layerSvg.remove()
114
+ box.querySelectorAll<SVGElement>('[data-mark]').forEach((m) => m.style.removeProperty('stroke-dashoffset'))
115
+ delete box.dataset.pouring
116
+ },
117
+ }
118
+ const draw = (p: number) => {
119
+ run.u = lerp(from, to, e(p))
120
+ const f = seg(run.u, 0, 0.78)
121
+ const cp = seg(run.u, 0.7, 1)
122
+ mark?.style.setProperty('stroke-dashoffset', `${1.01 * (1 - cp)}`)
123
+ if (p >= 1) {
124
+ liquid.removeAttribute('clip-path')
125
+ liquid.setAttribute('d', to ? outer : '')
126
+ return
127
+ }
128
+ liquid.setAttribute('clip-path', clipUrl)
129
+ liquid.setAttribute('d', f > 0 ? cupPath(cupLevel(f, s, cup), -1, W + 1, W + 2) : '')
130
+ }
131
+ run.halt = frames(ms, draw, () => run.stop())
132
+ return run
133
+ }
134
+
135
+ // ---------- field edge ----------
136
+
137
+ type Ring = Array<[number, number, number, number]>
138
+
139
+ function ringLength(cl: Ring) {
140
+ let len = 0
141
+ for (let i = 0; i < cl.length; i++) {
142
+ const a = cl[i]
143
+ const b = cl[(i + 1) % cl.length]
144
+ len += Math.hypot(b[0] - a[0], b[1] - a[1])
145
+ }
146
+ return len
147
+ }
148
+
149
+ /**
150
+ * The edge at progress `e` (0 to 1): two heads run both ways round the centre line `cl` from `i0`, a
151
+ * little fuller than the edge, and meet on the far side; the joined bump then relaxes into the ring.
152
+ */
153
+ export function edgeLoops(cl: Ring, i0: number, e: number, band: number, head = 1.2, over = 10): Pt[][] {
154
+ const N = cl.length
155
+ const P = ringLength(cl)
156
+ const ds = P / N
157
+ const at = (i: number) => cl[((i % N) + N) % N]
158
+ const wAt = (d: number) => band + head * Math.exp(-(d * d) / 9)
159
+ const a = e * (P / 2 + over)
160
+ const g = P - 2 * a
161
+ if (g > wAt(0)) {
162
+ const k = Math.max(1, Math.round(a / ds))
163
+ const out: Pt[] = []
164
+ const inn: Pt[] = []
165
+ for (let j = -k; j <= k; j++) {
166
+ const p = at(i0 + j)
167
+ const w = wAt((k - Math.abs(j)) * ds) / 2
168
+ out.push([p[0] + p[2] * w, p[1] + p[3] * w])
169
+ inn.push([p[0] - p[2] * w, p[1] - p[3] * w])
170
+ }
171
+ // A round drop head on each end.
172
+ const cap = (p: Ring[number], dir: number, from: number): Pt[] => {
173
+ const r = wAt(0) / 2
174
+ const n = [p[2] * from, p[3] * from]
175
+ const t = [-p[3] * dir, p[2] * dir]
176
+ const c: Pt[] = []
177
+ for (let q = 1; q < 10; q++) {
178
+ const th = (q / 10) * Math.PI
179
+ c.push([p[0] + r * (n[0] * Math.cos(th) + t[0] * Math.sin(th)), p[1] + r * (n[1] * Math.cos(th) + t[1] * Math.sin(th))])
180
+ }
181
+ return c
182
+ }
183
+ return [out.concat(cap(at(i0 + k), 1, 1), inn.reverse(), cap(at(i0 - k), -1, -1))]
184
+ }
185
+ const iM = (i0 + Math.round(N / 2)) % N
186
+ const m = clamp01((g + 2 * over) / (wAt(0) + 2 * over))
187
+ const out: Pt[] = []
188
+ const inn: Pt[] = []
189
+ for (let i = 0; i < N; i++) {
190
+ let dj = Math.abs(i - iM) % N
191
+ dj = Math.min(dj, N - dj) * ds
192
+ const p = cl[i]
193
+ const w = (band + head * m * Math.exp(-(dj * dj) / 16)) / 2
194
+ out.push([p[0] + p[2] * w, p[1] + p[3] * w])
195
+ inn.push([p[0] - p[2] * w, p[1] - p[3] * w])
196
+ }
197
+ return [out, inn]
198
+ }
199
+
200
+ function nearestOn(cl: Ring, x: number, y: number) {
201
+ let best = 0
202
+ let m = Infinity
203
+ cl.forEach((p, i) => {
204
+ const d = (p[0] - x) ** 2 + (p[1] - y) ** 2
205
+ if (d < m) {
206
+ m = d
207
+ best = i
208
+ }
209
+ })
210
+ return best
211
+ }
212
+
213
+ /**
214
+ * Field focus: while `target` matches `on`, it carries data-edge, which its CSS draws as the dark edge
215
+ * (the field's focus indicator, --gooee-line-focus thick).
216
+ * When it starts to match, the edge first flows both ways round from where it was pressed (or from the
217
+ * start edge for a key) and meets on the far side, then data-edge lands; when it stops mid-flow, the
218
+ * drawing fades. The layer sits in `host` (the target itself, or a wrapper for a textarea).
219
+ */
220
+ export function useEdgeFlow(target: RefObject<HTMLElement | null>, host: RefObject<HTMLElement | null>, on: string, cls: string) {
221
+ useEffect(() => {
222
+ const el = target.current
223
+ const root = host.current
224
+ if (!el || !root) return
225
+ let shown = el.matches(on)
226
+ if (shown) el.dataset.edge = ''
227
+ let pressed: { x: number; y: number; t: number } | null = null
228
+ let run: { svg: SVGSVGElement; stop: () => void } | null = null
229
+ const end = () => {
230
+ run?.stop()
231
+ run?.svg.remove()
232
+ run = null
233
+ }
234
+ const settle = () => {
235
+ end()
236
+ if (shown) el.dataset.edge = ''
237
+ }
238
+ const start = () => {
239
+ end()
240
+ const ms = motionFor('base') + motionFor('instant')
241
+ if (!ms) return settle()
242
+ const look = getComputedStyle(el)
243
+ // The settled edge is a focus indicator: --gooee-line-focus thick, inside the border box.
244
+ const bw = parseFloat(look.getPropertyValue('--gooee-line-focus')) || 2
245
+ const r = parseFloat(look.borderTopLeftRadius) || 0
246
+ const rect = el.getBoundingClientRect()
247
+ let W = el.offsetWidth
248
+ let H = el.offsetHeight
249
+ const ringOf = () => boxRing({ cx: W / 2, cy: H / 2, hw: (W - bw) / 2, hh: (H - bw) / 2, r: Math.max(0, r - bw / 2) }, 0.5)
250
+ let cl = ringOf()
251
+ const fresh = pressed && performance.now() - pressed.t < 1000
252
+ const rtl = look.direction === 'rtl'
253
+ const i0 = fresh ? nearestOn(cl, pressed!.x - rect.left, pressed!.y - rect.top) : nearestOn(cl, rtl ? W : 0, H / 2)
254
+ let at = i0 / cl.length
255
+ pressed = null
256
+ const { svg, paths } = layer(root, cls, null)
257
+ const path = paths[0]
258
+ path.style.fill = look.getPropertyValue('--gooee-flow-edge').trim() || 'currentColor'
259
+ path.setAttribute('fill-rule', 'evenodd')
260
+ path.setAttribute('data-measure', 'shape')
261
+ const place = () => {
262
+ svg.setAttribute('width', `${W}`)
263
+ svg.setAttribute('height', `${H}`)
264
+ svg.style.left = `${el === root ? -el.clientLeft : el.offsetLeft}px`
265
+ svg.style.top = `${el === root ? -el.clientTop : el.offsetTop}px`
266
+ }
267
+ place()
268
+ const e = morphEase()
269
+ const draw = (u: number) => {
270
+ // A field that grows while it flows (a textarea) keeps the flow on its edge.
271
+ if (el.offsetWidth !== W || el.offsetHeight !== H) {
272
+ W = el.offsetWidth
273
+ H = el.offsetHeight
274
+ cl = ringOf()
275
+ place()
276
+ }
277
+ if (u >= 1) {
278
+ path.setAttribute('d', boxPath({ cx: W / 2, cy: H / 2, hw: W / 2, hh: H / 2, r }) + boxPath({ cx: W / 2, cy: H / 2, hw: W / 2 - bw, hh: H / 2 - bw, r: Math.max(0, r - bw) }))
279
+ return
280
+ }
281
+ path.setAttribute('d', edgeLoops(cl, Math.round(at * cl.length), e(u), bw).map((l) => polyPath(l)).join(''))
282
+ }
283
+ run = { svg, stop: frames(ms, draw, settle) }
284
+ }
285
+ const leave = () => {
286
+ if (!run) return
287
+ const r = run
288
+ r.stop()
289
+ const ms = motionFor('fast')
290
+ if (!ms || typeof r.svg.animate !== 'function') return end()
291
+ const fade = r.svg.animate({ opacity: [1, 0] }, { duration: ms, easing: 'linear', fill: 'forwards' })
292
+ r.stop = () => fade.cancel()
293
+ fade.finished.then(() => run === r && end(), () => {})
294
+ }
295
+ const check = () => {
296
+ const now = el.matches(on)
297
+ if (now === shown) return
298
+ shown = now
299
+ if (now) start()
300
+ else {
301
+ delete el.dataset.edge
302
+ leave()
303
+ }
304
+ }
305
+ const later = () => queueMicrotask(check)
306
+ const down = (ev: PointerEvent) => {
307
+ pressed = { x: ev.clientX, y: ev.clientY, t: performance.now() }
308
+ }
309
+ const watch = new MutationObserver(check)
310
+ watch.observe(el, { attributes: true, attributeFilter: ['data-open', 'data-invalid'] })
311
+ el.addEventListener('pointerdown', down, true)
312
+ el.addEventListener('focusin', later)
313
+ el.addEventListener('focusout', later)
314
+ return () => {
315
+ watch.disconnect()
316
+ el.removeEventListener('pointerdown', down, true)
317
+ el.removeEventListener('focusin', later)
318
+ el.removeEventListener('focusout', later)
319
+ end()
320
+ delete el.dataset.edge
321
+ }
322
+ }, [target, host, on, cls])
323
+ }
324
+
325
+ // ---------- CopyButton ----------
326
+
327
+ const sdSeg = (x: number, y: number, a: Pt, b: Pt) => {
328
+ const bx = b[0] - a[0]
329
+ const by = b[1] - a[1]
330
+ const L = bx * bx + by * by
331
+ const h = L > 0 ? clamp01(((x - a[0]) * bx + (y - a[1]) * by) / L) : 0
332
+ return Math.hypot(x - a[0] - bx * h, y - a[1] - by * h)
333
+ }
334
+
335
+ /**
336
+ * Distance to a stroke of half width `w`: two arms from B (directions u1 and u2, lengths L1 and L2)
337
+ * joined by an arc of radius rc. Its outline is an exact offset of a smooth centre line, so it is as
338
+ * wide everywhere and has no point.
339
+ */
340
+ function strokeSdf(B: Pt, u1: Pt, u2: Pt, L1: number, L2: number, rc: number, w: number): Sdf {
341
+ const e1: Pt = [B[0] + u1[0] * L1, B[1] + u1[1] * L1]
342
+ const e2: Pt = [B[0] + u2[0] * L2, B[1] + u2[1] * L2]
343
+ const phi = Math.acos(Math.max(-1, Math.min(1, u1[0] * u2[0] + u1[1] * u2[1])))
344
+ const beta = Math.PI - phi
345
+ if (beta < 1e-4) return (x, y) => Math.min(sdSeg(x, y, B, e1), sdSeg(x, y, B, e2)) - w
346
+ const T = rc * Math.tan(beta / 2)
347
+ const p1: Pt = [B[0] + u1[0] * T, B[1] + u1[1] * T]
348
+ const p2: Pt = [B[0] + u2[0] * T, B[1] + u2[1] * T]
349
+ const bl = Math.hypot(u1[0] + u2[0], u1[1] + u2[1])
350
+ const oc = rc / Math.sin(phi / 2)
351
+ const O: Pt = [B[0] + ((u1[0] + u2[0]) / bl) * oc, B[1] + ((u1[1] + u2[1]) / bl) * oc]
352
+ const a: Pt = [p1[0] - O[0], p1[1] - O[1]]
353
+ const b: Pt = [p2[0] - O[0], p2[1] - O[1]]
354
+ const cr = (m: Pt, n: Pt) => m[0] * n[1] - m[1] * n[0]
355
+ const sg = Math.sign(cr(a, b))
356
+ return (x, y) => {
357
+ let d = Math.min(sdSeg(x, y, p1, e1), sdSeg(x, y, p2, e2))
358
+ const v: Pt = [x - O[0], y - O[1]]
359
+ if (cr(a, v) * sg >= 0 && cr(v, b) * sg >= 0) d = Math.min(d, Math.abs(Math.hypot(v[0], v[1]) - rc))
360
+ return d - w
361
+ }
362
+ }
363
+
364
+ /** The filled square (`sq`) and the check it becomes: its corner B, arm lengths and half width. */
365
+ export type CheckMorph = { sq: Box; B: Pt; L1: number; L2: number; w: number }
366
+
367
+ const ROUND = 0.04
368
+
369
+ /**
370
+ * The filled square at m = 0 becomes the check at m = 1 in one shape: it rounds into a drop (the first
371
+ * 4%), stretches into a bar, and bends into the check as it thins. Through the bend the join radius
372
+ * stays at least 1.9 times the half width, so its inside never pinches.
373
+ */
374
+ export function checkLoops(m: number, g: CheckMorph): Pt[][] {
375
+ const { sq } = g
376
+ if (m < ROUND) {
377
+ const q = m / ROUND
378
+ const r = lerp(sq.r, sq.hw, q * q * (3 - 2 * q))
379
+ return [boxRing({ ...sq, r }, 0.5).map((p) => [p[0], p[1]] as Pt)]
380
+ }
381
+ const t = (m - ROUND) / (1 - ROUND)
382
+ const w = lerp(sq.hw, g.w, t)
383
+ const rc = 1.9 * w + 0.2
384
+ const B: Pt = [lerp(sq.cx, g.B[0], t), lerp(sq.cy, g.B[1], t)]
385
+ const L1 = g.L1 * t
386
+ const L2 = g.L2 * t
387
+ const sm = t * t * (3 - 2 * t)
388
+ const beta = Math.min((Math.PI / 2) * sm, 2 * Math.atan((0.95 * Math.min(L1, L2)) / rc))
389
+ const phi = Math.PI - beta
390
+ const up = -Math.PI / 2
391
+ const u1: Pt = [Math.cos(up - phi / 2), Math.sin(up - phi / 2)]
392
+ const u2: Pt = [Math.cos(up + phi / 2), Math.sin(up + phi / 2)]
393
+ const T = rc * Math.tan(beta / 2)
394
+ return trace(strokeSdf(B, u1, u2, L1, L2, rc, w), [[B[0] + u1[0] * T, B[1] + u1[1] * T, 0, 1]])
395
+ }
396
+
397
+ /** Where a copy run is: front filled, back slid under, square morphed into the check, each 0 to 1. */
398
+ export type CopyState = { fill: number; slide: number; morph: number }
399
+
400
+ // The copy icon (internal/icons CopyIcon) in its 24-unit box, and the check's corner and arms.
401
+ const ICON = { front: 8.5, size: 11, r: 2.5, back: 4, stroke: 1.75, B: [9.5, 17] as Pt, L1: 4.5 * Math.SQRT2, L2: 9.5 * Math.SQRT2, check: 2.25 }
402
+ const BACK_D = 'M15.5 8.5V6.5a2 2 0 0 0-2-2h-7a2 2 0 0 0-2 2v7a2 2 0 0 0 2 2h2'
403
+
404
+ /**
405
+ * CopyButton: a drop forms where it was pressed (`at`, from the icon's corner; null for a key), falls
406
+ * into the copy icon and fills its front square, the back square slides under and merges, and the
407
+ * filled square turns into the check. `to` 0 plays it back from wherever it is. The icon box carries
408
+ * data-pouring while it runs, so the CSS icons stand down.
409
+ */
410
+ export function copyRun(icons: HTMLElement, to: 0 | 1, at: Pt | null, cls: string, prev: CopyRun | null): CopyRun | null {
411
+ if (prev?.done) prev = null
412
+ const from: CopyState = prev ? { ...prev.state } : to ? { fill: 0, slide: 0, morph: 0 } : { fill: 1, slide: 1, morph: 1 }
413
+ prev?.halt()
414
+ const k = motionFor('morph') / 600
415
+ if (!k) {
416
+ prev?.stop()
417
+ return null
418
+ }
419
+ const size = icons.offsetWidth
420
+ const s = size / 24
421
+ const o = (ICON.front - ICON.stroke / 2) * s
422
+ const S = (ICON.size + ICON.stroke) * s
423
+ const sq: Box = { cx: o + S / 2, cy: o + S / 2, hw: S / 2, hh: S / 2, r: (ICON.r + ICON.stroke / 2) * s }
424
+ const ring = ICON.stroke * s
425
+ const hole: Box = { ...sq, hw: S / 2 - ring, hh: S / 2 - ring, r: Math.max(0, sq.r - ring) }
426
+ const frontD = boxPath(sq)
427
+ const g: CheckMorph = { sq, B: [ICON.B[0] * s, ICON.B[1] * s], L1: ICON.L1 * s, L2: ICON.L2 * s, w: (ICON.check / 2) * s }
428
+ const inner = hole.hw * 2
429
+ const cup: Cup = { x0: hole.cx - hole.hw, x1: hole.cx + hole.hw, top: hole.cy - hole.hh, bot: hole.cy + hole.hh, tilt: inner * 0.29, men: inner * 0.1 }
430
+ const entry: Pt = [sq.cx, cup.top + 1]
431
+ const drop = !prev && to === 1
432
+ const P: Pt = at ?? [size / 2, size / 2]
433
+ const side = Math.max(-1, Math.min(1, (P[0] - entry[0]) / 32)) * 0.6
434
+ let svg = prev?.svg
435
+ let p: { back: SVGPathElement; liquid: SVGPathElement; front: SVGPathElement; drop: SVGPathElement; clip: string }
436
+ if (svg && prev) p = prev.paths
437
+ else {
438
+ const l = layer(icons, cls, null, 4)
439
+ svg = l.svg
440
+ svg.setAttribute('width', `${size}`)
441
+ svg.setAttribute('height', `${size}`)
442
+ const [back, liquid, front, dropPath] = l.paths
443
+ const inside = clipOf(svg)
444
+ inside.path.setAttribute('d', frontD)
445
+ // The back square shows only outside the front one, so it slides under it.
446
+ const outside = clipOf(svg)
447
+ outside.path.setAttribute('d', `M-${size} -${size}H${2 * size}V${2 * size}H-${size}Z${frontD}`)
448
+ outside.path.setAttribute('clip-rule', 'evenodd')
449
+ back.setAttribute('d', BACK_D)
450
+ back.setAttribute('clip-path', outside.url)
451
+ back.style.cssText = `fill:none;stroke:currentColor;stroke-width:${ICON.stroke};stroke-linecap:round;stroke-linejoin:round`
452
+ front.setAttribute('fill-rule', 'evenodd')
453
+ for (const q of [liquid, front, dropPath]) q.style.fill = 'currentColor'
454
+ dropPath.setAttribute('data-measure', 'shape')
455
+ p = { back, liquid, front, drop: dropPath, clip: inside.url }
456
+ }
457
+ icons.dataset.pouring = ''
458
+ const e = morphEase()
459
+ const s2 = (rel: number, a: number, b: number) => clamp01((rel - a * k) / ((b - a) * k))
460
+ const toward = (v: number, t: number) => lerp(v, to, e(t))
461
+ const ms = (to ? (drop ? 600 : 440) : 560) * k
462
+ const layerSvg = svg
463
+ const run: CopyRun = {
464
+ state: { ...from },
465
+ done: false,
466
+ svg: layerSvg,
467
+ paths: p,
468
+ halt: () => {},
469
+ stop: () => {
470
+ run.done = true
471
+ run.halt()
472
+ layerSvg.remove()
473
+ delete icons.dataset.pouring
474
+ },
475
+ }
476
+ const frame = (rel: number) => {
477
+ const st = run.state
478
+ let dp: { x: number; y: number; r: number } | null = null
479
+ if (to) {
480
+ // From rest: the drop forms, flies and sinks in, then the front fills, the back slides under and
481
+ // the square becomes the check. From part way: the same order, without the drop.
482
+ const [f0, f1, s0, s1, m0, m1] = drop ? [160, 300, 200, 300, 300, 600] : [0, 140, 40, 140, 140, 440]
483
+ st.fill = toward(from.fill, s2(rel, f0, f1))
484
+ st.slide = toward(from.slide, s2(rel, s0, s1))
485
+ st.morph = toward(from.morph, s2(rel, m0, m1))
486
+ if (drop) {
487
+ const form = s2(rel, 0, 70)
488
+ const fly = s2(rel, 70, 190)
489
+ const sink = s2(rel, 190, 240)
490
+ const q = fly * fly * (3 - 2 * fly)
491
+ const r = size * 0.15 * e(form) * (1 - sink)
492
+ if (r > 0.05) dp = { x: lerp(P[0], entry[0], q), y: lerp(P[1], entry[1], q) - size * 2 * q * (1 - q) + size * 0.25 * sink, r }
493
+ }
494
+ } else {
495
+ st.morph = toward(from.morph, s2(rel, 0, 300))
496
+ st.slide = toward(from.slide, s2(rel, 300, 400))
497
+ st.fill = toward(from.fill, s2(rel, 380, 560))
498
+ }
499
+ const full = st.fill >= 1
500
+ const { liquid } = p
501
+ if (st.fill <= 0) {
502
+ liquid.removeAttribute('clip-path')
503
+ liquid.removeAttribute('data-measure')
504
+ liquid.setAttribute('d', '')
505
+ } else if (!full) {
506
+ liquid.setAttribute('clip-path', p.clip)
507
+ liquid.setAttribute('data-measure', 'surface')
508
+ liquid.setAttribute('data-cup', cupAttr(cup))
509
+ liquid.setAttribute('d', cupPath(cupLevel(st.fill, side, cup), sq.cx - sq.hw - 1, sq.cx + sq.hw + 1, sq.cy + sq.hh + 2))
510
+ } else {
511
+ liquid.removeAttribute('clip-path')
512
+ // The square itself is the icon's own shape; the morph is judged from the first step on.
513
+ if (st.morph > 0) liquid.setAttribute('data-measure', 'morph')
514
+ else liquid.removeAttribute('data-measure')
515
+ liquid.setAttribute('d', st.morph > 0 ? checkLoops(st.morph, g).map((l) => polyPath(l)).join('') : frontD)
516
+ }
517
+ // Once the front is full, its outline and the hidden back square are under the ink.
518
+ p.front.setAttribute('d', full ? '' : frontD + boxPath(hole))
519
+ p.back.setAttribute('d', full && st.slide >= 1 ? '' : BACK_D)
520
+ const off = ICON.back * s * st.slide
521
+ p.back.setAttribute('transform', `translate(${off} ${off}) scale(${s})`)
522
+ p.drop.setAttribute('d', dp ? `M${dp.x - dp.r} ${dp.y}a${dp.r} ${dp.r} 0 1 0 ${2 * dp.r} 0a${dp.r} ${dp.r} 0 1 0 ${-2 * dp.r} 0Z` : '')
523
+ }
524
+ run.halt = frames(ms, (u) => frame(u * ms), () => run.stop())
525
+ return run
526
+ }
527
+
528
+ export type CopyRun = {
529
+ state: CopyState
530
+ done: boolean
531
+ svg: SVGSVGElement
532
+ paths: { back: SVGPathElement; liquid: SVGPathElement; front: SVGPathElement; drop: SVGPathElement; clip: string }
533
+ halt: () => void
534
+ stop: () => void
535
+ }
536
+
537
+ // ---------- menu bead ----------
538
+
539
+ function smin(a: number, b: number, k: number) {
540
+ if (k <= 1e-6) return Math.min(a, b)
541
+ const h = Math.max(k - Math.abs(a - b), 0) / k
542
+ return Math.min(a, b) - (h * h * h * k) / 6
543
+ }
544
+
545
+ // Up to SLIDE px apart, front and tail are one stretched box. Past it the tail narrows and a neck
546
+ // forms, never longer than NECK px.
547
+ const SLIDE = 24
548
+ const NECK = 24
549
+
550
+ /** The bead with its front at F and its tail at T (centres on y), each with its half height. */
551
+ export function beadLoops(cx: number, hw: number, r: number, F: { y: number; hh: number }, T: { y: number; hh: number }): Pt[][] {
552
+ const d = Math.abs(F.y - T.y)
553
+ if (d <= SLIDE) {
554
+ const y0 = Math.min(F.y - F.hh, T.y - T.hh)
555
+ const y1 = Math.max(F.y + F.hh, T.y + T.hh)
556
+ return [boxRing({ cx, cy: (y0 + y1) / 2, hw, hh: (y1 - y0) / 2, r }, 0.5).map((p) => [p[0], p[1]] as Pt)]
557
+ }
558
+ const e = clamp01((d - SLIDE) / 30)
559
+ const kk = 18 * e
560
+ const hwT = hw * (1 - 0.1 * e * e)
561
+ const hhT = T.hh - 6 * e
562
+ const my = (F.y + T.y) / 2
563
+ // Wide until the gap between the two is tall enough for round fillets, so no slot opens between them.
564
+ const gq = clamp01((d - F.hh - hhT - 12) / 14)
565
+ const nw = lerp(hwT - 2, Math.min(30, hwT - 2), gq * gq * (3 - 2 * gq))
566
+ const sdf: Sdf = (x, y) =>
567
+ smin(smin(sdBox(x, y, { cx, cy: F.y, hw, hh: F.hh, r }), sdBox(x, y, { cx, cy: T.y, hw: hwT, hh: hhT, r }), kk), sdBox(x, y, { cx, cy: my, hw: nw, hh: d / 2, r: Math.min(nw, 8) }), kk)
568
+ return trace(sdf, [[cx, F.y, 1, 0]])
569
+ }
570
+
571
+ /**
572
+ * Menu items: the hover fill is a bead that follows focus (which follows the pointer) from item to
573
+ * item. A next item settles in about 100ms with a slight stretch; a jump of two or more stretches a
574
+ * short neck. While it moves `panel` carries data-bead, so the items' own fill stands down; at rest
575
+ * the focused item's CSS fill is back. Focus leaving the panel mid-move fades the bead where it is.
576
+ * Only this panel's own items count (submenus sit inside it).
577
+ */
578
+ export function useBead(panel: RefObject<HTMLElement | null>, on: boolean, cls: string) {
579
+ useEffect(() => {
580
+ const root = panel.current
581
+ if (!root || !on) return
582
+ const st = { F: 0, T: 0, hF: 0, hT: 0 }
583
+ let goal: HTMLElement | null = null
584
+ let run: { svg: SVGSVGElement; path: SVGPathElement; id: number; last: number } | null = null
585
+ const itemOf = (t: EventTarget | null) => {
586
+ const el = t instanceof Element ? t.closest<HTMLElement>('[role^="menuitem"]') : null
587
+ return el && el.closest('[role="menu"]') === root ? el : null
588
+ }
589
+ // Layout boxes, not screen ones: the panel and its items may be mid scale or slide as they arrive.
590
+ const boxOf = (el: HTMLElement) => {
591
+ let x = 0
592
+ let y = 0
593
+ for (let n: HTMLElement | null = el; n && n !== root; n = n.offsetParent as HTMLElement | null) {
594
+ x += n.offsetLeft
595
+ y += n.offsetTop
596
+ if (n.offsetParent && n.offsetParent !== root) {
597
+ x += (n.offsetParent as HTMLElement).clientLeft
598
+ y += (n.offsetParent as HTMLElement).clientTop
599
+ }
600
+ }
601
+ return { x, y, w: el.offsetWidth, h: el.offsetHeight }
602
+ }
603
+ const end = () => {
604
+ if (!run) return
605
+ cancelAnimationFrame(run.id)
606
+ run.svg.remove()
607
+ run = null
608
+ // The focused item's own fill comes back at once, not through its fade.
609
+ const el = goal
610
+ if (el) el.style.transition = 'none'
611
+ delete root.dataset.bead
612
+ if (el) {
613
+ void getComputedStyle(el).backgroundColor
614
+ el.style.removeProperty('transition')
615
+ }
616
+ }
617
+ const tick = (now: number) => {
618
+ if (!run || !goal) return
619
+ const dt = Math.min(64, Math.max(0, now - run.last))
620
+ run.last = now
621
+ const fast = motionFor('fast')
622
+ const b = boxOf(goal)
623
+ const gy = b.y + b.h / 2
624
+ const gh = b.h / 2
625
+ st.F += (gy - st.F) * (1 - Math.exp(-dt / (fast / 9)))
626
+ st.hF += (gh - st.hF) * (1 - Math.exp(-dt / (fast / 9)))
627
+ st.T += (st.F - st.T) * (1 - Math.exp(-dt / (fast / 7)))
628
+ st.hT += (st.hF - st.hT) * (1 - Math.exp(-dt / (fast / 7)))
629
+ const most = st.hF + st.hT - 6 + NECK
630
+ if (Math.abs(st.F - st.T) > most) st.T = st.F - Math.sign(st.F - st.T) * most
631
+ const r = parseFloat(getComputedStyle(goal).borderTopLeftRadius) || 0
632
+ if (Math.abs(gy - st.F) < 0.05 && Math.abs(st.F - st.T) < 0.05 && Math.abs(gh - st.hT) < 0.05) {
633
+ run.path.setAttribute('d', boxPath({ cx: b.x + b.w / 2, cy: gy, hw: b.w / 2, hh: gh, r }))
634
+ end()
635
+ return
636
+ }
637
+ const loops = beadLoops(b.x + b.w / 2, b.w / 2, r, { y: st.F, hh: st.hF }, { y: st.T, hh: st.hT })
638
+ run.path.setAttribute('d', loops.map((l) => polyPath(l, 2)).join(''))
639
+ run.id = requestAnimationFrame(tick)
640
+ }
641
+ const focusIn = (ev: FocusEvent) => {
642
+ const to = itemOf(ev.target)
643
+ if (!to) return
644
+ const from = itemOf(ev.relatedTarget)
645
+ goal = to
646
+ if (!motionFor('fast')) return end()
647
+ if (!run) {
648
+ // A first highlight just appears (its CSS fade); only a move between items flows.
649
+ if (!from || from === to) return
650
+ root.dataset.bead = ''
651
+ const b = boxOf(from)
652
+ st.F = st.T = b.y + b.h / 2
653
+ st.hF = st.hT = b.h / 2
654
+ const { svg, paths } = layer(root, cls, root.firstChild)
655
+ svg.setAttribute('width', `${root.scrollWidth}`)
656
+ svg.setAttribute('height', `${root.scrollHeight}`)
657
+ paths[0].setAttribute('data-measure', 'shape')
658
+ run = { svg, path: paths[0], id: 0, last: performance.now() }
659
+ run.id = requestAnimationFrame(tick)
660
+ tick(performance.now())
661
+ }
662
+ if (run) run.path.style.fill = getComputedStyle(to).getPropertyValue('--gooee-bead').trim()
663
+ }
664
+ const focusOut = (ev: FocusEvent) => {
665
+ if (!run || itemOf(ev.relatedTarget)) return
666
+ const r = run
667
+ cancelAnimationFrame(r.id)
668
+ run = null
669
+ goal = null
670
+ delete root.dataset.bead
671
+ const ms = motionFor('fast')
672
+ if (!ms || typeof r.svg.animate !== 'function') return r.svg.remove()
673
+ r.svg.animate({ opacity: [1, 0] }, { duration: ms, easing: 'linear', fill: 'forwards' }).finished.then(
674
+ () => r.svg.remove(),
675
+ () => r.svg.remove(),
676
+ )
677
+ }
678
+ root.addEventListener('focusin', focusIn)
679
+ root.addEventListener('focusout', focusOut)
680
+ return () => {
681
+ root.removeEventListener('focusin', focusIn)
682
+ root.removeEventListener('focusout', focusOut)
683
+ end()
684
+ }
685
+ }, [panel, on, cls])
686
+ }