gooee 0.50.0 → 0.51.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (172) hide show
  1. package/README.md +17 -4
  2. package/package.json +5 -3
  3. package/scripts/add.js +28 -6
  4. package/src/components/Accordion/Accordion.vue +62 -0
  5. package/src/components/Accordion/AccordionItem.vue +45 -0
  6. package/src/components/Accordion/accordionContext.ts +6 -0
  7. package/src/components/Alert/Alert.tsx +7 -27
  8. package/src/components/Alert/Alert.vue +59 -0
  9. package/src/components/AlertDialog/AlertDialog.vue +126 -0
  10. package/src/components/AnimatedNumber/AnimatedNumber.vue +36 -0
  11. package/src/components/AspectRatio/AspectRatio.vue +17 -0
  12. package/src/components/Avatar/Avatar.vue +56 -0
  13. package/src/components/AvatarGroup/AvatarGroup.vue +42 -0
  14. package/src/components/Badge/Badge.vue +32 -0
  15. package/src/components/Breadcrumb/Breadcrumb.vue +58 -0
  16. package/src/components/Button/Button.vue +130 -0
  17. package/src/components/ButtonGroup/ButtonGroup.tsx +4 -21
  18. package/src/components/ButtonGroup/ButtonGroup.vue +33 -0
  19. package/src/components/Card/Card.vue +34 -0
  20. package/src/components/Center/Center.vue +32 -0
  21. package/src/components/Checkbox/Checkbox.tsx +8 -25
  22. package/src/components/Checkbox/Checkbox.vue +108 -0
  23. package/src/components/CheckboxGroup/CheckboxGroup.vue +76 -0
  24. package/src/components/Cluster/Cluster.vue +34 -0
  25. package/src/components/CodeBlock/CodeBlock.vue +45 -0
  26. package/src/components/Collapsible/Collapsible.vue +59 -0
  27. package/src/components/Container/Container.vue +22 -0
  28. package/src/components/CopyButton/CopyButton.tsx +3 -49
  29. package/src/components/CopyButton/CopyButton.vue +116 -0
  30. package/src/components/Dial/Dial.tsx +12 -120
  31. package/src/components/Dial/Dial.vue +228 -0
  32. package/src/components/Dialog/Dialog.vue +139 -0
  33. package/src/components/Divider/Divider.vue +27 -0
  34. package/src/components/DropdownMenu/DropdownMenu.tsx +11 -46
  35. package/src/components/DropdownMenu/DropdownMenu.vue +185 -0
  36. package/src/components/EmptyState/EmptyState.vue +33 -0
  37. package/src/components/Fab/Fab.tsx +4 -14
  38. package/src/components/Fab/Fab.vue +115 -0
  39. package/src/components/Fieldset/Fieldset.vue +42 -0
  40. package/src/components/FormField/FormField.tsx +2 -17
  41. package/src/components/FormField/FormField.vue +64 -0
  42. package/src/components/Grid/Grid.vue +43 -0
  43. package/src/components/HoldToConfirm/HoldToConfirm.tsx +13 -143
  44. package/src/components/HoldToConfirm/HoldToConfirm.vue +281 -0
  45. package/src/components/IconButton/IconButton.vue +25 -0
  46. package/src/components/Input/Input.vue +112 -0
  47. package/src/components/InputGroup/InputGroup.vue +102 -0
  48. package/src/components/Kbd/Kbd.tsx +2 -12
  49. package/src/components/Kbd/Kbd.vue +21 -0
  50. package/src/components/Link/Link.vue +38 -0
  51. package/src/components/LivePill/LivePill.tsx +11 -39
  52. package/src/components/LivePill/LivePill.vue +152 -0
  53. package/src/components/Meter/Meter.vue +88 -0
  54. package/src/components/MorphButton/MorphButton.tsx +11 -67
  55. package/src/components/MorphButton/MorphButton.vue +120 -0
  56. package/src/components/NumberInput/NumberInput.tsx +11 -48
  57. package/src/components/NumberInput/NumberInput.vue +184 -0
  58. package/src/components/Pagination/Pagination.tsx +2 -15
  59. package/src/components/Pagination/Pagination.vue +87 -0
  60. package/src/components/Pagination/range.ts +15 -0
  61. package/src/components/PasswordInput/PasswordInput.vue +53 -0
  62. package/src/components/PickerWheel/DateWheel.vue +48 -0
  63. package/src/components/PickerWheel/PickerWheel.tsx +15 -129
  64. package/src/components/PickerWheel/PickerWheel.vue +77 -0
  65. package/src/components/PickerWheel/TimeWheel.vue +50 -0
  66. package/src/components/PickerWheel/WheelColumn.vue +70 -0
  67. package/src/components/Popover/Popover.vue +108 -0
  68. package/src/components/ProgressBar/ProgressBar.vue +48 -0
  69. package/src/components/Prose/Prose.vue +18 -0
  70. package/src/components/Radio/Radio.vue +49 -0
  71. package/src/components/Radio/RadioGroup.vue +69 -0
  72. package/src/components/Retime/Retime.tsx +7 -40
  73. package/src/components/Retime/Retime.vue +64 -0
  74. package/src/components/RulerScrubber/RulerScrubber.tsx +9 -38
  75. package/src/components/RulerScrubber/RulerScrubber.vue +166 -0
  76. package/src/components/ScrollArea/ScrollArea.tsx +17 -129
  77. package/src/components/ScrollArea/ScrollArea.vue +58 -0
  78. package/src/components/SearchInput/SearchInput.tsx +2 -15
  79. package/src/components/SearchInput/SearchInput.vue +70 -0
  80. package/src/components/SegmentedControl/SegmentedControl.tsx +7 -51
  81. package/src/components/SegmentedControl/SegmentedControl.vue +111 -0
  82. package/src/components/Select/Select.tsx +16 -47
  83. package/src/components/Select/Select.vue +236 -0
  84. package/src/components/Sheet/Sheet.tsx +52 -231
  85. package/src/components/Sheet/Sheet.vue +249 -0
  86. package/src/components/Skeleton/Skeleton.vue +38 -0
  87. package/src/components/Slider/Slider.tsx +25 -84
  88. package/src/components/Slider/Slider.vue +113 -0
  89. package/src/components/Spinner/Spinner.vue +30 -0
  90. package/src/components/Split/Split.vue +34 -0
  91. package/src/components/SplitFlap/SplitFlap.tsx +11 -175
  92. package/src/components/SplitFlap/SplitFlap.vue +52 -0
  93. package/src/components/Stack/Stack.vue +22 -0
  94. package/src/components/SwipeDeck/SwipeDeck.tsx +30 -194
  95. package/src/components/SwipeDeck/SwipeDeck.vue +168 -0
  96. package/src/components/Switch/Switch.tsx +6 -75
  97. package/src/components/Switch/Switch.vue +66 -0
  98. package/src/components/Table/Table.module.css +12 -3
  99. package/src/components/Table/Table.tsx +8 -59
  100. package/src/components/Table/Table.vue +274 -0
  101. package/src/components/TableOfContents/TableOfContents.vue +90 -0
  102. package/src/components/Tabs/Tab.vue +37 -0
  103. package/src/components/Tabs/TabList.vue +47 -0
  104. package/src/components/Tabs/TabPanel.vue +26 -0
  105. package/src/components/Tabs/Tabs.module.css +2 -2
  106. package/src/components/Tabs/Tabs.tsx +11 -84
  107. package/src/components/Tabs/Tabs.vue +33 -0
  108. package/src/components/Tabs/tabsContext.ts +15 -0
  109. package/src/components/Tag/Tag.tsx +6 -26
  110. package/src/components/Tag/Tag.vue +63 -0
  111. package/src/components/Textarea/Textarea.tsx +2 -22
  112. package/src/components/Textarea/Textarea.vue +120 -0
  113. package/src/components/ThemeToggle/ThemeToggle.tsx +10 -84
  114. package/src/components/ThemeToggle/ThemeToggle.vue +90 -0
  115. package/src/components/Toast/Toast.tsx +34 -264
  116. package/src/components/Toast/Toast.vue +138 -0
  117. package/src/components/Toast/ToastItem.vue +108 -0
  118. package/src/components/Toggle/Toggle.vue +52 -0
  119. package/src/components/Tooltip/Tooltip.tsx +17 -48
  120. package/src/components/Tooltip/Tooltip.vue +95 -0
  121. package/src/components/VisuallyHidden/VisuallyHidden.vue +17 -0
  122. package/src/internal/FieldMessage.vue +41 -0
  123. package/src/internal/Roll.vue +20 -0
  124. package/src/internal/focus.ts +4 -47
  125. package/src/internal/icons.module.css +39 -27
  126. package/src/internal/layers.ts +7 -61
  127. package/src/internal/motion/alert.ts +17 -0
  128. package/src/internal/motion/buttonGroup.ts +21 -0
  129. package/src/internal/motion/checkbox.ts +39 -0
  130. package/src/internal/motion/collapseOut.ts +26 -0
  131. package/src/internal/motion/copyButton.ts +48 -0
  132. package/src/internal/motion/dial.ts +137 -0
  133. package/src/internal/motion/fab.ts +14 -0
  134. package/src/internal/motion/floating.ts +58 -0
  135. package/src/internal/motion/focus.ts +53 -0
  136. package/src/internal/motion/formField.ts +19 -0
  137. package/src/internal/motion/holdToConfirm.ts +147 -0
  138. package/src/internal/motion/keys.ts +42 -0
  139. package/src/internal/motion/layers.ts +78 -0
  140. package/src/internal/motion/listbox.ts +69 -0
  141. package/src/internal/motion/livePill.ts +71 -0
  142. package/src/internal/motion/menuKeys.ts +58 -0
  143. package/src/internal/motion/morphButton.ts +71 -0
  144. package/src/internal/motion/numberInput.ts +57 -0
  145. package/src/internal/motion/pickerWheel.ts +166 -0
  146. package/src/internal/motion/retime.ts +45 -0
  147. package/src/internal/motion/ruler.ts +57 -0
  148. package/src/internal/motion/scrollArea.ts +133 -0
  149. package/src/internal/motion/scrubLook.ts +15 -0
  150. package/src/internal/motion/segmented.ts +63 -0
  151. package/src/internal/motion/sheetDetents.ts +271 -0
  152. package/src/internal/motion/slider.ts +108 -0
  153. package/src/internal/motion/splitFlap.ts +205 -0
  154. package/src/internal/motion/swipeDeck.ts +236 -0
  155. package/src/internal/motion/switchThumb.ts +83 -0
  156. package/src/internal/motion/table.ts +77 -0
  157. package/src/internal/motion/tabs.ts +105 -0
  158. package/src/internal/motion/textarea.ts +22 -0
  159. package/src/internal/motion/themeIcon.ts +91 -0
  160. package/src/internal/motion/themeReveal.ts +71 -0
  161. package/src/internal/motion/toast.ts +291 -0
  162. package/src/internal/motion/tooltip.ts +58 -0
  163. package/src/internal/themeReveal.ts +3 -61
  164. package/src/internal/useFloating.ts +9 -49
  165. package/src/internal/useScrub.ts +1 -13
  166. package/src/internal/vue/trigger.ts +18 -0
  167. package/src/internal/vue/useFloating.ts +38 -0
  168. package/src/internal/vue/usePresence.ts +21 -0
  169. package/src/internal/vue/useScrub.ts +40 -0
  170. package/src/internal/vueIcons.ts +134 -0
  171. package/src/tokens.css +5 -0
  172. package/src/internal/relativeTime.ts +0 -26
@@ -1,26 +1,13 @@
1
1
  'use client'
2
2
 
3
- import {
4
- createContext,
5
- useCallback,
6
- useContext,
7
- useEffect,
8
- useLayoutEffect,
9
- useMemo,
10
- useRef,
11
- useState,
12
- type PointerEvent,
13
- type ReactNode,
14
- } from 'react'
3
+ import { createContext, useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState, type ReactNode } from 'react'
15
4
  import { CheckCircleIcon, ErrorIcon, InfoIcon, XIcon } from '../../internal/icons'
16
- import { castShadow, frames, layer, morphEase, motionFor, shadowOf } from '../../internal/liquidMatter'
17
- import { BEND, body, budScene, exitToward, loopsPath, pincher, sdBox, trace, type Box, type Pt } from '../../internal/liquidPath'
18
5
  import { motionMs } from '../../internal/motion'
19
- import { release, speedTracker } from '../../internal/fling'
6
+ import { createToastBud, createToastSwipe, toastId, toastLayout, toastSay, toastStack, toastTimer, watchToastHeight, type ToastLeaving, type ToastOrigin, type ToastVariant } from '../../internal/motion/toast'
20
7
  import { Spinner } from '../Spinner'
21
8
  import styles from './Toast.module.css'
22
9
 
23
- export type ToastVariant = 'default' | 'success' | 'error' | 'info' | 'loading'
10
+ export type { ToastVariant }
24
11
 
25
12
  export type ToastOptions = {
26
13
  title: string
@@ -30,10 +17,10 @@ export type ToastOptions = {
30
17
  duration?: number
31
18
  action?: { label: string; onClick: () => void }
32
19
  /** Where the action happened: the element pressed, or a point on screen. Within 240px of the toast, the drop buds out of it and a dismiss flows back into it. Further away, or without it, the drop buds out of the window's edge. */
33
- origin?: HTMLElement | { x: number; y: number }
20
+ origin?: ToastOrigin
34
21
  }
35
22
 
36
- type Item = ToastOptions & { id: number; leaving: false | 'down' | 'left' | 'right' }
23
+ type Item = ToastOptions & { id: number; leaving: ToastLeaving }
37
24
 
38
25
  type Api = {
39
26
  toast: (opts: ToastOptions) => number
@@ -60,12 +47,6 @@ export type ToastProviderProps = {
60
47
  dismissLabel?: string
61
48
  }
62
49
 
63
- let nextId = 1
64
- const GAP = 8
65
- const PEEK = 10
66
- // px per second: a toast thrown sideways this fast goes, however short the throw.
67
- const THROW_SPEED = 600
68
-
69
50
  export function ToastProvider({ children, visible = 3, label = 'Notifications', dismissLabel = 'Dismiss' }: ToastProviderProps) {
70
51
  const [items, setItems] = useState<Item[]>([])
71
52
  const [heights, setHeights] = useState<Record<number, number>>({})
@@ -82,9 +63,7 @@ export function ToastProvider({ children, visible = 3, label = 'Notifications',
82
63
  }, [])
83
64
 
84
65
  const say = (opts: Partial<ToastOptions>, variant?: ToastVariant) => {
85
- const text = `${opts.title}${opts.description ? `. ${opts.description}` : ''}`
86
- // The same text twice is not read again, so a trailing space makes it change.
87
- const fresh = (prev: string) => (prev === text ? `${text} ` : text)
66
+ const fresh = (prev: string) => toastSay(prev, opts)
88
67
  if (variant === 'error') setUrgent(fresh)
89
68
  else setAnnounce(fresh)
90
69
  }
@@ -102,7 +81,7 @@ export function ToastProvider({ children, visible = 3, label = 'Notifications',
102
81
  const api = useMemo<Api>(
103
82
  () => ({
104
83
  toast: (opts) => {
105
- const id = nextId++
84
+ const id = toastId()
106
85
  setItems((list) => [...list, { duration: 5000, variant: 'default', ...opts, id, leaving: false }])
107
86
  say(opts, opts.variant)
108
87
  return id
@@ -116,18 +95,7 @@ export function ToastProvider({ children, visible = 3, label = 'Notifications',
116
95
  [remove],
117
96
  )
118
97
 
119
- const live = items.filter((t) => !t.leaving)
120
- const order = [...items].reverse()
121
- let offset = 0
122
- const layout = order.map((t) => {
123
- const liveIndex = live.length - 1 - live.findIndex((x) => x.id === t.id)
124
- const index = t.leaving ? -1 : liveIndex
125
- const y = offset
126
- if (!t.leaving) offset += (heights[t.id] ?? 64) + GAP
127
- return { t, index, y }
128
- })
129
- const frontHeight = live.length ? heights[live[live.length - 1].id] ?? 64 : 0
130
- const stackHeight = expanded ? offset : frontHeight + Math.min(live.length - 1, visible - 1) * PEEK
98
+ const { layout, frontHeight, height } = toastLayout(items, heights, expanded, visible)
131
99
 
132
100
  return (
133
101
  <ToastCtx.Provider value={api}>
@@ -139,7 +107,7 @@ export function ToastProvider({ children, visible = 3, label = 'Notifications',
139
107
  // Toasts are not part of an open dialog, but tapping one should not close it.
140
108
  data-gooee-layer-ignore=""
141
109
  aria-label={label}
142
- style={{ height: live.length ? stackHeight : 0, ['--front-h' as string]: `${frontHeight}px` }}
110
+ style={{ height, ['--front-h' as string]: `${frontHeight}px` }}
143
111
  onPointerEnter={(e) => e.pointerType === 'mouse' && setExpanded(true)}
144
112
  onPointerLeave={(e) => e.pointerType === 'mouse' && setExpanded(false)}
145
113
  onFocus={() => setExpanded(true)}
@@ -184,186 +152,36 @@ const icons: Record<ToastVariant, ReactNode> = {
184
152
  loading: <Spinner size="sm" label={null} />,
185
153
  }
186
154
 
187
- const onScreen = (r: DOMRect) => r.width > 0 && r.bottom > 0 && r.right > 0 && r.top < window.innerHeight && r.left < window.innerWidth
188
- const gapBetween = (a: DOMRect, b: DOMRect) => Math.hypot(Math.max(0, a.left - b.right, b.left - a.right), Math.max(0, a.top - b.bottom, b.top - a.bottom))
189
- // An origin further than this from the toast would send the drop across the screen.
190
- const NEAR = 240
191
-
192
- /**
193
- * Bud: a drop buds out of `origin`, hangs on a neck that breaks, and grows into the toast (`way` in),
194
- * or the toast flows back into it (out). With no origin, or one far away, the drop buds out of the
195
- * window's edge nearest the stack, under the toast. An SVG over the page draws it; the toast itself
196
- * shows only at the end. Returns a stop, or null when motion is reduced.
197
- */
198
- function bud(toast: HTMLElement, origin: ToastOptions['origin'], way: 'in' | 'out') {
199
- const ms = motionFor(way === 'in' ? 'morph' : 'morph-out')
200
- if (!ms) return null
201
- const t = toast.getBoundingClientRect()
202
- const look = getComputedStyle(toast)
203
- const line = parseFloat(look.borderTopWidth) || 0
204
- const T: Box = { cx: t.left + t.width / 2, cy: t.top + t.height / 2, hw: t.width / 2, hh: t.height / 2, r: Math.min(t.height / 2, parseFloat(look.borderTopLeftRadius) || 0) }
205
- let O: Box | null = null
206
- let exit: Pt = [0, 0]
207
- let n: Pt = [0, 1]
208
- let rd = BEND + 3.5
209
- let cut = false
210
- const o = origin instanceof HTMLElement ? (origin.isConnected ? origin.getBoundingClientRect() : null) : origin ? new DOMRect(origin.x, origin.y, 1, 1) : null
211
- const near = !!o && onScreen(o) && gapBetween(o, t) <= NEAR
212
- if (near && origin instanceof HTMLElement) {
213
- const h = Math.min(o.width, o.height) / 2
214
- O = { cx: o.left + o.width / 2, cy: o.top + o.height / 2, hw: o.width / 2, hh: o.height / 2, r: Math.min(h, parseFloat(getComputedStyle(origin).borderTopLeftRadius) || 0) }
215
- rd = Math.min(rd, Math.max(BEND + 0.5, h * 0.6))
216
- ;({ at: exit, n } = exitToward(O, T, rd))
217
- cut = true
218
- } else if (near && origin && !(origin instanceof HTMLElement)) exit = [origin.x, origin.y]
219
- // Too close to hang a drop between them, or no origin to use: it buds out of the window's edge. The
220
- // edge is a body just past the screen, its top a stroke's width out, so only the drop and its neck show.
221
- if (!near || sdBox(exit[0], exit[1], T) < rd * 4) {
222
- const down = T.cy > window.innerHeight / 2 ? 1 : -1
223
- rd = BEND + 3.5
224
- const y = down > 0 ? window.innerHeight + line : -line
225
- O = { cx: T.cx, cy: y + down * rd * 2, hw: rd * 6, hh: rd * 2, r: rd * 2 }
226
- exit = [T.cx, y]
227
- n = [0, -down]
228
- cut = false
229
- }
230
- const { svg, paths } = layer(document.body, styles.bud, null)
231
- const path = paths[0]
232
- // The toast's own fill, edge and shadow. The edge is drawn on the outline, so the outline sits half
233
- // its width inside the toast's box, and the last frame lands on the box exactly.
234
- path.style.fill = look.backgroundColor
235
- path.style.stroke = look.borderTopColor
236
- path.style.strokeWidth = `${line}px`
237
- castShadow(svg, shadowOf(toast).shadows)
238
- const inset = line / 2
239
- const T2: Box = { cx: T.cx, cy: T.cy, hw: T.hw - inset, hh: T.hh - inset, r: Math.max(0, T.r - inset) }
240
- // The origin keeps its own look: the drawing is cut out where it is.
241
- if (O && cut) {
242
- const ns = 'http://www.w3.org/2000/svg'
243
- const clip = svg.appendChild(document.createElementNS(ns, 'clipPath'))
244
- const id = `gooee-bud-${Math.random().toString(36).slice(2)}`
245
- clip.id = id
246
- const hole = clip.appendChild(document.createElementNS(ns, 'path'))
247
- const r = Math.min(O.r + 1, O.hh + 1)
248
- hole.setAttribute('clip-rule', 'evenodd')
249
- hole.setAttribute(
250
- 'd',
251
- `M-1 -1H${window.innerWidth + 1}V${window.innerHeight + 1}H-1Z` +
252
- `M${O.cx - O.hw - 1 + r} ${O.cy - O.hh - 1}H${O.cx + O.hw + 1 - r}A${r} ${r} 0 0 1 ${O.cx + O.hw + 1} ${O.cy - O.hh - 1 + r}V${O.cy + O.hh + 1 - r}A${r} ${r} 0 0 1 ${O.cx + O.hw + 1 - r} ${O.cy + O.hh + 1}H${O.cx - O.hw - 1 + r}A${r} ${r} 0 0 1 ${O.cx - O.hw - 1} ${O.cy + O.hh + 1 - r}V${O.cy - O.hh - 1 + r}A${r} ${r} 0 0 1 ${O.cx - O.hw - 1 + r} ${O.cy - O.hh - 1}Z`,
253
- )
254
- path.setAttribute('clip-path', `url(#${id})`)
255
- }
256
- const e = morphEase()
257
- const recoil = motionFor('fast')
258
- const pin = pincher(e, recoil)
259
- const across: [number, number] = [n[1], n[0]]
260
- const anchor: Pt = O ? (n[0] === 0 ? [exit[0], O.cy] : [O.cx, exit[1]]) : exit
261
- // Once the drop is free it is never joined again, even where the toast sits close to the edge: going
262
- // in, after the broken neck has sprung back; going out, until the drop hangs again.
263
- let broke = 0
264
- // The toast shows once the drop has its shape: at the end going in, at the start going out.
265
- const show = toast.animate({ opacity: way === 'in' ? [0, 0, 1] : [1, 0, 0], offset: way === 'in' ? [0, 0.82, 1] : [0, 0.15, 1] }, { duration: ms, fill: 'forwards' })
266
- const stop = frames(
267
- ms,
268
- (k, now) => {
269
- const u = way === 'in' ? k : 1 - k
270
- const D = budScene(e, u, O, T2, exit, n, rd)
271
- let sdf = body(D).sdf
272
- if (O) {
273
- const free = way === 'in' ? (broke && now - broke >= recoil) || u >= 0.6 : u >= 0.4
274
- if (free) {
275
- const a = body(O).sdf
276
- const b = sdf
277
- sdf = (x, y) => Math.min(a(x, y), b(x, y))
278
- } else {
279
- const p = pin(body(O, anchor), body(D), now)
280
- if (p.pieces === 2 && !broke) broke = now
281
- sdf = p.sdf
282
- }
283
- }
284
- const seeds: Array<[number, number, number, number]> = [[D.cx, D.cy, ...across]]
285
- if (O) seeds.unshift([anchor[0], anchor[1], ...across])
286
- path.setAttribute('d', loopsPath(trace(sdf, seeds)))
287
- // A drop from a point fades in where it starts.
288
- if (!O) svg.style.opacity = String(Math.min(1, u / 0.1))
289
- },
290
- () => {
291
- svg.remove()
292
- if (way === 'in') show.cancel()
293
- },
294
- )
295
- return () => {
296
- stop()
297
- svg.remove()
298
- show.cancel()
299
- }
300
- }
301
-
302
155
  function ToastItem({ item, index, y, expanded, hidden, onHeight, onDismiss, dismissLabel }: ItemProps) {
303
156
  const ref = useRef<HTMLLIElement>(null)
304
- const swipe = useRef<{ x: number; dx: number; speed: ReturnType<typeof speedTracker> } | null>(null)
305
157
  const [dragging, setDragging] = useState(false)
306
158
  const [hover, setHover] = useState(false)
307
- // Where the toast is drawn sideways, set straight on --dx so a glide does not render every frame.
308
- const at = useRef(0)
309
- const stop = useRef(() => {})
310
- useEffect(() => () => stop.current(), [])
311
- const paint = (v: number) => {
312
- at.current = v
313
- ref.current?.style.setProperty('--dx', `${v}px`)
314
- }
315
- const fling = (to: number, speed: number) => {
316
- const el = ref.current
317
- stop.current()
318
- el?.setAttribute('data-fluid', '')
319
- stop.current = release(at.current, to, speed, paint, () => {
320
- if (to === 0) el?.removeAttribute('data-fluid')
321
- })
322
- }
159
+ const latest = useRef({ item, onDismiss })
160
+ latest.current = { item, onDismiss }
161
+ // Thrown sideways, it flies off at the speed of the swipe (motion/toast.ts).
162
+ const swipeRef = useRef<ReturnType<typeof createToastSwipe>>(null)
163
+ swipeRef.current ??= createToastSwipe(() => ref.current, {
164
+ leaving: () => latest.current.item.leaving !== false,
165
+ dragging: setDragging,
166
+ dismiss: (way) => latest.current.onDismiss(way),
167
+ })
168
+ const swipe = swipeRef.current
169
+ useEffect(() => () => swipe.stop(), [swipe])
323
170
 
324
- // A drop buds out of the origin, or out of the window's edge, and grows into the toast; a dismiss
325
- // flows back the same way. It plays once going in and once going out; going out it runs on after the
326
- // toast is gone, so only an entry is stopped on unmount. Only the front toast flows out: one behind
327
- // it folds away under it, and one thrown sideways keeps its throw.
328
- const budding = useRef<false | 'in' | 'out'>(false)
329
- const stopBud = useRef<(() => void) | null>(null)
330
- const front = useRef(false)
171
+ // A drop buds out of the origin, or out of the window's edge, and grows into the toast; a dismiss flows back the same way.
172
+ const budRef = useRef<ReturnType<typeof createToastBud>>(null)
173
+ budRef.current ??= createToastBud(() => ref.current, () => latest.current.item.origin, styles.bud)
174
+ const budding = budRef.current
331
175
  useLayoutEffect(() => {
332
- if (!item.leaving) front.current = index === 0
176
+ if (!item.leaving) budding.place(index)
333
177
  })
334
- useLayoutEffect(() => {
335
- const el = ref.current
336
- if (!el) return
337
- const thrown = item.leaving && item.leaving !== 'down'
338
- if (thrown || (item.leaving && !front.current)) {
339
- if (budding.current === 'in') stopBud.current?.()
340
- budding.current = false
341
- if (!thrown) delete el.dataset.liquid
342
- return
343
- }
344
- stopBud.current?.()
345
- const way = item.leaving ? 'out' : 'in'
346
- // The bud stands in for the part's CSS entry and exit; marked first, so the toast is measured in its place.
347
- el.dataset.liquid = ''
348
- stopBud.current = bud(el, item.origin, way)
349
- budding.current = stopBud.current ? way : false
350
- }, [item.leaving])
351
- useEffect(
352
- () => () => {
353
- if (budding.current === 'in') stopBud.current?.()
354
- },
355
- [],
356
- )
178
+ useLayoutEffect(() => budding.change(item.leaving), [item.leaving])
179
+ useEffect(() => () => budding.destroy(), [budding])
357
180
 
358
181
  useLayoutEffect(() => {
359
182
  const el = ref.current
360
183
  if (!el) return
361
- // scrollHeight is the natural height, even while clamped behind the front toast
362
- const measure = () => onHeight(el.scrollHeight + el.offsetHeight - el.clientHeight)
363
- const ro = new ResizeObserver(measure)
364
- ro.observe(el)
365
- measure()
366
- return () => ro.disconnect()
184
+ return watchToastHeight(el, onHeight)
367
185
  }, [])
368
186
 
369
187
  const paused = expanded || hover || dragging || item.leaving !== false
@@ -373,58 +191,10 @@ function ToastItem({ item, index, y, expanded, hidden, onHeight, onDismiss, dism
373
191
  }, [item.duration, item.variant])
374
192
  useEffect(() => {
375
193
  if (paused || !Number.isFinite(remaining.current) || item.variant === 'loading') return
376
- let start = performance.now()
377
- let t = window.setTimeout(() => onDismiss('down'), remaining.current)
378
- const onVis = () => {
379
- if (document.hidden) {
380
- window.clearTimeout(t)
381
- remaining.current -= performance.now() - start
382
- } else {
383
- start = performance.now()
384
- t = window.setTimeout(() => onDismiss('down'), remaining.current)
385
- }
386
- }
387
- document.addEventListener('visibilitychange', onVis)
388
- return () => {
389
- window.clearTimeout(t)
390
- if (!document.hidden) remaining.current -= performance.now() - start
391
- document.removeEventListener('visibilitychange', onVis)
392
- }
194
+ return toastTimer(remaining, () => onDismiss('down'))
393
195
  }, [paused, item.variant, item.duration])
394
196
 
395
- const onPointerDown = (e: PointerEvent<HTMLLIElement>) => {
396
- if ((e.target as HTMLElement).closest('button') || item.leaving) return
397
- stop.current()
398
- // Caught mid-glide, it carries on from where it is.
399
- swipe.current = { x: e.clientX - at.current, dx: at.current, speed: speedTracker() }
400
- swipe.current.speed.add(at.current)
401
- e.currentTarget.setPointerCapture(e.pointerId)
402
- setDragging(true)
403
- }
404
- const onPointerMove = (e: PointerEvent<HTMLLIElement>) => {
405
- const s = swipe.current
406
- if (!s) return
407
- s.dx = e.clientX - s.x
408
- s.speed.add(s.dx)
409
- paint(s.dx)
410
- }
411
- const onPointerUp = (e: PointerEvent<HTMLLIElement>) => {
412
- const s = swipe.current
413
- swipe.current = null
414
- setDragging(false)
415
- if (!s) return
416
- const speed = e.type === 'pointercancel' ? 0 : s.speed.speed()
417
- const thrown = Math.abs(speed) > THROW_SPEED && Math.abs(s.dx) > 20
418
- if (e.type !== 'pointercancel' && (Math.abs(s.dx) > 80 || thrown)) {
419
- // It leaves the way it was thrown, or the way it was pulled when let go slowly.
420
- const way = Math.sign(thrown ? speed : s.dx)
421
- fling(way * (ref.current?.offsetWidth ?? 0) * 1.1, speed)
422
- onDismiss(way < 0 ? 'left' : 'right')
423
- } else fling(0, speed)
424
- }
425
-
426
- const stackedY = expanded ? -y : -Math.min(index, 3) * PEEK
427
- const scale = expanded ? 1 : 1 - Math.min(index, 3) * 0.05
197
+ const { y: stackedY, scale } = toastStack(index, y, expanded)
428
198
 
429
199
  return (
430
200
  <li
@@ -443,10 +213,10 @@ function ToastItem({ item, index, y, expanded, hidden, onHeight, onDismiss, dism
443
213
  inert={hidden || undefined}
444
214
  onPointerEnter={() => setHover(true)}
445
215
  onPointerLeave={() => setHover(false)}
446
- onPointerDown={onPointerDown}
447
- onPointerMove={onPointerMove}
448
- onPointerUp={onPointerUp}
449
- onPointerCancel={onPointerUp}
216
+ onPointerDown={swipe.down}
217
+ onPointerMove={swipe.move}
218
+ onPointerUp={swipe.up}
219
+ onPointerCancel={swipe.up}
450
220
  >
451
221
  {icons[item.variant ?? 'default'] && <span className={styles.icon} data-variant={item.variant}>{icons[item.variant ?? 'default']}</span>}
452
222
  <div className={styles.text}>
@@ -0,0 +1,138 @@
1
+ <script lang="ts">
2
+ import { inject, type InjectionKey } from 'vue'
3
+ import type { ToastLeaving, ToastOrigin, ToastVariant } from '../../internal/motion/toast'
4
+
5
+ export type { ToastVariant }
6
+
7
+ export type ToastOptions = {
8
+ title: string
9
+ description?: string
10
+ variant?: ToastVariant
11
+ /** Milliseconds before it leaves. Infinity keeps it until dismissed. */
12
+ duration?: number
13
+ action?: { label: string; onClick: () => void }
14
+ /** Where the action happened: the element pressed, or a point on screen. Within 240px of the toast, the drop buds out of it and a dismiss flows back into it. Further away, or without it, the drop buds out of the window's edge. */
15
+ origin?: ToastOrigin
16
+ }
17
+
18
+ export type ToastItemData = ToastOptions & { id: number; leaving: ToastLeaving }
19
+
20
+ export type ToastApi = {
21
+ toast: (opts: ToastOptions) => number
22
+ update: (id: number, opts: Partial<ToastOptions>) => void
23
+ dismiss: (id: number) => void
24
+ }
25
+
26
+ /** Lets a second Vue app on the page send toasts through one provider: provide(toastKey, useToast()). */
27
+ export const toastKey: InjectionKey<ToastApi> = Symbol('gooee-toast')
28
+
29
+ /** Sends toasts from anywhere inside a ToastProvider (this file's default export). */
30
+ export function useToast(): ToastApi {
31
+ const c = inject(toastKey, null)
32
+ if (!c) throw new Error('useToast needs a <ToastProvider> above it')
33
+ return c
34
+ }
35
+ </script>
36
+
37
+ <script setup lang="ts">
38
+ import { computed, onBeforeUnmount, provide, ref, shallowRef } from 'vue'
39
+ import { motionMs } from '../../internal/motion'
40
+ import { toastId, toastLayout, toastSay } from '../../internal/motion/toast'
41
+ import ToastItem from './ToastItem.vue'
42
+ import styles from './Toast.module.css'
43
+
44
+ // The Vue ToastProvider: the same look (Toast.module.css), stack, bud and throw as the React one.
45
+ // Wrap your app in it; anything inside can call useToast().
46
+
47
+ const props = withDefaults(
48
+ defineProps<{
49
+ /** How many toasts stay visible in the stack. */
50
+ visible?: number
51
+ /** Name of the notification region for screen readers. */
52
+ label?: string
53
+ /** Screen reader name for each close button. The toast title is added after it. */
54
+ dismissLabel?: string
55
+ }>(),
56
+ { visible: 3, label: 'Notifications', dismissLabel: 'Dismiss' },
57
+ )
58
+
59
+ const items = shallowRef<ToastItemData[]>([])
60
+ const heights = shallowRef<Record<number, number>>({})
61
+ const expanded = ref(false)
62
+ const announce = ref('')
63
+ const urgent = ref('')
64
+ const timers = new Set<number>()
65
+ onBeforeUnmount(() => timers.forEach((t) => window.clearTimeout(t)))
66
+
67
+ const say = (opts: Partial<ToastOptions>, variant?: ToastVariant) => {
68
+ if (variant === 'error') urgent.value = toastSay(urgent.value, opts)
69
+ else announce.value = toastSay(announce.value, opts)
70
+ }
71
+
72
+ const remove = (id: number, dir: ToastLeaving = 'down') => {
73
+ items.value = items.value.map((t) => (t.id === id && !t.leaving ? { ...t, leaving: dir } : t))
74
+ const timer = window.setTimeout(() => {
75
+ timers.delete(timer)
76
+ items.value = items.value.filter((t) => t.id !== id)
77
+ const { [id]: _, ...rest } = heights.value
78
+ heights.value = rest
79
+ }, motionMs('morph-out'))
80
+ timers.add(timer)
81
+ }
82
+
83
+ provide(toastKey, {
84
+ toast: (opts) => {
85
+ const id = toastId()
86
+ items.value = [...items.value, { duration: 5000, variant: 'default', ...opts, id, leaving: false }]
87
+ say(opts, opts.variant)
88
+ return id
89
+ },
90
+ update: (id, opts) => {
91
+ const variant = items.value.find((t) => t.id === id)?.variant
92
+ items.value = items.value.map((t) => (t.id === id ? { ...t, ...opts } : t))
93
+ if (opts.title) say(opts, opts.variant ?? variant)
94
+ },
95
+ dismiss: (id) => remove(id),
96
+ })
97
+
98
+ const stack = computed(() => toastLayout(items.value, heights.value, expanded.value, props.visible))
99
+ const setHeight = (id: number, h: number) => {
100
+ if (heights.value[id] !== h) heights.value = { ...heights.value, [id]: h }
101
+ }
102
+ const onFocusOut = (e: FocusEvent) => !(e.currentTarget as HTMLElement).contains(e.relatedTarget as Node | null) && (expanded.value = false)
103
+ </script>
104
+
105
+ <template>
106
+ <slot />
107
+ <!-- In the body, so a parent's stacking or overflow never hides the stack. -->
108
+ <Teleport to="body">
109
+ <div :class="styles.srOnly" role="status" aria-live="polite" aria-atomic="true">{{ announce }}</div>
110
+ <div :class="styles.srOnly" role="alert" aria-atomic="true">{{ urgent }}</div>
111
+ <!-- Toasts are not part of an open dialog, but tapping one should not close it. -->
112
+ <section
113
+ :class="styles.viewport"
114
+ data-gooee-layer-ignore=""
115
+ :aria-label="label"
116
+ :style="{ height: `${stack.height}px`, '--front-h': `${stack.frontHeight}px` }"
117
+ @pointerenter="(e: PointerEvent) => e.pointerType === 'mouse' && (expanded = true)"
118
+ @pointerleave="(e: PointerEvent) => e.pointerType === 'mouse' && (expanded = false)"
119
+ @focusin="expanded = true"
120
+ @focusout="onFocusOut"
121
+ >
122
+ <ol :class="styles.list">
123
+ <ToastItem
124
+ v-for="{ t, index, y } in stack.layout"
125
+ :key="t.id"
126
+ :item="t"
127
+ :index="index"
128
+ :y="y"
129
+ :expanded="expanded"
130
+ :hidden="index >= props.visible"
131
+ :dismiss-label="dismissLabel"
132
+ @height="(h: number) => setHeight(t.id, h)"
133
+ @dismiss="(dir: ToastLeaving) => remove(t.id, dir)"
134
+ />
135
+ </ol>
136
+ </section>
137
+ </Teleport>
138
+ </template>
@@ -0,0 +1,108 @@
1
+ <script setup lang="ts">
2
+ import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue'
3
+ import { createToastBud, createToastSwipe, toastStack, toastTimer, watchToastHeight, type ToastLeaving } from '../../internal/motion/toast'
4
+ import { CheckCircleIcon, ErrorIcon, InfoIcon, XIcon } from '../../internal/vueIcons'
5
+ import Spinner from '../Spinner/Spinner.vue'
6
+ import type { ToastItemData } from './Toast.vue'
7
+ import styles from './Toast.module.css'
8
+
9
+ // One toast of the Vue ToastProvider (Toast.vue).
10
+
11
+ const props = defineProps<{ item: ToastItemData; index: number; y: number; expanded: boolean; hidden: boolean; dismissLabel: string }>()
12
+ const emit = defineEmits<{ height: [h: number]; dismiss: [dir: ToastLeaving] }>()
13
+
14
+ const el = ref<HTMLElement | null>(null)
15
+ const dragging = ref(false)
16
+ const hover = ref(false)
17
+ const dismiss = (dir: ToastLeaving) => emit('dismiss', dir)
18
+
19
+ // Thrown sideways, it flies off at the speed of the swipe (motion/toast.ts).
20
+ const swipe = createToastSwipe(() => el.value, {
21
+ leaving: () => props.item.leaving !== false,
22
+ dragging: (on) => (dragging.value = on),
23
+ dismiss,
24
+ })
25
+
26
+ // A drop buds out of the origin, or out of the window's edge, and grows into the toast; a dismiss flows back the same way.
27
+ const bud = createToastBud(
28
+ () => el.value,
29
+ () => props.item.origin,
30
+ styles.bud,
31
+ )
32
+ let stopHeight = () => {}
33
+ onMounted(() => {
34
+ bud.place(props.index)
35
+ bud.change(props.item.leaving)
36
+ stopHeight = watchToastHeight(el.value!, (h) => emit('height', h))
37
+ })
38
+ watch(
39
+ () => props.index,
40
+ (i) => !props.item.leaving && bud.place(i),
41
+ { flush: 'post' },
42
+ )
43
+ watch(
44
+ () => props.item.leaving,
45
+ (leaving) => bud.change(leaving),
46
+ { flush: 'post' },
47
+ )
48
+
49
+ // It leaves on its own, but waits while hovered, focused, held or hidden.
50
+ const paused = computed(() => props.expanded || hover.value || dragging.value || props.item.leaving !== false)
51
+ const remaining = { current: props.item.duration ?? 5000 }
52
+ watch([() => props.item.duration, () => props.item.variant], () => (remaining.current = props.item.duration ?? 5000))
53
+ watch(
54
+ [paused, () => props.item.variant, () => props.item.duration],
55
+ (_, __, onCleanup) => {
56
+ if (paused.value || !Number.isFinite(remaining.current) || props.item.variant === 'loading' || typeof window === 'undefined') return
57
+ onCleanup(toastTimer(remaining, () => dismiss('down')))
58
+ },
59
+ { immediate: true },
60
+ )
61
+
62
+ onBeforeUnmount(() => {
63
+ swipe.stop()
64
+ bud.destroy()
65
+ stopHeight()
66
+ })
67
+
68
+ const place = computed(() => toastStack(props.index, props.y, props.expanded))
69
+ const onAction = () => {
70
+ props.item.action?.onClick()
71
+ dismiss('down')
72
+ }
73
+ </script>
74
+
75
+ <template>
76
+ <li
77
+ ref="el"
78
+ :class="styles.toast"
79
+ :data-leaving="item.leaving || undefined"
80
+ :data-dragging="dragging || undefined"
81
+ :data-hidden="hidden || undefined"
82
+ :data-behind="(!expanded && index > 0) || undefined"
83
+ :style="{ '--y': `${place.y}px`, '--s': place.scale, zIndex: 100 - Math.max(index, 0) }"
84
+ :aria-hidden="hidden || undefined"
85
+ :inert="hidden || undefined"
86
+ @pointerenter="hover = true"
87
+ @pointerleave="hover = false"
88
+ @pointerdown="swipe.down"
89
+ @pointermove="swipe.move"
90
+ @pointerup="swipe.up"
91
+ @pointercancel="swipe.up"
92
+ >
93
+ <span v-if="item.variant && item.variant !== 'default'" :class="styles.icon" :data-variant="item.variant">
94
+ <CheckCircleIcon v-if="item.variant === 'success'" />
95
+ <ErrorIcon v-else-if="item.variant === 'error'" />
96
+ <InfoIcon v-else-if="item.variant === 'info'" />
97
+ <Spinner v-else size="sm" :label="null" />
98
+ </span>
99
+ <div :class="styles.text">
100
+ <p :class="styles.title">{{ item.title }}</p>
101
+ <p v-if="item.description" :class="styles.description">{{ item.description }}</p>
102
+ </div>
103
+ <button v-if="item.action" type="button" :class="styles.action" @click="onAction">{{ item.action.label }}</button>
104
+ <button type="button" :class="styles.close" :aria-label="`${dismissLabel}: ${item.title}`" @click="dismiss('down')">
105
+ <XIcon />
106
+ </button>
107
+ </li>
108
+ </template>