@thekestongroup/ui 0.2.0 → 0.3.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 (207) hide show
  1. package/README.md +90 -32
  2. package/dist/components/Age/Age.vue.d.ts +19 -0
  3. package/dist/components/AppShell/AppShell.vue.d.ts +108 -0
  4. package/dist/components/Avatar/Avatar.vue.d.ts +34 -0
  5. package/dist/components/AvatarGroup/AvatarGroup.vue.d.ts +25 -0
  6. package/dist/components/Badge/Badge.vue.d.ts +41 -0
  7. package/dist/components/Breadcrumb/Breadcrumb.vue.d.ts +39 -0
  8. package/dist/components/Button/Button.vue.d.ts +69 -0
  9. package/dist/components/Calendar/Calendar.vue.d.ts +34 -0
  10. package/dist/components/Card/Card.vue.d.ts +56 -0
  11. package/dist/components/Checkbox/Checkbox.vue.d.ts +40 -0
  12. package/dist/components/Checklist/Checklist.vue.d.ts +18 -0
  13. package/dist/components/Chip/Chip.vue.d.ts +44 -0
  14. package/dist/components/Divider/Divider.vue.d.ts +36 -0
  15. package/dist/components/{ToastItem.vue.d.ts → FormField/FieldMessage.vue.d.ts} +10 -2
  16. package/dist/components/FormField/FormField.vue.d.ts +61 -0
  17. package/dist/components/Header/Header.vue.d.ts +104 -0
  18. package/dist/components/InfoTip/InfoTip.vue.d.ts +20 -0
  19. package/dist/components/Input/Input.vue.d.ts +56 -0
  20. package/dist/components/InputCode/InputCode.vue.d.ts +53 -0
  21. package/dist/components/InputDate/InputDate.vue.d.ts +49 -0
  22. package/dist/components/InputNumber/InputNumber.vue.d.ts +44 -0
  23. package/dist/components/InputPassword/InputPassword.vue.d.ts +51 -0
  24. package/dist/components/InputRichText/InputRichText.vue.d.ts +44 -0
  25. package/dist/components/Link/Link.vue.d.ts +39 -0
  26. package/dist/components/Notification/Notification.vue.d.ts +62 -0
  27. package/dist/components/Notification/NotificationPane.vue.d.ts +38 -0
  28. package/dist/components/Notification/Toaster.vue.d.ts +51 -0
  29. package/dist/components/Notification/notification.samples.d.ts +3 -0
  30. package/dist/components/Notification/notifications.d.ts +115 -0
  31. package/dist/components/PageHeader/PageHeader.vue.d.ts +43 -0
  32. package/dist/components/Popover/Popover.vue.d.ts +83 -0
  33. package/dist/components/Popover/popover.d.ts +16 -0
  34. package/dist/components/Progress/Progress.vue.d.ts +26 -0
  35. package/dist/components/RadioGroup/RadioGroup.vue.d.ts +45 -0
  36. package/dist/components/RelativeTime/RelativeTime.vue.d.ts +22 -0
  37. package/dist/components/Select/Select.vue.d.ts +110 -0
  38. package/dist/components/Sidebar/Sidebar.vue.d.ts +106 -0
  39. package/dist/components/Sidebar/sidebar.d.ts +10 -0
  40. package/dist/components/Slideover/Slideover.vue.d.ts +125 -0
  41. package/dist/components/Spinner/Spinner.vue.d.ts +30 -0
  42. package/dist/components/StatTile/StatTile.vue.d.ts +46 -0
  43. package/dist/components/SummaryList/SummaryList.vue.d.ts +62 -0
  44. package/dist/components/Switch/Switch.vue.d.ts +39 -0
  45. package/dist/components/Table/Table.vue.d.ts +82 -0
  46. package/dist/components/Table/table.d.ts +36 -0
  47. package/dist/components/Tooltip/Tooltip.vue.d.ts +46 -0
  48. package/dist/index.d.ts +89 -25
  49. package/dist/keston-ui.js +4963 -14785
  50. package/dist/lib/cn.d.ts +4 -0
  51. package/dist/lib/dates.d.ts +27 -0
  52. package/dist/lib/initials.d.ts +2 -0
  53. package/dist/lib/parseDate.d.ts +44 -0
  54. package/dist/lib/useNow.d.ts +3 -0
  55. package/package.json +53 -24
  56. package/src/components/.gitkeep +0 -0
  57. package/src/components/Age/Age.stories.ts +24 -0
  58. package/src/components/Age/Age.vue +56 -0
  59. package/src/components/AppShell/AppShell.stories.ts +124 -0
  60. package/src/components/AppShell/AppShell.vue +212 -0
  61. package/src/components/Avatar/Avatar.stories.ts +56 -0
  62. package/src/components/Avatar/Avatar.vue +118 -0
  63. package/src/components/AvatarGroup/AvatarGroup.stories.ts +47 -0
  64. package/src/components/AvatarGroup/AvatarGroup.vue +97 -0
  65. package/src/components/Badge/Badge.stories.ts +92 -0
  66. package/src/components/Badge/Badge.vue +67 -0
  67. package/src/components/Breadcrumb/Breadcrumb.stories.ts +58 -0
  68. package/src/components/Breadcrumb/Breadcrumb.vue +109 -0
  69. package/src/components/Button/Button.mdx +79 -0
  70. package/src/components/Button/Button.stories.ts +207 -0
  71. package/src/components/Button/Button.vue +266 -0
  72. package/src/components/Calendar/Calendar.stories.ts +51 -0
  73. package/src/components/Calendar/Calendar.vue +364 -0
  74. package/src/components/Card/Card.stories.ts +109 -0
  75. package/src/components/Card/Card.vue +115 -0
  76. package/src/components/Checkbox/Checkbox.stories.ts +102 -0
  77. package/src/components/Checkbox/Checkbox.vue +102 -0
  78. package/src/components/Checklist/Checklist.stories.ts +67 -0
  79. package/src/components/Checklist/Checklist.vue +38 -0
  80. package/src/components/Chip/Chip.stories.ts +71 -0
  81. package/src/components/Chip/Chip.vue +86 -0
  82. package/src/components/Divider/Divider.stories.ts +57 -0
  83. package/src/components/Divider/Divider.vue +58 -0
  84. package/src/components/FormField/FieldMessage.vue +81 -0
  85. package/src/components/FormField/FormField.stories.ts +55 -0
  86. package/src/components/FormField/FormField.vue +160 -0
  87. package/src/components/Header/Header.stories.ts +168 -0
  88. package/src/components/Header/Header.vue +232 -0
  89. package/src/components/InfoTip/InfoTip.stories.ts +63 -0
  90. package/src/components/InfoTip/InfoTip.vue +50 -0
  91. package/src/components/Input/Input.stories.ts +206 -0
  92. package/src/components/Input/Input.vue +90 -0
  93. package/src/components/InputCode/InputCode.stories.ts +85 -0
  94. package/src/components/InputCode/InputCode.vue +145 -0
  95. package/src/components/InputDate/InputDate.stories.ts +58 -0
  96. package/src/components/InputDate/InputDate.vue +258 -0
  97. package/src/components/InputNumber/InputNumber.stories.ts +63 -0
  98. package/src/components/InputNumber/InputNumber.vue +109 -0
  99. package/src/components/InputPassword/InputPassword.stories.ts +105 -0
  100. package/src/components/InputPassword/InputPassword.vue +132 -0
  101. package/src/components/InputRichText/InputRichText.stories.ts +100 -0
  102. package/src/components/InputRichText/InputRichText.vue +194 -0
  103. package/src/components/Link/Link.stories.ts +73 -0
  104. package/src/components/Link/Link.vue +56 -0
  105. package/src/components/Notification/Notification.stories.ts +196 -0
  106. package/src/components/Notification/Notification.vue +136 -0
  107. package/src/components/Notification/NotificationPane.vue +187 -0
  108. package/src/components/Notification/Toaster.vue +446 -0
  109. package/src/components/Notification/notification.samples.ts +45 -0
  110. package/src/components/Notification/notifications.test.ts +99 -0
  111. package/src/components/Notification/notifications.ts +172 -0
  112. package/src/components/PageHeader/PageHeader.stories.ts +79 -0
  113. package/src/components/PageHeader/PageHeader.vue +66 -0
  114. package/src/components/Popover/Popover.stories.ts +135 -0
  115. package/src/components/Popover/Popover.vue +192 -0
  116. package/src/components/Popover/popover.ts +20 -0
  117. package/src/components/Progress/Progress.stories.ts +74 -0
  118. package/src/components/Progress/Progress.vue +75 -0
  119. package/src/components/RadioGroup/RadioGroup.stories.ts +90 -0
  120. package/src/components/RadioGroup/RadioGroup.vue +132 -0
  121. package/src/components/RelativeTime/RelativeTime.stories.ts +74 -0
  122. package/src/components/RelativeTime/RelativeTime.vue +51 -0
  123. package/src/components/Select/Select.mdx +202 -0
  124. package/src/components/Select/Select.stories.ts +223 -0
  125. package/src/components/Select/Select.vue +630 -0
  126. package/src/components/Sidebar/Sidebar.stories.ts +192 -0
  127. package/src/components/Sidebar/Sidebar.vue +382 -0
  128. package/src/components/Sidebar/sidebar.ts +20 -0
  129. package/src/components/Slideover/Slideover.stories.ts +225 -0
  130. package/src/components/Slideover/Slideover.vue +365 -0
  131. package/src/components/Spinner/Spinner.stories.ts +79 -0
  132. package/src/components/Spinner/Spinner.vue +97 -0
  133. package/src/components/StatTile/StatTile.stories.ts +75 -0
  134. package/src/components/StatTile/StatTile.vue +89 -0
  135. package/src/components/SummaryList/SummaryList.stories.ts +103 -0
  136. package/src/components/SummaryList/SummaryList.vue +127 -0
  137. package/src/components/Switch/Switch.stories.ts +100 -0
  138. package/src/components/Switch/Switch.vue +96 -0
  139. package/src/components/Table/Table.stories.ts +206 -0
  140. package/src/components/Table/Table.vue +220 -0
  141. package/src/components/Table/table.test.ts +65 -0
  142. package/src/components/Table/table.ts +70 -0
  143. package/src/components/Tooltip/Tooltip.stories.ts +57 -0
  144. package/src/components/Tooltip/Tooltip.vue +86 -0
  145. package/src/examples/AppPages.stories.ts +500 -0
  146. package/src/examples/FormLayout.stories.ts +185 -0
  147. package/src/examples/KestonIdPages.stories.ts +415 -0
  148. package/src/examples/SchoolPages.stories.ts +665 -0
  149. package/src/index.ts +114 -36
  150. package/src/lib/cn.ts +4 -0
  151. package/src/lib/dates.test.ts +68 -0
  152. package/src/lib/dates.ts +86 -0
  153. package/src/lib/initials.ts +8 -0
  154. package/src/lib/parseDate.test.ts +331 -0
  155. package/src/lib/parseDate.ts +561 -0
  156. package/src/lib/useNow.ts +20 -0
  157. package/src/styles/theme.css +101 -0
  158. package/dist/components/AppLauncher.vue.d.ts +0 -10
  159. package/dist/components/Avatar.vue.d.ts +0 -16
  160. package/dist/components/Button.vue.d.ts +0 -27
  161. package/dist/components/Checkbox.vue.d.ts +0 -18
  162. package/dist/components/CloseButton.vue.d.ts +0 -6
  163. package/dist/components/Dialog.vue.d.ts +0 -23
  164. package/dist/components/Drawer.vue.d.ts +0 -34
  165. package/dist/components/DropdownMenu.vue.d.ts +0 -21
  166. package/dist/components/DropdownMenuItem.vue.d.ts +0 -19
  167. package/dist/components/DropdownMenuSeparator.vue.d.ts +0 -6
  168. package/dist/components/Input.vue.d.ts +0 -14
  169. package/dist/components/Label.vue.d.ts +0 -16
  170. package/dist/components/NotificationsMenu.vue.d.ts +0 -3
  171. package/dist/components/Popover.vue.d.ts +0 -23
  172. package/dist/components/ProfileSwitcher.vue.d.ts +0 -24
  173. package/dist/components/Select.vue.d.ts +0 -28
  174. package/dist/components/Separator.vue.d.ts +0 -11
  175. package/dist/components/ToastRenderer.vue.d.ts +0 -3
  176. package/dist/components/Toaster.vue.d.ts +0 -3
  177. package/dist/components/Tooltip.vue.d.ts +0 -23
  178. package/dist/components/TopBar.vue.d.ts +0 -34
  179. package/dist/favicon-old.svg +0 -1
  180. package/dist/favicon.svg +0 -1
  181. package/dist/keston-ui.cjs +0 -15
  182. package/dist/lib/toast.d.ts +0 -29
  183. package/dist/lib/types.d.ts +0 -5
  184. package/src/components/AppLauncher.vue +0 -63
  185. package/src/components/Avatar.vue +0 -51
  186. package/src/components/Button.vue +0 -54
  187. package/src/components/Checkbox.vue +0 -47
  188. package/src/components/CloseButton.vue +0 -24
  189. package/src/components/Dialog.vue +0 -65
  190. package/src/components/Drawer.vue +0 -202
  191. package/src/components/DropdownMenu.vue +0 -41
  192. package/src/components/DropdownMenuItem.vue +0 -28
  193. package/src/components/DropdownMenuSeparator.vue +0 -12
  194. package/src/components/Input.vue +0 -21
  195. package/src/components/Label.vue +0 -15
  196. package/src/components/NotificationsMenu.vue +0 -115
  197. package/src/components/Popover.vue +0 -46
  198. package/src/components/ProfileSwitcher.vue +0 -113
  199. package/src/components/Select.vue +0 -97
  200. package/src/components/Separator.vue +0 -30
  201. package/src/components/ToastItem.vue +0 -158
  202. package/src/components/ToastRenderer.vue +0 -113
  203. package/src/components/Toaster.vue +0 -11
  204. package/src/components/Tooltip.vue +0 -48
  205. package/src/components/TopBar.vue +0 -52
  206. package/src/lib/toast.ts +0 -95
  207. package/src/lib/types.ts +0 -5
@@ -0,0 +1,446 @@
1
+ <script setup lang="ts">
2
+ import { computed, nextTick, onBeforeUnmount, onMounted, reactive, ref, watch, type ComponentPublicInstance, type HTMLAttributes } from 'vue'
3
+ import { ToastProvider, ToastRoot, ToastViewport } from 'reka-ui'
4
+ import { cn } from '../../lib/cn'
5
+ import Notification from './Notification.vue'
6
+ import { useNotifications, type Notifications } from './notifications'
7
+
8
+ /**
9
+ * Shows new notifications as toasts: cards that slide in at the edge of the
10
+ * screen and close by themselves. Put one in the app (e.g. beside AppShell),
11
+ * then call `notify()` from anywhere:
12
+ *
13
+ * const { notify } = useNotifications()
14
+ * notify({ type: 'success', title: 'Saved' })
15
+ *
16
+ * New toasts slide in from the bottom (top for top-right) and push the
17
+ * others along smoothly. Toasts that don't fit in `maxHeight` collapse into
18
+ * a "+5 more" pill on top of the stack saying how many aren't in view (they
19
+ * shrink into it, and grow out of it again); hovering the toasts, tapping the pill, or
20
+ * keyboard focus in them (F8), spreads them all out.
21
+ *
22
+ * If there are more than fit on the screen spread out, the oldest wait, and
23
+ * a "+3 more"
24
+ * row at the end opens NotificationPane, which has them all.
25
+ *
26
+ * Built on Reka UI's Toast: screen readers hear each one (errors and
27
+ * warnings straight away, others when they're idle), timers pause while the
28
+ * pointer is over them or the window isn't focused, and they swipe away.
29
+ *
30
+ * Closing a toast leaves the notification in the list (NotificationPane).
31
+ */
32
+ export interface ToasterProps {
33
+ /** The list to show. Default the shared one from `useNotifications()`. */
34
+ notifications?: Notifications
35
+ /** Where they appear. top-right sits under the 48px Header. */
36
+ position?: 'bottom-right' | 'top-right' | 'bottom-center'
37
+ /**
38
+ * How tall the toasts can be, spread out, before older ones collapse into
39
+ * a stack (in px). The newest always shows in full.
40
+ */
41
+ maxHeight?: number
42
+ /** The most toasts kept at once; older ones beyond this wait until one closes. */
43
+ limit?: number
44
+ class?: HTMLAttributes['class']
45
+ }
46
+
47
+ const props = withDefaults(defineProps<ToasterProps>(), {
48
+ position: 'bottom-right',
49
+ maxHeight: 320,
50
+ limit: 10
51
+ })
52
+
53
+ const GAP = 8 // between spread-out toasts
54
+ const INTO_MORE = 0.5 // toasts going into "+N more" shrink to this, and grow from it coming out
55
+ const MORE = 36 // the "+3 more" row when spread out (h-9)
56
+ const PILL = 28 // the "+5 more" pill on the collapsed stack (h-7)
57
+ // Space kept clear: the 48px Header and 16px below it, and 16px at the far edge.
58
+ const SCREEN_MARGIN = 80
59
+
60
+ const emit = defineEmits<{ more: [] }>()
61
+
62
+ const store = computed(() => props.notifications ?? useNotifications())
63
+ const fromTop = computed(() => props.position === 'top-right')
64
+
65
+ // The window's height, for fitting the toasts on screen.
66
+ const windowHeight = ref(typeof window === 'undefined' ? 800 : window.innerHeight)
67
+ const onResize = () => (windowHeight.value = window.innerHeight)
68
+ onMounted(() => window.addEventListener('resize', onResize))
69
+ onBeforeUnmount(() => window.removeEventListener('resize', onResize))
70
+ const available = computed(() => Math.max(windowHeight.value - SCREEN_MARGIN, MORE * 3))
71
+
72
+ // ── Which toasts show ───────────────────────────────────────────────────────
73
+ // As many as fit on the screen spread out, newest first, in the order they
74
+ // came in. The others wait (their timers don't start) and are counted in
75
+ // "+3 more".
76
+ // Before a toast has been shown its height isn't known, so guess.
77
+ const estimate = (item: { compact?: boolean; action?: unknown; description?: unknown }) =>
78
+ item.compact ? 42 : item.action ? 116 : item.description ? 78 : 56
79
+
80
+ const selection = computed(() => {
81
+ const candidates = store.value.toasts.slice(-props.limit).reverse() // newest first
82
+ const height = (toast: (typeof candidates)[number]) => heights.get(toast.id) ?? estimate(toast.item)
83
+ const open = candidates.filter((toast) => !closing.has(toast.id))
84
+ const all = open.reduce((sum, toast) => sum + height(toast) + GAP, -GAP)
85
+
86
+ const chosen = new Set<string>()
87
+ if (all <= available.value) {
88
+ open.forEach((toast) => chosen.add(toast.id))
89
+ } else {
90
+ let used = MORE + GAP // room for the "+3 more" row
91
+ for (const toast of open) {
92
+ const next = used + height(toast) + (chosen.size ? GAP : 0)
93
+ // Stop at the first that doesn't fit, so the ones shown are always the
94
+ // newest, with nothing older slotted in after a gap.
95
+ if (chosen.size && next > available.value) break
96
+ chosen.add(toast.id)
97
+ used = next
98
+ }
99
+ }
100
+ const hidden = store.value.toasts.filter((toast) => !closing.has(toast.id)).length - chosen.size
101
+ // Closing ones stay until they've faded.
102
+ return { shown: candidates.filter((toast) => chosen.has(toast.id) || closing.has(toast.id)), hidden }
103
+ })
104
+ // Newest first: the newest sits at the edge, older ones further in.
105
+ const shown = computed(() => selection.value.shown)
106
+ const hidden = computed(() => selection.value.hidden)
107
+
108
+ // ── Into and out of "+N more" ───────────────────────────────────────────────
109
+ // A toast that has to wait (newer ones took its place) shrinks into "+N
110
+ // more" before it goes; one whose turn comes grows out of it, rather than
111
+ // sliding in from the edge like a new one.
112
+ type Shown = (typeof shown.value)[number]
113
+ const leaving = reactive(new Map<string, Shown>())
114
+ const waited = reactive(new Set<string>())
115
+ watch(
116
+ selection,
117
+ (now, before) => {
118
+ const showing = new Set(now.shown.map((toast) => toast.id))
119
+ for (const toast of store.value.toasts) if (!showing.has(toast.id) && !closing.has(toast.id)) waited.add(toast.id)
120
+ for (const toast of before?.shown ?? []) {
121
+ const stillThere = store.value.toasts.some((t) => t.id === toast.id)
122
+ if (showing.has(toast.id) || !stillThere || closing.has(toast.id) || leaving.has(toast.id)) continue
123
+ leaving.set(toast.id, toast)
124
+ setTimeout(() => {
125
+ leaving.delete(toast.id)
126
+ // Placed again from "+N more" when its turn comes.
127
+ if (!selection.value.shown.some((t) => t.id === toast.id)) placed.delete(toast.id)
128
+ }, 300)
129
+ }
130
+ },
131
+ { flush: 'sync' }
132
+ )
133
+ // What's rendered: those showing, and those on their way into "+N more".
134
+ const rendered = computed(() => [...shown.value, ...[...leaving.values()].filter((t) => !shown.value.some((s) => s.id === t.id))])
135
+
136
+ // The pane opening shows them all, so put the toasts away: those showing
137
+ // fade out, those waiting go.
138
+ watch(
139
+ () => store.value.paneOpen,
140
+ (paneOpen) => {
141
+ if (!paneOpen) return
142
+ const showing = new Set(shown.value.map((toast) => toast.id))
143
+ for (const toast of [...store.value.toasts]) {
144
+ if (showing.has(toast.id)) setOpen(toast.id, false)
145
+ else store.value.hideToast(toast.id)
146
+ }
147
+ }
148
+ )
149
+
150
+ function showAll() {
151
+ store.value.openPane()
152
+ emit('more')
153
+ }
154
+
155
+ // ── Measuring ───────────────────────────────────────────────────────────────
156
+ // Each toast's natural height, so they can be placed (absolutely) and moved.
157
+ const heights = reactive(new Map<string, number>())
158
+ const elements = new Map<string, Element>()
159
+ const observer = new ResizeObserver((entries) => {
160
+ for (const entry of entries) {
161
+ const id = (entry.target as HTMLElement).dataset.toastId
162
+ if (id) heights.set(id, (entry.target as HTMLElement).offsetHeight)
163
+ }
164
+ })
165
+ function track(id: string, instance: Element | ComponentPublicInstance | null) {
166
+ const el = instance && ('$el' in instance ? (instance.$el as HTMLElement) : (instance as HTMLElement))
167
+ const previous = elements.get(id)
168
+ if (previous === el) return
169
+ if (previous) observer.unobserve(previous)
170
+ if (el instanceof HTMLElement) {
171
+ el.dataset.toastId = id
172
+ elements.set(id, el)
173
+ heights.set(id, el.offsetHeight)
174
+ observer.observe(el)
175
+ onShown(id)
176
+ } else {
177
+ // Keep its height: it may only be waiting, and the choice of which
178
+ // toasts fit shouldn't flip back and forth.
179
+ elements.delete(id)
180
+ }
181
+ }
182
+ onBeforeUnmount(() => observer.disconnect())
183
+
184
+ // ── Entering and closing ────────────────────────────────────────────────────
185
+ // A new toast starts just off the edge, then moves into place on the next
186
+ // frame, so it slides in (and the others move along at the same time).
187
+ const placed = reactive(new Set<string>())
188
+ function onShown(id: string) {
189
+ if (placed.has(id)) return
190
+ nextTick(() =>
191
+ requestAnimationFrame(() =>
192
+ requestAnimationFrame(() => {
193
+ placed.add(id)
194
+ waited.delete(id)
195
+ })
196
+ )
197
+ )
198
+ }
199
+
200
+ // Closing: it stops taking up space at once (the others close the gap) and
201
+ // fades where it is; then it's removed from the list.
202
+ const closing = reactive(new Set<string>())
203
+ const lastPlace = new Map<string, Place>()
204
+ function setOpen(id: string, open: boolean) {
205
+ if (open || closing.has(id)) return
206
+ closing.add(id)
207
+ setTimeout(() => {
208
+ store.value.hideToast(id)
209
+ closing.delete(id)
210
+ placed.delete(id)
211
+ lastPlace.delete(id)
212
+ heights.delete(id)
213
+ }, 200)
214
+ }
215
+
216
+ // ── Layout ──────────────────────────────────────────────────────────────────
217
+ interface Place {
218
+ /** Distance from the edge when spread out, and when collapsed. */
219
+ spread: number
220
+ stacked: number
221
+ scale: number
222
+ opacity: number
223
+ z: number
224
+ }
225
+
226
+ const layout = computed(() => {
227
+ const open = shown.value.filter((toast) => !closing.has(toast.id))
228
+ const places = new Map<string, Place>()
229
+
230
+ // Spread out: one after another.
231
+ const offsets: number[] = []
232
+ let total = 0
233
+ for (const toast of open) {
234
+ offsets.push(total)
235
+ total += (heights.get(toast.id) ?? 0) + GAP
236
+ }
237
+ const spreadHeight = Math.max(total - GAP, 0)
238
+
239
+ // How many fit in maxHeight, or the screen if that's smaller (at least the newest).
240
+ const limit = Math.min(props.maxHeight, available.value)
241
+ let fit = 0
242
+ while (fit < open.length && (fit === 0 || offsets[fit] + (heights.get(open[fit].id) ?? 0) <= limit)) fit++
243
+ const front = open[fit - 1]
244
+ const frontOffset = offsets[fit - 1] ?? 0
245
+ const frontHeight = front ? (heights.get(front.id) ?? 0) : 0
246
+
247
+ // Collapsed: the ones that don't fit shrink into the "+N more" pill (out of
248
+ // sight), and grow out of it when spread out.
249
+ const pillOffset = frontOffset + frontHeight + GAP
250
+ open.forEach((toast, i) => {
251
+ const intoPill = i >= fit
252
+ places.set(toast.id, {
253
+ spread: offsets[i],
254
+ stacked: intoPill ? pillOffset : offsets[i],
255
+ scale: intoPill ? INTO_MORE : 1,
256
+ opacity: intoPill ? 0 : 1,
257
+ z: open.length - i
258
+ })
259
+ })
260
+ for (const [id, place] of places) lastPlace.set(id, place)
261
+
262
+ // Everything not fully in view when collapsed: in the pill, or waiting.
263
+ const notInView = open.length - fit + hidden.value
264
+ const stackedHeight = frontOffset + frontHeight + (notInView ? GAP + PILL : 0)
265
+ // The "+3 more" row goes after the last one when spread out.
266
+ const more = hidden.value > 0 ? GAP + MORE : 0
267
+ return {
268
+ places,
269
+ spreadHeight: spreadHeight + more,
270
+ stackedHeight,
271
+ moreOffset: spreadHeight + (spreadHeight ? GAP : 0),
272
+ notInView,
273
+ pillOffset
274
+ }
275
+ })
276
+
277
+ function styleFor(id: string) {
278
+ const place = closing.has(id) ? lastPlace.get(id) : layout.value.places.get(id)
279
+ const sign = fromTop.value ? 1 : -1
280
+ const px = (value: number) => `${sign * value}px`
281
+ // In "+N more": the row when spread out, the pill when collapsed.
282
+ const inMore = {
283
+ '--toast-spread': px(layout.value.moreOffset),
284
+ '--toast-stacked': px(layout.value.pillOffset),
285
+ '--toast-scale': INTO_MORE,
286
+ '--toast-opacity': 0,
287
+ '--toast-spread-opacity': 0,
288
+ '--toast-spread-scale': INTO_MORE,
289
+ '--toast-z': 0
290
+ }
291
+ if (!place && leaving.has(id)) return inMore
292
+ if (place && !placed.has(id) && waited.has(id)) return inMore
293
+ if (!place || !placed.has(id)) {
294
+ // Bottom: just off the screen (its own height plus the 16px gap), so it
295
+ // slides up into place. Top: a short drop with a fade, rather than
296
+ // sliding across the Header.
297
+ const offscreen = fromTop.value ? '-1rem' : 'calc(100% + 1rem)'
298
+ return { '--toast-spread': offscreen, '--toast-stacked': offscreen, '--toast-scale': 1, '--toast-opacity': 0, '--toast-spread-opacity': 0, '--toast-spread-scale': 1, '--toast-z': 999 }
299
+ }
300
+ return {
301
+ '--toast-spread': px(place.spread),
302
+ '--toast-stacked': px(place.stacked),
303
+ '--toast-scale': place.scale,
304
+ '--toast-opacity': place.opacity,
305
+ '--toast-spread-opacity': 1,
306
+ '--toast-spread-scale': 1,
307
+ '--toast-z': place.z
308
+ }
309
+ }
310
+
311
+ const edge = computed(() => (fromTop.value ? 1 : -1))
312
+ const moreStyle = computed(() => ({
313
+ '--toast-more': `${edge.value * layout.value.moreOffset}px`,
314
+ '--toast-pill': `${edge.value * layout.value.pillOffset}px`
315
+ }))
316
+
317
+ // The same button does two things: collapsed (a pill) it spreads the toasts
318
+ // out; spread out (a row) it opens the pane.
319
+ function onMore(event: MouseEvent) {
320
+ const spread = (event.currentTarget as HTMLElement).closest('[data-expanded]')
321
+ if (spread) showAll()
322
+ else spreadOut()
323
+ }
324
+
325
+ // Tapping the pill spreads the toasts out (there's no hover on touch screens):
326
+ // focus in the toasts is what spreads them, so focus the newest.
327
+ function spreadOut() {
328
+ const newest = shown.value.find((toast) => !closing.has(toast.id))
329
+ const li = newest && elements.get(newest.id)?.parentElement
330
+ // No focus ring: this came from a tap or click, not the keyboard.
331
+ li?.focus({ focusVisible: false } as FocusOptions)
332
+ }
333
+
334
+ const viewportStyle = computed(() => ({
335
+ '--toast-stack-spread': `${layout.value.spreadHeight}px`,
336
+ '--toast-stack-stacked': `${layout.value.stackedHeight}px`
337
+ }))
338
+
339
+ const positions = {
340
+ 'bottom-right': 'right-4 bottom-4 left-4 sm:left-auto sm:w-96',
341
+ 'top-right': 'top-16 right-4 left-4 sm:left-auto sm:w-96',
342
+ 'bottom-center': 'bottom-4 left-1/2 w-[calc(100%-2rem)] -translate-x-1/2 sm:w-96'
343
+ } as const
344
+ </script>
345
+
346
+ <template>
347
+ <!-- label: said before each toast. The viewport's label names the area F8 jumps to. -->
348
+ <ToastProvider swipe-direction="right" label="Notification">
349
+ <ToastRoot
350
+ v-for="toast in rendered"
351
+ :key="toast.id"
352
+ :open="!closing.has(toast.id)"
353
+ :duration="toast.duration"
354
+ :type="toast.item.type === 'error' || toast.item.type === 'warning' ? 'foreground' : 'background'"
355
+ :style="styleFor(toast.id)"
356
+ :class="
357
+ cn(
358
+ 'absolute inset-x-0 z-(--toast-z) list-none rounded',
359
+ 'outline-2 outline-offset-1 outline-transparent focus-visible:outline-focus forced-colors:not-focus-visible:outline-0',
360
+ // Shrinking into the more row or pill keeps the edge nearest it in place.
361
+ fromTop ? 'top-0 origin-top' : 'bottom-0 origin-bottom',
362
+ // Collapsed (the default) and spread out (hovered or focused: data-expanded).
363
+ 'translate-y-(--toast-stacked) scale-(--toast-scale) opacity-(--toast-opacity)',
364
+ 'data-expanded:translate-y-(--toast-spread) data-expanded:scale-(--toast-spread-scale) data-expanded:opacity-(--toast-spread-opacity)',
365
+ 'transition-[translate,scale,opacity] duration-300 ease-out motion-reduce:transition-none',
366
+ 'data-[state=closed]:animate-fade-out',
367
+ 'data-[swipe=move]:translate-x-(--reka-toast-swipe-move-x) data-[swipe=move]:transition-none',
368
+ 'data-[swipe=cancel]:translate-x-0',
369
+ 'data-[swipe=end]:animate-slide-out-right'
370
+ )
371
+ "
372
+ @update:open="setOpen(toast.id, $event)"
373
+ >
374
+ <Notification
375
+ :ref="(el) => track(toast.id, el as ComponentPublicInstance | null)"
376
+ :type="toast.item.type"
377
+ :title="toast.item.title"
378
+ :description="toast.item.description"
379
+ :action="toast.item.action"
380
+ :compact="toast.item.compact"
381
+ class="shadow-md"
382
+ :dismiss-label="`Close: ${toast.item.title}`"
383
+ @dismiss="setOpen(toast.id, false)"
384
+ @action="setOpen(toast.id, false)"
385
+ />
386
+ </ToastRoot>
387
+
388
+ <ToastViewport
389
+ label="Notifications ({hotkey})"
390
+ :style="viewportStyle"
391
+ :class="
392
+ cn(
393
+ 'fixed z-100 m-0 list-none p-0 outline-none',
394
+ // As tall as the toasts (so hovering anywhere on them spreads them out).
395
+ 'h-(--toast-stack-stacked) data-expanded:h-(--toast-stack-spread)',
396
+ positions[position],
397
+ props.class
398
+ )
399
+ "
400
+ >
401
+ <!-- "+N more": one button that changes shape with the toasts.
402
+ Collapsed, a pill on the stack counting every toast not in view
403
+ (in it, or waiting); tapping it spreads them out. Spread out, a
404
+ full-width row after the last toast counting those still waiting;
405
+ pressing it opens the pane. Moving between the two, it morphs:
406
+ position, width, height, corners and text size all animate. -->
407
+ <li
408
+ v-if="layout.notInView"
409
+ :style="moreStyle"
410
+ :class="
411
+ cn(
412
+ 'absolute inset-x-0 z-0 flex list-none justify-center',
413
+ fromTop ? 'top-0' : 'bottom-0',
414
+ 'translate-y-(--toast-pill) transition-[translate,opacity,visibility] duration-300 ease-out motion-reduce:transition-none',
415
+ // Spread out with nothing waiting: nothing to count, so the pill
416
+ // just fades where it is rather than morphing into the row.
417
+ hidden
418
+ ? 'in-data-expanded:translate-y-(--toast-more)'
419
+ : 'in-data-expanded:invisible in-data-expanded:opacity-0 in-data-expanded:duration-150'
420
+ )
421
+ "
422
+ >
423
+ <button
424
+ type="button"
425
+ :class="
426
+ cn(
427
+ 'flex cursor-pointer items-center justify-center overflow-hidden border border-black/15 bg-white font-bold whitespace-nowrap text-black shadow-md hover:bg-surface-elevated',
428
+ 'h-7 w-24 rounded-2xl text-xs',
429
+ hidden && 'in-data-expanded:h-9 in-data-expanded:w-full in-data-expanded:rounded in-data-expanded:text-sm',
430
+ 'transition-[width,height,border-radius,font-size,background-color] duration-300 ease-out motion-reduce:transition-none',
431
+ 'outline-2 outline-offset-1 outline-transparent focus-visible:outline-focus forced-colors:not-focus-visible:outline-0'
432
+ )
433
+ "
434
+ @click="onMore"
435
+ >
436
+ <template v-if="hidden">
437
+ <span class="in-data-expanded:hidden">+{{ layout.notInView }} more</span>
438
+ <span class="hidden in-data-expanded:inline">+{{ hidden }} more</span>
439
+ </template>
440
+ <template v-else>+{{ layout.notInView }} more</template>
441
+ <span class="sr-only">&nbsp;notifications</span>
442
+ </button>
443
+ </li>
444
+ </ToastViewport>
445
+ </ToastProvider>
446
+ </template>
@@ -0,0 +1,45 @@
1
+ // Sample notifications for Storybook (not part of the library).
2
+ import { h } from 'vue'
3
+ import type { NotificationInput } from './notifications'
4
+
5
+ const minutesAgo = (minutes: number) => new Date(Date.now() - minutes * 60_000)
6
+
7
+ export const passwordExpiry: NotificationInput = {
8
+ type: 'warning',
9
+ title: 'Password Expiry',
10
+ description: () => ['Your password expires in ', h('b', '7 days'), '.'],
11
+ action: { label: 'Change password', onSelect: () => console.log('Change password') },
12
+ }
13
+
14
+ export function sampleNotifications(): NotificationInput[] {
15
+ return [
16
+ { ...passwordExpiry, createdAt: minutesAgo(4) },
17
+ {
18
+ type: 'success',
19
+ title: 'Timetable published',
20
+ description: 'The 2027–28 timetable is now visible to staff and students.',
21
+ createdAt: minutesAgo(52),
22
+ },
23
+ {
24
+ type: 'error',
25
+ title: 'Census upload failed',
26
+ description: () => ['The file ', h('b', 'census-autumn.xml'), ' has 3 errors. Fix them and upload it again.'],
27
+ action: { label: 'View errors' },
28
+ createdAt: minutesAgo(3 * 60),
29
+ },
30
+ {
31
+ type: 'info',
32
+ title: 'New application',
33
+ description: 'Amelia Clarke applied for Year 7.',
34
+ createdAt: minutesAgo(26 * 60),
35
+ read: true,
36
+ },
37
+ {
38
+ type: 'info',
39
+ title: 'Maintenance on Saturday',
40
+ description: 'Keston will be unavailable from 7am to 9am on 14 November.',
41
+ createdAt: minutesAgo(4 * 24 * 60),
42
+ read: true,
43
+ },
44
+ ]
45
+ }
@@ -0,0 +1,99 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { createNotifications, toastDuration } from './notifications'
3
+
4
+ describe('createNotifications', () => {
5
+ it('sorts the initial list newest first and fills in defaults', () => {
6
+ const n = createNotifications([
7
+ { title: 'Old', createdAt: new Date(2026, 0, 1) },
8
+ { title: 'New', createdAt: new Date(2026, 5, 1), type: 'warning', read: true },
9
+ ])
10
+ expect(n.items.map((i) => i.title)).toEqual(['New', 'Old'])
11
+ expect(n.items[1]).toMatchObject({ type: 'info', read: false })
12
+ expect(n.items[0].id).toBeTruthy()
13
+ expect(n.unreadCount.value).toBe(1)
14
+ expect(n.toasts).toEqual([])
15
+ })
16
+
17
+ it('notify adds to the top and shows a toast', () => {
18
+ const n = createNotifications([{ title: 'Old' }])
19
+ const id = n.notify({ title: 'Saved', type: 'success' })
20
+ expect(n.items[0]).toMatchObject({ id, title: 'Saved' })
21
+ expect(n.toasts.map((t) => t.item.title)).toEqual(['Saved'])
22
+ expect(n.toasts[0].duration).toBe(6000)
23
+ })
24
+
25
+ it('can skip the toast, and set the duration', () => {
26
+ const n = createNotifications()
27
+ n.notify({ title: 'Quiet' }, { toast: false })
28
+ n.notify({ title: 'Quick' }, { duration: 2000 })
29
+ expect(n.items).toHaveLength(2)
30
+ expect(n.toasts.map((t) => [t.item.title, t.duration])).toEqual([['Quick', 2000]])
31
+ })
32
+
33
+ it('notify with an existing id replaces it', () => {
34
+ const n = createNotifications()
35
+ n.notify({ id: 'upload', title: 'Uploading…' })
36
+ n.notify({ id: 'upload', title: 'Uploaded', type: 'success' })
37
+ expect(n.items.map((i) => i.title)).toEqual(['Uploaded'])
38
+ expect(n.toasts.map((t) => t.item.title)).toEqual(['Uploaded'])
39
+ })
40
+
41
+ it('dismiss removes from the list and the toasts; hideToast only the toast', () => {
42
+ const n = createNotifications()
43
+ const a = n.notify({ title: 'A' })
44
+ const b = n.notify({ title: 'B' })
45
+ n.hideToast(a)
46
+ expect(n.items).toHaveLength(2)
47
+ expect(n.toasts.map((t) => t.id)).toEqual([b])
48
+ n.dismiss(b)
49
+ expect(n.items.map((i) => i.id)).toEqual([a])
50
+ expect(n.toasts).toEqual([])
51
+ })
52
+
53
+ it('clear empties the list but leaves toasts showing', () => {
54
+ const n = createNotifications()
55
+ n.notify({ title: 'A' })
56
+ n.clear()
57
+ expect(n.items).toEqual([])
58
+ expect(n.toasts).toHaveLength(1)
59
+ })
60
+
61
+ it('openPane counts requests, for NotificationPane to watch', () => {
62
+ const n = createNotifications()
63
+ expect(n.paneRequests).toBe(0)
64
+ n.openPane()
65
+ n.openPane()
66
+ expect(n.paneRequests).toBe(2)
67
+ })
68
+
69
+ it('no toasts while the pane is open', () => {
70
+ const n = createNotifications()
71
+ n.setPaneOpen(true)
72
+ n.notify({ title: 'A' })
73
+ expect(n.items).toHaveLength(1)
74
+ expect(n.toasts).toEqual([])
75
+ n.setPaneOpen(false)
76
+ n.notify({ title: 'B' })
77
+ expect(n.toasts).toHaveLength(1)
78
+ })
79
+
80
+ it('markAllRead', () => {
81
+ const n = createNotifications([{ title: 'A' }, { title: 'B' }])
82
+ n.markAllRead()
83
+ expect(n.unreadCount.value).toBe(0)
84
+ })
85
+ })
86
+
87
+ describe('toastDuration', () => {
88
+ it('stays until closed with an action or for errors', () => {
89
+ expect(toastDuration({ type: 'info' })).toBe(6000)
90
+ expect(toastDuration({ type: 'error' })).toBe(Number.POSITIVE_INFINITY)
91
+ expect(toastDuration({ type: 'warning', action: { label: 'Fix' } })).toBe(Number.POSITIVE_INFINITY)
92
+ expect(toastDuration({ type: 'error' }, 3000)).toBe(3000)
93
+ })
94
+ it('gives a compact Undo 10 seconds', () => {
95
+ expect(toastDuration({ type: 'success', compact: true, action: { label: 'Undo' } })).toBe(10_000)
96
+ expect(toastDuration({ type: 'success', compact: true })).toBe(6000)
97
+ expect(toastDuration({ type: 'error', compact: true, action: { label: 'Retry' } })).toBe(Number.POSITIVE_INFINITY)
98
+ })
99
+ })