@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.
- package/README.md +98 -31
- package/dist/components/Age/Age.vue.d.ts +19 -0
- package/dist/components/AppShell/AppShell.vue.d.ts +108 -0
- package/dist/components/Avatar/Avatar.vue.d.ts +34 -0
- package/dist/components/AvatarGroup/AvatarGroup.vue.d.ts +25 -0
- package/dist/components/Badge/Badge.vue.d.ts +41 -0
- package/dist/components/Breadcrumb/Breadcrumb.vue.d.ts +39 -0
- package/dist/components/Button/Button.vue.d.ts +69 -0
- package/dist/components/Calendar/Calendar.vue.d.ts +34 -0
- package/dist/components/Card/Card.vue.d.ts +56 -0
- package/dist/components/Checkbox/Checkbox.vue.d.ts +40 -0
- package/dist/components/Checklist/Checklist.vue.d.ts +18 -0
- package/dist/components/Chip/Chip.vue.d.ts +44 -0
- package/dist/components/Divider/Divider.vue.d.ts +36 -0
- package/dist/components/EmptyState/EmptyState.vue.d.ts +55 -0
- package/dist/components/{ToastItem.vue.d.ts → FormField/FieldMessage.vue.d.ts} +10 -2
- package/dist/components/FormField/FormField.vue.d.ts +61 -0
- package/dist/components/Header/Header.vue.d.ts +104 -0
- package/dist/components/InfoTip/InfoTip.vue.d.ts +20 -0
- package/dist/components/Input/Input.vue.d.ts +56 -0
- package/dist/components/InputCode/InputCode.vue.d.ts +53 -0
- package/dist/components/InputDate/InputDate.vue.d.ts +49 -0
- package/dist/components/InputNumber/InputNumber.vue.d.ts +44 -0
- package/dist/components/InputPassword/InputPassword.vue.d.ts +51 -0
- package/dist/components/InputRichText/InputRichText.vue.d.ts +44 -0
- package/dist/components/Link/Link.vue.d.ts +39 -0
- package/dist/components/Notification/Notification.vue.d.ts +62 -0
- package/dist/components/Notification/NotificationPane.vue.d.ts +38 -0
- package/dist/components/Notification/Toaster.vue.d.ts +51 -0
- package/dist/components/Notification/notification.samples.d.ts +3 -0
- package/dist/components/Notification/notifications.d.ts +115 -0
- package/dist/components/PageHeader/PageHeader.vue.d.ts +43 -0
- package/dist/components/Popover/Popover.vue.d.ts +83 -0
- package/dist/components/Popover/popover.d.ts +16 -0
- package/dist/components/Progress/Progress.vue.d.ts +26 -0
- package/dist/components/RadioGroup/RadioGroup.vue.d.ts +45 -0
- package/dist/components/RelativeTime/RelativeTime.vue.d.ts +22 -0
- package/dist/components/Select/Select.vue.d.ts +110 -0
- package/dist/components/Sidebar/Sidebar.vue.d.ts +106 -0
- package/dist/components/Sidebar/sidebar.d.ts +10 -0
- package/dist/components/Slideover/Slideover.vue.d.ts +125 -0
- package/dist/components/Spinner/Spinner.vue.d.ts +30 -0
- package/dist/components/StatTile/StatTile.vue.d.ts +46 -0
- package/dist/components/SummaryList/SummaryList.vue.d.ts +62 -0
- package/dist/components/Switch/Switch.vue.d.ts +39 -0
- package/dist/components/Table/Table.vue.d.ts +82 -0
- package/dist/components/Table/table.d.ts +36 -0
- package/dist/components/Tooltip/Tooltip.vue.d.ts +46 -0
- package/dist/index.d.ts +91 -25
- package/dist/keston-ui.js +5030 -14786
- package/dist/lib/cn.d.ts +4 -0
- package/dist/lib/dates.d.ts +27 -0
- package/dist/lib/initials.d.ts +2 -0
- package/dist/lib/parseDate.d.ts +44 -0
- package/dist/lib/useNow.d.ts +3 -0
- package/package.json +57 -24
- package/src/components/.gitkeep +0 -0
- package/src/components/Age/Age.stories.ts +24 -0
- package/src/components/Age/Age.vue +56 -0
- package/src/components/AppShell/AppShell.stories.ts +124 -0
- package/src/components/AppShell/AppShell.vue +221 -0
- package/src/components/Avatar/Avatar.stories.ts +56 -0
- package/src/components/Avatar/Avatar.vue +118 -0
- package/src/components/AvatarGroup/AvatarGroup.stories.ts +47 -0
- package/src/components/AvatarGroup/AvatarGroup.vue +97 -0
- package/src/components/Badge/Badge.stories.ts +92 -0
- package/src/components/Badge/Badge.vue +67 -0
- package/src/components/Breadcrumb/Breadcrumb.stories.ts +58 -0
- package/src/components/Breadcrumb/Breadcrumb.vue +109 -0
- package/src/components/Button/Button.mdx +79 -0
- package/src/components/Button/Button.stories.ts +207 -0
- package/src/components/Button/Button.vue +266 -0
- package/src/components/Calendar/Calendar.stories.ts +51 -0
- package/src/components/Calendar/Calendar.vue +364 -0
- package/src/components/Card/Card.stories.ts +109 -0
- package/src/components/Card/Card.vue +115 -0
- package/src/components/Checkbox/Checkbox.stories.ts +102 -0
- package/src/components/Checkbox/Checkbox.vue +102 -0
- package/src/components/Checklist/Checklist.stories.ts +67 -0
- package/src/components/Checklist/Checklist.vue +38 -0
- package/src/components/Chip/Chip.stories.ts +71 -0
- package/src/components/Chip/Chip.vue +86 -0
- package/src/components/Divider/Divider.stories.ts +57 -0
- package/src/components/Divider/Divider.vue +58 -0
- package/src/components/EmptyState/EmptyState.stories.ts +99 -0
- package/src/components/EmptyState/EmptyState.vue +87 -0
- package/src/components/FormField/FieldMessage.vue +81 -0
- package/src/components/FormField/FormField.stories.ts +55 -0
- package/src/components/FormField/FormField.vue +160 -0
- package/src/components/Header/Header.stories.ts +168 -0
- package/src/components/Header/Header.vue +232 -0
- package/src/components/InfoTip/InfoTip.stories.ts +63 -0
- package/src/components/InfoTip/InfoTip.vue +50 -0
- package/src/components/Input/Input.stories.ts +206 -0
- package/src/components/Input/Input.vue +90 -0
- package/src/components/InputCode/InputCode.stories.ts +85 -0
- package/src/components/InputCode/InputCode.vue +145 -0
- package/src/components/InputDate/InputDate.stories.ts +58 -0
- package/src/components/InputDate/InputDate.vue +258 -0
- package/src/components/InputNumber/InputNumber.stories.ts +63 -0
- package/src/components/InputNumber/InputNumber.vue +109 -0
- package/src/components/InputPassword/InputPassword.stories.ts +105 -0
- package/src/components/InputPassword/InputPassword.vue +132 -0
- package/src/components/InputRichText/InputRichText.stories.ts +100 -0
- package/src/components/InputRichText/InputRichText.vue +194 -0
- package/src/components/Link/Link.stories.ts +73 -0
- package/src/components/Link/Link.vue +56 -0
- package/src/components/Notification/Notification.stories.ts +196 -0
- package/src/components/Notification/Notification.vue +136 -0
- package/src/components/Notification/NotificationPane.vue +186 -0
- package/src/components/Notification/Toaster.vue +446 -0
- package/src/components/Notification/notification.samples.ts +45 -0
- package/src/components/Notification/notifications.test.ts +99 -0
- package/src/components/Notification/notifications.ts +172 -0
- package/src/components/PageHeader/PageHeader.stories.ts +79 -0
- package/src/components/PageHeader/PageHeader.vue +66 -0
- package/src/components/Popover/Popover.stories.ts +135 -0
- package/src/components/Popover/Popover.vue +192 -0
- package/src/components/Popover/popover.ts +20 -0
- package/src/components/Progress/Progress.stories.ts +74 -0
- package/src/components/Progress/Progress.vue +75 -0
- package/src/components/RadioGroup/RadioGroup.stories.ts +90 -0
- package/src/components/RadioGroup/RadioGroup.vue +132 -0
- package/src/components/RelativeTime/RelativeTime.stories.ts +74 -0
- package/src/components/RelativeTime/RelativeTime.vue +51 -0
- package/src/components/Select/Select.mdx +202 -0
- package/src/components/Select/Select.stories.ts +223 -0
- package/src/components/Select/Select.vue +630 -0
- package/src/components/Sidebar/Sidebar.stories.ts +192 -0
- package/src/components/Sidebar/Sidebar.vue +382 -0
- package/src/components/Sidebar/sidebar.ts +20 -0
- package/src/components/Slideover/Slideover.stories.ts +225 -0
- package/src/components/Slideover/Slideover.vue +365 -0
- package/src/components/Spinner/Spinner.stories.ts +79 -0
- package/src/components/Spinner/Spinner.vue +97 -0
- package/src/components/StatTile/StatTile.stories.ts +75 -0
- package/src/components/StatTile/StatTile.vue +89 -0
- package/src/components/SummaryList/SummaryList.stories.ts +103 -0
- package/src/components/SummaryList/SummaryList.vue +127 -0
- package/src/components/Switch/Switch.stories.ts +100 -0
- package/src/components/Switch/Switch.vue +96 -0
- package/src/components/Table/Table.stories.ts +212 -0
- package/src/components/Table/Table.vue +223 -0
- package/src/components/Table/table.test.ts +65 -0
- package/src/components/Table/table.ts +70 -0
- package/src/components/Tooltip/Tooltip.stories.ts +57 -0
- package/src/components/Tooltip/Tooltip.vue +86 -0
- package/src/examples/AppPages.stories.ts +500 -0
- package/src/examples/FormLayout.stories.ts +185 -0
- package/src/examples/KestonIdPages.stories.ts +415 -0
- package/src/examples/SchoolPages.stories.ts +665 -0
- package/src/index.ts +116 -36
- package/src/lib/cn.ts +4 -0
- package/src/lib/dates.test.ts +68 -0
- package/src/lib/dates.ts +86 -0
- package/src/lib/initials.ts +8 -0
- package/src/lib/parseDate.test.ts +331 -0
- package/src/lib/parseDate.ts +561 -0
- package/src/lib/useNow.ts +20 -0
- package/src/styles/theme.css +101 -0
- package/dist/components/AppLauncher.vue.d.ts +0 -10
- package/dist/components/Avatar.vue.d.ts +0 -16
- package/dist/components/Button.vue.d.ts +0 -27
- package/dist/components/Checkbox.vue.d.ts +0 -18
- package/dist/components/CloseButton.vue.d.ts +0 -6
- package/dist/components/Dialog.vue.d.ts +0 -23
- package/dist/components/Drawer.vue.d.ts +0 -34
- package/dist/components/DropdownMenu.vue.d.ts +0 -21
- package/dist/components/DropdownMenuItem.vue.d.ts +0 -19
- package/dist/components/DropdownMenuSeparator.vue.d.ts +0 -6
- package/dist/components/Input.vue.d.ts +0 -14
- package/dist/components/Label.vue.d.ts +0 -16
- package/dist/components/NotificationsMenu.vue.d.ts +0 -3
- package/dist/components/Popover.vue.d.ts +0 -23
- package/dist/components/ProfileSwitcher.vue.d.ts +0 -24
- package/dist/components/Select.vue.d.ts +0 -28
- package/dist/components/Separator.vue.d.ts +0 -11
- package/dist/components/ToastRenderer.vue.d.ts +0 -3
- package/dist/components/Toaster.vue.d.ts +0 -3
- package/dist/components/Tooltip.vue.d.ts +0 -23
- package/dist/components/TopBar.vue.d.ts +0 -34
- package/dist/favicon-old.svg +0 -1
- package/dist/favicon.svg +0 -1
- package/dist/keston-ui.cjs +0 -15
- package/dist/lib/toast.d.ts +0 -29
- package/dist/lib/types.d.ts +0 -5
- package/src/components/AppLauncher.vue +0 -63
- package/src/components/Avatar.vue +0 -51
- package/src/components/Button.vue +0 -54
- package/src/components/Checkbox.vue +0 -47
- package/src/components/CloseButton.vue +0 -24
- package/src/components/Dialog.vue +0 -65
- package/src/components/Drawer.vue +0 -202
- package/src/components/DropdownMenu.vue +0 -41
- package/src/components/DropdownMenuItem.vue +0 -28
- package/src/components/DropdownMenuSeparator.vue +0 -12
- package/src/components/Input.vue +0 -21
- package/src/components/Label.vue +0 -15
- package/src/components/NotificationsMenu.vue +0 -115
- package/src/components/Popover.vue +0 -46
- package/src/components/ProfileSwitcher.vue +0 -113
- package/src/components/Select.vue +0 -97
- package/src/components/Separator.vue +0 -30
- package/src/components/ToastItem.vue +0 -158
- package/src/components/ToastRenderer.vue +0 -113
- package/src/components/Toaster.vue +0 -11
- package/src/components/Tooltip.vue +0 -48
- package/src/components/TopBar.vue +0 -52
- package/src/lib/toast.ts +0 -95
- package/src/lib/types.ts +0 -5
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from "@storybook/vue3-vite";
|
|
2
|
+
import { onBeforeUnmount, ref } from "vue";
|
|
3
|
+
import Button from "../Button/Button.vue";
|
|
4
|
+
import Progress, { type ProgressProps } from "./Progress.vue";
|
|
5
|
+
|
|
6
|
+
const meta = {
|
|
7
|
+
title: "Components/Element/Progress",
|
|
8
|
+
component: Progress,
|
|
9
|
+
tags: ["autodocs"],
|
|
10
|
+
argTypes: {
|
|
11
|
+
value: { control: { type: "number", min: 0 } },
|
|
12
|
+
max: { control: { type: "number", min: 1 } },
|
|
13
|
+
},
|
|
14
|
+
args: { label: "Uploading report.pdf", value: 40, max: 100, segments: false },
|
|
15
|
+
decorators: [() => ({ template: '<div class="max-w-80"><story /></div>' })],
|
|
16
|
+
} satisfies Meta<ProgressProps>;
|
|
17
|
+
|
|
18
|
+
export default meta;
|
|
19
|
+
type Story = StoryObj<typeof meta>;
|
|
20
|
+
|
|
21
|
+
/** Playground: a line filled to `value` out of `max`. */
|
|
22
|
+
export const Default: Story = {};
|
|
23
|
+
|
|
24
|
+
/** One segment per step, e.g. step 2 of 4 in a multi-step form. */
|
|
25
|
+
export const Segments: Story = {
|
|
26
|
+
args: { label: "Add a student", segments: true, value: 2, max: 4 },
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/** No `value`: something is happening but there's no telling how long it'll take. */
|
|
30
|
+
export const Indeterminate: Story = {
|
|
31
|
+
args: { label: "Loading students", value: null },
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/** Filling up over time, like an upload. */
|
|
35
|
+
export const Filling: Story = {
|
|
36
|
+
render: (args) => ({
|
|
37
|
+
components: { Progress, Button },
|
|
38
|
+
setup() {
|
|
39
|
+
const value = ref(0);
|
|
40
|
+
let timer: ReturnType<typeof setInterval> | undefined;
|
|
41
|
+
function start() {
|
|
42
|
+
clearInterval(timer);
|
|
43
|
+
value.value = 0;
|
|
44
|
+
timer = setInterval(() => {
|
|
45
|
+
value.value = Math.min(100, value.value + Math.random() * 15);
|
|
46
|
+
if (value.value >= 100) clearInterval(timer);
|
|
47
|
+
}, 400);
|
|
48
|
+
}
|
|
49
|
+
onBeforeUnmount(() => clearInterval(timer));
|
|
50
|
+
return { args, value, start };
|
|
51
|
+
},
|
|
52
|
+
template: `
|
|
53
|
+
<div class="flex flex-col gap-3">
|
|
54
|
+
<Progress :label="args.label" :value="value" />
|
|
55
|
+
<Button size="sm" class="self-start" @click="start">Upload</Button>
|
|
56
|
+
</div>`,
|
|
57
|
+
}),
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/** Moving between steps: segments colour in and out. */
|
|
61
|
+
export const Stepping: Story = {
|
|
62
|
+
render: () => ({
|
|
63
|
+
components: { Progress, Button },
|
|
64
|
+
setup: () => ({ step: ref(1) }),
|
|
65
|
+
template: `
|
|
66
|
+
<div class="flex flex-col gap-3">
|
|
67
|
+
<Progress label="Add a student" segments :value="step" :max="4" />
|
|
68
|
+
<div class="flex gap-2">
|
|
69
|
+
<Button size="sm" :disabled="step === 1" @click="step--">Previous</Button>
|
|
70
|
+
<Button size="sm" :disabled="step === 4" @click="step++">Next</Button>
|
|
71
|
+
</div>
|
|
72
|
+
</div>`,
|
|
73
|
+
}),
|
|
74
|
+
};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { computed, type HTMLAttributes } from 'vue'
|
|
3
|
+
import { ProgressIndicator, ProgressRoot } from 'reka-ui'
|
|
4
|
+
import { cn } from '../../lib/cn'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* How far along something is, as a thin bar. Three forms:
|
|
8
|
+
* - a line that fills up, e.g. an upload (`value` out of `max`);
|
|
9
|
+
* - segments, one per step, e.g. a multi-step form (`segments`, with `value`
|
|
10
|
+
* steps filled out of `max`);
|
|
11
|
+
* - indeterminate, when there's no telling how long it'll take (no `value`).
|
|
12
|
+
* Built on Reka UI's Progress, so it's announced as a progress bar.
|
|
13
|
+
*/
|
|
14
|
+
export interface ProgressProps {
|
|
15
|
+
/** Done so far, out of `max`. Leave it out (or null) for indeterminate. */
|
|
16
|
+
value?: number | null
|
|
17
|
+
/** The total. With `segments`, the number of segments. */
|
|
18
|
+
max?: number
|
|
19
|
+
/** Split the bar into `max` segments and fill `value` of them. */
|
|
20
|
+
segments?: boolean
|
|
21
|
+
/** What's progressing, e.g. "Uploading report.pdf". Its accessible name; not shown. */
|
|
22
|
+
label: string
|
|
23
|
+
class?: HTMLAttributes['class']
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const props = withDefaults(defineProps<ProgressProps>(), {
|
|
27
|
+
value: null,
|
|
28
|
+
max: 100
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
const indeterminate = computed(() => props.value === null || props.value === undefined)
|
|
32
|
+
const clamped = computed(() => Math.min(Math.max(props.value ?? 0, 0), props.max))
|
|
33
|
+
const percent = computed(() => (clamped.value / props.max) * 100)
|
|
34
|
+
|
|
35
|
+
// What screen readers say for the value: "Step 2 of 4" for segments,
|
|
36
|
+
// "40%" for a line, nothing (just "busy") when indeterminate.
|
|
37
|
+
const valueText = () =>
|
|
38
|
+
indeterminate.value
|
|
39
|
+
? undefined
|
|
40
|
+
: props.segments
|
|
41
|
+
? `Step ${clamped.value} of ${props.max}`
|
|
42
|
+
: `${Math.round(percent.value)}%`
|
|
43
|
+
|
|
44
|
+
const track = 'h-1.5 overflow-hidden rounded-full bg-black/10'
|
|
45
|
+
// Filling and segments colouring in ease over 300ms.
|
|
46
|
+
const fill = 'h-full rounded-full bg-primary transition-[translate,background-color] duration-300 ease-out motion-reduce:transition-none'
|
|
47
|
+
</script>
|
|
48
|
+
|
|
49
|
+
<template>
|
|
50
|
+
<ProgressRoot
|
|
51
|
+
:model-value="indeterminate ? null : clamped"
|
|
52
|
+
:max="max"
|
|
53
|
+
:get-value-label="() => label"
|
|
54
|
+
:get-value-text="valueText"
|
|
55
|
+
:aria-busy="indeterminate || undefined"
|
|
56
|
+
:class="cn(segments && !indeterminate ? 'flex gap-1' : track, props.class)"
|
|
57
|
+
>
|
|
58
|
+
<!-- Segments: one short track per step, filled up to the value. -->
|
|
59
|
+
<template v-if="segments && !indeterminate">
|
|
60
|
+
<div v-for="i in max" :key="i" :class="cn(track, 'flex-1')">
|
|
61
|
+
<div :class="cn(fill, i <= clamped ? 'translate-x-0' : '-translate-x-full')" />
|
|
62
|
+
</div>
|
|
63
|
+
</template>
|
|
64
|
+
|
|
65
|
+
<!-- Indeterminate: a short bar sweeping across (a gentle pulse with
|
|
66
|
+
reduced motion). -->
|
|
67
|
+
<ProgressIndicator
|
|
68
|
+
v-else-if="indeterminate"
|
|
69
|
+
class="h-full w-2/5 rounded-full bg-primary animate-progress-indeterminate motion-reduce:w-full motion-reduce:translate-x-0 motion-reduce:animate-pulse"
|
|
70
|
+
/>
|
|
71
|
+
|
|
72
|
+
<!-- Line: slides in from the left to the value. -->
|
|
73
|
+
<ProgressIndicator v-else :class="fill" :style="{ translate: `${percent - 100}% 0` }" />
|
|
74
|
+
</ProgressRoot>
|
|
75
|
+
</template>
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from "@storybook/vue3-vite";
|
|
2
|
+
import { computed, ref } from "vue";
|
|
3
|
+
import Button from "../Button/Button.vue";
|
|
4
|
+
import RadioGroup from "./RadioGroup.vue";
|
|
5
|
+
|
|
6
|
+
const year13 = [
|
|
7
|
+
{ label: "Mark as leavers", value: "leavers", description: "Off roll on 31 August" },
|
|
8
|
+
{ label: "Keep on roll", value: "keep", description: "Until results day, then mark as leavers" },
|
|
9
|
+
];
|
|
10
|
+
|
|
11
|
+
const meta = {
|
|
12
|
+
title: "Components/Form/RadioGroup",
|
|
13
|
+
component: RadioGroup,
|
|
14
|
+
tags: ["autodocs"],
|
|
15
|
+
argTypes: {
|
|
16
|
+
helperPlacement: { options: ["below", "icon"], control: { type: "inline-radio" } },
|
|
17
|
+
},
|
|
18
|
+
args: {
|
|
19
|
+
label: "Year 13 students",
|
|
20
|
+
options: year13,
|
|
21
|
+
helper: "",
|
|
22
|
+
helperPlacement: "below",
|
|
23
|
+
errorText: "",
|
|
24
|
+
disabled: false,
|
|
25
|
+
},
|
|
26
|
+
decorators: [() => ({ template: '<div class="max-w-80"><story /></div>' })],
|
|
27
|
+
render: (args) => ({
|
|
28
|
+
components: { RadioGroup },
|
|
29
|
+
setup: () => ({ args, value: ref<string | number>() }),
|
|
30
|
+
template: '<RadioGroup v-bind="args" v-model="value" />',
|
|
31
|
+
}),
|
|
32
|
+
} satisfies Meta<typeof RadioGroup>;
|
|
33
|
+
|
|
34
|
+
export default meta;
|
|
35
|
+
type Story = StoryObj<typeof meta>;
|
|
36
|
+
|
|
37
|
+
/** Playground. Tab into the group, then use the arrow keys to choose. */
|
|
38
|
+
export const Default: Story = {};
|
|
39
|
+
|
|
40
|
+
/** Options with just a label. */
|
|
41
|
+
export const LabelsOnly: Story = {
|
|
42
|
+
args: {
|
|
43
|
+
label: "Lunch",
|
|
44
|
+
options: [
|
|
45
|
+
{ label: "School dinners", value: "dinners" },
|
|
46
|
+
{ label: "Packed lunch", value: "packed" },
|
|
47
|
+
{ label: "Goes home", value: "home" },
|
|
48
|
+
],
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export const HelperBelowLabel: Story = {
|
|
53
|
+
args: { helper: "Students can still be kept on roll later, from their record." },
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export const HelperBehindIcon: Story = {
|
|
57
|
+
args: { helper: "Students can still be kept on roll later, from their record.", helperPlacement: "icon" },
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/** Try "Continue" without choosing; the error clears once something is chosen. */
|
|
61
|
+
export const Required: Story = {
|
|
62
|
+
render: (args) => ({
|
|
63
|
+
components: { RadioGroup, Button },
|
|
64
|
+
setup() {
|
|
65
|
+
const value = ref<string | number>();
|
|
66
|
+
const tried = ref(false);
|
|
67
|
+
const error = computed(() => (tried.value && value.value === undefined ? "Choose what happens to Year 13" : undefined));
|
|
68
|
+
return { args, value, tried, error };
|
|
69
|
+
},
|
|
70
|
+
template: `
|
|
71
|
+
<div class="flex flex-col items-start gap-4">
|
|
72
|
+
<RadioGroup v-bind="args" v-model="value" :error-text="error" required />
|
|
73
|
+
<Button variant="primary" @click="tried = true">Continue</Button>
|
|
74
|
+
</div>`,
|
|
75
|
+
}),
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/** One option that can't be chosen right now. */
|
|
79
|
+
export const DisabledOption: Story = {
|
|
80
|
+
args: {
|
|
81
|
+
label: "Interview format",
|
|
82
|
+
options: [
|
|
83
|
+
{ label: "In person", value: "in-person" },
|
|
84
|
+
{ label: "Video call", value: "video", description: "Turned on by your admin in Settings", disabled: true },
|
|
85
|
+
{ label: "Phone", value: "phone" },
|
|
86
|
+
],
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
export const Disabled: Story = { args: { disabled: true } };
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { computed, useId, type HTMLAttributes } from 'vue'
|
|
3
|
+
import { RadioGroupIndicator, RadioGroupItem, RadioGroupRoot } from 'reka-ui'
|
|
4
|
+
import { cn } from '../../lib/cn'
|
|
5
|
+
import InfoTip from '../InfoTip/InfoTip.vue'
|
|
6
|
+
import FieldMessage from '../FormField/FieldMessage.vue'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* One choice from a few (two to five) that people should see side by side,
|
|
10
|
+
* e.g. "Mark as leavers" or "Keep on roll". For more options, or when the
|
|
11
|
+
* choice is less important than the space it takes, use Select.
|
|
12
|
+
*/
|
|
13
|
+
export type RadioValue = string | number
|
|
14
|
+
|
|
15
|
+
export interface RadioOption {
|
|
16
|
+
label: string
|
|
17
|
+
value: RadioValue
|
|
18
|
+
/** More detail, in smaller text under the label. */
|
|
19
|
+
description?: string
|
|
20
|
+
disabled?: boolean
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface RadioGroupProps {
|
|
24
|
+
/** The question, above the options. Also the group's accessible name. */
|
|
25
|
+
label: string
|
|
26
|
+
options: RadioOption[]
|
|
27
|
+
/** Extra guidance about the choice. */
|
|
28
|
+
helper?: string
|
|
29
|
+
/** Show helper text under the label, or behind a `?` icon next to it. */
|
|
30
|
+
helperPlacement?: 'below' | 'icon'
|
|
31
|
+
/** Error message, e.g. "Choose what happens to Year 13". */
|
|
32
|
+
errorText?: string
|
|
33
|
+
/** Name for native form submission. */
|
|
34
|
+
name?: string
|
|
35
|
+
required?: boolean
|
|
36
|
+
disabled?: boolean
|
|
37
|
+
class?: HTMLAttributes['class']
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const props = withDefaults(defineProps<RadioGroupProps>(), {
|
|
41
|
+
helperPlacement: 'below'
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
/** The chosen option's value; undefined until one is chosen. */
|
|
45
|
+
const model = defineModel<RadioValue | undefined>()
|
|
46
|
+
|
|
47
|
+
const id = useId()
|
|
48
|
+
const labelId = `${id}-label`
|
|
49
|
+
const helperId = `${id}-helper`
|
|
50
|
+
const statusId = `${id}-status`
|
|
51
|
+
|
|
52
|
+
const describedBy = computed(
|
|
53
|
+
() => [props.helper && helperId, props.errorText && statusId].filter(Boolean).join(' ') || undefined
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
// Pressing anywhere in the text (the description, or the gap between it and
|
|
57
|
+
// the label) acts like pressing the label. The description isn't inside the
|
|
58
|
+
// <label>, which would make it part of the accessible name; it stays the
|
|
59
|
+
// description (aria-describedby). The label already presses the control
|
|
60
|
+
// itself, so it's skipped here. Keyboard use is unchanged.
|
|
61
|
+
function pressControl(event: MouseEvent, id: string) {
|
|
62
|
+
if ((event.target as Element).closest('label')) return
|
|
63
|
+
document.getElementById(id)?.click()
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// 16px circle, level with the first line of the label; the chosen one gets a
|
|
67
|
+
// dot. The border is darker than a text box's so each circle is easy to see.
|
|
68
|
+
const circle = [
|
|
69
|
+
'mt-0.5 flex size-4 shrink-0 cursor-pointer items-center justify-center rounded-full border bg-white transition',
|
|
70
|
+
'not-disabled:hover:border-black disabled:cursor-not-allowed',
|
|
71
|
+
'data-[state=checked]:border-primary data-[state=checked]:hover:border-primary',
|
|
72
|
+
'outline-2 outline-offset-2 outline-transparent focus-visible:outline-focus'
|
|
73
|
+
]
|
|
74
|
+
</script>
|
|
75
|
+
|
|
76
|
+
<template>
|
|
77
|
+
<div :class="cn('flex flex-col gap-1.5 text-black', disabled && 'opacity-40', props.class)">
|
|
78
|
+
<div class="flex flex-col gap-0.5">
|
|
79
|
+
<div class="flex items-center gap-1">
|
|
80
|
+
<span :id="labelId" class="text-sm">{{ label }}</span>
|
|
81
|
+
<InfoTip v-if="helper && helperPlacement === 'icon'" :text="helper" :label="`About ${label}`" />
|
|
82
|
+
</div>
|
|
83
|
+
<!-- Behind the ?: hidden, but still the group's description. -->
|
|
84
|
+
<p v-if="helper" :id="helperId" :hidden="helperPlacement === 'icon'" class="text-xs text-black/65">
|
|
85
|
+
{{ helper }}
|
|
86
|
+
</p>
|
|
87
|
+
</div>
|
|
88
|
+
|
|
89
|
+
<RadioGroupRoot
|
|
90
|
+
v-model="model"
|
|
91
|
+
:name="name"
|
|
92
|
+
:required="required"
|
|
93
|
+
:disabled="disabled"
|
|
94
|
+
:aria-labelledby="labelId"
|
|
95
|
+
:aria-describedby="describedBy"
|
|
96
|
+
:aria-invalid="errorText ? true : undefined"
|
|
97
|
+
class="flex flex-col gap-2"
|
|
98
|
+
>
|
|
99
|
+
<div v-for="(option, i) in options" :key="String(option.value)" class="flex items-start gap-2">
|
|
100
|
+
<RadioGroupItem
|
|
101
|
+
:id="`${id}-${i}`"
|
|
102
|
+
:value="option.value"
|
|
103
|
+
:disabled="option.disabled"
|
|
104
|
+
:aria-describedby="option.description ? `${id}-${i}-description` : undefined"
|
|
105
|
+
:class="cn(circle, errorText ? 'border-red' : 'border-black/55', option.disabled && !disabled && 'opacity-40')"
|
|
106
|
+
>
|
|
107
|
+
<RadioGroupIndicator class="size-2 rounded-full bg-primary" />
|
|
108
|
+
</RadioGroupItem>
|
|
109
|
+
<div
|
|
110
|
+
:class="cn(
|
|
111
|
+
'flex min-w-0 flex-col gap-0.5',
|
|
112
|
+
disabled || option.disabled ? 'cursor-not-allowed' : 'cursor-pointer',
|
|
113
|
+
option.disabled && !disabled && 'opacity-40'
|
|
114
|
+
)"
|
|
115
|
+
@click="pressControl($event, `${id}-${i}`)"
|
|
116
|
+
>
|
|
117
|
+
<label
|
|
118
|
+
:for="`${id}-${i}`"
|
|
119
|
+
:class="cn('text-sm', disabled || option.disabled ? 'cursor-not-allowed' : 'cursor-pointer')"
|
|
120
|
+
>
|
|
121
|
+
{{ option.label }}
|
|
122
|
+
</label>
|
|
123
|
+
<p v-if="option.description" :id="`${id}-${i}-description`" class="text-xs text-black/65">
|
|
124
|
+
{{ option.description }}
|
|
125
|
+
</p>
|
|
126
|
+
</div>
|
|
127
|
+
</div>
|
|
128
|
+
</RadioGroupRoot>
|
|
129
|
+
|
|
130
|
+
<FieldMessage :id="statusId" :status="errorText ? 'error' : undefined" :text="errorText" />
|
|
131
|
+
</div>
|
|
132
|
+
</template>
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from "@storybook/vue3-vite";
|
|
2
|
+
import { formatDate } from "../../lib/dates";
|
|
3
|
+
import Age from "../Age/Age.vue";
|
|
4
|
+
import RelativeTime from "./RelativeTime.vue";
|
|
5
|
+
|
|
6
|
+
const minutesAgo = (minutes: number) => new Date(Date.now() - minutes * 60_000);
|
|
7
|
+
|
|
8
|
+
const meta = {
|
|
9
|
+
title: "Components/Data/RelativeTime",
|
|
10
|
+
component: RelativeTime,
|
|
11
|
+
tags: ["autodocs"],
|
|
12
|
+
args: { date: minutesAgo(3 * 60) },
|
|
13
|
+
decorators: [() => ({ template: '<div class="p-12 text-sm"><story /></div>' })],
|
|
14
|
+
} satisfies Meta<typeof RelativeTime>;
|
|
15
|
+
|
|
16
|
+
export default meta;
|
|
17
|
+
type Story = StoryObj<typeof meta>;
|
|
18
|
+
|
|
19
|
+
/** Hover or tab to it: the exact date and time. */
|
|
20
|
+
export const Default: Story = {};
|
|
21
|
+
|
|
22
|
+
/** From just now to years ago, and in the future. */
|
|
23
|
+
export const Range: Story = {
|
|
24
|
+
render: () => ({
|
|
25
|
+
components: { RelativeTime },
|
|
26
|
+
setup: () => ({
|
|
27
|
+
dates: [0.2, 5, 3 * 60, 26 * 60, 3 * 24 * 60, 16 * 24 * 60, 70 * 24 * 60, 500 * 24 * 60, -2 * 24 * 60].map(minutesAgo),
|
|
28
|
+
}),
|
|
29
|
+
template: `
|
|
30
|
+
<ul class="flex flex-col gap-2">
|
|
31
|
+
<li v-for="d in dates" :key="d.getTime()"><RelativeTime :date="d" /></li>
|
|
32
|
+
</ul>`,
|
|
33
|
+
}),
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/** In a sentence, with a custom tooltip. */
|
|
37
|
+
export const InText: Story = {
|
|
38
|
+
render: () => ({
|
|
39
|
+
components: { RelativeTime },
|
|
40
|
+
setup: () => ({ edited: minutesAgo(52), tooltip: (d: Date) => `Edited by Ms Rahman on ${formatDate(d)}` }),
|
|
41
|
+
template: `<p>Last edited <RelativeTime :date="edited" :tooltip="tooltip" />.</p>`,
|
|
42
|
+
}),
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/** The same pattern for ages: the number, with the date of birth on hover (and an upcoming birthday). */
|
|
46
|
+
export const Ages: Story = {
|
|
47
|
+
render: () => ({
|
|
48
|
+
components: { Age },
|
|
49
|
+
setup() {
|
|
50
|
+
const today = new Date();
|
|
51
|
+
const iso = (d: Date) => `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")}`;
|
|
52
|
+
const yearsAgo = (years: number, days = 0) => iso(new Date(today.getFullYear() - years, today.getMonth(), today.getDate() + days));
|
|
53
|
+
return {
|
|
54
|
+
students: [
|
|
55
|
+
{ name: "Amelia Clarke", born: "2013-03-14" },
|
|
56
|
+
{ name: "Noah Patel", born: yearsAgo(12) },
|
|
57
|
+
{ name: "Isla Hughes", born: yearsAgo(16, 3) },
|
|
58
|
+
{ name: "Leo Okafor", born: yearsAgo(11, 1) },
|
|
59
|
+
],
|
|
60
|
+
};
|
|
61
|
+
},
|
|
62
|
+
template: `
|
|
63
|
+
<table class="text-left">
|
|
64
|
+
<thead class="text-black/65"><tr><th class="pr-8 pb-2 font-bold">Student</th><th class="pb-2 text-right font-bold">Age</th></tr></thead>
|
|
65
|
+
<tbody>
|
|
66
|
+
<tr v-for="s in students" :key="s.name">
|
|
67
|
+
<td class="py-1 pr-8">{{ s.name }}</td>
|
|
68
|
+
<td class="py-1 text-right"><Age :birth-date="s.born" /></td>
|
|
69
|
+
</tr>
|
|
70
|
+
</tbody>
|
|
71
|
+
</table>
|
|
72
|
+
<p class="mt-6">With a suffix: <Age birth-date="2013-03-14" suffix="years" /></p>`,
|
|
73
|
+
}),
|
|
74
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { computed, type HTMLAttributes } from 'vue'
|
|
3
|
+
import { cn } from '../../lib/cn'
|
|
4
|
+
import { formatDateTime, relativeTime, toDate, type DateInput } from '../../lib/dates'
|
|
5
|
+
import { useNow } from '../../lib/useNow'
|
|
6
|
+
import Tooltip from '../Tooltip/Tooltip.vue'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A time as people say it, "3 hours ago", with the exact date and time
|
|
10
|
+
* ("Wednesday 11 November 2026 at 09:52") in a tooltip on hover or focus.
|
|
11
|
+
* Keeps itself up to date (every 30 seconds).
|
|
12
|
+
*
|
|
13
|
+
* Use it wherever an app shows a relative date: notifications, "last
|
|
14
|
+
* edited", activity, comments.
|
|
15
|
+
*/
|
|
16
|
+
export interface RelativeTimeProps {
|
|
17
|
+
/** A Date, an ISO string, or a timestamp. */
|
|
18
|
+
date: DateInput
|
|
19
|
+
/** What "now" is, e.g. for tests or a fixed screenshot. Default the live time. */
|
|
20
|
+
now?: Date
|
|
21
|
+
/** Tooltip text. Default the full date and time; e.g. "Edited on {date}". */
|
|
22
|
+
tooltip?: (date: Date) => string
|
|
23
|
+
class?: HTMLAttributes['class']
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const props = defineProps<RelativeTimeProps>()
|
|
27
|
+
|
|
28
|
+
const liveNow = useNow()
|
|
29
|
+
const value = computed(() => toDate(props.date))
|
|
30
|
+
const text = computed(() => relativeTime(value.value, props.now ?? liveNow.value))
|
|
31
|
+
const full = computed(() => (props.tooltip ? props.tooltip(value.value) : formatDateTime(value.value)))
|
|
32
|
+
</script>
|
|
33
|
+
|
|
34
|
+
<template>
|
|
35
|
+
<Tooltip :text="full">
|
|
36
|
+
<!-- Focusable, so keyboard users can open the tooltip too. -->
|
|
37
|
+
<time
|
|
38
|
+
:datetime="value.toISOString()"
|
|
39
|
+
tabindex="0"
|
|
40
|
+
:class="
|
|
41
|
+
cn(
|
|
42
|
+
'cursor-default rounded-sm underline decoration-current/40 decoration-dotted underline-offset-2',
|
|
43
|
+
'outline-2 outline-offset-1 outline-transparent focus-visible:outline-focus forced-colors:not-focus-visible:outline-0',
|
|
44
|
+
props.class
|
|
45
|
+
)
|
|
46
|
+
"
|
|
47
|
+
>
|
|
48
|
+
{{ text }}
|
|
49
|
+
</time>
|
|
50
|
+
</Tooltip>
|
|
51
|
+
</template>
|