gooee 0.50.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 (171) 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 -143
  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 +11 -39
  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/focus.ts +4 -47
  124. package/src/internal/icons.module.css +39 -27
  125. package/src/internal/layers.ts +7 -61
  126. package/src/internal/motion/alert.ts +17 -0
  127. package/src/internal/motion/buttonGroup.ts +21 -0
  128. package/src/internal/motion/checkbox.ts +39 -0
  129. package/src/internal/motion/collapseOut.ts +26 -0
  130. package/src/internal/motion/copyButton.ts +48 -0
  131. package/src/internal/motion/dial.ts +137 -0
  132. package/src/internal/motion/fab.ts +14 -0
  133. package/src/internal/motion/floating.ts +58 -0
  134. package/src/internal/motion/focus.ts +53 -0
  135. package/src/internal/motion/formField.ts +19 -0
  136. package/src/internal/motion/holdToConfirm.ts +147 -0
  137. package/src/internal/motion/keys.ts +42 -0
  138. package/src/internal/motion/layers.ts +78 -0
  139. package/src/internal/motion/listbox.ts +69 -0
  140. package/src/internal/motion/livePill.ts +71 -0
  141. package/src/internal/motion/menuKeys.ts +58 -0
  142. package/src/internal/motion/morphButton.ts +71 -0
  143. package/src/internal/motion/numberInput.ts +57 -0
  144. package/src/internal/motion/pickerWheel.ts +166 -0
  145. package/src/internal/motion/retime.ts +45 -0
  146. package/src/internal/motion/ruler.ts +57 -0
  147. package/src/internal/motion/scrollArea.ts +133 -0
  148. package/src/internal/motion/scrubLook.ts +15 -0
  149. package/src/internal/motion/segmented.ts +63 -0
  150. package/src/internal/motion/sheetDetents.ts +271 -0
  151. package/src/internal/motion/slider.ts +108 -0
  152. package/src/internal/motion/splitFlap.ts +205 -0
  153. package/src/internal/motion/swipeDeck.ts +236 -0
  154. package/src/internal/motion/switchThumb.ts +83 -0
  155. package/src/internal/motion/table.ts +77 -0
  156. package/src/internal/motion/tabs.ts +105 -0
  157. package/src/internal/motion/textarea.ts +22 -0
  158. package/src/internal/motion/themeIcon.ts +91 -0
  159. package/src/internal/motion/themeReveal.ts +71 -0
  160. package/src/internal/motion/toast.ts +291 -0
  161. package/src/internal/motion/tooltip.ts +58 -0
  162. package/src/internal/themeReveal.ts +3 -61
  163. package/src/internal/useFloating.ts +9 -49
  164. package/src/internal/useScrub.ts +1 -13
  165. package/src/internal/vue/trigger.ts +18 -0
  166. package/src/internal/vue/useFloating.ts +38 -0
  167. package/src/internal/vue/usePresence.ts +21 -0
  168. package/src/internal/vue/useScrub.ts +40 -0
  169. package/src/internal/vueIcons.ts +134 -0
  170. package/src/tokens.css +5 -0
  171. package/src/internal/relativeTime.ts +0 -26
@@ -2,9 +2,11 @@
2
2
 
3
3
  import { useEffect, useRef, useState, type ComponentPropsWithRef } from 'react'
4
4
  import { cx } from '../../internal/cx'
5
- import { easing, readMotionMs } from '../../internal/motion'
5
+ import { createSplitFlap, flapCellsHtml, SPLIT_FLAP_CHARS, type FlapLook } from '../../internal/motion/splitFlap'
6
6
  import styles from './SplitFlap.module.css'
7
7
 
8
+ export { SPLIT_FLAP_CHARS }
9
+
8
10
  export type SplitFlapProps = Omit<ComponentPropsWithRef<'span'>, 'children'> & {
9
11
  /** The text the board shows. A change flips each cell forward through the drum to its new letter. */
10
12
  value: string
@@ -20,199 +22,33 @@ export type SplitFlapProps = Omit<ComponentPropsWithRef<'span'>, 'children'> & {
20
22
  onSettle?: (value: string) => void
21
23
  }
22
24
 
23
- /** The drum of a real board: a blank, letters, digits and a few marks. */
24
- export const SPLIT_FLAP_CHARS = ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789:.,-+%/'
25
-
26
- // A fold of about 90ms, fast steps of 40ms, and the last three slow down. A cell passes 8 letters at
27
- // most, and the cells start one after another within 350ms, so a whole change ends inside a second.
28
- const FOLD = 90
29
- const FAST = 40
30
- const TAIL = [70, 100, 140]
31
- const MAX_STEPS = 8
32
- const STAGGER = 35
33
- const CASCADE_CAP = 350
34
- // The fast motion token at its base, so Retime scales the board with everything else.
35
- const FAST_TOKEN = 140
36
-
37
- type Cell = { root: HTMLSpanElement; top: HTMLSpanElement; bottom: HTMLSpanElement; shown: string }
38
-
39
- /** The letters a cell passes on its way from `from` to `to`, at most MAX_STEPS, ending on `to`. */
40
- function route(from: string, to: string, drum: string) {
41
- const a = drum.indexOf(from)
42
- const b = drum.indexOf(to)
43
- if (a < 0 || b < 0) return from === to ? [] : [to]
44
- const n = Math.min((b - a + drum.length) % drum.length, MAX_STEPS)
45
- return Array.from({ length: n }, (_, i) => drum[(b - n + 1 + i + drum.length) % drum.length])
46
- }
47
-
48
- /** The wait before each step: fast in the middle, slow for the last three. */
49
- const gaps = (n: number) => Array.from({ length: n }, (_, i) => (n - 1 - i < TAIL.length ? TAIL[TAIL.length - n + i] : FAST))
50
-
51
- const NBSP = ' '
52
- const shownChar = (ch: string) => (ch === ' ' ? NBSP : ch)
53
- const escape = (ch: string) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;' })[ch] ?? ch
54
-
55
- const halfHtml = (cls: string, ch: string) => `<span class="${cls}"><span class="${styles.glyph}">${escape(shownChar(ch))}</span></span>`
56
- const cellHtml = (ch: string) => `<span class="${styles.cell}">${halfHtml(styles.top, ch)}${halfHtml(styles.bottom, ch)}</span>`
57
-
58
- const fromHtml = (html: string) => {
59
- const t = document.createElement('template')
60
- t.innerHTML = html
61
- return t.content.firstElementChild as HTMLSpanElement
62
- }
63
-
64
- const makeCell = (ch: string): Cell => {
65
- const root = fromHtml(cellHtml(ch))
66
- return { root, top: root.children[0] as HTMLSpanElement, bottom: root.children[1] as HTMLSpanElement, shown: ch }
67
- }
68
-
69
- const setHalf = (h: Element, ch: string) => {
70
- ;(h.firstElementChild as HTMLElement).textContent = shownChar(ch)
71
- }
25
+ const LOOK: FlapLook = { cell: styles.cell, top: styles.top, bottom: styles.bottom, glyph: styles.glyph, flap: styles.flap, front: styles.front, back: styles.back }
72
26
 
73
27
  export function SplitFlap({ value, length = 0, size = 'md', chars = SPLIT_FLAP_CHARS, announce = true, onSettle, className, ...rest }: SplitFlapProps) {
74
28
  const target = value.padEnd(length, ' ')
75
29
  // The first cells come with the markup, so a server page shows the board before any script runs.
76
- const [first] = useState(() => ({ __html: [...target].map(cellHtml).join('') }))
30
+ const [first] = useState(() => ({ __html: flapCellsHtml(LOOK, target) }))
77
31
  const [said, setSaid] = useState(value)
78
32
  const board = useRef<HTMLSpanElement>(null)
79
- const cells = useRef<Cell[] | null>(null)
80
- const timers = useRef<number[]>([])
81
- const anims = useRef(new Set<Animation>())
82
- const run = useRef(0)
83
- const shrinkEnd = useRef<(() => void) | null>(null)
33
+ const flap = useRef<ReturnType<typeof createSplitFlap> | null>(null)
84
34
  const settle = useRef(onSettle)
85
35
  settle.current = onSettle
86
36
 
87
- const stopAll = () => {
88
- timers.current.forEach(clearTimeout)
89
- timers.current = []
90
- anims.current.forEach((a) => a.finish())
91
- anims.current.clear()
92
- shrinkEnd.current?.()
93
- // Caught mid-run, each cell carries on from the letter it was showing.
94
- for (const c of cells.current ?? []) setHalf(c.bottom, c.shown)
95
- }
96
-
37
+ // The board runs in motion/splitFlap.ts. The first cells came with the markup.
97
38
  useEffect(() => {
98
39
  const b = board.current
99
40
  if (!b) return
100
- if (!cells.current) {
101
- cells.current = [...b.children].map((root, i) => ({ root: root as HTMLSpanElement, top: root.children[0] as HTMLSpanElement, bottom: root.children[1] as HTMLSpanElement, shown: [...target][i] ?? ' ' }))
41
+ if (!flap.current) {
42
+ flap.current = createSplitFlap(b, LOOK, target)
102
43
  return
103
44
  }
104
- const id = ++run.current
105
- const live = () => id === run.current
106
- const running = timers.current.length > 0 || anims.current.size > 0
107
- stopAll()
108
- const done = () => {
109
- if (!live()) return
110
- timers.current = []
45
+ flap.current.show(target, chars, () => {
111
46
  setSaid(value)
112
47
  settle.current?.(value)
113
- }
114
- const list = cells.current
115
- if (list.map((c) => c.shown).join('') === target) {
116
- if (running) done()
117
- return
118
- }
119
-
120
- const scale = readMotionMs('fast') / FAST_TOKEN
121
- // Reduced motion: the new text at once, with no flips and no morph.
122
- if (!scale) {
123
- cells.current = [...target].map(makeCell)
124
- b.replaceChildren(...cells.current.map((c) => c.root))
125
- done()
126
- return
127
- }
128
-
129
- const n0 = list.length
130
- const n1 = target.length
131
- const radius = getComputedStyle(b).borderRadius || '0px'
132
- const grow = { duration: readMotionMs('morph'), easing: easing('morph') }
133
- const shrink = { duration: readMotionMs('morph-out'), easing: easing('morph') }
134
- let left = 0
135
- const landed = () => {
136
- if (--left === 0) done()
137
- }
138
-
139
- // Longer: the new cells are laid out at once, and the board's edge grows over them.
140
- if (n1 > n0) {
141
- const before = b.offsetWidth
142
- for (let i = n0; i < n1; i++) {
143
- const c = makeCell(' ')
144
- list.push(c)
145
- b.append(c.root)
146
- }
147
- const wider = b.offsetWidth - before
148
- const a = b.animate({ clipPath: [`inset(0 ${wider}px 0 0 round ${radius})`, `inset(0 0 0 0 round ${radius})`] }, grow)
149
- anims.current.add(a)
150
- a.finished.then(() => anims.current.delete(a), () => {})
151
- }
152
-
153
- // Shorter: the board's edge folds over the extra cells while the rest flip, then they go.
154
- if (n1 < n0) {
155
- const keep = list.slice(0, n1)
156
- const box = b.getBoundingClientRect()
157
- const edge = keep.length ? keep[keep.length - 1].root.getBoundingClientRect().right : box.left
158
- const a = b.animate({ clipPath: [`inset(0 0 0 0 round ${radius})`, `inset(0 ${box.right - edge}px 0 0 round ${radius})`] }, { ...shrink, fill: 'forwards' })
159
- left++
160
- const end = () => {
161
- shrinkEnd.current = null
162
- list.slice(n1).forEach((c) => c.root.remove())
163
- cells.current = keep
164
- a.cancel()
165
- if (live()) landed()
166
- }
167
- shrinkEnd.current = end
168
- a.finished.then(() => shrinkEnd.current === end && end(), () => {})
169
- }
170
-
171
- const goal = [...target]
172
- const changed = goal.map((ch, i) => (list[i].shown !== ch ? i : -1)).filter((i) => i >= 0)
173
- const stagger = Math.min(STAGGER, CASCADE_CAP / Math.max(1, changed.length))
174
- left += changed.length
175
- if (!left) return done()
176
-
177
- const flip = (cell: Cell, next: string, last: boolean, fold: number) => {
178
- const prev = cell.shown
179
- cell.shown = next
180
- setHalf(cell.top, next)
181
- // The falling flap: the old letter's top half in front, the new letter's bottom half behind.
182
- const flap = fromHtml(`<span class="${styles.flap}">${halfHtml(styles.front, prev)}${halfHtml(styles.back, next)}</span>`)
183
- cell.root.append(flap)
184
- const a = flap.animate([{ transform: 'rotateX(0deg)' }, { transform: 'rotateX(-180deg)' }], { duration: fold, easing: easing('morph') })
185
- anims.current.add(a)
186
- const end = () => {
187
- anims.current.delete(a)
188
- setHalf(cell.bottom, next)
189
- flap.remove()
190
- if (last && live()) landed()
191
- }
192
- a.finished.then(end, end)
193
- }
194
-
195
- changed.forEach((i, order) => {
196
- const cell = list[i]
197
- const path = route(cell.shown, goal[i], chars)
198
- const wait = gaps(path.length)
199
- let t = order * stagger
200
- path.forEach((next, k) => {
201
- timers.current.push(window.setTimeout(() => flip(cell, next, k === path.length - 1, Math.max(FOLD, wait[k]) * scale), t * scale))
202
- t += wait[k]
203
- })
204
48
  })
205
49
  }, [target])
206
50
 
207
- useEffect(
208
- () => () => {
209
- // Gone: the run is over, so a cancelled flip ends without calling onSettle.
210
- run.current++
211
- timers.current.forEach(clearTimeout)
212
- anims.current.forEach((a) => a.cancel())
213
- },
214
- [],
215
- )
51
+ useEffect(() => () => flap.current?.destroy(), [])
216
52
 
217
53
  return (
218
54
  <span className={cx(styles.root, className)} data-size={size} {...rest}>
@@ -0,0 +1,52 @@
1
+ <script setup lang="ts">
2
+ import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue'
3
+ import { createSplitFlap, flapCellsHtml, SPLIT_FLAP_CHARS, type FlapLook } from '../../internal/motion/splitFlap'
4
+ import styles from './SplitFlap.module.css'
5
+
6
+ // The Vue SplitFlap: the same look (SplitFlap.module.css) and flips (motion/splitFlap.ts) as the React one.
7
+ // Other attributes go to the root.
8
+ const props = withDefaults(
9
+ defineProps<{
10
+ /** The text the board shows. A change flips each cell forward through the drum to its new letter. */
11
+ value: string
12
+ /** At least this many cells: shorter text is padded with blank ones, so a column keeps its width. */
13
+ length?: number
14
+ /** Text size of the cells. */
15
+ size?: 'sm' | 'md' | 'lg'
16
+ /** The drum's letters, in the order a cell passes them. A letter that is not on it flips straight in. */
17
+ chars?: string
18
+ /** Says the new text to screen readers once, when the board has landed. Turn off when one message covers several boards. */
19
+ announce?: boolean
20
+ }>(),
21
+ { length: 0, size: 'md', chars: SPLIT_FLAP_CHARS, announce: true },
22
+ )
23
+ /** settle: fires once with the text when every cell has landed. */
24
+ const emit = defineEmits<{ settle: [value: string] }>()
25
+
26
+ const LOOK: FlapLook = { cell: styles.cell, top: styles.top, bottom: styles.bottom, glyph: styles.glyph, flap: styles.flap, front: styles.front, back: styles.back }
27
+ const target = computed(() => props.value.padEnd(props.length, ' '))
28
+ // The first cells come with the markup, so a server page shows the board before any script runs.
29
+ const first = flapCellsHtml(LOOK, target.value)
30
+ const said = ref(props.value)
31
+ const board = ref<HTMLSpanElement | null>(null)
32
+ let flap: ReturnType<typeof createSplitFlap> | null = null
33
+
34
+ onMounted(() => {
35
+ if (board.value) flap = createSplitFlap(board.value, LOOK, target.value)
36
+ })
37
+ watch(target, (t) => {
38
+ const value = props.value
39
+ flap?.show(t, props.chars, () => {
40
+ said.value = value
41
+ emit('settle', value)
42
+ })
43
+ })
44
+ onBeforeUnmount(() => flap?.destroy())
45
+ </script>
46
+
47
+ <template>
48
+ <span :class="styles.root" :data-size="size">
49
+ <span :class="styles.sr" :aria-live="announce ? 'polite' : undefined" aria-atomic="true">{{ said }}</span>
50
+ <span ref="board" :class="styles.board" aria-hidden="true" v-html="first" />
51
+ </span>
52
+ </template>
@@ -0,0 +1,22 @@
1
+ <script setup lang="ts">
2
+ import { space, type Space } from '../../internal/space'
3
+ import styles from './Stack.module.css'
4
+
5
+ // The Vue Stack: the same look (Stack.module.css) as the React one. Other attributes go to the element.
6
+ withDefaults(
7
+ defineProps<{
8
+ /** Space between items, as a step of the space scale (0 to 12, read as --gooee-space-N). */
9
+ gap?: Space
10
+ /** Lines items up across the stack. "stretch" makes each item full width. */
11
+ align?: 'stretch' | 'start' | 'center' | 'end'
12
+ /** Element to render, so the stack can be a list, a section or a form. */
13
+ as?: 'div' | 'section' | 'article' | 'aside' | 'nav' | 'header' | 'footer' | 'main' | 'form' | 'fieldset' | 'ul' | 'ol'
14
+ }>(),
15
+ { gap: 5, align: 'stretch', as: 'div' },
16
+ )
17
+ defineSlots<{ default?: () => unknown }>()
18
+ </script>
19
+
20
+ <template>
21
+ <component :is="as" :class="[styles.root, styles[align]]" :style="{ '--stack-gap': space(gap) }"><slot /></component>
22
+ </template>
@@ -1,13 +1,10 @@
1
1
  'use client'
2
2
 
3
- import { useEffect, useLayoutEffect, useRef, useState, type ComponentPropsWithRef, type KeyboardEvent, type PointerEvent, type ReactNode } from 'react'
3
+ import { useEffect, useLayoutEffect, useRef, useState, type ComponentPropsWithRef, type KeyboardEvent, type ReactNode } from 'react'
4
4
  import { flushSync } from 'react-dom'
5
5
  import { cx } from '../../internal/cx'
6
- import { clamp, pull, speedTracker } from '../../internal/feel'
7
- import { release } from '../../internal/fling'
8
6
  import { UndoIcon } from '../../internal/icons'
9
- import { easing, readMotionMs } from '../../internal/motion'
10
- import { tokenNumber } from '../../internal/useScrub'
7
+ import { createSwipeDeck, type DeckSide } from '../../internal/motion/swipeDeck'
11
8
  import styles from './SwipeDeck.module.css'
12
9
 
13
10
  export type SwipeDeckSide = 'start' | 'end'
@@ -37,19 +34,9 @@ export type SwipeDeckProps<T> = Omit<ComponentPropsWithRef<'div'>, 'children' |
37
34
  empty?: ReactNode
38
35
  }
39
36
 
40
- type Side = -1 | 1
37
+ type Side = DeckSide
41
38
  type Done<T> = { item: T; side: Side; dir: SwipeDeckSide }
42
39
 
43
- // A press is a drag only once it moves this far sideways, so a tap on a card's own button stays a tap.
44
- const SLOP = 8
45
- // The throw rule: past --gooee-deck-throw of the card's width, or a flick this fast that has moved at least FLICK_MIN.
46
- const FLICK = 600
47
- const FLICK_MIN = 40
48
- const TILT = 0.06
49
- const TILT_MAX = 14
50
- // The speed a card leaves at, or comes back at on undo, when no hand threw it.
51
- const THROW_SPEED = 1800
52
-
53
40
  export function SwipeDeck<T>({
54
41
  items,
55
42
  getKey,
@@ -73,56 +60,33 @@ export function SwipeDeck<T>({
73
60
  const top = useRef<HTMLDivElement>(null)
74
61
  const knob = useRef<HTMLSpanElement>(null)
75
62
  const buttons = useRef<Record<Side, HTMLButtonElement | null>>({ [-1]: null, [1]: null })
76
- const at = useRef(0)
77
- const stop = useRef(() => {})
78
- const busy = useRef(false)
79
- const queued = useRef<Side | null>(null)
80
- const moved = useRef(false)
81
- const undoing = useRef<Side | null>(null)
82
- const drag = useRef<{ x: number; y: number; base: number; id: number; sideways: boolean; speed: ReturnType<typeof speedTracker> } | null>(null)
83
63
  // The top card is the first one not yet decided, found by key, so the app may trim or replace items.
84
64
  const gone = new Set(done.map((d) => getKey(d.item)))
85
65
  const left = items.filter((item) => !gone.has(getKey(item)))
86
66
  const latest = useRef({ left, count: done.length, onDecide, getName, decided })
87
67
  latest.current = { left, count: done.length, onDecide, getName, decided }
88
68
 
89
- const rtl = () => !!root.current && getComputedStyle(root.current).direction === 'rtl'
90
- // Right is the end side, or the start side in a right-to-left page.
91
- const named = (side: Side): SwipeDeckSide => ((side > 0) !== rtl() ? 'end' : 'start')
92
- const physical = (dir: SwipeDeckSide): Side => ((dir === 'end') !== rtl() ? 1 : -1)
93
- const widthOf = () => top.current?.offsetWidth || deck.current?.offsetWidth || 300
94
- const share = useRef(0.35)
95
- const line = () => widthOf() * share.current
96
- const reduced = () => readMotionMs('morph-out') === 0
97
-
98
- const paint = (x: number) => {
99
- at.current = x
100
- const el = top.current
101
- if (el) {
102
- el.style.transform = x ? `translate3d(${x.toFixed(1)}px, 0, 0) rotate(${clamp(x * TILT, -TILT_MAX, TILT_MAX).toFixed(2)}deg)` : ''
103
- // Past a card's width away it fades as it goes, so it leaves before any edge cuts it.
104
- const w = widthOf()
105
- const fade = clamp(1 - (Math.abs(x) - w) / (w * 0.8), 0, 1)
106
- el.style.opacity = fade < 1 ? fade.toFixed(3) : ''
107
- }
108
- const T = line()
109
- const near = pull(T - Math.min(Math.abs(x), T), T)
110
- deck.current?.style.setProperty('--rise', near.toFixed(3))
111
- const side: Side | 0 = x > 0 ? 1 : x < 0 ? -1 : 0
112
- for (const dir of ['start', 'end'] as SwipeDeckSide[]) {
113
- const btn = buttons.current[dir === 'start' ? -1 : 1]
114
- if (btn) btn.style.setProperty('--fill', side === physical(dir) ? near.toFixed(3) : '0')
115
- }
116
- setArmed(Math.abs(x) >= T - 0.5 ? side : 0)
117
- if (knob.current) knob.current.style.transform = `translateX(${clamp(x, -widthOf() / 2, widthOf() / 2).toFixed(1)}px)`
118
- }
119
-
120
- const offStage = (side: Side) => side * ((root.current?.offsetWidth ?? 400) / 2 + widthOf() * 1.2)
69
+ // The drag, the throw and undo's way back (motion/swipeDeck.ts).
70
+ const core = useRef<ReturnType<typeof createSwipeDeck>>(null)
71
+ core.current ??= createSwipeDeck({
72
+ root: () => root.current,
73
+ deck: () => deck.current,
74
+ top: () => top.current,
75
+ knob: () => knob.current,
76
+ button: (side) => buttons.current[side],
77
+ setArmed,
78
+ hasCards: () => latest.current.left.length > 0,
79
+ // The next card must be in place before the top card's transform resets, or the thrown card
80
+ // shows in the middle for a frame.
81
+ commit: (side) => flushSync(() => commit(side)),
82
+ })
83
+ const swipe = core.current
84
+ const { named, physical, fly } = swipe
121
85
 
122
86
  const commit = (side: Side) => {
123
87
  const { left, count, onDecide, getName, decided } = latest.current
124
88
  const [item, next] = left
125
- if (!item) return void (busy.current = false)
89
+ if (!item) return
126
90
  const dir = named(side)
127
91
  setDone((d) => [...d, { item, side, dir }])
128
92
  latest.current = { ...latest.current, left: left.slice(1), count: count + 1 }
@@ -131,143 +95,20 @@ export function SwipeDeck<T>({
131
95
  onDecide?.(item, dir)
132
96
  }
133
97
 
134
- const finish = (side: Side) => {
135
- stop.current = () => {}
136
- // The next card must be in place before the top card's transform resets, or the thrown card
137
- // shows in the middle for a frame.
138
- flushSync(() => commit(side))
139
- paint(0)
140
- }
141
-
142
- const fly = (side: Side, velocity: number) => {
143
- const el = top.current
144
- if (!el || !latest.current.left.length) return
145
- if (busy.current) {
146
- queued.current = side
147
- return
148
- }
149
- busy.current = true
150
- stop.current()
151
- if (reduced()) {
152
- const anim = el.animate({ opacity: [1, 0] }, { duration: readMotionMs('base'), easing: easing('out'), fill: 'forwards' })
153
- let ended = false
154
- const end = () => {
155
- if (ended) return
156
- ended = true
157
- anim.cancel()
158
- finish(side)
159
- }
160
- anim.finished.then(end, end)
161
- // Stopped (the deck went away mid-fade): the fade ends and nothing is decided.
162
- stop.current = () => {
163
- ended = true
164
- anim.cancel()
165
- }
166
- return
167
- }
168
- const speed = side * Math.max(Math.abs(velocity), THROW_SPEED)
169
- stop.current = release(at.current, offStage(side), speed, paint, () => finish(side))
170
- }
171
-
172
- const back = (velocity = 0) => {
173
- stop.current()
174
- stop.current = release(at.current, 0, velocity, paint)
175
- }
176
-
177
98
  const undo = () => {
178
99
  if (!done.length) return
179
- if (busy.current) return
180
100
  const last = done[done.length - 1]
181
- busy.current = true
182
- queued.current = null
183
- undoing.current = last.side
101
+ if (!swipe.undo(last.side)) return
184
102
  setDone((d) => d.slice(0, -1))
185
103
  const count = done.length - 1
186
104
  setNews(`Brought back: ${getName(last.item)}. Card ${count + 1} of ${count + left.length + 1}.`)
187
105
  onUndo?.(last.item)
188
106
  }
189
107
 
190
- // A key pressed mid-move plays once, when the move has ended.
191
- const playQueue = () => {
192
- busy.current = false
193
- const q = queued.current
194
- queued.current = null
195
- if (q !== null) fly(q, 0)
196
- }
197
-
198
- // Undo plays the throw backwards: the card comes in from where it left.
199
- useLayoutEffect(() => {
200
- const side = undoing.current
201
- const el = top.current
202
- if (side === null) return playQueue()
203
- undoing.current = null
204
- if (!el) return playQueue()
205
- if (reduced()) {
206
- const anim = el.animate({ opacity: [0, 1] }, { duration: readMotionMs('base'), easing: easing('out') })
207
- let ended = false
208
- anim.finished.then(
209
- () => !ended && playQueue(),
210
- () => !ended && playQueue(),
211
- )
212
- stop.current = () => {
213
- ended = true
214
- anim.cancel()
215
- }
216
- return
217
- }
218
- paint(offStage(side))
219
- stop.current = release(offStage(side), 0, -side * THROW_SPEED, paint, playQueue)
220
- }, [done.length])
221
-
222
- useLayoutEffect(() => {
223
- share.current = tokenNumber(root.current, '--gooee-deck-throw') || share.current
224
- }, [])
225
- useEffect(() => () => stop.current(), [])
226
-
227
- const onPointerDown = (e: PointerEvent<HTMLDivElement>) => {
228
- if ((e.pointerType === 'mouse' && e.button !== 0) || busy.current) return
229
- moved.current = false
230
- drag.current = { x: e.clientX, y: e.clientY, base: at.current, id: e.pointerId, sideways: false, speed: speedTracker() }
231
- }
232
-
233
- const onPointerMove = (e: PointerEvent<HTMLDivElement>) => {
234
- const d = drag.current
235
- if (!d || d.id !== e.pointerId) return
236
- const dx = e.clientX - d.x
237
- const dy = e.clientY - d.y
238
- if (!d.sideways) {
239
- // Up or down first means the page scrolls, and the card lets go.
240
- if (Math.abs(dy) > SLOP && Math.abs(dy) > Math.abs(dx)) return void (drag.current = null)
241
- if (Math.abs(dx) < SLOP) return
242
- d.sideways = true
243
- moved.current = true
244
- stop.current()
245
- d.base = at.current - dx
246
- try {
247
- e.currentTarget.setPointerCapture(e.pointerId)
248
- } catch {
249
- // A pointer the browser no longer knows cannot be captured, and needs no capture.
250
- }
251
- deck.current?.setAttribute('data-dragging', '')
252
- }
253
- const raw = d.base + dx
254
- d.speed.add(raw, e.timeStamp)
255
- paint(raw)
256
- }
257
-
258
- const letGo = (e: PointerEvent<HTMLDivElement>, cancelled: boolean) => {
259
- const d = drag.current
260
- if (!d || d.id !== e.pointerId) return
261
- drag.current = null
262
- if (!d.sideways) return
263
- deck.current?.removeAttribute('data-dragging')
264
- if (cancelled) return back()
265
- const x = at.current
266
- const v = d.speed.speed(e.timeStamp)
267
- if (Math.abs(x) >= line()) return fly(x > 0 ? 1 : -1, v)
268
- if (Math.abs(v) > FLICK && Math.abs(x) >= FLICK_MIN && Math.sign(v) === Math.sign(x)) return fly(v > 0 ? 1 : -1, v)
269
- back(v)
270
- }
108
+ // Undo plays the throw backwards, and a key pressed mid-move plays once the move has ended.
109
+ useLayoutEffect(() => swipe.changed(), [done.length])
110
+ useLayoutEffect(() => swipe.mount(), [])
111
+ useEffect(() => () => swipe.destroy(), [])
271
112
 
272
113
  const onKeyDown = (e: KeyboardEvent<HTMLDivElement>) => {
273
114
  if (e.target !== e.currentTarget || e.altKey || e.ctrlKey || e.metaKey) return
@@ -311,16 +152,11 @@ export function SwipeDeck<T>({
311
152
  data-depth={depth}
312
153
  inert={depth > 0}
313
154
  aria-hidden={depth > 0 || undefined}
314
- onPointerDown={depth === 0 ? onPointerDown : undefined}
315
- onPointerMove={depth === 0 ? onPointerMove : undefined}
316
- onPointerUp={depth === 0 ? (e) => letGo(e, false) : undefined}
317
- onPointerCancel={depth === 0 ? (e) => letGo(e, true) : undefined}
318
- onClickCapture={(e) => {
319
- if (!moved.current) return
320
- moved.current = false
321
- e.preventDefault()
322
- e.stopPropagation()
323
- }}
155
+ onPointerDown={depth === 0 ? swipe.down : undefined}
156
+ onPointerMove={depth === 0 ? swipe.move : undefined}
157
+ onPointerUp={depth === 0 ? swipe.up : undefined}
158
+ onPointerCancel={depth === 0 ? swipe.cancel : undefined}
159
+ onClickCapture={swipe.clickCapture}
324
160
  onDragStart={(e) => e.preventDefault()}
325
161
  >
326
162
  {renderCard(item)}