@thekestongroup/ui 0.2.1 → 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,172 @@
1
+ import { computed, reactive, type VNodeChild } from 'vue'
2
+
3
+ export type NotificationType = 'info' | 'success' | 'warning' | 'error'
4
+
5
+ export interface NotificationAction {
6
+ label: string
7
+ /** Run when pressed. The toast closes afterwards (the notification stays in the pane). */
8
+ onSelect?: () => void
9
+ /** Make the action a link instead. */
10
+ href?: string
11
+ }
12
+
13
+ export interface NotificationInput {
14
+ /** Generated if left out. Notifying with an id that's already there replaces it. */
15
+ id?: string
16
+ /** Colour and icon. Default info. */
17
+ type?: NotificationType
18
+ title: string
19
+ /**
20
+ * Text under the title. A string, or a render function for formatting,
21
+ * e.g. `() => ['Your password expires in ', h('b', '7 days'), '.']`.
22
+ * (No HTML strings: they'd let anything that reaches a notification run
23
+ * as code.)
24
+ */
25
+ description?: string | (() => VNodeChild)
26
+ action?: NotificationAction
27
+ /**
28
+ * A one-line toast: icon, title, action, × (e.g. "✓ Student disabled
29
+ * [Undo]"). For quick confirmations; the description isn't shown in the
30
+ * toast. In the pane it's a normal row.
31
+ */
32
+ compact?: boolean
33
+ /** When it happened. Default now. */
34
+ createdAt?: Date
35
+ read?: boolean
36
+ }
37
+
38
+ export interface NotificationItem extends NotificationInput {
39
+ id: string
40
+ type: NotificationType
41
+ createdAt: Date
42
+ read: boolean
43
+ }
44
+
45
+ export interface ToastOptions {
46
+ /** Pop up a toast as well as adding it to the history. Default true. */
47
+ toast?: boolean
48
+ /**
49
+ * How long the toast stays, in ms. Default 6000; until closed for errors,
50
+ * and for toasts with an action (people need time to use it); compact ones
51
+ * with an action (Undo) stay 10 seconds.
52
+ */
53
+ duration?: number
54
+ }
55
+
56
+ export interface ShownToast {
57
+ id: string
58
+ duration: number
59
+ item: NotificationItem
60
+ }
61
+
62
+ let counter = 0
63
+ const newId = () => `notification-${Date.now().toString(36)}-${(counter++).toString(36)}`
64
+
65
+ /** Default toast length: long enough to read; forever when there's something to do. */
66
+ export function toastDuration(item: Pick<NotificationItem, 'type' | 'action' | 'compact'>, duration?: number) {
67
+ if (duration !== undefined) return duration
68
+ if (item.type === 'error') return Number.POSITIVE_INFINITY
69
+ // A quick "Undo" shouldn't wait forever, but needs longer than reading.
70
+ if (item.action) return item.compact ? 10_000 : Number.POSITIVE_INFINITY
71
+ return 6000
72
+ }
73
+
74
+ /**
75
+ * A list of notifications (newest first), and the ones currently showing as
76
+ * toasts. Pass `items` to NotificationPane and `toasts` to Toaster, or call
77
+ * `useNotifications()` for one shared list for the whole app.
78
+ */
79
+ export function createNotifications(initial: NotificationInput[] = []) {
80
+ const state = reactive({
81
+ items: initial.map(toItem).sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime()) as NotificationItem[],
82
+ toasts: [] as ShownToast[],
83
+ paneRequests: 0,
84
+ paneOpen: false
85
+ })
86
+
87
+ function toItem(input: NotificationInput): NotificationItem {
88
+ return { ...input, id: input.id ?? newId(), type: input.type ?? 'info', createdAt: input.createdAt ?? new Date(), read: input.read ?? false }
89
+ }
90
+
91
+ /** Add a notification (to the top), and pop it up as a toast. Returns its id. */
92
+ function notify(input: NotificationInput, options: ToastOptions = {}): string {
93
+ const item = toItem(input)
94
+ state.items = [item, ...state.items.filter((existing) => existing.id !== item.id)]
95
+ // While the pane is open it's already showing there, so no toast.
96
+ if (options.toast !== false && !state.paneOpen) {
97
+ state.toasts = [...state.toasts.filter((toast) => toast.id !== item.id), { id: item.id, duration: toastDuration(item, options.duration), item }]
98
+ }
99
+ return item.id
100
+ }
101
+
102
+ /** Remove it from the history (and its toast). */
103
+ function dismiss(id: string) {
104
+ state.items = state.items.filter((item) => item.id !== id)
105
+ hideToast(id)
106
+ }
107
+
108
+ /** Close its toast; it stays in the history. */
109
+ function hideToast(id: string) {
110
+ state.toasts = state.toasts.filter((toast) => toast.id !== id)
111
+ }
112
+
113
+ /** Remove everything from the history. Toasts showing stay until they close. */
114
+ function clear() {
115
+ state.items = []
116
+ }
117
+
118
+ function markAllRead() {
119
+ state.items = state.items.map((item) => (item.read ? item : { ...item, read: true }))
120
+ }
121
+
122
+ const unreadCount = computed(() => state.items.filter((item) => !item.read).length)
123
+
124
+ /** Ask NotificationPane to open, e.g. from the Toaster's "+3 more". */
125
+ function openPane() {
126
+ state.paneRequests++
127
+ }
128
+
129
+ /**
130
+ * NotificationPane says when it opens and closes. While it's open, toasts
131
+ * showing are put away (the Toaster fades them) and new ones don't pop up.
132
+ */
133
+ function setPaneOpen(open: boolean) {
134
+ state.paneOpen = open
135
+ }
136
+
137
+ return {
138
+ get items() {
139
+ return state.items
140
+ },
141
+ /** Toasts showing, oldest first (the order they stack in). */
142
+ get toasts() {
143
+ return state.toasts
144
+ },
145
+ /** Goes up each time `openPane()` is called; NotificationPane opens when it changes. */
146
+ get paneRequests() {
147
+ return state.paneRequests
148
+ },
149
+ get paneOpen() {
150
+ return state.paneOpen
151
+ },
152
+ unreadCount,
153
+ openPane,
154
+ setPaneOpen,
155
+ notify,
156
+ dismiss,
157
+ hideToast,
158
+ clear,
159
+ markAllRead
160
+ }
161
+ }
162
+
163
+ export type Notifications = ReturnType<typeof createNotifications>
164
+
165
+ let shared: Notifications | undefined
166
+ /** One shared notification list for the whole app. */
167
+ export function useNotifications(): Notifications {
168
+ return (shared ??= createNotifications())
169
+ }
170
+
171
+ // Kept here too, for existing imports.
172
+ export { relativeTime } from '../../lib/dates'
@@ -0,0 +1,79 @@
1
+ import type { Meta, StoryObj } from "@storybook/vue3-vite";
2
+ import { Building2, CalendarCog, Layers, UserPlus } from "@lucide/vue";
3
+ import AvatarGroup from "../AvatarGroup/AvatarGroup.vue";
4
+ import Badge from "../Badge/Badge.vue";
5
+ import Button from "../Button/Button.vue";
6
+ import PageHeader from "./PageHeader.vue";
7
+
8
+ const meta = {
9
+ title: "Components/Layout/PageHeader",
10
+ component: PageHeader,
11
+ tags: ["autodocs"],
12
+ parameters: { layout: "padded" },
13
+ args: {
14
+ title: "New School Year Setup",
15
+ description: "Set up 2027–28 before the summer. Nothing changes for staff or students until you start the new year.",
16
+ breadcrumb: [
17
+ { label: "School", icon: Building2 },
18
+ { label: "School Structure", icon: Layers, href: "#" },
19
+ { label: "New School Year Setup", icon: CalendarCog },
20
+ ],
21
+ },
22
+ render: (args) => ({
23
+ components: { PageHeader, Badge, Button },
24
+ setup: () => ({ args }),
25
+ template: `
26
+ <PageHeader v-bind="args">
27
+ <template #badge><Badge>Draft</Badge></template>
28
+ <template #actions>
29
+ <Button>Save draft</Button>
30
+ <Button variant="primary">Start new year</Button>
31
+ </template>
32
+ </PageHeader>`,
33
+ }),
34
+ } satisfies Meta<typeof PageHeader>;
35
+
36
+ export default meta;
37
+ type Story = StoryObj<typeof meta>;
38
+
39
+ /** Breadcrumb, title with a status badge, description and actions. Narrow the preview to see the actions wrap. */
40
+ export const Default: Story = {};
41
+
42
+ /** A top-level page: just the title and its main action (no breadcrumb for a single step). */
43
+ export const TopLevel: Story = {
44
+ args: { title: "Applications", description: undefined, breadcrumb: [{ label: "Applications" }] },
45
+ render: (args) => ({
46
+ components: { PageHeader, Button, UserPlus },
47
+ setup: () => ({ args }),
48
+ template: `
49
+ <PageHeader v-bind="args">
50
+ <template #actions>
51
+ <Button variant="primary"><template #leading><UserPlus /></template>New application</Button>
52
+ </template>
53
+ </PageHeader>`,
54
+ }),
55
+ };
56
+
57
+ /** Who's involved, as an AvatarGroup beside the actions. */
58
+ export const WithPeople: Story = {
59
+ render: (args) => ({
60
+ components: { PageHeader, Badge, Button, AvatarGroup },
61
+ setup: () => ({
62
+ args,
63
+ people: [
64
+ { name: "Ben Blissett", role: "Head of Year 9", email: "ben.blissett@keston.school" },
65
+ { name: "Zara Khan", role: "Deputy Head", email: "zara.khan@keston.school" },
66
+ { name: "Oscar Hughes", role: "Data Manager", email: "oscar.hughes@keston.school" },
67
+ ],
68
+ }),
69
+ template: `
70
+ <PageHeader v-bind="args">
71
+ <template #badge><Badge>Draft</Badge></template>
72
+ <template #actions>
73
+ <AvatarGroup :users="people" size="sm" label="Editing with you" class="mr-2" />
74
+ <Button>Save draft</Button>
75
+ <Button variant="primary">Start new year</Button>
76
+ </template>
77
+ </PageHeader>`,
78
+ }),
79
+ };
@@ -0,0 +1,66 @@
1
+ <script setup lang="ts">
2
+ import type { Component, HTMLAttributes } from 'vue'
3
+ import { cn } from '../../lib/cn'
4
+ import Breadcrumb, { type BreadcrumbItem } from '../Breadcrumb/Breadcrumb.vue'
5
+
6
+ /**
7
+ * The top of a page: where it is (breadcrumb), what it is (the h1 title, with
8
+ * an optional status badge and description), and its main actions on the
9
+ * right. Actions wrap under the title on narrow screens.
10
+ *
11
+ * Slots: badge (after the title, e.g. a Badge), description (instead of the
12
+ * prop, for links or formatting), actions (e.g. Buttons), default (anything
13
+ * under, e.g. an alert or tabs).
14
+ */
15
+ export interface PageHeaderProps {
16
+ /** The page's name: its h1. */
17
+ title: string
18
+ /** A sentence under the title. */
19
+ description?: string
20
+ /** Where the page is. Pass `sidebarTrail(items)` to follow a nested Sidebar. */
21
+ breadcrumb?: BreadcrumbItem[]
22
+ /** Component used for breadcrumb steps with `to`, e.g. RouterLink. */
23
+ linkComponent?: string | Component
24
+ class?: HTMLAttributes['class']
25
+ /** Extra classes for the parts. */
26
+ ui?: {
27
+ title?: HTMLAttributes['class']
28
+ description?: HTMLAttributes['class']
29
+ actions?: HTMLAttributes['class']
30
+ }
31
+ }
32
+
33
+ const props = defineProps<PageHeaderProps>()
34
+
35
+ defineSlots<{
36
+ badge?(): unknown
37
+ description?(): unknown
38
+ actions?(): unknown
39
+ default?(): unknown
40
+ }>()
41
+ </script>
42
+
43
+ <template>
44
+ <header :class="cn('flex flex-col gap-2', props.class)">
45
+ <!-- Only worth showing once there's somewhere to go back to. -->
46
+ <Breadcrumb v-if="breadcrumb && breadcrumb.length > 1" :items="breadcrumb" :link-component="linkComponent" />
47
+
48
+ <div class="flex flex-wrap items-start justify-between gap-x-6 gap-y-3">
49
+ <div class="flex min-w-0 flex-col gap-1">
50
+ <div class="flex flex-wrap items-center gap-x-2 gap-y-1">
51
+ <h1 :class="cn('min-w-0 text-2xl font-bold break-words', ui?.title)">{{ title }}</h1>
52
+ <slot name="badge" />
53
+ </div>
54
+ <p v-if="description || $slots.description" :class="cn('max-w-prose text-sm text-black/65', ui?.description)">
55
+ <slot name="description">{{ description }}</slot>
56
+ </p>
57
+ </div>
58
+
59
+ <div v-if="$slots.actions" :class="cn('flex flex-wrap items-center gap-2', ui?.actions)">
60
+ <slot name="actions" />
61
+ </div>
62
+ </div>
63
+
64
+ <slot />
65
+ </header>
66
+ </template>
@@ -0,0 +1,135 @@
1
+ import type { Meta, StoryObj } from "@storybook/vue3-vite";
2
+ import { CircleHelp, Settings } from "@lucide/vue";
3
+ import { ref } from "vue";
4
+ import Button from "../Button/Button.vue";
5
+ import Input from "../Input/Input.vue";
6
+ import Popover from "./Popover.vue";
7
+
8
+ const meta = {
9
+ title: "Components/Overlay/Popover",
10
+ component: Popover,
11
+ tags: ["autodocs"],
12
+ argTypes: {
13
+ mode: { options: ["click", "hover"], control: { type: "inline-radio" } },
14
+ },
15
+ args: { mode: "click", arrow: false, dismissible: true, modal: false },
16
+ decorators: [() => ({ template: '<div class="flex min-h-80 items-start justify-center pt-24"><story /></div>' })],
17
+ render: (args) => ({
18
+ components: { Popover, Button },
19
+ setup: () => ({ args }),
20
+ template: `
21
+ <Popover v-bind="args">
22
+ <Button>Open popover</Button>
23
+ <template #content>
24
+ <div class="max-w-64 p-3 text-sm">
25
+ A floating panel. Click outside or press Escape to close it.
26
+ </div>
27
+ </template>
28
+ </Popover>`,
29
+ }),
30
+ } satisfies Meta<typeof Popover>;
31
+
32
+ export default meta;
33
+ type Story = StoryObj<typeof meta>;
34
+
35
+ /** Playground: use the controls below. */
36
+ export const Default: Story = {};
37
+
38
+ /** Click mode moves focus into the panel, so it suits interactive content. */
39
+ export const WithForm: Story = {
40
+ render: (args) => ({
41
+ components: { Popover, Button, Input, Settings },
42
+ setup() {
43
+ const name = ref("Year 9 Maths");
44
+ return { args, name };
45
+ },
46
+ template: `
47
+ <Popover v-bind="args">
48
+ <Button><template #leading><Settings /></template>Class settings</Button>
49
+ <template #content="{ close }">
50
+ <div class="flex w-72 flex-col gap-3 p-4">
51
+ <Input v-model="name" label="Class name" />
52
+ <div class="flex justify-end gap-2">
53
+ <Button size="sm" @click="close">Cancel</Button>
54
+ <Button size="sm" variant="primary" @click="close">Save</Button>
55
+ </div>
56
+ </div>
57
+ </template>
58
+ </Popover>`,
59
+ }),
60
+ };
61
+
62
+ /** Hover mode: opens on hover; click or tap keeps it open (works on touch). Focus isn't moved. */
63
+ export const Hover: Story = {
64
+ args: { mode: "hover" },
65
+ render: (args) => ({
66
+ components: { Popover, CircleHelp },
67
+ setup: () => ({ args }),
68
+ template: `
69
+ <Popover v-bind="args">
70
+ <button type="button" aria-label="About attendance codes" class="inline-flex rounded-full p-1 transition hover:bg-black/10 [&_svg]:size-4">
71
+ <CircleHelp />
72
+ </button>
73
+ <template #content>
74
+ <p class="max-w-64 px-3 py-2 text-xs">/ and \\ mean present, N means no reason yet, L means late.</p>
75
+ </template>
76
+ </Popover>`,
77
+ }),
78
+ };
79
+
80
+ export const WithArrow: Story = { args: { arrow: true } };
81
+
82
+ export const Sides: Story = {
83
+ render: () => ({
84
+ components: { Popover, Button },
85
+ template: `
86
+ <div class="grid grid-cols-2 gap-4">
87
+ <Popover v-for="side in ['top', 'right', 'bottom', 'left']" :key="side" :content="{ side }" arrow>
88
+ <Button>{{ side }}</Button>
89
+ <template #content><div class="p-3 text-sm">Opens {{ side }}</div></template>
90
+ </Popover>
91
+ </div>`,
92
+ }),
93
+ };
94
+
95
+ /** Escape and clicking outside don't close it; it emits close:prevent instead. */
96
+ export const NotDismissible: Story = {
97
+ args: { dismissible: false },
98
+ render: (args) => ({
99
+ components: { Popover, Button },
100
+ setup() {
101
+ const attempts = ref(0);
102
+ return { args, attempts };
103
+ },
104
+ template: `
105
+ <Popover v-bind="args" @close:prevent="attempts++">
106
+ <Button>Open</Button>
107
+ <template #content="{ close }">
108
+ <div class="flex w-64 flex-col gap-3 p-3 text-sm">
109
+ <p>Only the button below closes this.</p>
110
+ <p class="text-black/60">Blocked close attempts: {{ attempts }}</p>
111
+ <Button size="sm" @click="close">Close</Button>
112
+ </div>
113
+ </template>
114
+ </Popover>`,
115
+ }),
116
+ };
117
+
118
+ /** Opened and closed from outside with v-model:open. */
119
+ export const Controlled: Story = {
120
+ render: (args) => ({
121
+ components: { Popover, Button },
122
+ setup() {
123
+ const open = ref(false);
124
+ return { args, open };
125
+ },
126
+ template: `
127
+ <div class="flex items-center gap-3">
128
+ <Popover v-bind="args" v-model:open="open">
129
+ <Button>Trigger</Button>
130
+ <template #content><div class="p-3 text-sm">Controlled popover</div></template>
131
+ </Popover>
132
+ <Button variant="primary" @click="open = !open">{{ open ? 'Close' : 'Open' }} from outside</Button>
133
+ </div>`,
134
+ }),
135
+ };
@@ -0,0 +1,192 @@
1
+ <script setup lang="ts">
2
+ import { onBeforeUnmount, ref, watch, type HTMLAttributes } from 'vue'
3
+ import {
4
+ PopoverAnchor,
5
+ PopoverArrow,
6
+ PopoverContent,
7
+ PopoverPortal,
8
+ PopoverRoot,
9
+ PopoverTrigger,
10
+ type PopoverArrowProps,
11
+ type PopoverContentProps
12
+ } from 'reka-ui'
13
+ import { cn } from '../../lib/cn'
14
+ import { popoverArrow, popoverContent } from './popover'
15
+
16
+ /**
17
+ * A floating panel attached to a trigger. Built on Reka UI's Popover, which
18
+ * handles positioning, focus, Escape and clicking outside.
19
+ *
20
+ * - mode "click" (default): opens and closes on click / tap / Enter. Focus
21
+ * moves into the panel, so it suits interactive content (menus, forms).
22
+ * - mode "hover": opens on mouse hover, and also on click / tap, which keeps
23
+ * it open (so it works on touch screens). Focus stays where it is, so it
24
+ * suits information, e.g. a field's help text.
25
+ *
26
+ * Slots: default (the trigger, e.g. a Button), content ({ close }), anchor
27
+ * (position against something other than the trigger).
28
+ */
29
+ export interface PopoverProps {
30
+ /** How it opens. */
31
+ mode?: 'click' | 'hover'
32
+ /**
33
+ * Where the panel goes and how it behaves, passed to Reka's PopoverContent,
34
+ * e.g. `{ side: 'top', align: 'start' }`. Default
35
+ * `{ side: 'bottom', sideOffset: 8, collisionPadding: 8 }`.
36
+ */
37
+ content?: Omit<PopoverContentProps, 'as' | 'asChild' | 'forceMount'>
38
+ /** Show an arrow pointing at the trigger (or Reka arrow options). */
39
+ arrow?: boolean | Omit<PopoverArrowProps, 'as' | 'asChild'>
40
+ /**
41
+ * Render the panel at the end of the page (true), inside a given element
42
+ * (a selector or element), or in place (false).
43
+ */
44
+ portal?: boolean | string | HTMLElement
45
+ /** Close on Escape and on clicking outside. If false, emits close:prevent instead. */
46
+ dismissible?: boolean
47
+ /** Block interaction with the rest of the page while open. */
48
+ modal?: boolean
49
+ /** Open when first shown. */
50
+ defaultOpen?: boolean
51
+ /** Hover mode: wait this long before opening, in ms. */
52
+ openDelay?: number
53
+ /** Hover mode: wait this long before closing, in ms (at least 100, so the pointer can reach the panel). */
54
+ closeDelay?: number
55
+ /** Extra classes for the panel and the arrow. */
56
+ ui?: { content?: HTMLAttributes['class']; arrow?: HTMLAttributes['class'] }
57
+ }
58
+
59
+ const props = withDefaults(defineProps<PopoverProps>(), {
60
+ mode: 'click',
61
+ arrow: false,
62
+ portal: true,
63
+ dismissible: true,
64
+ modal: false,
65
+ openDelay: 0,
66
+ closeDelay: 0
67
+ })
68
+
69
+ const emit = defineEmits<{ 'close:prevent': [] }>()
70
+
71
+ defineSlots<{
72
+ default?(): unknown
73
+ content?(props: { close: () => void }): unknown
74
+ anchor?(): unknown
75
+ }>()
76
+
77
+ /** Whether the panel is open. Optional: use v-model:open to control it. */
78
+ const openModel = defineModel<boolean | undefined>('open', { default: undefined })
79
+ const isOpen = ref(openModel.value ?? props.defaultOpen ?? false)
80
+ watch(openModel, (value) => {
81
+ if (value !== undefined) isOpen.value = value
82
+ })
83
+ function setOpen(value: boolean) {
84
+ isOpen.value = value
85
+ openModel.value = value
86
+ }
87
+ function close() {
88
+ pinned = false
89
+ setOpen(false)
90
+ }
91
+
92
+ const contentDefaults = { side: 'bottom', sideOffset: 8, collisionPadding: 8 } as const
93
+
94
+ // ── Hover mode ──────────────────────────────────────────────────────────────
95
+ // Mouse hover opens it and leaving closes it (after a short grace period, so
96
+ // the pointer can move onto the panel). A click / tap opens it and keeps it
97
+ // open ("pinned"); clicking again, Escape or clicking outside closes it.
98
+ let pinned = false
99
+ let pinOnNextToggle = false
100
+ let openTimer: ReturnType<typeof setTimeout> | undefined
101
+ let closeTimer: ReturnType<typeof setTimeout> | undefined
102
+
103
+ function hoverOpen(event: PointerEvent) {
104
+ if (props.mode !== 'hover' || event.pointerType !== 'mouse') return
105
+ clearTimeout(closeTimer)
106
+ clearTimeout(openTimer)
107
+ openTimer = setTimeout(() => setOpen(true), props.openDelay)
108
+ }
109
+ function hoverClose(event: PointerEvent) {
110
+ if (props.mode !== 'hover' || event.pointerType !== 'mouse' || pinned) return
111
+ clearTimeout(openTimer)
112
+ closeTimer = setTimeout(() => setOpen(false), Math.max(props.closeDelay, 100))
113
+ }
114
+ function triggerPointerDown() {
115
+ if (props.mode === 'hover') pinOnNextToggle = isOpen.value && !pinned
116
+ }
117
+ onBeforeUnmount(() => {
118
+ clearTimeout(openTimer)
119
+ clearTimeout(closeTimer)
120
+ })
121
+
122
+ function onOpenChange(value: boolean) {
123
+ if (props.mode === 'hover') {
124
+ if (!value && pinOnNextToggle) {
125
+ // A click on the trigger while it was open from hover: keep it open.
126
+ pinOnNextToggle = false
127
+ pinned = true
128
+ return
129
+ }
130
+ pinOnNextToggle = false
131
+ pinned = value
132
+ }
133
+ setOpen(value)
134
+ }
135
+
136
+ // ── Not dismissible ─────────────────────────────────────────────────────────
137
+ // One outside click can be reported twice by Reka (the press, then focus
138
+ // leaving). Both are prevented, but close:prevent is emitted once per attempt.
139
+ let lastPrevented = 0
140
+ function preventDismiss(event: Event) {
141
+ if (props.dismissible) return
142
+ event.preventDefault()
143
+ const now = performance.now()
144
+ if (now - lastPrevented > 50) emit('close:prevent')
145
+ lastPrevented = now
146
+ }
147
+
148
+ // Hover mode never moves focus: the panel is information, and moving focus
149
+ // would pull it away from whatever the user is doing (e.g. typing).
150
+ function keepFocus(event: Event) {
151
+ if (props.mode === 'hover') event.preventDefault()
152
+ }
153
+
154
+ const portalTarget = () =>
155
+ typeof props.portal === 'string' || props.portal instanceof HTMLElement ? props.portal : undefined
156
+ </script>
157
+
158
+ <template>
159
+ <PopoverRoot :open="isOpen" :modal="modal" @update:open="onOpenChange">
160
+ <PopoverAnchor v-if="$slots.anchor" as-child>
161
+ <slot name="anchor" />
162
+ </PopoverAnchor>
163
+
164
+ <PopoverTrigger
165
+ v-if="$slots.default"
166
+ as-child
167
+ @pointerdown="triggerPointerDown"
168
+ @pointerenter="hoverOpen"
169
+ @pointerleave="hoverClose"
170
+ >
171
+ <slot />
172
+ </PopoverTrigger>
173
+
174
+ <PopoverPortal :disabled="portal === false" :to="portalTarget()">
175
+ <PopoverContent
176
+ v-bind="{ ...contentDefaults, ...content }"
177
+ :class="cn(popoverContent, ui?.content)"
178
+ @pointerenter="hoverOpen"
179
+ @pointerleave="hoverClose"
180
+ @escape-key-down="preventDismiss"
181
+ @interact-outside="preventDismiss"
182
+ @open-auto-focus="keepFocus"
183
+ @close-auto-focus="keepFocus"
184
+ >
185
+ <slot name="content" :close="close" />
186
+ <PopoverArrow v-if="arrow" v-bind="typeof arrow === 'object' ? arrow : {}" as-child>
187
+ <span :class="cn(popoverArrow, ui?.arrow)" />
188
+ </PopoverArrow>
189
+ </PopoverContent>
190
+ </PopoverPortal>
191
+ </PopoverRoot>
192
+ </template>
@@ -0,0 +1,20 @@
1
+ /**
2
+ * The shared look of floating panels: Popover, and anything else built on
3
+ * Reka's popper (e.g. InputDate's calendar), so they all match.
4
+ *
5
+ * White panel with the same border and radius as a basic Button, a shadow,
6
+ * and the theme's fade in / out (Reka waits for the fade-out to finish
7
+ * before removing the panel).
8
+ */
9
+ export const popoverContent = [
10
+ 'z-50 rounded border border-black/15 bg-white text-black shadow-md outline-none',
11
+ 'data-[state=open]:animate-fade-in data-[state=closed]:animate-fade-out'
12
+ ]
13
+
14
+ /**
15
+ * The arrow pointing at the trigger: a white square turned 45° with the
16
+ * panel's border on its two outer edges, half overlapping the panel so it
17
+ * covers the panel's border where they meet. Reka rotates the arrow's wrapper
18
+ * for each side, so this only needs drawing pointing down.
19
+ */
20
+ export const popoverArrow = 'block size-2.5 -translate-y-1/2 rotate-45 border-r border-b border-black/15 bg-white'