@thekestongroup/ui 0.2.1 → 0.4.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 (210) hide show
  1. package/README.md +98 -31
  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/EmptyState/EmptyState.vue.d.ts +55 -0
  16. package/dist/components/{ToastItem.vue.d.ts → FormField/FieldMessage.vue.d.ts} +10 -2
  17. package/dist/components/FormField/FormField.vue.d.ts +61 -0
  18. package/dist/components/Header/Header.vue.d.ts +104 -0
  19. package/dist/components/InfoTip/InfoTip.vue.d.ts +20 -0
  20. package/dist/components/Input/Input.vue.d.ts +56 -0
  21. package/dist/components/InputCode/InputCode.vue.d.ts +53 -0
  22. package/dist/components/InputDate/InputDate.vue.d.ts +49 -0
  23. package/dist/components/InputNumber/InputNumber.vue.d.ts +44 -0
  24. package/dist/components/InputPassword/InputPassword.vue.d.ts +51 -0
  25. package/dist/components/InputRichText/InputRichText.vue.d.ts +44 -0
  26. package/dist/components/Link/Link.vue.d.ts +39 -0
  27. package/dist/components/Notification/Notification.vue.d.ts +62 -0
  28. package/dist/components/Notification/NotificationPane.vue.d.ts +38 -0
  29. package/dist/components/Notification/Toaster.vue.d.ts +51 -0
  30. package/dist/components/Notification/notification.samples.d.ts +3 -0
  31. package/dist/components/Notification/notifications.d.ts +115 -0
  32. package/dist/components/PageHeader/PageHeader.vue.d.ts +43 -0
  33. package/dist/components/Popover/Popover.vue.d.ts +83 -0
  34. package/dist/components/Popover/popover.d.ts +16 -0
  35. package/dist/components/Progress/Progress.vue.d.ts +26 -0
  36. package/dist/components/RadioGroup/RadioGroup.vue.d.ts +45 -0
  37. package/dist/components/RelativeTime/RelativeTime.vue.d.ts +22 -0
  38. package/dist/components/Select/Select.vue.d.ts +110 -0
  39. package/dist/components/Sidebar/Sidebar.vue.d.ts +106 -0
  40. package/dist/components/Sidebar/sidebar.d.ts +10 -0
  41. package/dist/components/Slideover/Slideover.vue.d.ts +125 -0
  42. package/dist/components/Spinner/Spinner.vue.d.ts +30 -0
  43. package/dist/components/StatTile/StatTile.vue.d.ts +46 -0
  44. package/dist/components/SummaryList/SummaryList.vue.d.ts +62 -0
  45. package/dist/components/Switch/Switch.vue.d.ts +39 -0
  46. package/dist/components/Table/Table.vue.d.ts +82 -0
  47. package/dist/components/Table/table.d.ts +36 -0
  48. package/dist/components/Tooltip/Tooltip.vue.d.ts +46 -0
  49. package/dist/index.d.ts +91 -25
  50. package/dist/keston-ui.js +5030 -14786
  51. package/dist/lib/cn.d.ts +4 -0
  52. package/dist/lib/dates.d.ts +27 -0
  53. package/dist/lib/initials.d.ts +2 -0
  54. package/dist/lib/parseDate.d.ts +44 -0
  55. package/dist/lib/useNow.d.ts +3 -0
  56. package/package.json +57 -24
  57. package/src/components/.gitkeep +0 -0
  58. package/src/components/Age/Age.stories.ts +24 -0
  59. package/src/components/Age/Age.vue +56 -0
  60. package/src/components/AppShell/AppShell.stories.ts +124 -0
  61. package/src/components/AppShell/AppShell.vue +221 -0
  62. package/src/components/Avatar/Avatar.stories.ts +56 -0
  63. package/src/components/Avatar/Avatar.vue +118 -0
  64. package/src/components/AvatarGroup/AvatarGroup.stories.ts +47 -0
  65. package/src/components/AvatarGroup/AvatarGroup.vue +97 -0
  66. package/src/components/Badge/Badge.stories.ts +92 -0
  67. package/src/components/Badge/Badge.vue +67 -0
  68. package/src/components/Breadcrumb/Breadcrumb.stories.ts +58 -0
  69. package/src/components/Breadcrumb/Breadcrumb.vue +109 -0
  70. package/src/components/Button/Button.mdx +79 -0
  71. package/src/components/Button/Button.stories.ts +207 -0
  72. package/src/components/Button/Button.vue +266 -0
  73. package/src/components/Calendar/Calendar.stories.ts +51 -0
  74. package/src/components/Calendar/Calendar.vue +364 -0
  75. package/src/components/Card/Card.stories.ts +109 -0
  76. package/src/components/Card/Card.vue +115 -0
  77. package/src/components/Checkbox/Checkbox.stories.ts +102 -0
  78. package/src/components/Checkbox/Checkbox.vue +102 -0
  79. package/src/components/Checklist/Checklist.stories.ts +67 -0
  80. package/src/components/Checklist/Checklist.vue +38 -0
  81. package/src/components/Chip/Chip.stories.ts +71 -0
  82. package/src/components/Chip/Chip.vue +86 -0
  83. package/src/components/Divider/Divider.stories.ts +57 -0
  84. package/src/components/Divider/Divider.vue +58 -0
  85. package/src/components/EmptyState/EmptyState.stories.ts +99 -0
  86. package/src/components/EmptyState/EmptyState.vue +87 -0
  87. package/src/components/FormField/FieldMessage.vue +81 -0
  88. package/src/components/FormField/FormField.stories.ts +55 -0
  89. package/src/components/FormField/FormField.vue +160 -0
  90. package/src/components/Header/Header.stories.ts +168 -0
  91. package/src/components/Header/Header.vue +232 -0
  92. package/src/components/InfoTip/InfoTip.stories.ts +63 -0
  93. package/src/components/InfoTip/InfoTip.vue +50 -0
  94. package/src/components/Input/Input.stories.ts +206 -0
  95. package/src/components/Input/Input.vue +90 -0
  96. package/src/components/InputCode/InputCode.stories.ts +85 -0
  97. package/src/components/InputCode/InputCode.vue +145 -0
  98. package/src/components/InputDate/InputDate.stories.ts +58 -0
  99. package/src/components/InputDate/InputDate.vue +258 -0
  100. package/src/components/InputNumber/InputNumber.stories.ts +63 -0
  101. package/src/components/InputNumber/InputNumber.vue +109 -0
  102. package/src/components/InputPassword/InputPassword.stories.ts +105 -0
  103. package/src/components/InputPassword/InputPassword.vue +132 -0
  104. package/src/components/InputRichText/InputRichText.stories.ts +100 -0
  105. package/src/components/InputRichText/InputRichText.vue +194 -0
  106. package/src/components/Link/Link.stories.ts +73 -0
  107. package/src/components/Link/Link.vue +56 -0
  108. package/src/components/Notification/Notification.stories.ts +196 -0
  109. package/src/components/Notification/Notification.vue +136 -0
  110. package/src/components/Notification/NotificationPane.vue +186 -0
  111. package/src/components/Notification/Toaster.vue +446 -0
  112. package/src/components/Notification/notification.samples.ts +45 -0
  113. package/src/components/Notification/notifications.test.ts +99 -0
  114. package/src/components/Notification/notifications.ts +172 -0
  115. package/src/components/PageHeader/PageHeader.stories.ts +79 -0
  116. package/src/components/PageHeader/PageHeader.vue +66 -0
  117. package/src/components/Popover/Popover.stories.ts +135 -0
  118. package/src/components/Popover/Popover.vue +192 -0
  119. package/src/components/Popover/popover.ts +20 -0
  120. package/src/components/Progress/Progress.stories.ts +74 -0
  121. package/src/components/Progress/Progress.vue +75 -0
  122. package/src/components/RadioGroup/RadioGroup.stories.ts +90 -0
  123. package/src/components/RadioGroup/RadioGroup.vue +132 -0
  124. package/src/components/RelativeTime/RelativeTime.stories.ts +74 -0
  125. package/src/components/RelativeTime/RelativeTime.vue +51 -0
  126. package/src/components/Select/Select.mdx +202 -0
  127. package/src/components/Select/Select.stories.ts +223 -0
  128. package/src/components/Select/Select.vue +630 -0
  129. package/src/components/Sidebar/Sidebar.stories.ts +192 -0
  130. package/src/components/Sidebar/Sidebar.vue +382 -0
  131. package/src/components/Sidebar/sidebar.ts +20 -0
  132. package/src/components/Slideover/Slideover.stories.ts +225 -0
  133. package/src/components/Slideover/Slideover.vue +365 -0
  134. package/src/components/Spinner/Spinner.stories.ts +79 -0
  135. package/src/components/Spinner/Spinner.vue +97 -0
  136. package/src/components/StatTile/StatTile.stories.ts +75 -0
  137. package/src/components/StatTile/StatTile.vue +89 -0
  138. package/src/components/SummaryList/SummaryList.stories.ts +103 -0
  139. package/src/components/SummaryList/SummaryList.vue +127 -0
  140. package/src/components/Switch/Switch.stories.ts +100 -0
  141. package/src/components/Switch/Switch.vue +96 -0
  142. package/src/components/Table/Table.stories.ts +212 -0
  143. package/src/components/Table/Table.vue +223 -0
  144. package/src/components/Table/table.test.ts +65 -0
  145. package/src/components/Table/table.ts +70 -0
  146. package/src/components/Tooltip/Tooltip.stories.ts +57 -0
  147. package/src/components/Tooltip/Tooltip.vue +86 -0
  148. package/src/examples/AppPages.stories.ts +500 -0
  149. package/src/examples/FormLayout.stories.ts +185 -0
  150. package/src/examples/KestonIdPages.stories.ts +415 -0
  151. package/src/examples/SchoolPages.stories.ts +665 -0
  152. package/src/index.ts +116 -36
  153. package/src/lib/cn.ts +4 -0
  154. package/src/lib/dates.test.ts +68 -0
  155. package/src/lib/dates.ts +86 -0
  156. package/src/lib/initials.ts +8 -0
  157. package/src/lib/parseDate.test.ts +331 -0
  158. package/src/lib/parseDate.ts +561 -0
  159. package/src/lib/useNow.ts +20 -0
  160. package/src/styles/theme.css +101 -0
  161. package/dist/components/AppLauncher.vue.d.ts +0 -10
  162. package/dist/components/Avatar.vue.d.ts +0 -16
  163. package/dist/components/Button.vue.d.ts +0 -27
  164. package/dist/components/Checkbox.vue.d.ts +0 -18
  165. package/dist/components/CloseButton.vue.d.ts +0 -6
  166. package/dist/components/Dialog.vue.d.ts +0 -23
  167. package/dist/components/Drawer.vue.d.ts +0 -34
  168. package/dist/components/DropdownMenu.vue.d.ts +0 -21
  169. package/dist/components/DropdownMenuItem.vue.d.ts +0 -19
  170. package/dist/components/DropdownMenuSeparator.vue.d.ts +0 -6
  171. package/dist/components/Input.vue.d.ts +0 -14
  172. package/dist/components/Label.vue.d.ts +0 -16
  173. package/dist/components/NotificationsMenu.vue.d.ts +0 -3
  174. package/dist/components/Popover.vue.d.ts +0 -23
  175. package/dist/components/ProfileSwitcher.vue.d.ts +0 -24
  176. package/dist/components/Select.vue.d.ts +0 -28
  177. package/dist/components/Separator.vue.d.ts +0 -11
  178. package/dist/components/ToastRenderer.vue.d.ts +0 -3
  179. package/dist/components/Toaster.vue.d.ts +0 -3
  180. package/dist/components/Tooltip.vue.d.ts +0 -23
  181. package/dist/components/TopBar.vue.d.ts +0 -34
  182. package/dist/favicon-old.svg +0 -1
  183. package/dist/favicon.svg +0 -1
  184. package/dist/keston-ui.cjs +0 -15
  185. package/dist/lib/toast.d.ts +0 -29
  186. package/dist/lib/types.d.ts +0 -5
  187. package/src/components/AppLauncher.vue +0 -63
  188. package/src/components/Avatar.vue +0 -51
  189. package/src/components/Button.vue +0 -54
  190. package/src/components/Checkbox.vue +0 -47
  191. package/src/components/CloseButton.vue +0 -24
  192. package/src/components/Dialog.vue +0 -65
  193. package/src/components/Drawer.vue +0 -202
  194. package/src/components/DropdownMenu.vue +0 -41
  195. package/src/components/DropdownMenuItem.vue +0 -28
  196. package/src/components/DropdownMenuSeparator.vue +0 -12
  197. package/src/components/Input.vue +0 -21
  198. package/src/components/Label.vue +0 -15
  199. package/src/components/NotificationsMenu.vue +0 -115
  200. package/src/components/Popover.vue +0 -46
  201. package/src/components/ProfileSwitcher.vue +0 -113
  202. package/src/components/Select.vue +0 -97
  203. package/src/components/Separator.vue +0 -30
  204. package/src/components/ToastItem.vue +0 -158
  205. package/src/components/ToastRenderer.vue +0 -113
  206. package/src/components/Toaster.vue +0 -11
  207. package/src/components/Tooltip.vue +0 -48
  208. package/src/components/TopBar.vue +0 -52
  209. package/src/lib/toast.ts +0 -95
  210. package/src/lib/types.ts +0 -5
@@ -0,0 +1,232 @@
1
+ <script setup lang="ts">
2
+ import { computed, type HTMLAttributes } from 'vue'
3
+ import { ArrowLeftRight, LogOut, Menu, Settings } from '@lucide/vue'
4
+ import { cn } from '../../lib/cn'
5
+ import Avatar from '../Avatar/Avatar.vue'
6
+ import Button from '../Button/Button.vue'
7
+ import { initials as toInitials } from '../../lib/initials'
8
+ import Popover from '../Popover/Popover.vue'
9
+
10
+ export interface HeaderUser {
11
+ name: string
12
+ email?: string
13
+ /** Organisation or school the account belongs to, shown at the top of the account menu. */
14
+ organisation?: string
15
+ /** Picture URL. Initials are shown without one. */
16
+ avatar?: string
17
+ }
18
+
19
+ export type HeaderUserAction = 'account-settings' | 'switch-account' | 'log-out'
20
+
21
+ /**
22
+ * The bar across the top of every Keston app. It is the same in every app:
23
+ * a menu button (to open the Sidebar on small screens), the app's name, an
24
+ * optional middle area, actions, and the signed-in user's account menu on
25
+ * the right (Account settings, Switch account, Log out). Each part can be
26
+ * overridden with a slot.
27
+ *
28
+ * Slots: leading (before the title, e.g. a logo), title, default (the middle,
29
+ * fills the space between title and trailing), trailing (actions before the
30
+ * account menu), account (replaces the account menu, or the Sign in button),
31
+ * account-menu (extra content in the account menu, above Switch account /
32
+ * Log out: `{ close }`).
33
+ */
34
+ export interface HeaderProps {
35
+ /** Name of the app or area, shown next to the menu button. */
36
+ title?: string
37
+ /**
38
+ * Show the menu button, which emits `menu` when pressed. Hide it on wide
39
+ * screens with `ui.menu`, e.g. `{ menu: 'lg:hidden' }`.
40
+ */
41
+ menu?: boolean
42
+ /** Whether what the menu button opens is open, for screen readers (aria-expanded). */
43
+ menuOpen?: boolean
44
+ /** Id of what the menu button opens (aria-controls). */
45
+ menuControls?: string
46
+ /** Accessible name of the menu button. */
47
+ menuLabel?: string
48
+ /**
49
+ * The signed-in user. Shows their picture or initials on the right, which
50
+ * opens the account menu. Without it, a Sign in button is shown instead
51
+ * (see `signIn`).
52
+ */
53
+ user?: HeaderUser
54
+ /**
55
+ * Which actions the account menu offers. Each emits an event of the same
56
+ * name. Account settings sits under the user's name; Switch account and Log
57
+ * out share the bottom row (left and right).
58
+ */
59
+ userActions?: HeaderUserAction[]
60
+ /** Show a Sign in button (emits `sign-in`) when there is no `user`. */
61
+ signIn?: boolean
62
+ /**
63
+ * Stay at the top while the page scrolls (on by default). Turn off when the
64
+ * header isn't inside the element that scrolls.
65
+ */
66
+ sticky?: boolean
67
+ class?: HTMLAttributes['class']
68
+ /** Extra classes for the parts. */
69
+ ui?: {
70
+ menu?: HTMLAttributes['class']
71
+ title?: HTMLAttributes['class']
72
+ middle?: HTMLAttributes['class']
73
+ trailing?: HTMLAttributes['class']
74
+ userMenu?: HTMLAttributes['class']
75
+ }
76
+ }
77
+
78
+ const props = withDefaults(defineProps<HeaderProps>(), {
79
+ menu: false,
80
+ menuOpen: undefined,
81
+ menuLabel: 'Menu',
82
+ userActions: () => ['account-settings', 'switch-account', 'log-out'],
83
+ signIn: true,
84
+ sticky: true
85
+ })
86
+
87
+ const emit = defineEmits<{
88
+ menu: []
89
+ 'account-settings': []
90
+ 'switch-account': []
91
+ 'log-out': []
92
+ 'sign-in': []
93
+ }>()
94
+
95
+ defineSlots<{
96
+ leading?(): unknown
97
+ title?(): unknown
98
+ default?(): unknown
99
+ trailing?(): unknown
100
+ account?(): unknown
101
+ 'account-menu'?(props: { close: () => void }): unknown
102
+ }>()
103
+
104
+ // "Ben Smith" → "BS", "Ben" → "B".
105
+ const initials = computed(() => toInitials(props.user?.name ?? ''))
106
+
107
+ // The avatar button's name. When it shows initials (no picture), they are
108
+ // its visible text, so the name starts with them, for voice control ("click
109
+ // BS"): "BS, account: Ben Smith". Not in brackets: axe doesn't match "(BS)".
110
+ const accountLabel = computed(() => {
111
+ if (!props.user) return ''
112
+ if (props.user.avatar || !initials.value) return `Account: ${props.user.name}`
113
+ return `${initials.value}, account: ${props.user.name}`
114
+ })
115
+
116
+ function runAction(action: HeaderUserAction, close: () => void) {
117
+ close()
118
+ // Vue can't emit a name held in a variable with full typing, so switch.
119
+ if (action === 'account-settings') emit('account-settings')
120
+ else if (action === 'switch-account') emit('switch-account')
121
+ else emit('log-out')
122
+ }
123
+
124
+ </script>
125
+
126
+ <template>
127
+ <header
128
+ :class="cn(
129
+ 'flex h-12 shrink-0 items-center gap-2 border-b border-black/15 bg-white px-4 text-black',
130
+ sticky && 'sticky top-0 z-40',
131
+ props.class
132
+ )"
133
+ >
134
+ <!-- A ghost icon Button; the negative margin lines its icon up with the
135
+ header's padding, as the × does in Slideover. -->
136
+ <Button
137
+ v-if="menu"
138
+ variant="ghost"
139
+ square
140
+ :aria-label="menuLabel"
141
+ :aria-expanded="menuOpen"
142
+ :aria-controls="menuControls"
143
+ :class="cn('-ml-2.5', ui?.menu)"
144
+ @click="emit('menu')"
145
+ >
146
+ <Menu />
147
+ </Button>
148
+
149
+ <slot name="leading" />
150
+
151
+ <span
152
+ v-if="title || $slots.title"
153
+ :class="cn('min-w-0 truncate text-base font-bold', ui?.title)"
154
+ >
155
+ <slot name="title">{{ title }}</slot>
156
+ </span>
157
+
158
+ <div :class="cn('flex min-w-0 flex-1 items-center', ui?.middle)">
159
+ <slot />
160
+ </div>
161
+
162
+ <div
163
+ v-if="$slots.trailing || $slots.account || user || signIn"
164
+ :class="cn('flex shrink-0 items-center gap-1', ui?.trailing)"
165
+ >
166
+ <slot name="trailing" />
167
+
168
+ <slot name="account">
169
+ <!-- Account menu: the user's avatar opens a panel with who is signed
170
+ in and the account actions. -->
171
+ <Popover v-if="user" :content="{ align: 'end' }" :ui="{ content: cn('w-72', ui?.userMenu) }">
172
+ <Button variant="ghost" square :aria-label="accountLabel" class="-mr-1.5 rounded-full">
173
+ <Avatar :user="user" size="sm" decorative />
174
+ </Button>
175
+
176
+ <template #content="{ close }">
177
+ <div class="flex flex-col">
178
+ <p
179
+ v-if="user.organisation"
180
+ class="truncate px-4 pt-3 text-xs font-bold text-black/65 uppercase"
181
+ >
182
+ {{ user.organisation }}
183
+ </p>
184
+ <!-- Two columns: the avatar at the top left, beside the name and
185
+ email; Account settings sits under them in the text column,
186
+ without moving the avatar. -->
187
+ <div class="grid grid-cols-[auto_minmax(0,1fr)] items-start gap-x-3 gap-y-2 px-4 py-3">
188
+ <Avatar :user="user" size="lg" decorative />
189
+ <div class="flex min-w-0 flex-col">
190
+ <span class="truncate font-bold">{{ user.name }}</span>
191
+ <span v-if="user.email" class="truncate text-sm text-black/65">{{ user.email }}</span>
192
+ </div>
193
+ <Button
194
+ v-if="userActions.includes('account-settings')"
195
+ size="sm"
196
+ class="col-start-2 justify-self-start"
197
+ @click="runAction('account-settings', close)"
198
+ >
199
+ <template #leading><Settings /></template>
200
+ Account settings
201
+ </Button>
202
+ </div>
203
+
204
+ <slot name="account-menu" :close="close" />
205
+
206
+ <!-- Switch account on the left, Log out on the right. -->
207
+ <div
208
+ v-if="userActions.includes('switch-account') || userActions.includes('log-out')"
209
+ class="flex items-center gap-2 border-t border-black/15 px-4 py-3"
210
+ >
211
+ <Button v-if="userActions.includes('switch-account')" @click="runAction('switch-account', close)">
212
+ <template #leading><ArrowLeftRight /></template>
213
+ Switch account
214
+ </Button>
215
+ <Button
216
+ v-if="userActions.includes('log-out')"
217
+ class="ml-auto"
218
+ @click="runAction('log-out', close)"
219
+ >
220
+ <template #leading><LogOut /></template>
221
+ Log out
222
+ </Button>
223
+ </div>
224
+ </div>
225
+ </template>
226
+ </Popover>
227
+
228
+ <Button v-else-if="signIn" size="sm" variant="primary" @click="emit('sign-in')">Sign in</Button>
229
+ </slot>
230
+ </div>
231
+ </header>
232
+ </template>
@@ -0,0 +1,63 @@
1
+ import type { Meta, StoryObj } from "@storybook/vue3-vite";
2
+ import StatTile from "../StatTile/StatTile.vue";
3
+ import InfoTip from "./InfoTip.vue";
4
+
5
+ const meta = {
6
+ title: "Components/Overlay/InfoTip",
7
+ component: InfoTip,
8
+ tags: ["autodocs"],
9
+ args: {
10
+ label: "About attendance",
11
+ text: "Sessions attended as a share of possible sessions this term. Authorised absences count against it.",
12
+ },
13
+ decorators: [() => ({ template: '<div class="p-16"><story /></div>' })],
14
+ render: (args) => ({
15
+ components: { InfoTip },
16
+ setup: () => ({ args }),
17
+ template: `
18
+ <span class="inline-flex items-center gap-1 text-sm">Attendance <InfoTip v-bind="args" /></span>`,
19
+ }),
20
+ } satisfies Meta<typeof InfoTip>;
21
+
22
+ export default meta;
23
+ type Story = StoryObj<typeof meta>;
24
+
25
+ /**
26
+ * Hover, click or tab to the `?`. Screen readers hear its label ("About
27
+ * attendance") and the text as its description, without opening it.
28
+ */
29
+ export const Default: Story = {};
30
+
31
+ /** Explaining a column heading. */
32
+ export const InTableHeading: Story = {
33
+ args: { label: "About FSM", text: "Free school meals: eligible at any point in the last six years." },
34
+ render: (args) => ({
35
+ components: { InfoTip },
36
+ setup: () => ({ args }),
37
+ template: `
38
+ <table class="text-sm">
39
+ <thead><tr class="border-b border-black/15">
40
+ <th class="px-3 py-2 text-left font-bold">Student</th>
41
+ <th class="px-3 py-2 text-left font-bold"><span class="inline-flex items-center gap-1">FSM <InfoTip v-bind="args" /></span></th>
42
+ </tr></thead>
43
+ <tbody>
44
+ <tr><td class="px-3 py-2">Ada Okafor</td><td class="px-3 py-2">Yes</td></tr>
45
+ <tr><td class="px-3 py-2">Sam Patel</td><td class="px-3 py-2">No</td></tr>
46
+ </tbody>
47
+ </table>`,
48
+ }),
49
+ };
50
+
51
+ /** Fields have it built in: set `helper-placement="icon"` on Input, Select, RadioGroup and the rest. */
52
+ export const BesideAFigure: Story = {
53
+ decorators: [() => ({ template: '<div class="max-w-64 p-16"><story /></div>' })],
54
+ render: (args) => ({
55
+ components: { InfoTip, StatTile },
56
+ setup: () => ({ args }),
57
+ template: `
58
+ <div class="flex items-start gap-1">
59
+ <StatTile label="Attendance" value="94.2%" class="flex-1" />
60
+ <InfoTip v-bind="args" />
61
+ </div>`,
62
+ }),
63
+ };
@@ -0,0 +1,50 @@
1
+ <script setup lang="ts">
2
+ import { useId, type HTMLAttributes } from 'vue'
3
+ import { CircleHelp } from '@lucide/vue'
4
+ import { cn } from '../../lib/cn'
5
+ import Popover from '../Popover/Popover.vue'
6
+
7
+ /**
8
+ * A `?` that explains the thing beside it: a field's label, a table column,
9
+ * a figure. Opens on hover, and on click / tap (which keeps it open, so it
10
+ * works on touch screens). Focus is never moved, so typing in a nearby
11
+ * control isn't interrupted.
12
+ */
13
+ export interface InfoTipProps {
14
+ /** The explanation. Keep it to a sentence or two. */
15
+ text: string
16
+ /**
17
+ * The button's accessible name: what it explains, e.g. "About Student ID".
18
+ * Screen readers then hear `text` as its description.
19
+ */
20
+ label: string
21
+ class?: HTMLAttributes['class']
22
+ }
23
+
24
+ const props = defineProps<InfoTipProps>()
25
+
26
+ const descriptionId = `${useId()}-description`
27
+ </script>
28
+
29
+ <template>
30
+ <Popover mode="hover" :content="{ side: 'top', sideOffset: 6 }">
31
+ <button
32
+ type="button"
33
+ :aria-label="label"
34
+ :aria-describedby="descriptionId"
35
+ :class="cn(
36
+ 'inline-flex shrink-0 rounded-full p-0.5 align-middle text-black transition hover:bg-black/10 data-[state=open]:bg-black/10',
37
+ 'outline-2 outline-offset-1 outline-transparent focus-visible:outline-focus [&_svg]:size-4',
38
+ props.class
39
+ )"
40
+ >
41
+ <CircleHelp aria-hidden="true" />
42
+ <!-- Hidden, but still read as the button's description (the
43
+ aria-label replaces the button's content as its name). -->
44
+ <span :id="descriptionId" hidden>{{ text }}</span>
45
+ </button>
46
+ <template #content>
47
+ <p class="max-w-64 px-3 py-2 text-xs">{{ text }}</p>
48
+ </template>
49
+ </Popover>
50
+ </template>
@@ -0,0 +1,206 @@
1
+ import type { Meta, StoryObj } from "@storybook/vue3-vite";
2
+ import { AtSign, Lock, Mail, Search, User, X } from "@lucide/vue";
3
+ import { computed, ref, watch } from "vue";
4
+ import Button from "../Button/Button.vue";
5
+ import Input, { type InputProps } from "./Input.vue";
6
+
7
+ // Icons offered in the Controls panel. Add any Lucide icon here.
8
+ const icons = { None: null, Search, User, Mail, AtSign, Lock, X };
9
+ type IconName = keyof typeof icons;
10
+
11
+ // Story-only controls on top of the real props.
12
+ type InputStoryArgs = InputProps & {
13
+ placeholder: string;
14
+ leadingIcon: IconName;
15
+ trailingIcon: IconName;
16
+ };
17
+
18
+ const iconControl = {
19
+ options: Object.keys(icons),
20
+ control: { type: "select" },
21
+ table: { category: "Content" },
22
+ } as const;
23
+
24
+ // Splits the story-only args from the real props and resolves the icons.
25
+ function useStoryInput(args: InputStoryArgs) {
26
+ const inputProps = computed(() => {
27
+ const { leadingIcon: _leading, trailingIcon: _trailing, ...rest } = args;
28
+ return rest;
29
+ });
30
+ const leading = computed(() => icons[args.leadingIcon]);
31
+ const trailing = computed(() => icons[args.trailingIcon]);
32
+ const value = ref("");
33
+ return { args, inputProps, leading, trailing, value };
34
+ }
35
+
36
+ const inputTemplate = `
37
+ <Input v-bind="inputProps" v-model="value">
38
+ <template v-if="leading" #leading><component :is="leading" /></template>
39
+ <template v-if="trailing" #trailing><component :is="trailing" /></template>
40
+ </Input>`;
41
+
42
+ const meta = {
43
+ title: "Components/Form/Input",
44
+ component: Input,
45
+ tags: ["autodocs"],
46
+ argTypes: {
47
+ placeholder: { control: "text", table: { category: "Content" } },
48
+ leadingIcon: iconControl,
49
+ trailingIcon: iconControl,
50
+ helperPlacement: { options: ["below", "icon"], control: { type: "inline-radio" } },
51
+ status: { options: [undefined, "error", "success", "loading"], control: { type: "inline-radio" } },
52
+ },
53
+ args: {
54
+ label: "First name",
55
+ placeholder: "",
56
+ helper: "",
57
+ helperPlacement: "below",
58
+ leadingIcon: "None",
59
+ trailingIcon: "None",
60
+ disabled: false,
61
+ },
62
+ decorators: [() => ({ template: '<div class="max-w-80"><story /></div>' })],
63
+ render: (args) => ({
64
+ components: { Input },
65
+ setup: () => useStoryInput(args),
66
+ template: inputTemplate,
67
+ }),
68
+ } satisfies Meta<InputStoryArgs>;
69
+
70
+ export default meta;
71
+ type Story = StoryObj<typeof meta>;
72
+
73
+ /** Playground: use the controls below. */
74
+ export const Default: Story = {};
75
+
76
+ export const HelperBelowLabel: Story = {
77
+ args: {
78
+ label: "Student ID",
79
+ helper: "Found on the student's enrolment letter, e.g. KS-20491.",
80
+ },
81
+ };
82
+
83
+ export const HelperBehindIcon: Story = {
84
+ args: {
85
+ label: "Student ID",
86
+ helper: "Found on the student's enrolment letter, e.g. KS-20491.",
87
+ helperPlacement: "icon",
88
+ },
89
+ };
90
+
91
+ /** Type fewer than 3 characters and leave the field, or more than 20. */
92
+ export const LengthCheck: Story = {
93
+ args: { label: "Username", minLength: 3, maxLength: 20, leadingIcon: "AtSign" },
94
+ };
95
+
96
+ export const WithIcons: Story = {
97
+ args: { label: "Search students", placeholder: "Name or ID", leadingIcon: "Search" },
98
+ };
99
+
100
+ /** `errorText` alone puts the field in its error state. */
101
+ export const WithErrorText: Story = {
102
+ args: { label: "Email", errorText: "Enter a valid email address", leadingIcon: "Mail" },
103
+ };
104
+
105
+ /**
106
+ * How a schema (Zod, VeeValidate…) drives the Input: the form validates and
107
+ * passes the message, or `undefined` when valid, to `errorText`. Checked when
108
+ * you leave the field, then again as you type once an error is showing.
109
+ * In an app this would be e.g. `:error-text="errors.email"` from VeeValidate.
110
+ */
111
+ export const SchemaValidation: Story = {
112
+ args: { label: "Email", placeholder: "name@school.org", leadingIcon: "Mail" },
113
+ render: (args) => ({
114
+ components: { Input },
115
+ setup() {
116
+ const story = useStoryInput(args);
117
+ const error = ref<string>();
118
+ // Stand-in for z.string().min(1, …).email(…)
119
+ const validate = (value: string) =>
120
+ !value ? "Enter an email address"
121
+ : !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value) ? "Enter a valid email address"
122
+ : undefined;
123
+ const onBlur = () => (error.value = validate(story.value.value));
124
+ const onInput = () => { if (error.value) error.value = validate(story.value.value); };
125
+ return { ...story, error, onBlur, onInput };
126
+ },
127
+ template: `
128
+ <Input v-bind="inputProps" v-model="value" :error-text="error" @blur="onBlur" @input="onInput">
129
+ <template v-if="leading" #leading><component :is="leading" /></template>
130
+ </Input>`,
131
+ }),
132
+ };
133
+
134
+ export const ErrorStatus: Story = {
135
+ args: { label: "Email", status: "error", statusText: "Enter a valid email address", leadingIcon: "Mail" },
136
+ };
137
+
138
+ export const SuccessStatus: Story = {
139
+ args: { label: "Username", status: "success", statusText: "Username is available", leadingIcon: "AtSign" },
140
+ };
141
+
142
+ /**
143
+ * Checking a username as you type: a small spinner shows "Checking
144
+ * availability…" until typing pauses and the check comes back, then the result. Here the check is faked with a
145
+ * short delay; try "admin", "ben" or "keston" for one that's taken.
146
+ */
147
+ export const UsernameAvailability: Story = {
148
+ args: { label: "Username", placeholder: "e.g. jsmith", leadingIcon: "AtSign", minLength: 3 },
149
+ render: (args) => ({
150
+ components: { Input },
151
+ setup() {
152
+ const story = useStoryInput(args);
153
+ const taken = ["admin", "ben", "keston", "jsmith"];
154
+ const status = ref<InputProps["status"]>();
155
+ const statusText = ref<string>();
156
+ let timer: ReturnType<typeof setTimeout> | undefined;
157
+ let check = 0;
158
+
159
+ watch(story.value, (value) => {
160
+ clearTimeout(timer);
161
+ // Only the latest check counts, in case an older one comes back late.
162
+ const id = ++check;
163
+ const name = value.trim().toLowerCase();
164
+ // Too short: nothing to check yet (the length check says why on blur).
165
+ if (name.length < 3) {
166
+ status.value = statusText.value = undefined;
167
+ return;
168
+ }
169
+ // Show it's checking straight away, so an earlier result is never
170
+ // shown for a different name, then "ask the server" once typing pauses.
171
+ status.value = "loading";
172
+ statusText.value = "Checking availability…";
173
+ timer = setTimeout(async () => {
174
+ await new Promise((resolve) => setTimeout(resolve, 1200));
175
+ if (id !== check) return;
176
+ const free = !taken.includes(name);
177
+ status.value = free ? "success" : "error";
178
+ statusText.value = free ? "Username available" : "Username unavailable";
179
+ }, 400);
180
+ });
181
+
182
+ return { ...story, status, statusText };
183
+ },
184
+ template: `
185
+ <Input v-bind="inputProps" v-model="value" :status="status" :status-text="statusText" autocomplete="username">
186
+ <template v-if="leading" #leading><component :is="leading" /></template>
187
+ </Input>`,
188
+ }),
189
+ };
190
+
191
+ export const Disabled: Story = { args: { disabled: true, placeholder: "Not editable" } };
192
+
193
+ /** Lined up with a medium basic Button to compare height, border and padding. */
194
+ export const NextToButton: Story = {
195
+ args: { label: "Search students", placeholder: "Name or ID", leadingIcon: "Search" },
196
+ decorators: [() => ({ template: '<div class="max-w-120"><story /></div>' })],
197
+ render: (args) => ({
198
+ components: { Input, Button },
199
+ setup: () => useStoryInput(args),
200
+ template: `
201
+ <div class="flex items-end gap-2">
202
+ <div class="flex-1">${inputTemplate}</div>
203
+ <Button><template #leading><component :is="leading" /></template>Search</Button>
204
+ </div>`,
205
+ }),
206
+ };
@@ -0,0 +1,90 @@
1
+ <script setup lang="ts">
2
+ import { computed, ref, type HTMLAttributes } from 'vue'
3
+ import FormField, { type FormFieldStatus } from '../FormField/FormField.vue'
4
+
5
+ // Attributes like placeholder, type, name and autocomplete go to the <input>,
6
+ // not the wrapper.
7
+ defineOptions({ inheritAttrs: false })
8
+
9
+ export type InputStatus = FormFieldStatus
10
+
11
+ export interface InputProps {
12
+ /** Visible label. Always set it: it is also the input's accessible name. */
13
+ label: string
14
+ /** Extra guidance about what to enter. */
15
+ helper?: string
16
+ /** Show helper text under the label, or behind a `?` icon next to it. */
17
+ helperPlacement?: 'below' | 'icon'
18
+ /**
19
+ * Error message, e.g. from a Zod / VeeValidate schema. When set (and not
20
+ * empty) the field shows as an error with this text; leave it undefined
21
+ * when the value is valid. Overrides `status`, `statusText` and the length
22
+ * check.
23
+ */
24
+ errorText?: string
25
+ /** Colours the field and the status text. Overrides the length check. */
26
+ status?: InputStatus
27
+ /** Message under the field, e.g. why the value is invalid. */
28
+ statusText?: string
29
+ /** Shows an error once the user leaves a field that is too short. */
30
+ minLength?: number
31
+ /** Shows an error as soon as the value is too long. Typing is not blocked. */
32
+ maxLength?: number
33
+ disabled?: boolean
34
+ class?: HTMLAttributes['class']
35
+ }
36
+
37
+ const props = withDefaults(defineProps<InputProps>(), {
38
+ helperPlacement: 'below'
39
+ })
40
+
41
+ const model = defineModel<string>({ default: '' })
42
+
43
+ // ── Length check ────────────────────────────────────────────────────────────
44
+ // "Too short" waits until the user has left the field once, so it doesn't
45
+ // complain while they are still typing. "Too long" shows straight away.
46
+ const touched = ref(false)
47
+
48
+ const lengthError = computed(() => {
49
+ const length = model.value.length
50
+ if (props.maxLength !== undefined && length > props.maxLength) {
51
+ return `Must be ${props.maxLength} characters or fewer`
52
+ }
53
+ if (touched.value && props.minLength !== undefined && length > 0 && length < props.minLength) {
54
+ return `Must be at least ${props.minLength} characters`
55
+ }
56
+ return undefined
57
+ })
58
+
59
+ // errorText is handled by FormField; status / statusText fall back to the length check.
60
+ const status = computed(() => props.status ?? (lengthError.value ? 'error' : undefined))
61
+ const statusText = computed(() => props.statusText ?? lengthError.value)
62
+ </script>
63
+
64
+ <template>
65
+ <FormField
66
+ :label="label"
67
+ :helper="helper"
68
+ :helper-placement="helperPlacement"
69
+ :error-text="errorText"
70
+ :status="status"
71
+ :status-text="statusText"
72
+ :disabled="disabled"
73
+ :class="props.class"
74
+ >
75
+ <template v-if="$slots.leading" #leading><slot name="leading" /></template>
76
+ <template #default="{ id, describedBy, invalid }">
77
+ <input
78
+ :id="id"
79
+ v-model="model"
80
+ v-bind="$attrs"
81
+ :disabled="disabled"
82
+ :aria-invalid="invalid"
83
+ :aria-describedby="describedBy"
84
+ class="h-5 min-w-0 flex-1 bg-transparent outline-none placeholder:text-black/55 disabled:cursor-not-allowed"
85
+ @blur="touched = true"
86
+ />
87
+ </template>
88
+ <template v-if="$slots.trailing" #trailing><slot name="trailing" /></template>
89
+ </FormField>
90
+ </template>