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,17 +1,17 @@
1
1
  'use client'
2
2
 
3
- import { useEffect, useId, useLayoutEffect, useRef, useState, type FocusEvent, type KeyboardEvent, type PointerEvent, type ReactNode, type RefObject } from 'react'
3
+ import { useEffect, useId, useLayoutEffect, useRef, useState, type ReactNode, type RefObject } from 'react'
4
4
  import { cx } from '../../internal/cx'
5
- import { release, rubber, speedTracker, useDragClose } from '../../internal/fling'
6
- import { returnTarget, useFocusTrap } from '../../internal/focus'
5
+ import { useDragClose } from '../../internal/fling'
6
+ import { useFocusTrap } from '../../internal/focus'
7
7
  import { XIcon } from '../../internal/icons'
8
+ import { createDetentSheet, DETENT_NAME, detentHeights, detentOrder, type SheetDetent } from '../../internal/motion/sheetDetents'
8
9
  import { useDismiss, useScrollLock } from '../../internal/layers'
9
10
  import { Portal } from '../../internal/Portal'
10
11
  import { usePresence } from '../../internal/usePresence'
11
12
  import styles from './Sheet.module.css'
12
13
 
13
- /** A height a sheet can rest at: low with only its title showing, half the screen, or nearly all of it. */
14
- export type SheetDetent = 'peek' | 'half' | 'full'
14
+ export type { SheetDetent }
15
15
 
16
16
  export type SheetProps = {
17
17
  /** Whether the sheet is open. You always control it. */
@@ -117,38 +117,18 @@ function PlainSheet({ open, onOpenChange, title, description, children, footer,
117
117
  )
118
118
  }
119
119
 
120
- // Shares of the screen, from the heights Apple Maps on the web and Vaul rest at.
121
- const SHARE: Record<SheetDetent, number> = { peek: 0.19, half: 0.5, full: 0.96 }
122
- const ORDER: SheetDetent[] = ['peek', 'half', 'full']
123
- const NAME: Record<SheetDetent, string> = { peek: 'Low', half: 'Half', full: 'Full' }
124
- // px per second: faster than this, a let-go is a flick and moves one height its way.
125
- const FLICK = 400
126
- // How far ahead, in seconds, a slow let-go is carried before the nearest height is picked.
127
- const PROJECT = 0.2
128
- // A press is not a drag until it moves this far, so taps on the content still work.
129
- const SLOP = 6
130
-
131
- const nearest = (v: number, hs: number[]) => hs.reduce((best, h, i) => (Math.abs(h - v) < Math.abs(hs[best] - v) ? i : best), 0)
132
-
133
- // A flick moves one height from where the drag began, never two, even when the hand has already
134
- // passed the next one. Real sheets measured all do this.
135
- function pick(pos: number, v: number, start: number, hs: number[]) {
136
- if (Math.abs(v) <= FLICK) return nearest(pos + v * PROJECT, hs)
137
- return Math.max(0, Math.min(hs.length - 1, start + Math.sign(v)))
138
- }
139
-
140
120
  type DetentProps = BaseProps & Required<Pick<SheetProps, 'detents' | 'heightLabel'>> & Pick<SheetProps, 'detent' | 'defaultDetent' | 'onDetentChange'>
141
121
 
142
122
  function DetentSheet({ open, onOpenChange, title, description, children, footer, closeLabel, initialFocus, className, detents, detent, defaultDetent, onDetentChange, heightLabel }: DetentProps) {
143
123
  const id = useId()
144
- const order = ORDER.filter((d) => detents.includes(d))
124
+ const order = detentOrder(detents)
145
125
  const top = order.length - 1
146
126
  const [inner, setInner] = useState<SheetDetent>(defaultDetent ?? order[0])
147
127
  const index = Math.max(0, order.indexOf(detent ?? inner))
148
128
  const atTop = index === top
149
129
  const { mounted, state } = usePresence(open, 'morph-out')
150
130
  const [vh, setVh] = useState(() => (typeof window === 'undefined' ? 0 : window.innerHeight))
151
- const heights = order.map((d) => Math.round(vh * SHARE[d]))
131
+ const heights = detentHeights(order, vh)
152
132
  const [dragging, setDragging] = useState(false)
153
133
  const [target, setTarget] = useState(index)
154
134
  const [said, setSaid] = useState('')
@@ -157,69 +137,38 @@ function DetentSheet({ open, onOpenChange, title, description, children, footer,
157
137
  const overlayRef = useRef<HTMLDivElement>(null)
158
138
  const grabRef = useRef<HTMLDivElement>(null)
159
139
  const bodyRef = useRef<HTMLDivElement>(null)
160
- const at = useRef(0)
161
- const aimed = useRef(-1)
162
- const stopGlide = useRef(() => {})
163
- const unlisten = useRef(() => {})
164
- const opener = useRef<HTMLElement | null>(null)
165
140
  const trapStart = useRef<HTMLElement | null>(null)
166
- const drag = useRef<{ y0: number; at0: number; start: number; pointer: number; live: boolean; speed: ReturnType<typeof speedTracker> } | null>(null)
167
- const live = useRef({ heights, index, open })
168
- live.current = { heights, index, open }
169
-
170
- const change = (i: number) => {
171
- if (detent === undefined) setInner(order[i])
172
- onDetentChange?.(order[i])
173
- }
174
-
175
- const paint = (v: number) => {
176
- at.current = v
177
- const hs = live.current.heights
178
- const panel = panelRef.current
179
- if (!panel) return
180
- panel.style.transform = `translateY(${hs[hs.length - 1] - v}px)`
181
- // The dim follows the height between the two highest, so it never jumps.
182
- const lo = hs.length > 1 ? hs[hs.length - 2] : 0
183
- const dim = Math.max(0, Math.min(1, (v - lo) / (hs[hs.length - 1] - lo || 1)))
184
- overlayRef.current?.style.setProperty('opacity', String(dim))
141
+ const live = useRef({ order, heights, index, open, onOpenChange, change: (_: number) => {} })
142
+ live.current = {
143
+ order,
144
+ heights,
145
+ index,
146
+ open,
147
+ onOpenChange,
148
+ change: (i: number) => {
149
+ if (detent === undefined) setInner(order[i])
150
+ onDetentChange?.(order[i])
151
+ },
185
152
  }
186
153
 
187
- const settle = (to: number, v: number, done?: () => void) => {
188
- stopGlide.current()
189
- stopGlide.current = release(at.current, to, v, paint, done)
190
- }
191
-
192
- const shownHidden = () => {
193
- const active = document.activeElement as HTMLElement | null
194
- return !!active && !!bodyRef.current?.contains(active) && active.getBoundingClientRect().top > window.innerHeight - 8
195
- }
196
-
197
- const go = (i: number, v = 0, spoken = true) => {
198
- const hs = live.current.heights
199
- if (i < 0) {
200
- // A glide carries it off at the hand's speed, so the CSS close stays out of the way.
201
- panelRef.current?.setAttribute('data-fluid', '')
202
- settle(0, v)
203
- onOpenChange(false)
204
- requestAnimationFrame(() => {
205
- if (live.current.open) {
206
- panelRef.current?.removeAttribute('data-fluid')
207
- settle(hs[live.current.index], 0)
208
- }
209
- })
210
- return
211
- }
212
- aimed.current = i
213
- setTarget(i)
214
- if (i !== live.current.index) {
215
- change(i)
216
- if (spoken) setSaid(`${NAME[order[i]]} height`)
217
- }
218
- // Shrunk below a focused control: focus goes to the grabber, so it never sits on something hidden.
219
- settle(hs[i], v, () => shownHidden() && grabRef.current?.focus({ preventScroll: true }))
220
- }
221
-
222
- const stepDown = () => (live.current.index > 0 ? go(live.current.index - 1) : onOpenChange(false))
154
+ // Drag, flick, the grabber's keys and where focus goes (motion/sheetDetents.ts).
155
+ const core = useRef<ReturnType<typeof createDetentSheet>>(null)
156
+ core.current ??= createDetentSheet({
157
+ panel: () => panelRef.current,
158
+ overlay: () => overlayRef.current,
159
+ grabber: () => grabRef.current,
160
+ body: () => bodyRef.current,
161
+ order: () => live.current.order,
162
+ heights: () => live.current.heights,
163
+ index: () => live.current.index,
164
+ open: () => live.current.open,
165
+ change: (i) => live.current.change(i),
166
+ close: () => live.current.onOpenChange(false),
167
+ target: setTarget,
168
+ dragging: setDragging,
169
+ say: setSaid,
170
+ })
171
+ const sheet = core.current
223
172
 
224
173
  useEffect(() => {
225
174
  const onResize = () => setVh(window.innerHeight)
@@ -228,38 +177,24 @@ function DetentSheet({ open, onOpenChange, title, description, children, footer,
228
177
  }, [])
229
178
 
230
179
  useLayoutEffect(() => {
231
- if (!open) return
232
- opener.current = returnTarget()
233
- aimed.current = -1
234
- }, [open])
180
+ if (open) sheet.opening()
181
+ }, [open, sheet])
235
182
 
236
183
  useLayoutEffect(() => {
237
- if (!open || !mounted) return
238
- if (aimed.current === -1) {
239
- aimed.current = index
240
- setTarget(index)
241
- paint(heights[index])
242
- if (!atTop) (initialFocus?.current ?? grabRef.current)?.focus({ preventScroll: true })
243
- return
244
- }
245
- if (drag.current?.live) return
246
- if (aimed.current !== index) {
247
- aimed.current = index
248
- setTarget(index)
249
- settle(heights[index], 0)
250
- } else paint(heights[index])
184
+ if (open && mounted) sheet.sync(initialFocus?.current)
185
+ // Synced when the height or the screen changes; initialFocus is read at that moment.
186
+ // eslint-disable-next-line react-hooks/exhaustive-deps
251
187
  }, [open, mounted, index, vh])
252
188
 
253
189
  // Closed, it gives focus back to its opener, and opens next time at the default height.
254
190
  useEffect(() => {
255
191
  if (open) return
256
192
  setInner(defaultDetent ?? order[0])
257
- const panel = panelRef.current
258
- const active = document.activeElement
259
- if (panel && opener.current?.isConnected && (!active || active === document.body || panel.contains(active))) opener.current.focus({ preventScroll: true })
193
+ sheet.closed()
194
+ // eslint-disable-next-line react-hooks/exhaustive-deps
260
195
  }, [open])
261
196
 
262
- useEffect(() => () => stopGlide.current(), [])
197
+ useEffect(() => () => sheet.destroy(), [sheet])
263
198
 
264
199
  // Before the trap's own effect: at the top, focus stays where it is in the sheet.
265
200
  useLayoutEffect(() => {
@@ -269,127 +204,13 @@ function DetentSheet({ open, onOpenChange, title, description, children, footer,
269
204
  }, [atTop, open])
270
205
  useScrollLock(open && atTop)
271
206
  useFocusTrap(panelRef, open && mounted && atTop, { initialFocus: trapStart, restore: false })
272
- useDismiss(open, [panelRef], stepDown, { outside: atTop })
207
+ useDismiss(open, [panelRef], sheet.stepDown, { outside: atTop })
273
208
 
274
- const onPointerMove = (e: globalThis.PointerEvent) => {
275
- const d = drag.current
276
- const hs = live.current.heights
277
- if (!d || d.pointer !== e.pointerId) return
278
- const dy = d.y0 - e.clientY
279
- if (!d.live) {
280
- if (Math.abs(dy) < SLOP) return
281
- if (bodyRef.current?.contains(e.target as Node) && live.current.index === top && dy > 0) return endDrag()
282
- // A tap must not stop a settle half way, so the glide is caught only once it is a drag.
283
- stopGlide.current()
284
- d.live = true
285
- d.at0 = at.current
286
- d.start = nearest(at.current, hs)
287
- d.speed.add(at.current)
288
- d.y0 = e.clientY + (dy > 0 ? SLOP : -SLOP)
289
- // Only now, so a tap on a button inside still clicks it.
290
- if (e.target instanceof Element && panelRef.current?.contains(e.target)) panelRef.current.setPointerCapture?.(e.pointerId)
291
- window.addEventListener('selectstart', block)
292
- setDragging(true)
293
- }
294
- // A drag moves the sheet, never selects the page's text: a selection left over from one drag
295
- // would turn the next press on it into the browser's own drag, which cancels the pointer. Safari
296
- // keeps growing the selection it began on the press, so it is cleared on every move.
297
- const selection = window.getSelection()
298
- if (selection && !selection.isCollapsed) selection.removeAllRanges()
299
- let raw = d.at0 + (d.y0 - e.clientY)
300
- const full = hs[hs.length - 1]
301
- if (raw > full) raw = full + rubber(raw - full, window.innerHeight - full)
302
- if (raw < 0) raw = 0
303
- d.speed.add(raw)
304
- paint(raw)
305
- const next = pick(raw, d.speed.speed(), d.start, hs)
306
- setTarget((t) => (t === next ? t : next))
307
- }
308
-
309
- const onPointerUp = (e: globalThis.PointerEvent) => {
310
- const d = drag.current
311
- if (!d || d.pointer !== e.pointerId) return
312
- endDrag()
313
- if (!d.live) return
314
- setDragging(false)
315
- const hs = live.current.heights
316
- const cancelled = e.type === 'pointercancel'
317
- const v = cancelled ? 0 : d.speed.speed()
318
- const pos = at.current
319
- if (!cancelled && d.start === 0 && (v < -FLICK || pos < hs[0] * 0.5)) return go(-1, v)
320
- go(pick(pos, v, d.start, hs), v)
321
- }
322
-
323
- // The hand often leaves the sheet before the drag counts as one, so the moves are followed on the window.
324
- const block = (e: Event) => e.preventDefault()
325
-
326
- const endDrag = () => {
327
- drag.current = null
328
- unlisten.current()
329
- }
330
-
331
- const onPointerDown = (e: PointerEvent<HTMLDivElement>) => {
332
- if ((e.pointerType === 'mouse' && e.button !== 0) || drag.current) return
333
- const el = e.target as Element
334
- if (el.closest('input, textarea, select, [contenteditable="true"]')) return
335
- const hs = live.current.heights
336
- // At the top the content scrolls; a drag on it moves the sheet only from the top of the content.
337
- if (bodyRef.current?.contains(el) && live.current.index === top && (bodyRef.current?.scrollTop ?? 0) > 0) return
338
- drag.current = { y0: e.clientY, at0: at.current, start: nearest(at.current, hs), pointer: e.pointerId, live: false, speed: speedTracker() }
339
- const move = onPointerMove
340
- const up = onPointerUp
341
- window.addEventListener('pointermove', move)
342
- window.addEventListener('pointerup', up)
343
- window.addEventListener('pointercancel', up)
344
- window.addEventListener('dragstart', block)
345
- // Safari starts a text selection on the press itself, so a press outside the content (the grabber,
346
- // the edges) blocks it at once. Text in the content can still be selected by dragging inside it.
347
- if (!bodyRef.current?.contains(el)) window.addEventListener('selectstart', block)
348
- unlisten.current = () => {
349
- window.removeEventListener('pointermove', move)
350
- window.removeEventListener('pointerup', up)
351
- window.removeEventListener('pointercancel', up)
352
- window.removeEventListener('dragstart', block)
353
- window.removeEventListener('selectstart', block)
354
- }
355
- }
356
-
357
- useEffect(() => () => unlisten.current(), [])
358
-
359
- // A touch at the top of the content, moving down, belongs to the sheet: the browser would scroll it.
360
209
  useEffect(() => {
361
210
  const body = bodyRef.current
362
211
  if (!body) return
363
- let y = 0
364
- const start = (e: TouchEvent) => (y = e.touches[0].clientY)
365
- const move = (e: TouchEvent) => {
366
- if (drag.current?.live || (live.current.index === live.current.heights.length - 1 && body.scrollTop <= 0 && e.touches[0].clientY > y)) e.preventDefault()
367
- }
368
- body.addEventListener('touchstart', start, { passive: true })
369
- body.addEventListener('touchmove', move, { passive: false })
370
- return () => {
371
- body.removeEventListener('touchstart', start)
372
- body.removeEventListener('touchmove', move)
373
- }
374
- }, [mounted])
375
-
376
- const onGrabKey = (e: KeyboardEvent<HTMLDivElement>) => {
377
- const i = live.current.index
378
- if (e.key === 'Enter' || e.key === ' ') {
379
- e.preventDefault()
380
- return go(i === top ? 0 : i + 1, 0, false)
381
- }
382
- const keys: Record<string, number> = { ArrowUp: i + 1, ArrowRight: i + 1, ArrowDown: i - 1, ArrowLeft: i - 1, Home: 0, End: top, PageUp: top, PageDown: 0 }
383
- if (!(e.key in keys)) return
384
- e.preventDefault()
385
- go(Math.max(0, Math.min(top, keys[e.key])), 0, false)
386
- }
387
-
388
- // Tabbing onto a control hidden below the screen's edge grows the sheet to the top, so it shows.
389
- const onFocus = (e: FocusEvent<HTMLDivElement>) => {
390
- if (!bodyRef.current?.contains(e.target) || live.current.index === top) return
391
- if (e.target.getBoundingClientRect().bottom > window.innerHeight - 4) go(top)
392
- }
212
+ return sheet.holdTouch(body)
213
+ }, [mounted, sheet])
393
214
 
394
215
  if (!mounted) return null
395
216
  const shown = order[dragging ? target : index]
@@ -410,8 +231,8 @@ function DetentSheet({ open, onOpenChange, title, description, children, footer,
410
231
  data-top={atTop || undefined}
411
232
  data-dragging={dragging || undefined}
412
233
  style={{ height: heights[top] }}
413
- onPointerDown={onPointerDown}
414
- onFocus={onFocus}
234
+ onPointerDown={sheet.down}
235
+ onFocus={(e) => sheet.focusIn(e.target)}
415
236
  >
416
237
  <div
417
238
  ref={grabRef}
@@ -423,11 +244,11 @@ function DetentSheet({ open, onOpenChange, title, description, children, footer,
423
244
  aria-valuemin={0}
424
245
  aria-valuemax={top}
425
246
  aria-valuenow={index}
426
- aria-valuetext={NAME[order[index]]}
247
+ aria-valuetext={DETENT_NAME[order[index]]}
427
248
  aria-controls={`${id}-sheet`}
428
249
  data-detent={shown}
429
- onKeyDown={onGrabKey}
430
- onClick={() => go(index === top ? 0 : index + 1)}
250
+ onKeyDown={sheet.grabKey}
251
+ onClick={sheet.grabClick}
431
252
  >
432
253
  <span className={styles.pill} />
433
254
  </div>
@@ -0,0 +1,249 @@
1
+ <script setup lang="ts">
2
+ import { computed, onBeforeUnmount, onMounted, ref, useId, watch } from 'vue'
3
+ import { createDragClose } from '../../internal/motion/fling'
4
+ import { trapFocus } from '../../internal/motion/focus'
5
+ import { dismissable, lockScroll } from '../../internal/motion/layers'
6
+ import { createDetentSheet, DETENT_NAME, detentHeights, detentOrder, type SheetDetent } from '../../internal/motion/sheetDetents'
7
+ import { usePresence } from '../../internal/vue/usePresence'
8
+ import { XIcon } from '../../internal/vueIcons'
9
+ import styles from './Sheet.module.css'
10
+
11
+ // The Vue Sheet: the same look (Sheet.module.css), drag, heights and focus as the React one.
12
+ // With detents, the grabber becomes a control for the height, and the sheet is modal only at its highest.
13
+ // Other attributes, such as class, go to the panel.
14
+ defineOptions({ inheritAttrs: false })
15
+
16
+ const props = withDefaults(
17
+ defineProps<{
18
+ /** Visible heading, also the dialog's accessible name. The title slot replaces it. */
19
+ title?: string
20
+ /** Text under the title, also read as the dialog's description. The description slot replaces it. */
21
+ description?: string
22
+ /** Screen reader name for the close button. */
23
+ closeLabel?: string
24
+ /** Element to focus first. Defaults to the first focusable element in the content. */
25
+ initialFocus?: HTMLElement | null
26
+ /** Heights the sheet rests at, such as ['peek', 'half', 'full']. Below the highest one it is not modal: the page behind stays usable. */
27
+ detents?: SheetDetent[]
28
+ /** The height it rests at. Use v-model:detent to control it. */
29
+ detent?: SheetDetent
30
+ /** The height it opens at when you do not control it. Defaults to the lowest. */
31
+ defaultDetent?: SheetDetent
32
+ /** Screen reader name for the grabber, which sets the height. */
33
+ heightLabel?: string
34
+ }>(),
35
+ { closeLabel: 'Close', heightLabel: 'Sheet height' },
36
+ )
37
+ /** Whether the sheet is open. Use v-model:open; Esc, an outside tap, a drag or the close button set it false. */
38
+ const open = defineModel<boolean>('open', { required: true })
39
+ const emit = defineEmits<{ 'update:detent': [detent: SheetDetent] }>()
40
+ const slots = defineSlots<{ default?: () => unknown; title?: () => unknown; description?: () => unknown; footer?: () => unknown }>()
41
+
42
+ const id = useId()
43
+ const panel = ref<HTMLElement | null>(null)
44
+ const overlay = ref<HTMLElement | null>(null)
45
+ const grabber = ref<HTMLElement | null>(null)
46
+ const body = ref<HTMLElement | null>(null)
47
+ const hasDescription = computed(() => !!(props.description || slots.description))
48
+ const close = () => (open.value = false)
49
+ const { mounted, state } = usePresence(open, 'morph-out')
50
+ const withDetents = computed(() => !!props.detents?.length)
51
+
52
+ // On the server (Nuxt) there is no page to hold, so the watchers wait for the browser.
53
+ const server = typeof window === 'undefined'
54
+ const releases: Record<string, () => void> = {}
55
+ const hold = (key: string, make: (() => () => void) | false) => {
56
+ releases[key]?.()
57
+ releases[key] = make && !server ? make() : () => {}
58
+ }
59
+
60
+ // Without detents: the hand pulls it down to close.
61
+ const drag = createDragClose(() => panel.value, { onClose: close, isOpen: () => open.value })
62
+ watch(open, (now) => {
63
+ if (withDetents.value) return
64
+ if (now) drag.fling.opened()
65
+ else drag.fling.closed()
66
+ })
67
+ watch(open, (now) => hold('lock', !withDetents.value && now && lockScroll), { immediate: true })
68
+ watch(open, (now) => hold('dismiss', !withDetents.value && now && (() => dismissable(() => [panel.value], close))), { immediate: true })
69
+ watch(
70
+ () => !withDetents.value && open.value && mounted.value,
71
+ (on) => hold('trap', on && !!panel.value && (() => trapFocus(panel.value!, { initialFocus: props.initialFocus }))),
72
+ { flush: 'post', immediate: true },
73
+ )
74
+ const onAnimationEnd = (e: AnimationEvent) => {
75
+ if (e.target === panel.value && state.value === 'open') (e.target as HTMLElement).style.transform = ''
76
+ }
77
+
78
+ // With detents: drag, flick, the grabber's keys and where focus goes (motion/sheetDetents.ts).
79
+ const order = computed(() => detentOrder(props.detents ?? []))
80
+ const top = computed(() => order.value.length - 1)
81
+ const inner = ref<SheetDetent>(props.defaultDetent ?? order.value[0])
82
+ const index = computed(() => Math.max(0, order.value.indexOf(props.detent ?? inner.value)))
83
+ const atTop = computed(() => index.value === top.value)
84
+ const vh = ref(server ? 0 : window.innerHeight)
85
+ const heights = computed(() => detentHeights(order.value, vh.value))
86
+ const dragging = ref(false)
87
+ const target = ref(index.value)
88
+ const said = ref('')
89
+ const shown = computed(() => order.value[dragging.value ? target.value : index.value])
90
+
91
+ const sheet = createDetentSheet({
92
+ panel: () => panel.value,
93
+ overlay: () => overlay.value,
94
+ grabber: () => grabber.value,
95
+ body: () => body.value,
96
+ order: () => order.value,
97
+ heights: () => heights.value,
98
+ index: () => index.value,
99
+ open: () => open.value,
100
+ change: (i) => {
101
+ if (props.detent === undefined) inner.value = order.value[i]
102
+ emit('update:detent', order.value[i])
103
+ },
104
+ close,
105
+ target: (i) => (target.value = i),
106
+ dragging: (on) => (dragging.value = on),
107
+ say: (text) => (said.value = text),
108
+ })
109
+
110
+ const onResize = () => (vh.value = window.innerHeight)
111
+ onMounted(() => window.addEventListener('resize', onResize))
112
+
113
+ // Before the new state renders, so the opener is read while it still has focus.
114
+ watch(open, (now) => withDetents.value && now && !server && sheet.opening(), { flush: 'pre', immediate: true })
115
+ watch(
116
+ [open, mounted, index, vh],
117
+ () => {
118
+ if (withDetents.value && open.value && mounted.value && !server) sheet.sync(props.initialFocus)
119
+ },
120
+ { flush: 'post', immediate: true },
121
+ )
122
+ // Closed, it gives focus back to its opener, and opens next time at the default height.
123
+ watch(
124
+ open,
125
+ (now) => {
126
+ if (!withDetents.value || now) return
127
+ inner.value = props.defaultDetent ?? order.value[0]
128
+ sheet.closed()
129
+ },
130
+ { flush: 'post' },
131
+ )
132
+ // At the top it is modal: focus stays where it is in the sheet, or starts on initialFocus.
133
+ watch(
134
+ () => withDetents.value && open.value && mounted.value && atTop.value,
135
+ (on) =>
136
+ hold(
137
+ 'detentTrap',
138
+ on &&
139
+ !!panel.value &&
140
+ (() => {
141
+ const el = panel.value!
142
+ const active = document.activeElement as HTMLElement | null
143
+ return trapFocus(el, { initialFocus: active && el.contains(active) ? active : (props.initialFocus ?? null), restore: false })
144
+ }),
145
+ ),
146
+ { flush: 'post', immediate: true },
147
+ )
148
+ watch(
149
+ () => withDetents.value && open.value && atTop.value,
150
+ (on) => hold('detentLock', on && lockScroll),
151
+ { immediate: true },
152
+ )
153
+ watch([open, atTop], () => hold('detentDismiss', withDetents.value && open.value && (() => dismissable(() => [panel.value], sheet.stepDown, { outside: atTop.value }))), { immediate: true })
154
+ watch(mounted, () => hold('touch', withDetents.value && !!body.value && (() => sheet.holdTouch(body.value!))), { flush: 'post', immediate: true })
155
+
156
+ onBeforeUnmount(() => {
157
+ Object.values(releases).forEach((r) => r())
158
+ drag.fling.stop()
159
+ sheet.destroy()
160
+ if (!server) window.removeEventListener('resize', onResize)
161
+ })
162
+ </script>
163
+
164
+ <template>
165
+ <Teleport v-if="mounted" to="body">
166
+ <template v-if="!withDetents">
167
+ <div :class="styles.overlay" :data-state="state" aria-hidden="true" />
168
+ <div
169
+ v-bind="$attrs"
170
+ ref="panel"
171
+ role="dialog"
172
+ aria-modal="true"
173
+ :aria-labelledby="`${id}-title`"
174
+ :aria-describedby="hasDescription ? `${id}-desc` : undefined"
175
+ tabindex="-1"
176
+ :class="styles.sheet"
177
+ :data-state="state"
178
+ @animationend="onAnimationEnd"
179
+ >
180
+ <div :class="styles.grab" @pointerdown="drag.down" @pointermove="drag.move" @pointerup="drag.up" @pointercancel="drag.up">
181
+ <span :class="styles.handle" aria-hidden="true" />
182
+ <header :class="styles.header">
183
+ <div :class="styles.titles">
184
+ <h2 :id="`${id}-title`" :class="styles.title"><slot name="title">{{ title }}</slot></h2>
185
+ <p v-if="hasDescription" :id="`${id}-desc`" :class="styles.description"><slot name="description">{{ description }}</slot></p>
186
+ </div>
187
+ <button type="button" :class="styles.close" :aria-label="closeLabel" data-skip-initial-focus="true" @click="close">
188
+ <XIcon />
189
+ </button>
190
+ </header>
191
+ </div>
192
+ <div v-if="slots.default" :class="styles.body"><slot /></div>
193
+ <footer v-if="slots.footer" :class="styles.footer"><slot name="footer" /></footer>
194
+ </div>
195
+ </template>
196
+ <template v-else>
197
+ <div ref="overlay" :class="styles.overlay" :data-state="state" data-detents="true" :data-top="atTop || undefined" aria-hidden="true" />
198
+ <div
199
+ v-bind="$attrs"
200
+ :id="`${id}-sheet`"
201
+ ref="panel"
202
+ role="dialog"
203
+ :aria-modal="atTop || undefined"
204
+ :aria-labelledby="`${id}-title`"
205
+ :aria-describedby="hasDescription ? `${id}-desc` : undefined"
206
+ tabindex="-1"
207
+ :class="styles.sheet"
208
+ :data-state="state"
209
+ data-detents="true"
210
+ :data-top="atTop || undefined"
211
+ :data-dragging="dragging || undefined"
212
+ :style="{ height: `${heights[top]}px` }"
213
+ @pointerdown="sheet.down"
214
+ @focusin="sheet.focusIn($event.target)"
215
+ >
216
+ <div
217
+ ref="grabber"
218
+ :class="styles.grabber"
219
+ role="slider"
220
+ tabindex="0"
221
+ :aria-label="heightLabel"
222
+ aria-orientation="vertical"
223
+ aria-valuemin="0"
224
+ :aria-valuemax="top"
225
+ :aria-valuenow="index"
226
+ :aria-valuetext="DETENT_NAME[order[index]]"
227
+ :aria-controls="`${id}-sheet`"
228
+ :data-detent="shown"
229
+ @keydown="sheet.grabKey"
230
+ @click="sheet.grabClick"
231
+ >
232
+ <span :class="styles.pill" />
233
+ </div>
234
+ <header :class="styles.header">
235
+ <div :class="styles.titles">
236
+ <h2 :id="`${id}-title`" :class="styles.title"><slot name="title">{{ title }}</slot></h2>
237
+ <p v-if="hasDescription" :id="`${id}-desc`" :class="styles.description"><slot name="description">{{ description }}</slot></p>
238
+ </div>
239
+ <button type="button" :class="styles.close" :aria-label="closeLabel" data-skip-initial-focus="true" @click="close">
240
+ <XIcon />
241
+ </button>
242
+ </header>
243
+ <div v-if="slots.default" ref="body" :class="styles.body"><slot /></div>
244
+ <footer v-if="slots.footer" :class="styles.footer"><slot name="footer" /></footer>
245
+ <p :class="styles.srOnly" aria-live="polite">{{ said }}</p>
246
+ </div>
247
+ </template>
248
+ </Teleport>
249
+ </template>
@@ -0,0 +1,38 @@
1
+ <script setup lang="ts">
2
+ import { computed } from 'vue'
3
+ import styles from './Skeleton.module.css'
4
+
5
+ // The Vue Skeleton: the same shapes (Skeleton.module.css) as the React one.
6
+ // Decorative only. Mark the loading container with aria-busy and a label instead.
7
+ const props = withDefaults(
8
+ defineProps<{
9
+ /** Text draws lines, rect a box, circle a round shape such as an avatar. */
10
+ shape?: 'text' | 'rect' | 'circle'
11
+ /** Any CSS width, such as 120 or "50%". */
12
+ width?: number | string
13
+ /** Any CSS height. Ignored for text, where lines set the height. */
14
+ height?: number | string
15
+ /** For text: number of lines. The last one is shorter, like real text. */
16
+ lines?: number
17
+ /** Solid grey shapes, or a sketch: the same shapes as dashed outlines, like a pencil layout. */
18
+ variant?: 'solid' | 'sketch'
19
+ }>(),
20
+ { shape: 'rect', lines: 1, variant: 'solid' },
21
+ )
22
+
23
+ // A number is pixels, as in React.
24
+ const px = (v: number | string | undefined) => (typeof v === 'number' ? `${v}px` : v)
25
+ const look = computed(() => props.variant === 'sketch' && styles.sketch)
26
+ </script>
27
+
28
+ <template>
29
+ <span v-if="shape === 'text'" :class="styles.lines" :style="{ width: px(width) }" aria-hidden="true">
30
+ <span
31
+ v-for="i in lines"
32
+ :key="i - 1"
33
+ :class="[styles.bone, styles.text, look]"
34
+ :style="{ width: lines > 1 && i === lines ? '62%' : '100%', '--i': i - 1 }"
35
+ />
36
+ </span>
37
+ <span v-else :class="[styles.bone, styles[shape], look]" :style="{ width: px(width), height: px(height) }" aria-hidden="true" />
38
+ </template>