gooee 0.48.0 → 0.51.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (185) hide show
  1. package/package.json +4 -2
  2. package/scripts/add.js +28 -6
  3. package/src/components/Accordion/Accordion.vue +62 -0
  4. package/src/components/Accordion/AccordionItem.vue +45 -0
  5. package/src/components/Accordion/accordionContext.ts +6 -0
  6. package/src/components/Alert/Alert.tsx +7 -27
  7. package/src/components/Alert/Alert.vue +59 -0
  8. package/src/components/AlertDialog/AlertDialog.vue +126 -0
  9. package/src/components/AnimatedNumber/AnimatedNumber.vue +36 -0
  10. package/src/components/AspectRatio/AspectRatio.vue +17 -0
  11. package/src/components/Avatar/Avatar.vue +56 -0
  12. package/src/components/AvatarGroup/AvatarGroup.vue +42 -0
  13. package/src/components/Badge/Badge.vue +32 -0
  14. package/src/components/Breadcrumb/Breadcrumb.vue +58 -0
  15. package/src/components/Button/Button.vue +130 -0
  16. package/src/components/ButtonGroup/ButtonGroup.tsx +4 -21
  17. package/src/components/ButtonGroup/ButtonGroup.vue +33 -0
  18. package/src/components/Card/Card.vue +34 -0
  19. package/src/components/Center/Center.vue +32 -0
  20. package/src/components/Checkbox/Checkbox.tsx +8 -25
  21. package/src/components/Checkbox/Checkbox.vue +108 -0
  22. package/src/components/CheckboxGroup/CheckboxGroup.vue +76 -0
  23. package/src/components/Cluster/Cluster.vue +34 -0
  24. package/src/components/CodeBlock/CodeBlock.vue +45 -0
  25. package/src/components/Collapsible/Collapsible.vue +59 -0
  26. package/src/components/Container/Container.vue +22 -0
  27. package/src/components/CopyButton/CopyButton.tsx +3 -49
  28. package/src/components/CopyButton/CopyButton.vue +116 -0
  29. package/src/components/Dial/Dial.tsx +12 -120
  30. package/src/components/Dial/Dial.vue +228 -0
  31. package/src/components/Dialog/Dialog.vue +139 -0
  32. package/src/components/Divider/Divider.vue +27 -0
  33. package/src/components/DropdownMenu/DropdownMenu.tsx +11 -46
  34. package/src/components/DropdownMenu/DropdownMenu.vue +185 -0
  35. package/src/components/EmptyState/EmptyState.vue +33 -0
  36. package/src/components/Fab/Fab.tsx +4 -14
  37. package/src/components/Fab/Fab.vue +115 -0
  38. package/src/components/Fieldset/Fieldset.vue +42 -0
  39. package/src/components/FormField/FormField.tsx +2 -17
  40. package/src/components/FormField/FormField.vue +64 -0
  41. package/src/components/Grid/Grid.vue +43 -0
  42. package/src/components/HoldToConfirm/HoldToConfirm.tsx +13 -142
  43. package/src/components/HoldToConfirm/HoldToConfirm.vue +281 -0
  44. package/src/components/IconButton/IconButton.vue +25 -0
  45. package/src/components/Input/Input.vue +112 -0
  46. package/src/components/InputGroup/InputGroup.vue +102 -0
  47. package/src/components/Kbd/Kbd.tsx +2 -12
  48. package/src/components/Kbd/Kbd.vue +21 -0
  49. package/src/components/Link/Link.vue +38 -0
  50. package/src/components/LivePill/LivePill.tsx +12 -40
  51. package/src/components/LivePill/LivePill.vue +152 -0
  52. package/src/components/Meter/Meter.vue +88 -0
  53. package/src/components/MorphButton/MorphButton.tsx +11 -67
  54. package/src/components/MorphButton/MorphButton.vue +120 -0
  55. package/src/components/NumberInput/NumberInput.tsx +11 -48
  56. package/src/components/NumberInput/NumberInput.vue +184 -0
  57. package/src/components/Pagination/Pagination.tsx +2 -15
  58. package/src/components/Pagination/Pagination.vue +87 -0
  59. package/src/components/Pagination/range.ts +15 -0
  60. package/src/components/PasswordInput/PasswordInput.vue +53 -0
  61. package/src/components/PickerWheel/DateWheel.vue +48 -0
  62. package/src/components/PickerWheel/PickerWheel.tsx +15 -129
  63. package/src/components/PickerWheel/PickerWheel.vue +77 -0
  64. package/src/components/PickerWheel/TimeWheel.vue +50 -0
  65. package/src/components/PickerWheel/WheelColumn.vue +70 -0
  66. package/src/components/Popover/Popover.vue +108 -0
  67. package/src/components/ProgressBar/ProgressBar.vue +48 -0
  68. package/src/components/Prose/Prose.vue +18 -0
  69. package/src/components/Radio/Radio.vue +49 -0
  70. package/src/components/Radio/RadioGroup.vue +69 -0
  71. package/src/components/Retime/Retime.tsx +7 -40
  72. package/src/components/Retime/Retime.vue +64 -0
  73. package/src/components/RulerScrubber/RulerScrubber.tsx +9 -38
  74. package/src/components/RulerScrubber/RulerScrubber.vue +166 -0
  75. package/src/components/ScrollArea/ScrollArea.tsx +17 -129
  76. package/src/components/ScrollArea/ScrollArea.vue +58 -0
  77. package/src/components/SearchInput/SearchInput.tsx +2 -15
  78. package/src/components/SearchInput/SearchInput.vue +70 -0
  79. package/src/components/SegmentedControl/SegmentedControl.tsx +7 -51
  80. package/src/components/SegmentedControl/SegmentedControl.vue +111 -0
  81. package/src/components/Select/Select.tsx +16 -47
  82. package/src/components/Select/Select.vue +236 -0
  83. package/src/components/Sheet/Sheet.tsx +52 -231
  84. package/src/components/Sheet/Sheet.vue +249 -0
  85. package/src/components/Skeleton/Skeleton.vue +38 -0
  86. package/src/components/Slider/Slider.tsx +25 -84
  87. package/src/components/Slider/Slider.vue +113 -0
  88. package/src/components/Spinner/Spinner.vue +30 -0
  89. package/src/components/Split/Split.vue +34 -0
  90. package/src/components/SplitFlap/SplitFlap.tsx +11 -175
  91. package/src/components/SplitFlap/SplitFlap.vue +52 -0
  92. package/src/components/Stack/Stack.vue +22 -0
  93. package/src/components/SwipeDeck/SwipeDeck.tsx +30 -194
  94. package/src/components/SwipeDeck/SwipeDeck.vue +168 -0
  95. package/src/components/Switch/Switch.tsx +6 -75
  96. package/src/components/Switch/Switch.vue +66 -0
  97. package/src/components/Table/Table.module.css +12 -3
  98. package/src/components/Table/Table.tsx +8 -59
  99. package/src/components/Table/Table.vue +274 -0
  100. package/src/components/TableOfContents/TableOfContents.vue +90 -0
  101. package/src/components/Tabs/Tab.vue +37 -0
  102. package/src/components/Tabs/TabList.vue +47 -0
  103. package/src/components/Tabs/TabPanel.vue +26 -0
  104. package/src/components/Tabs/Tabs.module.css +2 -2
  105. package/src/components/Tabs/Tabs.tsx +11 -84
  106. package/src/components/Tabs/Tabs.vue +33 -0
  107. package/src/components/Tabs/tabsContext.ts +15 -0
  108. package/src/components/Tag/Tag.tsx +6 -26
  109. package/src/components/Tag/Tag.vue +63 -0
  110. package/src/components/Textarea/Textarea.tsx +2 -22
  111. package/src/components/Textarea/Textarea.vue +120 -0
  112. package/src/components/ThemeToggle/ThemeToggle.tsx +10 -84
  113. package/src/components/ThemeToggle/ThemeToggle.vue +90 -0
  114. package/src/components/Toast/Toast.tsx +34 -264
  115. package/src/components/Toast/Toast.vue +138 -0
  116. package/src/components/Toast/ToastItem.vue +108 -0
  117. package/src/components/Toggle/Toggle.vue +52 -0
  118. package/src/components/Tooltip/Tooltip.tsx +17 -48
  119. package/src/components/Tooltip/Tooltip.vue +95 -0
  120. package/src/components/VisuallyHidden/VisuallyHidden.vue +17 -0
  121. package/src/internal/FieldMessage.vue +41 -0
  122. package/src/internal/Roll.vue +20 -0
  123. package/src/internal/fling.ts +29 -106
  124. package/src/internal/focus.ts +4 -79
  125. package/src/internal/icons.module.css +39 -27
  126. package/src/internal/layers.ts +7 -61
  127. package/src/internal/liquid.ts +26 -213
  128. package/src/internal/liquidMatter.ts +9 -181
  129. package/src/internal/liquidMore.ts +6 -669
  130. package/src/internal/motion/alert.ts +17 -0
  131. package/src/internal/motion/buttonGroup.ts +21 -0
  132. package/src/internal/motion/checkbox.ts +39 -0
  133. package/src/internal/motion/collapseOut.ts +26 -0
  134. package/src/internal/motion/copyButton.ts +48 -0
  135. package/src/internal/motion/dial.ts +137 -0
  136. package/src/internal/motion/fab.ts +14 -0
  137. package/src/internal/motion/fling.ts +147 -0
  138. package/src/internal/motion/floating.ts +58 -0
  139. package/src/internal/motion/focus.ts +88 -0
  140. package/src/internal/motion/formField.ts +19 -0
  141. package/src/internal/motion/holdToConfirm.ts +147 -0
  142. package/src/internal/motion/index.ts +20 -0
  143. package/src/internal/motion/keys.ts +42 -0
  144. package/src/internal/motion/layers.ts +78 -0
  145. package/src/internal/motion/liquid.ts +241 -0
  146. package/src/internal/motion/liquidMatter.ts +198 -0
  147. package/src/internal/motion/liquidMore.ts +676 -0
  148. package/src/internal/motion/listbox.ts +69 -0
  149. package/src/internal/motion/livePill.ts +71 -0
  150. package/src/internal/motion/menuKeys.ts +58 -0
  151. package/src/internal/motion/morphButton.ts +71 -0
  152. package/src/internal/motion/numberInput.ts +57 -0
  153. package/src/internal/motion/pickerWheel.ts +166 -0
  154. package/src/internal/motion/retime.ts +45 -0
  155. package/src/internal/motion/ruler.ts +57 -0
  156. package/src/internal/motion/scrollArea.ts +133 -0
  157. package/src/internal/motion/scrollFrame.ts +152 -0
  158. package/src/internal/motion/scrubLook.ts +15 -0
  159. package/src/internal/motion/scrubber.ts +154 -0
  160. package/src/internal/motion/segmented.ts +63 -0
  161. package/src/internal/motion/sheetDetents.ts +271 -0
  162. package/src/internal/motion/slider.ts +108 -0
  163. package/src/internal/motion/softPress.ts +334 -0
  164. package/src/internal/motion/splitFlap.ts +205 -0
  165. package/src/internal/motion/swipeDeck.ts +236 -0
  166. package/src/internal/motion/switchThumb.ts +83 -0
  167. package/src/internal/motion/table.ts +77 -0
  168. package/src/internal/motion/tabs.ts +105 -0
  169. package/src/internal/motion/textarea.ts +22 -0
  170. package/src/internal/motion/themeIcon.ts +91 -0
  171. package/src/internal/motion/themeReveal.ts +71 -0
  172. package/src/internal/motion/toast.ts +291 -0
  173. package/src/internal/motion/tooltip.ts +58 -0
  174. package/src/internal/scrollFrame.ts +4 -145
  175. package/src/internal/softPress.ts +27 -289
  176. package/src/internal/themeReveal.ts +3 -61
  177. package/src/internal/useFloating.ts +9 -49
  178. package/src/internal/useScrub.ts +15 -120
  179. package/src/internal/vue/trigger.ts +18 -0
  180. package/src/internal/vue/useFloating.ts +38 -0
  181. package/src/internal/vue/usePresence.ts +21 -0
  182. package/src/internal/vue/useScrub.ts +40 -0
  183. package/src/internal/vueIcons.ts +134 -0
  184. package/src/tokens.css +5 -0
  185. package/src/internal/relativeTime.ts +0 -26
@@ -1,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)}
@@ -0,0 +1,168 @@
1
+ <script setup lang="ts" generic="T">
2
+ import { computed, nextTick, onBeforeUnmount, onMounted, ref, shallowRef, watch } from 'vue'
3
+ import { createSwipeDeck, type DeckDir, type DeckSide } from '../../internal/motion/swipeDeck'
4
+ import { UndoIcon } from '../../internal/vueIcons'
5
+ import styles from './SwipeDeck.module.css'
6
+
7
+ export type SwipeDeckSide = DeckDir
8
+
9
+ // The Vue SwipeDeck: the same look (SwipeDeck.module.css), drag, throw and undo as the React one. Each
10
+ // card is drawn by the card slot, which gets the item. Other attributes go to the outer box.
11
+ const props = withDefaults(
12
+ defineProps<{
13
+ /** The cards, top card first. The top card is the first whose key is not decided yet, so you may remove decided ones. */
14
+ items: T[]
15
+ /** A key that stays the same for each item. */
16
+ getKey: (item: T) => string
17
+ /** A short name for an item, said by screen readers after each decision. */
18
+ getName: (item: T) => string
19
+ /** Screen reader name of the deck, such as "Flats to rent". */
20
+ label: string
21
+ /** The two button words, for the start side then the end side. */
22
+ actions?: [string, string]
23
+ /** What screen readers say was done, start then end, such as "Passed" and "Saved". */
24
+ decided?: [string, string]
25
+ /** Screen reader name of the undo button. */
26
+ undoLabel?: string
27
+ /** Shown when every card has been decided. The empty slot replaces it. */
28
+ empty?: string
29
+ }>(),
30
+ { actions: () => ['Pass', 'Save'], decided: () => ['Passed', 'Saved'], undoLabel: 'Undo the last decision', empty: 'That was the last card.' },
31
+ )
32
+ /** decide: once for each card, when it has been thrown or a button or key decided it. undo: Undo brought a card back; if you removed it from items, put it back here. */
33
+ const emit = defineEmits<{ decide: [item: T, side: SwipeDeckSide]; undo: [item: T] }>()
34
+ defineSlots<{ card(props: { item: T }): unknown; empty?: () => unknown }>()
35
+
36
+ type Done = { item: T; side: DeckSide; dir: SwipeDeckSide }
37
+
38
+ const done = shallowRef<Done[]>([])
39
+ const news = ref('')
40
+ const armed = ref<DeckSide | 0>(0)
41
+ const root = ref<HTMLElement | null>(null)
42
+ const deck = ref<HTMLElement | null>(null)
43
+ const knob = ref<HTMLElement | null>(null)
44
+ let top: HTMLElement | null = null
45
+ const buttons: Record<DeckSide, HTMLElement | null> = { [-1]: null, [1]: null }
46
+ // The top card is the first one not yet decided, found by key, so the app may trim or replace items.
47
+ const left = computed(() => {
48
+ const gone = new Set(done.value.map((d) => props.getKey(d.item)))
49
+ return props.items.filter((item) => !gone.has(props.getKey(item)))
50
+ })
51
+
52
+ const commit = (side: DeckSide) => {
53
+ const [item, next] = left.value
54
+ if (!item) return
55
+ const count = done.value.length
56
+ const n = left.value.length
57
+ const dir = swipe.named(side)
58
+ done.value = [...done.value, { item, side, dir }]
59
+ const said = `${props.decided[dir === 'start' ? 0 : 1]}: ${props.getName(item)}.`
60
+ news.value = next ? `${said} Card ${count + 2} of ${count + n}: ${props.getName(next)}.` : `${said} No more cards.`
61
+ emit('decide', item, dir)
62
+ }
63
+
64
+ // The drag, the throw and undo's way back (motion/swipeDeck.ts).
65
+ const swipe = createSwipeDeck({
66
+ root: () => root.value,
67
+ deck: () => deck.value,
68
+ top: () => top,
69
+ knob: () => knob.value,
70
+ button: (side) => buttons[side],
71
+ setArmed: (side) => (armed.value = side),
72
+ hasCards: () => left.value.length > 0,
73
+ // The next card must be in place before the top card's transform resets, or the thrown card
74
+ // shows in the middle for a frame.
75
+ commit: (side) => {
76
+ commit(side)
77
+ return nextTick()
78
+ },
79
+ })
80
+
81
+ const undo = () => {
82
+ if (!done.value.length) return
83
+ const last = done.value[done.value.length - 1]
84
+ if (!swipe.undo(last.side)) return
85
+ const count = done.value.length - 1
86
+ const n = left.value.length
87
+ done.value = done.value.slice(0, -1)
88
+ news.value = `Brought back: ${props.getName(last.item)}. Card ${count + 1} of ${count + n + 1}.`
89
+ emit('undo', last.item)
90
+ }
91
+
92
+ // Undo plays the throw backwards, and a key pressed mid-move plays once the move has ended.
93
+ watch(() => done.value.length, swipe.changed, { flush: 'post' })
94
+ onMounted(swipe.mount)
95
+ onBeforeUnmount(swipe.destroy)
96
+
97
+ const onKeyDown = (e: KeyboardEvent) => {
98
+ if (e.target !== e.currentTarget || e.altKey || e.ctrlKey || e.metaKey) return
99
+ if (e.key === 'ArrowLeft') swipe.fly(-1, 0)
100
+ else if (e.key === 'ArrowRight') swipe.fly(1, 0)
101
+ else if (e.key === 'Backspace') undo()
102
+ else return
103
+ e.preventDefault()
104
+ }
105
+
106
+ const shown = computed(() =>
107
+ left.value
108
+ .slice(0, 3)
109
+ .map((item, depth) => ({ item, depth }))
110
+ .reverse(),
111
+ )
112
+ const over = computed(() => !left.value.length)
113
+ const armedDir = computed(() => (armed.value ? swipe.named(armed.value) : undefined))
114
+ const setTop = (el: unknown) => (top = el as HTMLElement | null)
115
+ const buttonRef = (dir: SwipeDeckSide) => (el: unknown) => (buttons[dir === 'start' ? -1 : 1] = el as HTMLElement | null)
116
+ const choose = (dir: SwipeDeckSide) => !over.value && swipe.fly(swipe.physical(dir), 0)
117
+ </script>
118
+
119
+ <template>
120
+ <div ref="root" :class="styles.root" :data-armed="armedDir">
121
+ <div :class="styles.stage">
122
+ <div ref="deck" :class="styles.deck" role="group" aria-roledescription="card deck" :aria-label="label" aria-keyshortcuts="ArrowLeft ArrowRight Backspace" tabindex="0" @keydown="onKeyDown">
123
+ <div v-if="over" :class="styles.empty"><slot name="empty">{{ empty }}</slot></div>
124
+ <div
125
+ v-for="{ item, depth } in shown"
126
+ :key="getKey(item)"
127
+ :ref="depth === 0 ? setTop : undefined"
128
+ :class="styles.card"
129
+ :data-depth="depth"
130
+ :inert="depth > 0"
131
+ :aria-hidden="depth > 0 || undefined"
132
+ @pointerdown="depth === 0 && swipe.down($event)"
133
+ @pointermove="depth === 0 && swipe.move($event)"
134
+ @pointerup="depth === 0 && swipe.up($event)"
135
+ @pointercancel="depth === 0 && swipe.cancel($event)"
136
+ @click.capture="swipe.clickCapture"
137
+ @dragstart.prevent
138
+ >
139
+ <slot name="card" :item="item" />
140
+ </div>
141
+ </div>
142
+ </div>
143
+ <div :class="styles.ruler" aria-hidden="true">
144
+ <span :class="styles.mark" data-side="left" :data-armed="armed === -1 || undefined" />
145
+ <span :class="styles.centre" />
146
+ <span :class="styles.mark" data-side="right" :data-armed="armed === 1 || undefined" />
147
+ <span ref="knob" :class="styles.knob" :data-armed="armed !== 0 || undefined" />
148
+ </div>
149
+ <div :class="styles.bar">
150
+ <button :ref="buttonRef('start')" type="button" :class="styles.choice" data-side="start" :aria-disabled="over || undefined" @click="choose('start')">
151
+ <span :class="styles.word">{{ actions[0] }}</span>
152
+ <span :class="styles.fill" aria-hidden="true">
153
+ <span :class="styles.word">{{ actions[0] }}</span>
154
+ </span>
155
+ </button>
156
+ <button type="button" :class="styles.undo" :aria-label="undoLabel" :aria-disabled="!done.length || undefined" @click="undo">
157
+ <UndoIcon />
158
+ </button>
159
+ <button :ref="buttonRef('end')" type="button" :class="styles.choice" data-side="end" :aria-disabled="over || undefined" @click="choose('end')">
160
+ <span :class="styles.word">{{ actions[1] }}</span>
161
+ <span :class="styles.fill" aria-hidden="true">
162
+ <span :class="styles.word">{{ actions[1] }}</span>
163
+ </span>
164
+ </button>
165
+ </div>
166
+ <span :class="styles.sr" role="status" aria-live="polite" aria-atomic="true">{{ news }}</span>
167
+ </div>
168
+ </template>
@@ -2,15 +2,10 @@
2
2
 
3
3
  import { useId, useLayoutEffect, useRef, type ComponentPropsWithRef, type ReactNode } from 'react'
4
4
  import { cx } from '../../internal/cx'
5
- import { frames, morphEase, motionFor } from '../../internal/liquidMatter'
5
+ import { createSwitchThumb } from '../../internal/motion/switchThumb'
6
6
  import { useControllable } from '../../internal/useControllable'
7
7
  import styles from './Switch.module.css'
8
8
 
9
- // The front edge moves over this share of the time; the back edge starts the rest of the way in.
10
- const LEAD = 0.75
11
- // The longest the thumb gets, as a share of its round size.
12
- const STRETCH = 1.4
13
-
14
9
  export type SwitchProps = Omit<ComponentPropsWithRef<'button'>, 'onChange' | 'value'> & {
15
10
  /** Visible text next to the switch, also its accessible name. */
16
11
  label: ReactNode
@@ -47,76 +42,12 @@ export function Switch({
47
42
  const switchId = id ?? auto
48
43
  const [checked, setChecked] = useControllable(checkedProp, defaultChecked, onCheckedChange)
49
44
  const describedBy = [description && `${switchId}-desc`, describedByProp].filter(Boolean).join(' ')
50
- // The thumb stretches: its front edge leaves first and the back edge catches up, so it stays one
51
- // shape. The on fill's edge rides on the thumb's edge, so the colour never sits apart from it.
45
+ // The thumb's stretch (motion/switchThumb.ts).
52
46
  const thumbRef = useRef<HTMLSpanElement>(null)
53
- const was = useRef(checked)
54
- const run = useRef<{ a: number; b: number; stop: () => void } | null>(null)
55
- const end = () => {
56
- const thumb = thumbRef.current
57
- run.current?.stop()
58
- run.current = null
59
- if (!thumb) return
60
- thumb.style.removeProperty('transform')
61
- thumb.style.removeProperty('width')
62
- const track = thumb.parentElement
63
- if (track) {
64
- delete track.dataset.moving
65
- track.style.removeProperty('--fill')
66
- }
67
- }
68
- useLayoutEffect(() => {
69
- const thumb = thumbRef.current
70
- const track = thumb?.parentElement
71
- if (was.current === checked || !thumb || !track) return
72
- was.current = checked
73
- const ms = motionFor('slow')
74
- if (!ms) {
75
- end()
76
- return
77
- }
78
- const s = getComputedStyle(track)
79
- const line = parseFloat(s.borderLeftWidth) || 0
80
- const pad = parseFloat(s.getPropertyValue('--pad')) || 0
81
- const size = thumb.offsetHeight
82
- const rest = thumb.offsetLeft
83
- const offX = rest
84
- const onX = track.clientWidth - rest - size
85
- const to = checked ? onX : offX
86
- const from = run.current ? { a: run.current.a, b: run.current.b } : { a: checked ? offX : onX, b: (checked ? offX : onX) + size }
87
- run.current?.stop()
88
- const ahead = to + size / 2 >= (from.a + from.b) / 2
89
- const e = morphEase()
90
- const outer = track.offsetWidth
91
- const live = { ...from, stop: () => {} }
92
- run.current = live
93
- track.dataset.moving = ''
94
- live.stop = frames(
95
- ms,
96
- (u) => {
97
- const lead = e(Math.min(1, u / LEAD))
98
- const trail = e(Math.max(0, (u - 1 + LEAD) / LEAD))
99
- live.a = from.a + (to - from.a) * (ahead ? trail : lead)
100
- live.b = from.b + (to + size - from.b) * (ahead ? lead : trail)
101
- // A short pill at most, so the fill behind it stays in view.
102
- const long = live.b - live.a - size * STRETCH
103
- if (long > 0) {
104
- if (ahead) live.a += long
105
- else live.b -= long
106
- }
107
- thumb.style.transform = `translateX(${live.a - rest}px)`
108
- thumb.style.width = `${live.b - live.a}px`
109
- // How far on the thumb is, 0 to 1: the fill opens out to the track's edge as it goes.
110
- const p = Math.min(1, Math.max(0, ((live.a + live.b) / 2 - offX - size / 2) / (onX - offX)))
111
- const side = (pad + line) * (1 - p)
112
- const right = outer - live.b - line + line * (1 - p) - pad * p
113
- track.style.setProperty('--fill', `inset(${side}px ${Math.max(0, right)}px ${side}px ${side}px round var(--gooee-radius-full))`)
114
- },
115
- end,
116
- )
117
- }, [checked])
118
- // eslint-disable-next-line react-hooks/exhaustive-deps
119
- useLayoutEffect(() => () => end(), [])
47
+ const thumb = useRef<ReturnType<typeof createSwitchThumb>>(null)
48
+ thumb.current ??= createSwitchThumb(() => thumbRef.current, checked)
49
+ useLayoutEffect(() => thumb.current!.set(checked), [checked])
50
+ useLayoutEffect(() => () => thumb.current!.destroy(), [])
120
51
  return (
121
52
  <div className={cx(styles.root, labelPosition === 'start' && styles.start, className)} data-disabled={disabled || undefined}>
122
53
  <button
@@ -0,0 +1,66 @@
1
+ <script setup lang="ts">
2
+ import { computed, onBeforeUnmount, ref, useAttrs, useId, watch } from 'vue'
3
+ import { createSwitchThumb } from '../../internal/motion/switchThumb'
4
+ import styles from './Switch.module.css'
5
+
6
+ // The Vue Switch: the same look (Switch.module.css) and thumb motion as the React one.
7
+ // Other attributes, such as aria-label or a click listener, go to the switch button.
8
+ defineOptions({ inheritAttrs: false })
9
+
10
+ const props = withDefaults(
11
+ defineProps<{
12
+ /** Visible text next to the switch, also its accessible name. The label slot replaces it. */
13
+ label?: string
14
+ /** Extra text under the label, also read by screen readers. The description slot replaces it. */
15
+ description?: string
16
+ /** Puts the label before the switch, like a settings row. */
17
+ labelPosition?: 'start' | 'end'
18
+ /** Form field name. The form gets "on" while the switch is on, and nothing while it is off. */
19
+ name?: string
20
+ disabled?: boolean
21
+ id?: string
22
+ }>(),
23
+ { labelPosition: 'end' },
24
+ )
25
+
26
+ /** Whether the switch is on. Use v-model:checked, or leave it out and the switch keeps its own state. */
27
+ const checked = defineModel<boolean>('checked', { default: false })
28
+ const slots = defineSlots<{ label?: () => unknown; description?: () => unknown }>()
29
+
30
+ const auto = useId()
31
+ const switchId = computed(() => props.id ?? auto)
32
+ const hasDescription = computed(() => !!(props.description || slots.description))
33
+ const attrs = useAttrs()
34
+ const describedBy = computed(() => [hasDescription.value && `${switchId.value}-desc`, attrs['aria-describedby']].filter(Boolean).join(' ') || undefined)
35
+
36
+ // The thumb's stretch (motion/switchThumb.ts), played after the DOM has the new state.
37
+ const thumbEl = ref<HTMLElement | null>(null)
38
+ const thumb = createSwitchThumb(() => thumbEl.value, checked.value)
39
+ watch(checked, (on) => thumb.set(on), { flush: 'post' })
40
+ onBeforeUnmount(thumb.destroy)
41
+ </script>
42
+
43
+ <template>
44
+ <div :class="[styles.root, labelPosition === 'start' && styles.start]" :data-disabled="disabled || undefined">
45
+ <button
46
+ v-bind="$attrs"
47
+ :id="switchId"
48
+ type="button"
49
+ role="switch"
50
+ :aria-checked="checked"
51
+ :aria-describedby="describedBy"
52
+ :class="styles.track"
53
+ :data-on="checked || undefined"
54
+ :disabled="disabled"
55
+ @click="checked = !checked"
56
+ >
57
+ <span ref="thumbEl" :class="styles.thumb" aria-hidden="true" />
58
+ </button>
59
+ <span :class="styles.text">
60
+ <label :for="switchId" :class="styles.label"><slot name="label">{{ label }}</slot></label>
61
+ <span v-if="hasDescription" :id="`${switchId}-desc`" :class="styles.description"><slot name="description">{{ description }}</slot></span>
62
+ </span>
63
+ <!-- Submits like a checkbox: "on" when on, nothing when off -->
64
+ <input v-if="name && checked" type="hidden" :name="name" value="on" />
65
+ </div>
66
+ </template>
@@ -4,14 +4,23 @@
4
4
  position: relative;
5
5
  container-type: inline-size;
6
6
  min-width: 0;
7
- /* The line is drawn inside the edge and takes no space, so the outer size is the content's own 4px steps. */
8
- outline: var(--gooee-line) solid var(--gooee-color-border);
9
- outline-offset: calc(var(--gooee-line) * -1);
7
+ isolation: isolate;
10
8
  border-radius: var(--gooee-radius-panel);
11
9
  background: var(--gooee-color-surface);
12
10
  overflow: hidden;
13
11
  }
14
12
 
13
+ /* The frame is a layer over the rows, so sticky cells and the fade (::after) never cover it. Inside the edge, it takes no space. */
14
+ .root::before {
15
+ content: "";
16
+ position: absolute;
17
+ inset: 0;
18
+ z-index: 3;
19
+ border: var(--gooee-line) solid var(--gooee-color-border);
20
+ border-radius: inherit;
21
+ pointer-events: none;
22
+ }
23
+
15
24
  .scroll {
16
25
  overflow: auto;
17
26
  /* Room for the sticky header row and first column, so focus never lands under them. */