@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,109 @@
1
+ <script setup lang="ts">
2
+ import { computed, type HTMLAttributes } from 'vue'
3
+ import { NumberFieldDecrement, NumberFieldIncrement, NumberFieldInput, NumberFieldRoot } from 'reka-ui'
4
+ import { Minus, Plus } from '@lucide/vue'
5
+ import FormField, { type FormFieldStatus } from '../FormField/FormField.vue'
6
+
7
+ // Attributes like placeholder and name go to the <input>, not the wrapper.
8
+ defineOptions({ inheritAttrs: false })
9
+
10
+ export interface InputNumberProps {
11
+ /** Visible label. Always set it: it is also the input's accessible name. */
12
+ label: string
13
+ /** Extra guidance about what to enter. */
14
+ helper?: string
15
+ /** Show helper text under the label, or behind a `?` icon next to it. */
16
+ helperPlacement?: 'below' | 'icon'
17
+ /** Error message, e.g. from a Zod schema. Overrides `status` / `statusText`. */
18
+ errorText?: string
19
+ /** Colours the field and the status text. */
20
+ status?: FormFieldStatus
21
+ /** Message under the field. */
22
+ statusText?: string
23
+ /** Lowest allowed value. The − button is disabled when reached. */
24
+ min?: number
25
+ /** Highest allowed value. The + button is disabled when reached. */
26
+ max?: number
27
+ /** How much − / + and the arrow keys change the value. */
28
+ step?: number
29
+ /** How the number is displayed, e.g. `{ style: 'currency', currency: 'GBP' }`. */
30
+ formatOptions?: Intl.NumberFormatOptions
31
+ /** Locale for formatting. Defaults to the browser's. */
32
+ locale?: string
33
+ disabled?: boolean
34
+ class?: HTMLAttributes['class']
35
+ }
36
+
37
+ const props = withDefaults(defineProps<InputNumberProps>(), {
38
+ helperPlacement: 'below',
39
+ step: 1
40
+ })
41
+
42
+ /** The number, or undefined when the field is empty. */
43
+ const model = defineModel<number | undefined>()
44
+
45
+ // What screen readers say for the value. Without it, VoiceOver reads a number
46
+ // field that has a min and max as a percentage of that range (year group 9 of
47
+ // 7–13 → "33%"). This is the number as displayed, e.g. "9" or "£12.50".
48
+ const valueText = computed(() =>
49
+ model.value === undefined
50
+ ? undefined
51
+ : new Intl.NumberFormat(props.locale, props.formatOptions).format(model.value)
52
+ )
53
+
54
+ // The − / + buttons are full-height square segments at each end of the box,
55
+ // split from the value by a divider, so the field looks like
56
+ // [ − | value | + ]. Negative margins cancel the box's padding (px-3 py-2), so
57
+ // each button is 36×36 inside the 38px box. They behave like a basic Button:
58
+ // white fill that darkens on hover / press, icon at full colour (the box mutes
59
+ // decorative icons). When disabled (min / max reached) only the icon fades.
60
+ const stepButton = [
61
+ 'flex w-9 shrink-0 -my-2 self-stretch items-center justify-center bg-white cursor-pointer transition',
62
+ 'not-disabled:hover:brightness-90 not-disabled:active:brightness-80 [&_svg]:opacity-100!',
63
+ 'disabled:cursor-not-allowed disabled:[&_svg]:opacity-40!'
64
+ ]
65
+ const decrement = '-ml-3 rounded-l border-r border-black/15'
66
+ const increment = '-mr-3 rounded-r border-l border-black/15'
67
+ </script>
68
+
69
+ <template>
70
+ <FormField
71
+ :label="label"
72
+ :helper="helper"
73
+ :helper-placement="helperPlacement"
74
+ :error-text="errorText"
75
+ :status="status"
76
+ :status-text="statusText"
77
+ :disabled="disabled"
78
+ :class="$props.class"
79
+ >
80
+ <template #default="{ id, describedBy, invalid }">
81
+ <!-- `contents` lets the buttons and input sit directly in the box's row. -->
82
+ <NumberFieldRoot
83
+ :id="id"
84
+ v-model="model"
85
+ :min="min"
86
+ :max="max"
87
+ :step="step"
88
+ :format-options="formatOptions"
89
+ :locale="locale"
90
+ :disabled="disabled"
91
+ class="contents"
92
+ >
93
+ <NumberFieldDecrement :class="[stepButton, decrement]">
94
+ <Minus />
95
+ </NumberFieldDecrement>
96
+ <NumberFieldInput
97
+ v-bind="$attrs"
98
+ :aria-invalid="invalid"
99
+ :aria-describedby="describedBy"
100
+ :aria-valuetext="valueText"
101
+ class="h-5 min-w-0 flex-1 bg-transparent text-center tabular-nums outline-none placeholder:text-black/55 disabled:cursor-not-allowed"
102
+ />
103
+ <NumberFieldIncrement :class="[stepButton, increment]">
104
+ <Plus />
105
+ </NumberFieldIncrement>
106
+ </NumberFieldRoot>
107
+ </template>
108
+ </FormField>
109
+ </template>
@@ -0,0 +1,105 @@
1
+ import type { Meta, StoryObj } from "@storybook/vue3-vite";
2
+ import { Lock } from "@lucide/vue";
3
+ import { ref } from "vue";
4
+ import Button from "../Button/Button.vue";
5
+ import Input from "../Input/Input.vue";
6
+ import InputPassword, { type InputPasswordProps } from "./InputPassword.vue";
7
+
8
+ // Story-only controls on top of the real props.
9
+ type InputPasswordStoryArgs = InputPasswordProps & {
10
+ placeholder: string;
11
+ autocomplete: "current-password" | "new-password";
12
+ };
13
+
14
+ const meta = {
15
+ title: "Components/Form/InputPassword",
16
+ component: InputPassword,
17
+ tags: ["autodocs"],
18
+ argTypes: {
19
+ placeholder: { control: "text", table: { category: "Content" } },
20
+ autocomplete: {
21
+ options: ["current-password", "new-password"],
22
+ control: { type: "inline-radio" },
23
+ table: { category: "Attributes" },
24
+ },
25
+ helperPlacement: { options: ["below", "icon"], control: { type: "inline-radio" } },
26
+ status: { options: [undefined, "error", "success", "loading"], control: { type: "inline-radio" } },
27
+ },
28
+ args: {
29
+ label: "Password",
30
+ placeholder: "",
31
+ helper: "",
32
+ helperPlacement: "below",
33
+ autocomplete: "current-password",
34
+ disabled: false,
35
+ },
36
+ decorators: [() => ({ template: '<div class="max-w-80"><story /></div>' })],
37
+ render: (args) => ({
38
+ components: { InputPassword },
39
+ setup: () => ({ args, value: ref("") }),
40
+ template: `<InputPassword v-bind="args" v-model="value" />`,
41
+ }),
42
+ } satisfies Meta<InputPasswordStoryArgs>;
43
+
44
+ export default meta;
45
+ type Story = StoryObj<typeof meta>;
46
+
47
+ /** Playground: type, then press the eye to show or hide the password. Turn Caps Lock on to see the note. */
48
+ export const Default: Story = {};
49
+
50
+ /** Choosing a password: `autocomplete="new-password"` lets password managers suggest and save one. */
51
+ export const NewPassword: Story = {
52
+ args: {
53
+ label: "New password",
54
+ helper: "At least 12 characters. A few unrelated words works well.",
55
+ autocomplete: "new-password",
56
+ },
57
+ };
58
+
59
+ export const WithLeadingIcon: Story = {
60
+ render: (args) => ({
61
+ components: { InputPassword, Lock },
62
+ setup: () => ({ args, value: ref("") }),
63
+ template: `
64
+ <InputPassword v-bind="args" v-model="value">
65
+ <template #leading><Lock /></template>
66
+ </InputPassword>`,
67
+ }),
68
+ };
69
+
70
+ /** `errorText`, e.g. after a failed sign-in. It replaces the Caps Lock note. */
71
+ export const WithErrorText: Story = {
72
+ args: { errorText: "Incorrect password" },
73
+ };
74
+
75
+ export const Disabled: Story = { args: { disabled: true } };
76
+
77
+ /**
78
+ * A sign-in form. Show the password, then press Sign in: it's hidden again
79
+ * when the form is submitted.
80
+ */
81
+ export const SignInForm: Story = {
82
+ render: () => ({
83
+ components: { InputPassword, Input, Button },
84
+ setup() {
85
+ const email = ref("");
86
+ const password = ref("");
87
+ const error = ref<string>();
88
+ const submitting = ref(false);
89
+ async function signIn() {
90
+ error.value = undefined;
91
+ submitting.value = true;
92
+ await new Promise((resolve) => setTimeout(resolve, 800));
93
+ submitting.value = false;
94
+ if (password.value !== "correct horse") error.value = "Incorrect email or password";
95
+ }
96
+ return { email, password, error, submitting, signIn };
97
+ },
98
+ template: `
99
+ <form class="flex flex-col gap-4" @submit.prevent="signIn">
100
+ <Input v-model="email" label="Email" type="email" autocomplete="username" />
101
+ <InputPassword v-model="password" label="Password" :error-text="error" />
102
+ <Button type="submit" variant="primary" :status="submitting ? 'loading' : 'idle'" loading-text="Signing in">Sign in</Button>
103
+ </form>`,
104
+ }),
105
+ };
@@ -0,0 +1,132 @@
1
+ <script setup lang="ts">
2
+ import { computed, onBeforeUnmount, onMounted, ref, useAttrs, type HTMLAttributes } from 'vue'
3
+ import { Eye, EyeOff } from '@lucide/vue'
4
+ import FormField, { type FormFieldStatus } from '../FormField/FormField.vue'
5
+
6
+ /**
7
+ * A password field: Input with a Show / Hide button at the end, a "Caps Lock
8
+ * is on" note while typing, and the password hidden again when its form is
9
+ * submitted (so it's never left on screen, and password managers still see
10
+ * a password field).
11
+ */
12
+
13
+ // Attributes like name, autocomplete and placeholder go to the <input>.
14
+ defineOptions({ inheritAttrs: false })
15
+
16
+ export type InputPasswordStatus = FormFieldStatus
17
+
18
+ export interface InputPasswordProps {
19
+ /** Visible label. Always set it: it is also the input's accessible name. */
20
+ label: string
21
+ /** Extra guidance, e.g. the password rules when choosing a new one. */
22
+ helper?: string
23
+ /** Show helper text under the label, or behind a `?` icon next to it. */
24
+ helperPlacement?: 'below' | 'icon'
25
+ /**
26
+ * Error message, e.g. from a Zod / VeeValidate schema or "Incorrect
27
+ * password". When set (and not empty) the field shows as an error with this
28
+ * text. Overrides `status`, `statusText` and the Caps Lock note.
29
+ */
30
+ errorText?: string
31
+ /** Colours the field and the status text. */
32
+ status?: InputPasswordStatus
33
+ /** Message under the field. Overrides the Caps Lock note. */
34
+ statusText?: string
35
+ disabled?: boolean
36
+ class?: HTMLAttributes['class']
37
+ }
38
+
39
+ const props = withDefaults(defineProps<InputPasswordProps>(), {
40
+ helperPlacement: 'below'
41
+ })
42
+
43
+ const model = defineModel<string>({ default: '' })
44
+ const attrs = useAttrs()
45
+
46
+ // Signing in is the common case; pass autocomplete="new-password" when
47
+ // choosing one, so password managers offer to generate and save it.
48
+ const autocomplete = computed(() => (attrs.autocomplete as string | undefined) ?? 'current-password')
49
+
50
+ const input = ref<HTMLInputElement>()
51
+ const visible = ref(false)
52
+
53
+ // ── Hide on submit ──────────────────────────────────────────────────────────
54
+ // Submitting with the password shown would leave it on screen, and some
55
+ // password managers only save fields that are type="password".
56
+ const hide = () => (visible.value = false)
57
+ let form: HTMLFormElement | null = null
58
+ onMounted(() => {
59
+ form = input.value?.form ?? null
60
+ form?.addEventListener('submit', hide)
61
+ })
62
+ onBeforeUnmount(() => form?.removeEventListener('submit', hide))
63
+
64
+ // ── Caps Lock ───────────────────────────────────────────────────────────────
65
+ // Browsers only report Caps Lock on key and pointer events, so the note shows
66
+ // from the first key press and clears when the field loses focus.
67
+ const capsLock = ref(false)
68
+ function checkCapsLock(event: KeyboardEvent) {
69
+ capsLock.value = event.getModifierState?.('CapsLock') ?? false
70
+ }
71
+
72
+ const statusText = computed(() => props.statusText ?? (capsLock.value ? 'Caps Lock is on' : undefined))
73
+
74
+ // Same square end segment as InputNumber's + button, so the field reads
75
+ // [ password | 👁 ]. Pressing it keeps focus (and the caret) in the input.
76
+ const toggle = [
77
+ 'flex w-9 shrink-0 -my-2 -mr-3 self-stretch items-center justify-center',
78
+ 'bg-white rounded-r border-l border-black/15 cursor-pointer transition',
79
+ 'not-disabled:hover:brightness-90 not-disabled:active:brightness-80 [&_svg]:opacity-100!',
80
+ 'outline-none focus-visible:bg-black/5 disabled:cursor-not-allowed'
81
+ ]
82
+ </script>
83
+
84
+ <template>
85
+ <FormField
86
+ :label="label"
87
+ :helper="helper"
88
+ :helper-placement="helperPlacement"
89
+ :error-text="errorText"
90
+ :status="status"
91
+ :status-text="statusText"
92
+ :disabled="disabled"
93
+ :class="props.class"
94
+ >
95
+ <template v-if="$slots.leading" #leading><slot name="leading" /></template>
96
+ <!-- e.g. "Forgot password?": shown by the label, reached by Tab after the field. -->
97
+ <template v-if="$slots['label-trailing']" #label-trailing><slot name="label-trailing" /></template>
98
+ <template #default="{ id, describedBy, invalid }">
99
+ <input
100
+ :id="id"
101
+ ref="input"
102
+ v-model="model"
103
+ v-bind="$attrs"
104
+ :type="visible ? 'text' : 'password'"
105
+ :autocomplete="autocomplete"
106
+ autocapitalize="off"
107
+ autocorrect="off"
108
+ spellcheck="false"
109
+ :disabled="disabled"
110
+ :aria-invalid="invalid"
111
+ :aria-describedby="describedBy"
112
+ class="h-5 min-w-0 flex-1 bg-transparent outline-none placeholder:text-black/55 disabled:cursor-not-allowed"
113
+ @keydown="checkCapsLock"
114
+ @keyup="checkCapsLock"
115
+ @blur="capsLock = false"
116
+ />
117
+ <button
118
+ type="button"
119
+ :class="toggle"
120
+ :aria-controls="id"
121
+ :aria-pressed="visible"
122
+ :disabled="disabled"
123
+ @pointerdown.prevent
124
+ @click="visible = !visible"
125
+ >
126
+ <span class="sr-only">Show password</span>
127
+ <EyeOff v-if="visible" aria-hidden="true" />
128
+ <Eye v-else aria-hidden="true" />
129
+ </button>
130
+ </template>
131
+ </FormField>
132
+ </template>
@@ -0,0 +1,100 @@
1
+ import type { Meta, StoryObj } from "@storybook/vue3-vite";
2
+ import { ref, watch } from "vue";
3
+ import InputRichText, { type InputRichTextProps } from "./InputRichText.vue";
4
+
5
+ const meta = {
6
+ title: "Components/Form/InputRichText",
7
+ component: InputRichText,
8
+ tags: ["autodocs"],
9
+ argTypes: {
10
+ helperPlacement: { options: ["below", "icon"], control: { type: "inline-radio" } },
11
+ status: { options: [undefined, "error", "success", "loading"], control: { type: "inline-radio" } },
12
+ },
13
+ args: {
14
+ label: "Notes",
15
+ placeholder: "Add a note…",
16
+ helper: "",
17
+ helperPlacement: "below",
18
+ disabled: false,
19
+ },
20
+ decorators: [() => ({ template: '<div class="max-w-120"><story /></div>' })],
21
+ render: (args) => ({
22
+ components: { InputRichText },
23
+ setup: () => ({ args, value: ref("") }),
24
+ template: `
25
+ <InputRichText v-bind="args" v-model="value" />
26
+ <pre class="mt-4 text-xs whitespace-pre-wrap text-black/65">{{ value || '(empty)' }}</pre>`,
27
+ }),
28
+ } satisfies Meta<typeof InputRichText>;
29
+
30
+ export default meta;
31
+ type Story = StoryObj<typeof meta>;
32
+
33
+ /**
34
+ * Playground. Select text and press **B**, *I* or U, or use ⌘B / ⌘I / ⌘U
35
+ * (Ctrl on Windows). The HTML it produces shows underneath.
36
+ */
37
+ export const Default: Story = {};
38
+
39
+ export const WithText: Story = {
40
+ args: { label: "Notes on Ada Okafor" },
41
+ render: (args) => ({
42
+ components: { InputRichText },
43
+ setup: () => ({
44
+ args,
45
+ value: ref(
46
+ "<p>Settled in well. <strong>Allergic to nuts</strong>: kitchen told on 2 September.</p><p>Mum prefers calls after 3pm; <em>not</em> on Fridays. <u>Check the medical form is signed</u> before the trip.</p>",
47
+ ),
48
+ }),
49
+ template: '<InputRichText v-bind="args" v-model="value" />',
50
+ }),
51
+ };
52
+
53
+ export const HelperBehindIcon: Story = {
54
+ args: { helper: "Seen by staff only, not by parents.", helperPlacement: "icon" },
55
+ };
56
+
57
+ /** Long notes grow the field to 15 lines, then it scrolls. */
58
+ export const LongNotes: Story = {
59
+ render: (args) => ({
60
+ components: { InputRichText },
61
+ setup: () => ({
62
+ args,
63
+ value: ref(
64
+ Array.from({ length: 12 }, (_, i) => `<p>Week ${i + 1}: attended every session, homework handed in.</p>`).join(""),
65
+ ),
66
+ }),
67
+ template: '<InputRichText v-bind="args" v-model="value" />',
68
+ }),
69
+ };
70
+
71
+ /**
72
+ * Saving as you type: "Saving…" while the note saves (faked here with a
73
+ * delay after typing pauses), then "Saved".
74
+ */
75
+ export const AutoSave: Story = {
76
+ render: (args) => ({
77
+ components: { InputRichText },
78
+ setup() {
79
+ const value = ref("");
80
+ const status = ref<InputRichTextProps["status"]>();
81
+ const statusText = ref<string>();
82
+ let timer: ReturnType<typeof setTimeout> | undefined;
83
+ watch(value, () => {
84
+ clearTimeout(timer);
85
+ status.value = "loading";
86
+ statusText.value = "Saving…";
87
+ timer = setTimeout(() => {
88
+ status.value = "success";
89
+ statusText.value = "Saved";
90
+ }, 1000);
91
+ });
92
+ return { args, value, status, statusText };
93
+ },
94
+ template: '<InputRichText v-bind="args" v-model="value" :status="status" :status-text="statusText" />',
95
+ }),
96
+ };
97
+
98
+ export const WithErrorText: Story = { args: { errorText: "Add a note before saving" } };
99
+
100
+ export const Disabled: Story = { args: { disabled: true } };
@@ -0,0 +1,194 @@
1
+ <script setup lang="ts">
2
+ import { defineComponent, onBeforeUnmount, ref, watch, watchEffect, type HTMLAttributes, type PropType } from 'vue'
3
+ import { EditorContent, useEditor } from '@tiptap/vue-3'
4
+ import type { Editor } from '@tiptap/core'
5
+ import Document from '@tiptap/extension-document'
6
+ import Paragraph from '@tiptap/extension-paragraph'
7
+ import Text from '@tiptap/extension-text'
8
+ import HardBreak from '@tiptap/extension-hard-break'
9
+ import Bold from '@tiptap/extension-bold'
10
+ import Italic from '@tiptap/extension-italic'
11
+ import Underline from '@tiptap/extension-underline'
12
+ import { Placeholder, UndoRedo } from '@tiptap/extensions'
13
+ import { Bold as BoldIcon, Italic as ItalicIcon, Underline as UnderlineIcon } from '@lucide/vue'
14
+ import { cn } from '../../lib/cn'
15
+ import FormField, { type FormFieldStatus } from '../FormField/FormField.vue'
16
+ import Button from '../Button/Button.vue'
17
+ import Tooltip from '../Tooltip/Tooltip.vue'
18
+
19
+ /**
20
+ * Several lines of text with bold, italic and underline, e.g. notes on a
21
+ * student or an application. Only those three: no headings, lists, links or
22
+ * colours, so notes look the same everywhere. Pasted text keeps its bold,
23
+ * italic and underline and loses everything else.
24
+ *
25
+ * The value is HTML using only <p>, <br>, <strong>, <em> and <u>. Sanitise
26
+ * it on the server like any HTML from a browser before showing it elsewhere.
27
+ */
28
+ export interface InputRichTextProps {
29
+ /** Visible label. Always set it: it is also the editor's accessible name. */
30
+ label: string
31
+ /** Extra guidance about what to write. */
32
+ helper?: string
33
+ /** Show helper text under the label, or behind a `?` icon next to it. */
34
+ helperPlacement?: 'below' | 'icon'
35
+ /** Error message, e.g. from a Zod schema. Overrides `status` / `statusText`. */
36
+ errorText?: string
37
+ /** Colours the field and the status text, e.g. 'loading' while it saves. */
38
+ status?: FormFieldStatus
39
+ /** Message under the field. */
40
+ statusText?: string
41
+ /** Shown in the empty editor, e.g. "Add a note…". */
42
+ placeholder?: string
43
+ disabled?: boolean
44
+ class?: HTMLAttributes['class']
45
+ }
46
+
47
+ const props = withDefaults(defineProps<InputRichTextProps>(), {
48
+ helperPlacement: 'below'
49
+ })
50
+
51
+ /** The text as HTML; an empty string when there's nothing in it. */
52
+ const model = defineModel<string>({ default: '' })
53
+
54
+ // An empty editor still holds an empty paragraph; report that as ''.
55
+ const html = () => (editor.value?.isEmpty ? '' : (editor.value?.getHTML() ?? ''))
56
+
57
+ const editor = useEditor({
58
+ content: model.value,
59
+ editable: !props.disabled,
60
+ extensions: [
61
+ Document,
62
+ Paragraph,
63
+ Text,
64
+ HardBreak,
65
+ Bold,
66
+ Italic,
67
+ Underline,
68
+ UndoRedo,
69
+ Placeholder.configure({ placeholder: () => props.placeholder ?? '' })
70
+ ],
71
+ onUpdate: () => {
72
+ model.value = html()
73
+ },
74
+ // Every change, including moving the cursor, updates the toolbar.
75
+ onCreate: ({ editor }) => updateActive(editor),
76
+ onTransaction: ({ editor }) => updateActive(editor)
77
+ })
78
+
79
+ // Which formats are on at the cursor (or for the next text typed), for the
80
+ // toolbar's pressed state. Tiptap's isActive() isn't reactive by itself.
81
+ type Mark = 'bold' | 'italic' | 'underline'
82
+ const active = ref<Record<Mark, boolean>>({ bold: false, italic: false, underline: false })
83
+ function updateActive(e: Editor) {
84
+ active.value = { bold: e.isActive('bold'), italic: e.isActive('italic'), underline: e.isActive('underline') }
85
+ }
86
+
87
+ // A value set from outside (e.g. a form reset) replaces the text; the
88
+ // editor's own updates come back unchanged and are skipped.
89
+ watch(model, (value) => {
90
+ if (editor.value && value !== html()) editor.value.commands.setContent(value, { emitUpdate: false })
91
+ })
92
+
93
+ watch(
94
+ () => props.disabled,
95
+ (disabled) => editor.value?.setEditable(!disabled)
96
+ )
97
+
98
+ onBeforeUnmount(() => editor.value?.destroy())
99
+
100
+ // The editable area gets the id and ARIA values FormField hands out. It's a
101
+ // contenteditable <div>, so it needs the textbox role and the label by id.
102
+ // A render-less child, so the values (only known inside FormField's slot)
103
+ // are applied in a watcher rather than while rendering.
104
+ type FieldIds = { id: string; labelId: string; describedBy?: string; invalid?: true }
105
+ const EditorAttributes = defineComponent({
106
+ props: { ids: { type: Object as PropType<FieldIds>, required: true } },
107
+ setup(attrs) {
108
+ watchEffect(() => {
109
+ const { id, labelId, describedBy, invalid } = attrs.ids
110
+ editor.value?.setOptions({
111
+ editorProps: {
112
+ attributes: {
113
+ id,
114
+ role: 'textbox',
115
+ 'aria-multiline': 'true',
116
+ 'aria-labelledby': labelId,
117
+ ...(describedBy ? { 'aria-describedby': describedBy } : {}),
118
+ ...(invalid ? { 'aria-invalid': 'true' } : {}),
119
+ class: editable
120
+ }
121
+ }
122
+ })
123
+ })
124
+ return () => null
125
+ }
126
+ })
127
+
128
+ const isMac = typeof navigator !== 'undefined' && /Mac|iPhone|iPad/.test(navigator.platform)
129
+ const mod = isMac ? '⌘' : 'Ctrl+'
130
+
131
+ const marks = [
132
+ { name: 'bold', label: 'Bold', icon: BoldIcon, key: 'B', toggle: () => editor.value?.chain().focus().toggleBold().run() },
133
+ { name: 'italic', label: 'Italic', icon: ItalicIcon, key: 'I', toggle: () => editor.value?.chain().focus().toggleItalic().run() },
134
+ { name: 'underline', label: 'Underline', icon: UnderlineIcon, key: 'U', toggle: () => editor.value?.chain().focus().toggleUnderline().run() }
135
+ ] as const
136
+
137
+ // The text area: at least 4 lines, then it grows to 15 lines and scrolls.
138
+ // Paragraphs are 8px apart. The placeholder is drawn by Tiptap's Placeholder
139
+ // extension on the first empty paragraph.
140
+ const editable = [
141
+ // 16px on touch screens, so iPhone doesn't zoom in when it's tapped.
142
+ 'min-h-24 max-h-80 overflow-y-auto px-3 py-2 text-sm pointer-coarse:text-base outline-none cursor-text',
143
+ '[&_p+p]:mt-2',
144
+ '[&_p.is-editor-empty:first-child]:before:pointer-events-none [&_p.is-editor-empty:first-child]:before:float-left',
145
+ '[&_p.is-editor-empty:first-child]:before:h-0 [&_p.is-editor-empty:first-child]:before:text-black/55',
146
+ '[&_p.is-editor-empty:first-child]:before:content-[attr(data-placeholder)]'
147
+ ].join(' ')
148
+ </script>
149
+
150
+ <template>
151
+ <FormField
152
+ :label="label"
153
+ :helper="helper"
154
+ :helper-placement="helperPlacement"
155
+ :error-text="errorText"
156
+ :status="status"
157
+ :status-text="statusText"
158
+ :disabled="disabled"
159
+ :class="props.class"
160
+ >
161
+ <template #default="slot">
162
+ <!-- Fills FormField's box: negative margins cancel its padding, so the
163
+ toolbar's line and the text area run to the edges. -->
164
+ <div class="-mx-3 -my-2 flex min-w-0 flex-1 flex-col self-stretch">
165
+ <!-- Pressing a button keeps focus (and the selection) in the text. -->
166
+ <div
167
+ role="toolbar"
168
+ :aria-label="`Formatting for ${label}`"
169
+ :aria-controls="slot.id"
170
+ class="flex items-center gap-0.5 border-b border-black/15 px-1.5 py-1 [&_svg]:opacity-100!"
171
+ @mousedown.prevent
172
+ >
173
+ <Tooltip v-for="mark in marks" :key="mark.name" :text="`${mark.label} (${mod}${mark.key})`" :disabled="disabled">
174
+ <Button
175
+ variant="ghost"
176
+ size="sm"
177
+ square
178
+ :aria-label="mark.label"
179
+ :aria-pressed="active[mark.name]"
180
+ :aria-keyshortcuts="`${isMac ? 'Meta' : 'Control'}+${mark.key}`"
181
+ :disabled="disabled"
182
+ class="aria-pressed:bg-black/15"
183
+ @click="mark.toggle()"
184
+ >
185
+ <component :is="mark.icon" />
186
+ </Button>
187
+ </Tooltip>
188
+ </div>
189
+ <EditorAttributes :ids="slot" />
190
+ <EditorContent :editor="editor" :class="cn('min-w-0', disabled && 'cursor-not-allowed [&_*]:cursor-not-allowed')" />
191
+ </div>
192
+ </template>
193
+ </FormField>
194
+ </template>