gooee 0.50.0 → 0.51.1

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 (172) hide show
  1. package/README.md +17 -4
  2. package/package.json +5 -3
  3. package/scripts/add.js +28 -6
  4. package/src/components/Accordion/Accordion.vue +62 -0
  5. package/src/components/Accordion/AccordionItem.vue +45 -0
  6. package/src/components/Accordion/accordionContext.ts +6 -0
  7. package/src/components/Alert/Alert.tsx +7 -27
  8. package/src/components/Alert/Alert.vue +59 -0
  9. package/src/components/AlertDialog/AlertDialog.vue +126 -0
  10. package/src/components/AnimatedNumber/AnimatedNumber.vue +36 -0
  11. package/src/components/AspectRatio/AspectRatio.vue +17 -0
  12. package/src/components/Avatar/Avatar.vue +56 -0
  13. package/src/components/AvatarGroup/AvatarGroup.vue +42 -0
  14. package/src/components/Badge/Badge.vue +32 -0
  15. package/src/components/Breadcrumb/Breadcrumb.vue +58 -0
  16. package/src/components/Button/Button.vue +130 -0
  17. package/src/components/ButtonGroup/ButtonGroup.tsx +4 -21
  18. package/src/components/ButtonGroup/ButtonGroup.vue +33 -0
  19. package/src/components/Card/Card.vue +34 -0
  20. package/src/components/Center/Center.vue +32 -0
  21. package/src/components/Checkbox/Checkbox.tsx +8 -25
  22. package/src/components/Checkbox/Checkbox.vue +108 -0
  23. package/src/components/CheckboxGroup/CheckboxGroup.vue +76 -0
  24. package/src/components/Cluster/Cluster.vue +34 -0
  25. package/src/components/CodeBlock/CodeBlock.vue +45 -0
  26. package/src/components/Collapsible/Collapsible.vue +59 -0
  27. package/src/components/Container/Container.vue +22 -0
  28. package/src/components/CopyButton/CopyButton.tsx +3 -49
  29. package/src/components/CopyButton/CopyButton.vue +116 -0
  30. package/src/components/Dial/Dial.tsx +12 -120
  31. package/src/components/Dial/Dial.vue +228 -0
  32. package/src/components/Dialog/Dialog.vue +139 -0
  33. package/src/components/Divider/Divider.vue +27 -0
  34. package/src/components/DropdownMenu/DropdownMenu.tsx +11 -46
  35. package/src/components/DropdownMenu/DropdownMenu.vue +185 -0
  36. package/src/components/EmptyState/EmptyState.vue +33 -0
  37. package/src/components/Fab/Fab.tsx +4 -14
  38. package/src/components/Fab/Fab.vue +115 -0
  39. package/src/components/Fieldset/Fieldset.vue +42 -0
  40. package/src/components/FormField/FormField.tsx +2 -17
  41. package/src/components/FormField/FormField.vue +64 -0
  42. package/src/components/Grid/Grid.vue +43 -0
  43. package/src/components/HoldToConfirm/HoldToConfirm.tsx +13 -143
  44. package/src/components/HoldToConfirm/HoldToConfirm.vue +281 -0
  45. package/src/components/IconButton/IconButton.vue +25 -0
  46. package/src/components/Input/Input.vue +112 -0
  47. package/src/components/InputGroup/InputGroup.vue +102 -0
  48. package/src/components/Kbd/Kbd.tsx +2 -12
  49. package/src/components/Kbd/Kbd.vue +21 -0
  50. package/src/components/Link/Link.vue +38 -0
  51. package/src/components/LivePill/LivePill.tsx +11 -39
  52. package/src/components/LivePill/LivePill.vue +152 -0
  53. package/src/components/Meter/Meter.vue +88 -0
  54. package/src/components/MorphButton/MorphButton.tsx +11 -67
  55. package/src/components/MorphButton/MorphButton.vue +120 -0
  56. package/src/components/NumberInput/NumberInput.tsx +11 -48
  57. package/src/components/NumberInput/NumberInput.vue +184 -0
  58. package/src/components/Pagination/Pagination.tsx +2 -15
  59. package/src/components/Pagination/Pagination.vue +87 -0
  60. package/src/components/Pagination/range.ts +15 -0
  61. package/src/components/PasswordInput/PasswordInput.vue +53 -0
  62. package/src/components/PickerWheel/DateWheel.vue +48 -0
  63. package/src/components/PickerWheel/PickerWheel.tsx +15 -129
  64. package/src/components/PickerWheel/PickerWheel.vue +77 -0
  65. package/src/components/PickerWheel/TimeWheel.vue +50 -0
  66. package/src/components/PickerWheel/WheelColumn.vue +70 -0
  67. package/src/components/Popover/Popover.vue +108 -0
  68. package/src/components/ProgressBar/ProgressBar.vue +48 -0
  69. package/src/components/Prose/Prose.vue +18 -0
  70. package/src/components/Radio/Radio.vue +49 -0
  71. package/src/components/Radio/RadioGroup.vue +69 -0
  72. package/src/components/Retime/Retime.tsx +7 -40
  73. package/src/components/Retime/Retime.vue +64 -0
  74. package/src/components/RulerScrubber/RulerScrubber.tsx +9 -38
  75. package/src/components/RulerScrubber/RulerScrubber.vue +166 -0
  76. package/src/components/ScrollArea/ScrollArea.tsx +17 -129
  77. package/src/components/ScrollArea/ScrollArea.vue +58 -0
  78. package/src/components/SearchInput/SearchInput.tsx +2 -15
  79. package/src/components/SearchInput/SearchInput.vue +70 -0
  80. package/src/components/SegmentedControl/SegmentedControl.tsx +7 -51
  81. package/src/components/SegmentedControl/SegmentedControl.vue +111 -0
  82. package/src/components/Select/Select.tsx +16 -47
  83. package/src/components/Select/Select.vue +236 -0
  84. package/src/components/Sheet/Sheet.tsx +52 -231
  85. package/src/components/Sheet/Sheet.vue +249 -0
  86. package/src/components/Skeleton/Skeleton.vue +38 -0
  87. package/src/components/Slider/Slider.tsx +25 -84
  88. package/src/components/Slider/Slider.vue +113 -0
  89. package/src/components/Spinner/Spinner.vue +30 -0
  90. package/src/components/Split/Split.vue +34 -0
  91. package/src/components/SplitFlap/SplitFlap.tsx +11 -175
  92. package/src/components/SplitFlap/SplitFlap.vue +52 -0
  93. package/src/components/Stack/Stack.vue +22 -0
  94. package/src/components/SwipeDeck/SwipeDeck.tsx +30 -194
  95. package/src/components/SwipeDeck/SwipeDeck.vue +168 -0
  96. package/src/components/Switch/Switch.tsx +6 -75
  97. package/src/components/Switch/Switch.vue +66 -0
  98. package/src/components/Table/Table.module.css +12 -3
  99. package/src/components/Table/Table.tsx +8 -59
  100. package/src/components/Table/Table.vue +274 -0
  101. package/src/components/TableOfContents/TableOfContents.vue +90 -0
  102. package/src/components/Tabs/Tab.vue +37 -0
  103. package/src/components/Tabs/TabList.vue +47 -0
  104. package/src/components/Tabs/TabPanel.vue +26 -0
  105. package/src/components/Tabs/Tabs.module.css +2 -2
  106. package/src/components/Tabs/Tabs.tsx +11 -84
  107. package/src/components/Tabs/Tabs.vue +33 -0
  108. package/src/components/Tabs/tabsContext.ts +15 -0
  109. package/src/components/Tag/Tag.tsx +6 -26
  110. package/src/components/Tag/Tag.vue +63 -0
  111. package/src/components/Textarea/Textarea.tsx +2 -22
  112. package/src/components/Textarea/Textarea.vue +120 -0
  113. package/src/components/ThemeToggle/ThemeToggle.tsx +10 -84
  114. package/src/components/ThemeToggle/ThemeToggle.vue +90 -0
  115. package/src/components/Toast/Toast.tsx +34 -264
  116. package/src/components/Toast/Toast.vue +138 -0
  117. package/src/components/Toast/ToastItem.vue +108 -0
  118. package/src/components/Toggle/Toggle.vue +52 -0
  119. package/src/components/Tooltip/Tooltip.tsx +17 -48
  120. package/src/components/Tooltip/Tooltip.vue +95 -0
  121. package/src/components/VisuallyHidden/VisuallyHidden.vue +17 -0
  122. package/src/internal/FieldMessage.vue +41 -0
  123. package/src/internal/Roll.vue +20 -0
  124. package/src/internal/focus.ts +4 -47
  125. package/src/internal/icons.module.css +39 -27
  126. package/src/internal/layers.ts +7 -61
  127. package/src/internal/motion/alert.ts +17 -0
  128. package/src/internal/motion/buttonGroup.ts +21 -0
  129. package/src/internal/motion/checkbox.ts +39 -0
  130. package/src/internal/motion/collapseOut.ts +26 -0
  131. package/src/internal/motion/copyButton.ts +48 -0
  132. package/src/internal/motion/dial.ts +137 -0
  133. package/src/internal/motion/fab.ts +14 -0
  134. package/src/internal/motion/floating.ts +58 -0
  135. package/src/internal/motion/focus.ts +53 -0
  136. package/src/internal/motion/formField.ts +19 -0
  137. package/src/internal/motion/holdToConfirm.ts +147 -0
  138. package/src/internal/motion/keys.ts +42 -0
  139. package/src/internal/motion/layers.ts +78 -0
  140. package/src/internal/motion/listbox.ts +69 -0
  141. package/src/internal/motion/livePill.ts +71 -0
  142. package/src/internal/motion/menuKeys.ts +58 -0
  143. package/src/internal/motion/morphButton.ts +71 -0
  144. package/src/internal/motion/numberInput.ts +57 -0
  145. package/src/internal/motion/pickerWheel.ts +166 -0
  146. package/src/internal/motion/retime.ts +45 -0
  147. package/src/internal/motion/ruler.ts +57 -0
  148. package/src/internal/motion/scrollArea.ts +133 -0
  149. package/src/internal/motion/scrubLook.ts +15 -0
  150. package/src/internal/motion/segmented.ts +63 -0
  151. package/src/internal/motion/sheetDetents.ts +271 -0
  152. package/src/internal/motion/slider.ts +108 -0
  153. package/src/internal/motion/splitFlap.ts +205 -0
  154. package/src/internal/motion/swipeDeck.ts +236 -0
  155. package/src/internal/motion/switchThumb.ts +83 -0
  156. package/src/internal/motion/table.ts +77 -0
  157. package/src/internal/motion/tabs.ts +105 -0
  158. package/src/internal/motion/textarea.ts +22 -0
  159. package/src/internal/motion/themeIcon.ts +91 -0
  160. package/src/internal/motion/themeReveal.ts +71 -0
  161. package/src/internal/motion/toast.ts +291 -0
  162. package/src/internal/motion/tooltip.ts +58 -0
  163. package/src/internal/themeReveal.ts +3 -61
  164. package/src/internal/useFloating.ts +9 -49
  165. package/src/internal/useScrub.ts +1 -13
  166. package/src/internal/vue/trigger.ts +18 -0
  167. package/src/internal/vue/useFloating.ts +38 -0
  168. package/src/internal/vue/usePresence.ts +21 -0
  169. package/src/internal/vue/useScrub.ts +40 -0
  170. package/src/internal/vueIcons.ts +134 -0
  171. package/src/tokens.css +5 -0
  172. package/src/internal/relativeTime.ts +0 -26
@@ -0,0 +1,105 @@
1
+ import { bezier } from '../feel'
2
+ import { morphCurve, motionMs, prefersReducedMotion } from '../motion'
3
+
4
+ // The parts of Tabs that are the same in every framework: where the chosen tab is, the tab stop, the
5
+ // arrow keys and the rise that stretches over to a new choice.
6
+
7
+ export type TabBar = { x: number; w: number; room: number; value: string }
8
+
9
+ /** The chosen tab's place in the row, or null when no tab is chosen. */
10
+ export function measureTab(list: HTMLElement, value: string): TabBar | null {
11
+ const el = list.querySelector<HTMLElement>('[role="tab"][aria-selected="true"]')
12
+ return el ? { x: el.offsetLeft, w: el.offsetWidth, room: list.offsetWidth, value } : null
13
+ }
14
+
15
+ /** A Tab only knows itself, so the list picks the tab stop when the value matches no enabled tab. */
16
+ export function setTabStop(list: HTMLElement | null) {
17
+ const tabs = Array.from(list?.querySelectorAll<HTMLButtonElement>('[role="tab"]') ?? [])
18
+ const enabled = tabs.filter((t) => !t.disabled)
19
+ const stop = enabled.find((t) => t.getAttribute('aria-selected') === 'true') ?? enabled[0]
20
+ for (const t of tabs) t.tabIndex = t === stop ? 0 : -1
21
+ }
22
+
23
+ /** Automatic activation, per the WAI-ARIA tabs pattern. */
24
+ export function tabKey(list: HTMLElement | null, e: { key: string; preventDefault(): void }) {
25
+ const tabs = Array.from(list?.querySelectorAll<HTMLButtonElement>('[role="tab"]:not(:disabled)') ?? [])
26
+ const i = tabs.indexOf(document.activeElement as HTMLButtonElement)
27
+ if (i < 0) return
28
+ let next = -1
29
+ if (e.key === 'ArrowRight') next = (i + 1) % tabs.length
30
+ else if (e.key === 'ArrowLeft') next = (i - 1 + tabs.length) % tabs.length
31
+ else if (e.key === 'Home') next = 0
32
+ else if (e.key === 'End') next = tabs.length - 1
33
+ if (next < 0) return
34
+ e.preventDefault()
35
+ tabs[next].focus()
36
+ tabs[next].click()
37
+ }
38
+
39
+ /** Brings a picked tab into view in a row that scrolls sideways. */
40
+ export function revealTab(id: string) {
41
+ document.getElementById(id)?.scrollIntoView({ block: 'nearest', inline: 'nearest', behavior: prefersReducedMotion() ? 'auto' : 'smooth' })
42
+ }
43
+
44
+ /**
45
+ * The rise moves by script, so each frame knows how far each edge is from the row's ends. Near an end the
46
+ * panel's corner and the rise's inside corner shrink together; at the end the rise and the panel share
47
+ * one straight edge. Call `place` with each new measure.
48
+ */
49
+ export function createTabsRise() {
50
+ let drawn: { l: number; r: number; value: string } | null = null
51
+ let raf = 0
52
+ // Where a running move is heading. A re-measure of the same tab (the size watcher reports once more
53
+ // after every change) only updates this; it never restarts or cuts the move short.
54
+ let aim: { l: number; r: number; value: string } | null = null
55
+
56
+ const place = (rise: HTMLElement | null, bar: TabBar | null) => {
57
+ const root = rise?.closest<HTMLElement>('[data-gooee-tabs]')
58
+ if (!bar || !rise || !root) return
59
+ const R = parseFloat(getComputedStyle(root).getPropertyValue('--tabs-r')) || 0
60
+ // Within two corners of an end, the panel's corner and the inside corner each take half the room, so
61
+ // they meet as one even S; at the end both are gone.
62
+ const curve = (gap: number) => `${Math.max(0, Math.min(R, gap / 2))}px`
63
+ const draw = (l: number, r: number) => {
64
+ rise.style.left = `${l}px`
65
+ rise.style.right = `${r}px`
66
+ rise.style.setProperty('--tabs-in-start', curve(l))
67
+ rise.style.setProperty('--tabs-in-end', curve(r))
68
+ root.style.setProperty('--tabs-corner-start', curve(l))
69
+ root.style.setProperty('--tabs-corner-end', curve(r))
70
+ drawn = { l, r, value: aim?.value ?? bar.value }
71
+ }
72
+ const to = { l: bar.x, r: bar.room - bar.x - bar.w }
73
+ if (aim?.value === bar.value) {
74
+ aim = { ...to, value: bar.value }
75
+ return
76
+ }
77
+ const from = drawn
78
+ cancelAnimationFrame(raf)
79
+ aim = null
80
+ const length = motionMs('morph-out')
81
+ // Only a new choice moves; a resize or the first paint just puts it in place.
82
+ if (!from || from.value === bar.value || !length || prefersReducedMotion()) return draw(to.l, to.r)
83
+ // The edge on the way leads; the other follows a beat later, so the rise stretches, then settles.
84
+ const lag = motionMs('instant')
85
+ const ease = bezier(morphCurve())
86
+ const toEnd = to.l > from.l
87
+ const t0 = performance.now()
88
+ aim = { ...to, value: bar.value }
89
+ const step = (now: number) => {
90
+ const at = aim
91
+ if (!at) return
92
+ const t = now - t0
93
+ const lead = ease(Math.min(1, t / length))
94
+ const trail = ease(Math.min(1, Math.max(0, (t - lag) / length)))
95
+ const ul = toEnd ? trail : lead
96
+ const ur = toEnd ? lead : trail
97
+ draw(from.l + (at.l - from.l) * ul, from.r + (at.r - from.r) * ur)
98
+ if (t < length + lag) raf = requestAnimationFrame(step)
99
+ else aim = null
100
+ }
101
+ raf = requestAnimationFrame(step)
102
+ }
103
+
104
+ return { place, destroy: () => cancelAnimationFrame(raf) }
105
+ }
@@ -0,0 +1,22 @@
1
+ // Textarea: the field grows smoothly with its text between minRows and maxRows, and the live count follows.
2
+ export function growTextarea(el: HTMLTextAreaElement | null, count: HTMLElement | null, o: { autoGrow: boolean; minRows: number; maxRows: number; maxLength?: number }) {
3
+ if (!el) return
4
+ if (count) count.textContent = `${el.value.length} / ${o.maxLength}`
5
+ if (!o.autoGrow) return
6
+ const cs = getComputedStyle(el)
7
+ const line = parseFloat(cs.lineHeight) || 24
8
+ const pad = parseFloat(cs.paddingTop) + parseFloat(cs.paddingBottom)
9
+ const border = parseFloat(cs.borderTopWidth) + parseFloat(cs.borderBottomWidth)
10
+ const prev = el.style.height
11
+ el.style.transition = 'none'
12
+ el.style.height = 'auto'
13
+ const min = line * o.minRows + pad + border
14
+ const max = line * o.maxRows + pad + border
15
+ const next = Math.min(Math.max(el.scrollHeight + border, min), max)
16
+ // Restore the old height and force layout, so the transition runs
17
+ el.style.height = prev || `${next}px`
18
+ void el.offsetHeight
19
+ el.style.transition = ''
20
+ el.style.height = `${next}px`
21
+ el.style.overflowY = el.scrollHeight + border > max ? 'auto' : 'hidden'
22
+ }
@@ -0,0 +1,91 @@
1
+ import { bezier } from '../feel'
2
+ import { morphCurve, motionMs, prefersReducedMotion } from '../motion'
3
+
4
+ // Sun to moon the way the real moon works: a round disc with a shadow passing over it. The disc stays
5
+ // round and grows from the sun's size to the moon's; a second circle, the bite, slides in from the top
6
+ // right and carves the crescent. The outline is drawn exactly each frame as "disc minus bite", so every
7
+ // frame is a true shape. The rays draw back into the disc and fade first, before it grows into them.
8
+ const C = 12
9
+ const lerp = (a: number, b: number, t: number) => a + (b - a) * t
10
+ const part = (t: number, a: number, b: number) => Math.min(1, Math.max(0, (t - a) / (b - a)))
11
+ const f = (n: number) => Math.round(n * 100) / 100
12
+ const BITE = 7
13
+ const TOWARD = -Math.PI / 4
14
+ export function bodyAt(t: number) {
15
+ const r = lerp(4, 8, part(t, 0.2, 1))
16
+ // The bite starts clear of the disc and ends 5.6 from its centre.
17
+ const d = lerp(r + BITE + 1, 5.6, part(t, 0.15, 1))
18
+ if (d >= r + BITE) return `M${C - r} ${C}A${f(r)} ${f(r)} 0 1 1 ${C + r} ${C}A${f(r)} ${f(r)} 0 1 1 ${C - r} ${C}Z`
19
+ const half = Math.acos((r * r + d * d - BITE * BITE) / (2 * r * d))
20
+ const at = (o: [number, number], rad: number, ang: number): [number, number] => [o[0] + rad * Math.cos(ang), o[1] + rad * Math.sin(ang)]
21
+ const centre: [number, number] = [C, C]
22
+ const bite = at(centre, d, TOWARD)
23
+ const A = at(centre, r, TOWARD + half)
24
+ const B = at(centre, r, TOWARD - half)
25
+ // The disc's edge from A the long way round to B, then the bite's edge back to A through the disc.
26
+ const outer = 2 * Math.PI - 2 * half
27
+ // Of the two ways round the bite from B to A, the one whose middle lies inside the disc.
28
+ const angA = Math.atan2(A[1] - bite[1], A[0] - bite[0])
29
+ const angB = Math.atan2(B[1] - bite[1], B[0] - bite[0])
30
+ const up = (((angA - angB) % (2 * Math.PI)) + 2 * Math.PI) % (2 * Math.PI)
31
+ const mid = at(bite, BITE, angB + up / 2)
32
+ const sweep = Math.hypot(mid[0] - C, mid[1] - C) < r ? 1 : 0
33
+ const inner = sweep ? up : 2 * Math.PI - up
34
+ return `M${f(A[0])} ${f(A[1])}A${f(r)} ${f(r)} 0 ${outer > Math.PI ? 1 : 0} 1 ${f(B[0])} ${f(B[1])}A${BITE} ${BITE} 0 ${inner > Math.PI ? 1 : 0} ${sweep} ${f(A[0])} ${f(A[1])}Z`
35
+ }
36
+ export const RAYS = Array.from({ length: 8 }, (_, i) => {
37
+ const a = (i * Math.PI) / 4
38
+ return `M${f(C + 7 * Math.cos(a))} ${f(C + 7 * Math.sin(a))}L${f(C + 9.5 * Math.cos(a))} ${f(C + 9.5 * Math.sin(a))}`
39
+ }).join('')
40
+ // The rays draw in toward the disc, turn a little and fade, all within the first 40%.
41
+ export const raysAt = (t: number) => {
42
+ const k = part(t, 0, 0.4)
43
+ return { transform: `rotate(${f(-30 * k)}deg) scale(${f(1 - 0.45 * k)})`, opacity: String(f(1 - k)) }
44
+ }
45
+ function draw(body: SVGPathElement, t: number) {
46
+ body.setAttribute('d', bodyAt(t))
47
+ const rays = body.nextElementSibling as SVGPathElement | null
48
+ if (rays) Object.assign(rays.style, raysAt(t))
49
+ }
50
+
51
+ /**
52
+ * The theme toggle's icon: 1 is the moon, 0 the sun. The body morphs on the morph curve, in the reveal's
53
+ * time, so the icon and the theme's circle end together; the rays are drawn on the same frames.
54
+ */
55
+ export function createThemeIcon(body: () => SVGPathElement | null, start: number) {
56
+ let shown = start
57
+ let aim = start
58
+ let raf = 0
59
+ const morphTo = (to: number) => {
60
+ const el = body()
61
+ aim = to
62
+ cancelAnimationFrame(raf)
63
+ if (!el) return
64
+ const from = shown
65
+ const length = motionMs('reveal')
66
+ if (from === to || !length || prefersReducedMotion()) {
67
+ shown = to
68
+ draw(el, to)
69
+ return
70
+ }
71
+ const ease = bezier(morphCurve())
72
+ const t0 = performance.now()
73
+ const step = (now: number) => {
74
+ const u = Math.min(1, (now - t0) / length)
75
+ shown = from + (to - from) * ease(u)
76
+ draw(el, shown)
77
+ if (u < 1) raf = requestAnimationFrame(step)
78
+ }
79
+ raf = requestAnimationFrame(step)
80
+ }
81
+ return {
82
+ get shown() {
83
+ return shown
84
+ },
85
+ get aim() {
86
+ return aim
87
+ },
88
+ morphTo,
89
+ destroy: () => cancelAnimationFrame(raf),
90
+ }
91
+ }
@@ -0,0 +1,71 @@
1
+ import { easing, motionMs, prefersReducedMotion } from '../motion'
2
+
3
+ // The new theme grows from the spot you pressed: the page is snapshot as it is, and the new page opens
4
+ // over it in a circle from the button's centre to the farthest corner, at an even pace. The old snapshot
5
+ // stays still under it, and no element plays its own colour change meanwhile, so nothing flips piece by
6
+ // piece. A second press finishes the running one first. Without View Transitions the page fades in once;
7
+ // with reduced motion it changes at once.
8
+
9
+ let running: ViewTransition | null = null
10
+
11
+ // The radius whose circle covers `share` of the screen, by halving, counted on a coarse grid.
12
+ function radiusFor(share: number, x: number, y: number, reach: number) {
13
+ if (share >= 1) return reach
14
+ const cols = 48, rows = 32
15
+ const covered = (r: number) => {
16
+ let n = 0
17
+ 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++
18
+ return n / (cols * rows)
19
+ }
20
+ let lo = 0, hi = reach
21
+ for (let k = 0; k < 18; k++) {
22
+ const m = (lo + hi) / 2
23
+ if (covered(m) < share) lo = m
24
+ else hi = m
25
+ }
26
+ return hi
27
+ }
28
+
29
+ /**
30
+ * Plays the theme change from `origin`. `applyNow` must put the new theme in the page before it returns
31
+ * (or before its promise settles), since the view transition photographs the page right after it: React
32
+ * wraps its update in flushSync, Vue awaits nextTick.
33
+ */
34
+ export function themeReveal(origin: Element | null, applyNow: () => void | Promise<void>) {
35
+ const root = document.documentElement
36
+ const ms = motionMs('reveal')
37
+ if (prefersReducedMotion() || !ms) return void applyNow()
38
+ running?.skipTransition()
39
+ root.dataset.gooeeThemeSwitch = ''
40
+ const done = () => delete root.dataset.gooeeThemeSwitch
41
+ if (typeof document.startViewTransition !== 'function') {
42
+ void applyNow()
43
+ document.body.animate([{ opacity: 0.4 }, { opacity: 1 }], { duration: ms * 0.6, easing: easing('morph') }).finished.then(done, done)
44
+ return
45
+ }
46
+ const r = origin?.getBoundingClientRect()
47
+ const x = r ? r.left + r.width / 2 : innerWidth / 2
48
+ const y = r ? r.top + r.height / 2 : 0
49
+ const reach = Math.hypot(Math.max(x, innerWidth - x), Math.max(y, innerHeight - y))
50
+ const vt = document.startViewTransition(applyNow)
51
+ running = vt
52
+ vt.ready.then(
53
+ // The share of the screen covered grows at an even, calm pace (ease out): a circle's area grows with
54
+ // the radius squared, so each step's radius is solved for its share; linear in between.
55
+ () =>
56
+ root.animate(
57
+ { clipPath: Array.from({ length: 21 }, (_, i) => `circle(${radiusFor(1 - (1 - i / 20) ** 2, x, y, reach)}px at ${x}px ${y}px)`) },
58
+ { duration: ms, easing: 'linear', pseudoElement: '::view-transition-new(root)' },
59
+ ),
60
+ () => {},
61
+ )
62
+ vt.finished.finally(() => {
63
+ if (running !== vt) return
64
+ running = null
65
+ done()
66
+ })
67
+ }
68
+
69
+ /** Put in <head>, before the page paints, so a saved dark theme never flashes light first. */
70
+ export const themeScript = (storageKey = 'gooee-theme') =>
71
+ `try{var t=localStorage.getItem(${JSON.stringify(storageKey)});if(t!=='light'&&t!=='dark')t=matchMedia('(prefers-color-scheme: dark)').matches?'dark':'light';document.documentElement.dataset.theme=t}catch(e){}`
@@ -0,0 +1,291 @@
1
+ import { speedTracker } from '../feel'
2
+ import { BEND, body, budScene, exitToward, loopsPath, pincher, sdBox, trace, type Box, type Pt } from '../liquidPath'
3
+ import { release } from './fling'
4
+ import { castShadow, frames, layer, morphEase, motionFor, shadowOf } from './liquidMatter'
5
+
6
+ // The toast stack, the same in every framework: where each toast sits, how long it stays, the throw
7
+ // that sends it away, and the drop it buds out of.
8
+
9
+ export type ToastVariant = 'default' | 'success' | 'error' | 'info' | 'loading'
10
+ export type ToastOrigin = HTMLElement | { x: number; y: number }
11
+ export type ToastLeaving = false | 'down' | 'left' | 'right'
12
+
13
+ const GAP = 8
14
+ export const TOAST_PEEK = 10
15
+ // px per second: a toast thrown sideways this fast goes, however short the throw.
16
+ const THROW_SPEED = 600
17
+
18
+ let nextId = 1
19
+ export const toastId = () => nextId++
20
+
21
+ /** What a screen reader hears. The same text twice is not read again, so a trailing space makes it change. */
22
+ export function toastSay(prev: string, opts: { title?: string; description?: string }) {
23
+ const text = `${opts.title}${opts.description ? `. ${opts.description}` : ''}`
24
+ return prev === text ? `${text} ` : text
25
+ }
26
+
27
+ /** Where each toast sits, newest first, and how tall the stack is. */
28
+ export function toastLayout<T extends { id: number; leaving: ToastLeaving }>(items: T[], heights: Record<number, number>, expanded: boolean, visible: number) {
29
+ const live = items.filter((t) => !t.leaving)
30
+ const order = [...items].reverse()
31
+ let offset = 0
32
+ const layout = order.map((t) => {
33
+ const liveIndex = live.length - 1 - live.findIndex((x) => x.id === t.id)
34
+ const index = t.leaving ? -1 : liveIndex
35
+ const y = offset
36
+ if (!t.leaving) offset += (heights[t.id] ?? 64) + GAP
37
+ return { t, index, y }
38
+ })
39
+ const frontHeight = live.length ? (heights[live[live.length - 1].id] ?? 64) : 0
40
+ const stackHeight = expanded ? offset : frontHeight + Math.min(live.length - 1, visible - 1) * TOAST_PEEK
41
+ return { layout, live: live.length, frontHeight, height: live.length ? stackHeight : 0 }
42
+ }
43
+
44
+ /** A toast's place in the stack: up by its offset when fanned out, else peeking out behind the front one. */
45
+ export const toastStack = (index: number, y: number, expanded: boolean) => ({
46
+ y: expanded ? -y : -Math.min(index, 3) * TOAST_PEEK,
47
+ scale: expanded ? 1 : 1 - Math.min(index, 3) * 0.05,
48
+ })
49
+
50
+ /** The toast's natural height, even while clamped behind the front toast. Calls `onHeight` as it changes. Returns a stop. */
51
+ export function watchToastHeight(el: HTMLElement, onHeight: (h: number) => void) {
52
+ const measure = () => onHeight(el.scrollHeight + el.offsetHeight - el.clientHeight)
53
+ const ro = new ResizeObserver(measure)
54
+ ro.observe(el)
55
+ measure()
56
+ return () => ro.disconnect()
57
+ }
58
+
59
+ /**
60
+ * Counts down `left.current` ms, then calls `done`. While the tab is hidden it waits. Returns a stop
61
+ * that keeps what is left in `left.current`, so the count goes on where it paused.
62
+ */
63
+ export function toastTimer(left: { current: number }, done: () => void) {
64
+ let start = performance.now()
65
+ let t = window.setTimeout(done, left.current)
66
+ const onVis = () => {
67
+ if (document.hidden) {
68
+ window.clearTimeout(t)
69
+ left.current -= performance.now() - start
70
+ } else {
71
+ start = performance.now()
72
+ t = window.setTimeout(done, left.current)
73
+ }
74
+ }
75
+ document.addEventListener('visibilitychange', onVis)
76
+ return () => {
77
+ window.clearTimeout(t)
78
+ if (!document.hidden) left.current -= performance.now() - start
79
+ document.removeEventListener('visibilitychange', onVis)
80
+ }
81
+ }
82
+
83
+ type PointerLike = { clientX: number; pointerId: number; type: string; target: EventTarget | null; currentTarget: EventTarget | null }
84
+
85
+ /**
86
+ * A toast the hand throws sideways. Where it is drawn goes straight on --dx, so a glide does not render
87
+ * every frame. A let-go past 80px, or a throw, sends it off the way it went; any other let-go glides it back.
88
+ */
89
+ export function createToastSwipe(
90
+ target: () => HTMLElement | null,
91
+ { leaving, dragging, dismiss }: { leaving: () => boolean; dragging: (on: boolean) => void; dismiss: (way: 'left' | 'right') => void },
92
+ ) {
93
+ let at = 0
94
+ let stop = () => {}
95
+ let swipe: { x: number; dx: number; speed: ReturnType<typeof speedTracker> } | null = null
96
+ const paint = (v: number) => {
97
+ at = v
98
+ target()?.style.setProperty('--dx', `${v}px`)
99
+ }
100
+ const fling = (to: number, speed: number) => {
101
+ const el = target()
102
+ stop()
103
+ el?.setAttribute('data-fluid', '')
104
+ stop = release(at, to, speed, paint, () => {
105
+ if (to === 0) el?.removeAttribute('data-fluid')
106
+ })
107
+ }
108
+ return {
109
+ stop: () => stop(),
110
+ down(e: PointerLike) {
111
+ if ((e.target as HTMLElement).closest('button') || leaving()) return
112
+ stop()
113
+ // Caught mid-glide, it carries on from where it is.
114
+ swipe = { x: e.clientX - at, dx: at, speed: speedTracker() }
115
+ swipe.speed.add(at)
116
+ ;(e.currentTarget as HTMLElement).setPointerCapture(e.pointerId)
117
+ dragging(true)
118
+ },
119
+ move(e: PointerLike) {
120
+ const s = swipe
121
+ if (!s) return
122
+ s.dx = e.clientX - s.x
123
+ s.speed.add(s.dx)
124
+ paint(s.dx)
125
+ },
126
+ up(e: PointerLike) {
127
+ const s = swipe
128
+ swipe = null
129
+ dragging(false)
130
+ if (!s) return
131
+ const speed = e.type === 'pointercancel' ? 0 : s.speed.speed()
132
+ const thrown = Math.abs(speed) > THROW_SPEED && Math.abs(s.dx) > 20
133
+ if (e.type !== 'pointercancel' && (Math.abs(s.dx) > 80 || thrown)) {
134
+ // It leaves the way it was thrown, or the way it was pulled when let go slowly.
135
+ const way = Math.sign(thrown ? speed : s.dx)
136
+ fling(way * (target()?.offsetWidth ?? 0) * 1.1, speed)
137
+ dismiss(way < 0 ? 'left' : 'right')
138
+ } else fling(0, speed)
139
+ },
140
+ }
141
+ }
142
+
143
+ const onScreen = (r: DOMRect) => r.width > 0 && r.bottom > 0 && r.right > 0 && r.top < window.innerHeight && r.left < window.innerWidth
144
+ const gapBetween = (a: DOMRect, b: DOMRect) => Math.hypot(Math.max(0, a.left - b.right, b.left - a.right), Math.max(0, a.top - b.bottom, b.top - a.bottom))
145
+ // An origin further than this from the toast would send the drop across the screen.
146
+ const NEAR = 240
147
+
148
+ /**
149
+ * Bud: a drop buds out of `origin`, hangs on a neck that breaks, and grows into the toast (`way` in),
150
+ * or the toast flows back into it (out). With no origin, or one far away, the drop buds out of the
151
+ * window's edge nearest the stack, under the toast. An SVG over the page, with the class `cls`, draws
152
+ * it; the toast itself shows only at the end. Returns a stop, or null when motion is reduced.
153
+ */
154
+ export function bud(toast: HTMLElement, origin: ToastOrigin | undefined, way: 'in' | 'out', cls: string) {
155
+ const ms = motionFor(way === 'in' ? 'morph' : 'morph-out')
156
+ if (!ms) return null
157
+ const t = toast.getBoundingClientRect()
158
+ const look = getComputedStyle(toast)
159
+ const line = parseFloat(look.borderTopWidth) || 0
160
+ const T: Box = { cx: t.left + t.width / 2, cy: t.top + t.height / 2, hw: t.width / 2, hh: t.height / 2, r: Math.min(t.height / 2, parseFloat(look.borderTopLeftRadius) || 0) }
161
+ let O: Box | null = null
162
+ let exit: Pt = [0, 0]
163
+ let n: Pt = [0, 1]
164
+ let rd = BEND + 3.5
165
+ let cut = false
166
+ const o = origin instanceof HTMLElement ? (origin.isConnected ? origin.getBoundingClientRect() : null) : origin ? new DOMRect(origin.x, origin.y, 1, 1) : null
167
+ const near = !!o && onScreen(o) && gapBetween(o, t) <= NEAR
168
+ if (near && origin instanceof HTMLElement) {
169
+ const h = Math.min(o.width, o.height) / 2
170
+ O = { cx: o.left + o.width / 2, cy: o.top + o.height / 2, hw: o.width / 2, hh: o.height / 2, r: Math.min(h, parseFloat(getComputedStyle(origin).borderTopLeftRadius) || 0) }
171
+ rd = Math.min(rd, Math.max(BEND + 0.5, h * 0.6))
172
+ ;({ at: exit, n } = exitToward(O, T, rd))
173
+ cut = true
174
+ } else if (near && origin && !(origin instanceof HTMLElement)) exit = [origin.x, origin.y]
175
+ // Too close to hang a drop between them, or no origin to use: it buds out of the window's edge. The
176
+ // edge is a body just past the screen, its top a stroke's width out, so only the drop and its neck show.
177
+ if (!near || sdBox(exit[0], exit[1], T) < rd * 4) {
178
+ const down = T.cy > window.innerHeight / 2 ? 1 : -1
179
+ rd = BEND + 3.5
180
+ const y = down > 0 ? window.innerHeight + line : -line
181
+ O = { cx: T.cx, cy: y + down * rd * 2, hw: rd * 6, hh: rd * 2, r: rd * 2 }
182
+ exit = [T.cx, y]
183
+ n = [0, -down]
184
+ cut = false
185
+ }
186
+ const { svg, paths } = layer(document.body, cls, null)
187
+ const path = paths[0]
188
+ // The toast's own fill, edge and shadow. The edge is drawn on the outline, so the outline sits half
189
+ // its width inside the toast's box, and the last frame lands on the box exactly.
190
+ path.style.fill = look.backgroundColor
191
+ path.style.stroke = look.borderTopColor
192
+ path.style.strokeWidth = `${line}px`
193
+ castShadow(svg, shadowOf(toast).shadows)
194
+ const inset = line / 2
195
+ const T2: Box = { cx: T.cx, cy: T.cy, hw: T.hw - inset, hh: T.hh - inset, r: Math.max(0, T.r - inset) }
196
+ // The origin keeps its own look: the drawing is cut out where it is.
197
+ if (O && cut) {
198
+ const ns = 'http://www.w3.org/2000/svg'
199
+ const clip = svg.appendChild(document.createElementNS(ns, 'clipPath'))
200
+ const id = `gooee-bud-${Math.random().toString(36).slice(2)}`
201
+ clip.id = id
202
+ const hole = clip.appendChild(document.createElementNS(ns, 'path'))
203
+ const r = Math.min(O.r + 1, O.hh + 1)
204
+ hole.setAttribute('clip-rule', 'evenodd')
205
+ hole.setAttribute(
206
+ 'd',
207
+ `M-1 -1H${window.innerWidth + 1}V${window.innerHeight + 1}H-1Z` +
208
+ `M${O.cx - O.hw - 1 + r} ${O.cy - O.hh - 1}H${O.cx + O.hw + 1 - r}A${r} ${r} 0 0 1 ${O.cx + O.hw + 1} ${O.cy - O.hh - 1 + r}V${O.cy + O.hh + 1 - r}A${r} ${r} 0 0 1 ${O.cx + O.hw + 1 - r} ${O.cy + O.hh + 1}H${O.cx - O.hw - 1 + r}A${r} ${r} 0 0 1 ${O.cx - O.hw - 1} ${O.cy + O.hh + 1 - r}V${O.cy - O.hh - 1 + r}A${r} ${r} 0 0 1 ${O.cx - O.hw - 1 + r} ${O.cy - O.hh - 1}Z`,
209
+ )
210
+ path.setAttribute('clip-path', `url(#${id})`)
211
+ }
212
+ const e = morphEase()
213
+ const recoil = motionFor('fast')
214
+ const pin = pincher(e, recoil)
215
+ const across: [number, number] = [n[1], n[0]]
216
+ const anchor: Pt = O ? (n[0] === 0 ? [exit[0], O.cy] : [O.cx, exit[1]]) : exit
217
+ // Once the drop is free it is never joined again, even where the toast sits close to the edge: going
218
+ // in, after the broken neck has sprung back; going out, until the drop hangs again.
219
+ let broke = 0
220
+ // The toast shows once the drop has its shape: at the end going in, at the start going out.
221
+ const show = toast.animate({ opacity: way === 'in' ? [0, 0, 1] : [1, 0, 0], offset: way === 'in' ? [0, 0.82, 1] : [0, 0.15, 1] }, { duration: ms, fill: 'forwards' })
222
+ const stop = frames(
223
+ ms,
224
+ (k, now) => {
225
+ const u = way === 'in' ? k : 1 - k
226
+ const D = budScene(e, u, O, T2, exit, n, rd)
227
+ let sdf = body(D).sdf
228
+ if (O) {
229
+ const free = way === 'in' ? (broke && now - broke >= recoil) || u >= 0.6 : u >= 0.4
230
+ if (free) {
231
+ const a = body(O).sdf
232
+ const b = sdf
233
+ sdf = (x, y) => Math.min(a(x, y), b(x, y))
234
+ } else {
235
+ const p = pin(body(O, anchor), body(D), now)
236
+ if (p.pieces === 2 && !broke) broke = now
237
+ sdf = p.sdf
238
+ }
239
+ }
240
+ const seeds: Array<[number, number, number, number]> = [[D.cx, D.cy, ...across]]
241
+ if (O) seeds.unshift([anchor[0], anchor[1], ...across])
242
+ path.setAttribute('d', loopsPath(trace(sdf, seeds)))
243
+ // A drop from a point fades in where it starts.
244
+ if (!O) svg.style.opacity = String(Math.min(1, u / 0.1))
245
+ },
246
+ () => {
247
+ svg.remove()
248
+ if (way === 'in') show.cancel()
249
+ },
250
+ )
251
+ return () => {
252
+ stop()
253
+ svg.remove()
254
+ show.cancel()
255
+ }
256
+ }
257
+
258
+ /**
259
+ * The bud of one toast. It plays once going in and once going out; going out it runs on after the toast
260
+ * is gone, so only an entry is stopped on `destroy`. Only the front toast flows out: one behind it folds
261
+ * away under it, and one thrown sideways keeps its throw. Call `place` on each render while it is not
262
+ * leaving, and `change` when it starts or stops leaving.
263
+ */
264
+ export function createToastBud(target: () => HTMLElement | null, origin: () => ToastOrigin | undefined, cls: string) {
265
+ let budding: false | 'in' | 'out' = false
266
+ let stopBud: (() => void) | null = null
267
+ let front = false
268
+ return {
269
+ place: (index: number) => (front = index === 0),
270
+ change(leaving: ToastLeaving) {
271
+ const el = target()
272
+ if (!el) return
273
+ const thrown = leaving && leaving !== 'down'
274
+ if (thrown || (leaving && !front)) {
275
+ if (budding === 'in') stopBud?.()
276
+ budding = false
277
+ if (!thrown) delete el.dataset.liquid
278
+ return
279
+ }
280
+ stopBud?.()
281
+ const way = leaving ? 'out' : 'in'
282
+ // The bud stands in for the part's CSS entry and exit; marked first, so the toast is measured in its place.
283
+ el.dataset.liquid = ''
284
+ stopBud = bud(el, origin(), way, cls)
285
+ budding = stopBud ? way : false
286
+ },
287
+ destroy() {
288
+ if (budding === 'in') stopBud?.()
289
+ },
290
+ }
291
+ }
@@ -0,0 +1,58 @@
1
+ import { motionMs } from '../motion'
2
+ import type { Side } from './floating'
3
+
4
+ // When a tooltip shows and hides, the same in every framework: hover after a delay, keyboard focus at
5
+ // once, a tap for a moment on touch screens.
6
+
7
+ // How long a tip stays after a tap on a touch screen, so it can be read.
8
+ const TOUCH_SHOW_MS = 1500
9
+
10
+ // Shared by every tooltip on the page: right after one closes, the next shows at once.
11
+ let lastClosedAt = 0
12
+
13
+ /** The edge of the target the tip grows out of: a sliver as wide as the target, on the tip's side. */
14
+ export function tipEdge(anchor: HTMLElement | null, side: Side) {
15
+ const r = anchor?.getBoundingClientRect()
16
+ if (!r) return null
17
+ if (side === 'top') return new DOMRect(r.left, r.top, r.width, 2)
18
+ if (side === 'bottom') return new DOMRect(r.left, r.bottom - 2, r.width, 2)
19
+ if (side === 'left') return new DOMRect(r.left, r.top, 2, r.height)
20
+ return new DOMRect(r.right - 2, r.top, 2, r.height)
21
+ }
22
+
23
+ type PointerLike = { pointerType: string }
24
+
25
+ /** Feed it the trigger's events; it calls `setOpen`. `delay` is the hover delay, or undefined for the token. */
26
+ export function createTipTimer(setOpen: (open: boolean) => void, delay: () => number | undefined) {
27
+ let timer: number | undefined
28
+ const clear = () => window.clearTimeout(timer)
29
+ const show = (wait: number) => {
30
+ clear()
31
+ timer = window.setTimeout(() => setOpen(true), wait)
32
+ }
33
+ const hide = (wait = 80) => {
34
+ clear()
35
+ timer = window.setTimeout(() => {
36
+ setOpen(false)
37
+ lastClosedAt = Date.now()
38
+ }, wait)
39
+ }
40
+ const hoverDelay = () => (Date.now() - lastClosedAt < 400 ? 0 : (delay() ?? motionMs('delay-tooltip')))
41
+
42
+ return {
43
+ clear,
44
+ hide,
45
+ enter: (e: PointerLike) => e.pointerType === 'mouse' && show(hoverDelay()),
46
+ leave: (e: PointerLike) => e.pointerType === 'mouse' && hide(),
47
+ // Touch has no hover: a tap shows the tip at once, and it fades a moment after the finger lifts.
48
+ // Opened now, not on a timer: the finger lifts in the same moment, which would cancel it.
49
+ down(e: PointerLike) {
50
+ if (e.pointerType !== 'touch') return
51
+ clear()
52
+ setOpen(true)
53
+ },
54
+ up: (e: PointerLike) => e.pointerType === 'touch' && hide(TOUCH_SHOW_MS),
55
+ focus: (e: { target: EventTarget | null }) => (e.target as Element).matches(':focus-visible') && show(0),
56
+ blur: () => hide(0),
57
+ }
58
+ }