@thekestongroup/ui 0.15.0 → 0.16.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 (55) hide show
  1. package/dist/components/ActionList/ActionList.vue.d.ts +63 -0
  2. package/dist/components/Alert/Alert.vue.d.ts +55 -0
  3. package/dist/components/AvatarGroup/AvatarGroup.vue.d.ts +1 -1
  4. package/dist/components/BarChart/BarChart.vue.d.ts +37 -0
  5. package/dist/components/BarChart/ChartData.vue.d.ts +20 -0
  6. package/dist/components/Button/Button.vue.d.ts +1 -1
  7. package/dist/components/Collapsible/Collapsible.vue.d.ts +48 -0
  8. package/dist/components/FileUpload/FileUpload.vue.d.ts +55 -0
  9. package/dist/components/Footer/Footer.vue.d.ts +11 -1
  10. package/dist/components/Header/Header.vue.d.ts +1 -1
  11. package/dist/components/LineChart/LineChart.vue.d.ts +37 -0
  12. package/dist/components/Notification/Notification.vue.d.ts +2 -2
  13. package/dist/components/Progress/Progress.vue.d.ts +14 -1
  14. package/dist/components/Slideover/Slideover.vue.d.ts +7 -5
  15. package/dist/components/Spinner/Spinner.vue.d.ts +1 -1
  16. package/dist/components/ThemeSwitcher/ThemeSwitcher.vue.d.ts +18 -0
  17. package/dist/i18n/en.d.ts +27 -0
  18. package/dist/index.d.ts +17 -0
  19. package/dist/keston-ui.js +4106 -3247
  20. package/dist/lib/chart.d.ts +32 -0
  21. package/dist/lib/theme.d.ts +23 -0
  22. package/package.json +8 -3
  23. package/src/components/ActionList/ActionList.stories.ts +59 -0
  24. package/src/components/ActionList/ActionList.vue +116 -0
  25. package/src/components/Alert/Alert.stories.ts +65 -0
  26. package/src/components/Alert/Alert.vue +110 -0
  27. package/src/components/BarChart/BarChart.stories.ts +62 -0
  28. package/src/components/BarChart/BarChart.vue +153 -0
  29. package/src/components/BarChart/ChartData.vue +64 -0
  30. package/src/components/Calendar/Calendar.vue +3 -2
  31. package/src/components/CardSelect/CardSelect.vue +1 -1
  32. package/src/components/Checkbox/Checkbox.vue +1 -1
  33. package/src/components/CheckboxGroup/CheckboxGroup.vue +1 -1
  34. package/src/components/Collapsible/Collapsible.stories.ts +32 -0
  35. package/src/components/Collapsible/Collapsible.vue +68 -0
  36. package/src/components/FileUpload/FileUpload.stories.ts +38 -0
  37. package/src/components/FileUpload/FileUpload.vue +150 -0
  38. package/src/components/Footer/Footer.stories.ts +10 -2
  39. package/src/components/Footer/Footer.vue +22 -4
  40. package/src/components/Header/Header.stories.ts +20 -0
  41. package/src/components/LineChart/LineChart.stories.ts +47 -0
  42. package/src/components/LineChart/LineChart.vue +173 -0
  43. package/src/components/Progress/Progress.stories.ts +18 -0
  44. package/src/components/Progress/Progress.vue +57 -0
  45. package/src/components/RadioGroup/RadioGroup.vue +2 -2
  46. package/src/components/Select/Select.vue +6 -4
  47. package/src/components/Slideover/Slideover.vue +107 -31
  48. package/src/components/ThemeSwitcher/ThemeSwitcher.stories.ts +22 -0
  49. package/src/components/ThemeSwitcher/ThemeSwitcher.vue +48 -0
  50. package/src/components/Tooltip/Tooltip.vue +1 -1
  51. package/src/i18n/en.ts +16 -4
  52. package/src/index.ts +17 -0
  53. package/src/lib/chart.ts +47 -0
  54. package/src/lib/theme.ts +75 -0
  55. package/src/styles/theme.css +23 -0
@@ -0,0 +1,32 @@
1
+ /** One series in a chart: a name for the legend and table, and its colour slot. */
2
+ export interface ChartSeries {
3
+ /** The key of this series' number in each point's `values`. */
4
+ key: string;
5
+ /** Its name in the legend, the tooltip and the table, e.g. "Failed". */
6
+ label: string;
7
+ /**
8
+ * Which of the eight chart colours (1–8). Default: the series' place in
9
+ * the list. Set it so a series keeps its colour when others are filtered
10
+ * out, e.g. failed sign-ins always red (8).
11
+ */
12
+ color?: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8;
13
+ }
14
+ /** One point along the chart: a day, a month, a school. */
15
+ export interface ChartPoint {
16
+ /** Short label under the bar or along the axis, e.g. "Mon". */
17
+ label: string;
18
+ /** Full label for the tooltip and table, e.g. "Monday 24 November". Default `label`. */
19
+ fullLabel?: string;
20
+ /** A number for each series, by its key. Missing counts as 0. */
21
+ values: Record<string, number | null | undefined>;
22
+ }
23
+ export declare function seriesColor(series: ChartSeries, index: number): string;
24
+ export declare const valueOf: (point: ChartPoint, key: string) => number;
25
+ /**
26
+ * A clean top for the value axis and the ticks up to it: 0, 5, 10, 15
27
+ * rather than 0, 4.3, 8.6, 12.9. About `count` steps of 1, 2, 2.5 or 5 × 10ⁿ.
28
+ */
29
+ export declare function niceScale(max: number, count?: number): {
30
+ max: number;
31
+ ticks: number[];
32
+ };
@@ -0,0 +1,23 @@
1
+ import { Ref } from 'vue';
2
+ /**
3
+ * Light, dark or following the device. Keston UI's colours switch with the
4
+ * `data-theme` attribute on `<html>` (see theme.css, "Dark mode").
5
+ */
6
+ export type Theme = 'light' | 'dark' | 'system';
7
+ /**
8
+ * Applies the person's saved choice (or `fallback` if there is none) and keeps
9
+ * other tabs in step. Call it once in main.ts, before mounting, so the page
10
+ * doesn't flash the wrong colours:
11
+ *
12
+ * initTheme() // follows the device until someone picks
13
+ * initTheme('light') // an app that starts light
14
+ */
15
+ export declare function initTheme(fallback?: Theme): void;
16
+ /**
17
+ * The current theme and a way to change it, e.g. for ThemeSwitcher. A change
18
+ * applies at once and is remembered in this browser.
19
+ */
20
+ export declare function useTheme(): {
21
+ theme: Readonly<Ref<Theme>>;
22
+ setTheme: (theme: Theme) => void;
23
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thekestongroup/ui",
3
- "version": "0.15.0",
3
+ "version": "0.16.0",
4
4
  "description": "Keston UI — Vue 3 + Reka UI + Tailwind CSS component library",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "repository": {
@@ -34,7 +34,8 @@
34
34
  "docs:preview": "vitepress preview site",
35
35
  "new": "node scripts/new-component.mjs",
36
36
  "changeset": "changeset",
37
- "test": "vitest run",
37
+ "test": "vitest run --project unit",
38
+ "test:a11y": "VITE_THEME=light vitest run --project stories && VITE_THEME=dark vitest run --project stories",
38
39
  "test:watch": "vitest"
39
40
  },
40
41
  "peerDependencies": {
@@ -69,12 +70,16 @@
69
70
  "@changesets/cli": "^3.0.3",
70
71
  "@storybook/addon-a11y": "^10.6.5",
71
72
  "@storybook/addon-docs": "^10.6.5",
73
+ "@storybook/addon-vitest": "^10.6.9",
72
74
  "@storybook/builder-vite": "^10.6.5",
73
75
  "@storybook/vue3-vite": "^10.6.5",
74
76
  "@tailwindcss/vite": "^4.3.4",
75
77
  "@types/node": "^26.6.6",
76
78
  "@vitejs/plugin-vue": "^6.0.10",
79
+ "@vitest/browser": "^5.1.2",
80
+ "@vitest/browser-playwright": "^5.1.2",
77
81
  "@vue/tsconfig": "^0.9.1",
82
+ "playwright": "^1.65.1",
78
83
  "remark-gfm": "^4.0.1",
79
84
  "storybook": "^10.6.5",
80
85
  "tailwindcss": "^4.3.4",
@@ -82,7 +87,7 @@
82
87
  "vite": "^8.3.5",
83
88
  "vite-plugin-dts": "^5.1.2",
84
89
  "vitepress": "1.6.4",
85
- "vitest": "^5.1.0",
90
+ "vitest": "^5.1.2",
86
91
  "vue": "^3.5.44",
87
92
  "vue-i18n": "^11.4.20",
88
93
  "vue-tsc": "^3.3.14"
@@ -0,0 +1,59 @@
1
+ import type { Meta, StoryObj } from '@storybook/vue3-vite'
2
+ import { Building2, Search } from '@lucide/vue'
3
+ import ActionList from './ActionList.vue'
4
+ import Avatar from '../Avatar/Avatar.vue'
5
+ import Badge from '../Badge/Badge.vue'
6
+ import Button from '../Button/Button.vue'
7
+
8
+ const meta = {
9
+ title: 'Components/Navigation/ActionList',
10
+ component: ActionList,
11
+ tags: ['autodocs'],
12
+ args: {
13
+ label: 'Your organisations',
14
+ items: [
15
+ { label: 'The Keston School', description: 'keston.school', icon: Building2, current: true },
16
+ { label: 'Keston Prep', description: 'prep.keston.school', icon: Building2 },
17
+ { label: 'Keston Sixth Form', description: "You don't have access to manage it", icon: Building2, disabled: true },
18
+ { label: 'Browse all organisations', description: 'Search every organisation on Keston', icon: Search },
19
+ ],
20
+ },
21
+ decorators: [() => ({ template: '<div class="max-w-md"><story /></div>' })],
22
+ } satisfies Meta<typeof ActionList>
23
+
24
+ export default meta
25
+ type Story = StoryObj<typeof meta>
26
+
27
+ export const Default: Story = {}
28
+
29
+ /** Accounts: an Avatar in the leading slot, a Badge after the name, one switching. */
30
+ export const Accounts: Story = {
31
+ args: {
32
+ label: 'Signed-in accounts',
33
+ items: [
34
+ { key: '1', label: 'Ben Smith', description: 'ben.smith@keston.school' },
35
+ { key: '2', label: 'Jo Bloggs', description: 'jo.bloggs@keston.school', loading: true },
36
+ ],
37
+ },
38
+ render: (args) => ({
39
+ components: { ActionList, Avatar, Badge },
40
+ setup: () => ({ args }),
41
+ template: `
42
+ <ActionList v-bind="args">
43
+ <template #leading="{ item }"><Avatar :user="{ name: item.label }" size="md" decorative /></template>
44
+ <template #badges="{ item }"><Badge v-if="item.key === '1'" size="sm" color="green">Using now</Badge></template>
45
+ </ActionList>`,
46
+ }),
47
+ }
48
+
49
+ /** A second action beside a row, outside its button: e.g. Leave. */
50
+ export const WithAfter: Story = {
51
+ render: (args) => ({
52
+ components: { ActionList, Button },
53
+ setup: () => ({ args }),
54
+ template: `
55
+ <ActionList v-bind="args">
56
+ <template #after="{ item }"><Button v-if="item.label === 'Keston Prep'" size="sm">Leave</Button></template>
57
+ </ActionList>`,
58
+ }),
59
+ }
@@ -0,0 +1,116 @@
1
+ <script setup lang="ts">
2
+ import type { Component, HTMLAttributes } from 'vue'
3
+ import { ChevronRight } from '@lucide/vue'
4
+ import { cn } from '../../lib/cn'
5
+ import Spinner from '../Spinner/Spinner.vue'
6
+
7
+ export interface ActionListItem {
8
+ /** Unique key. Default the label. */
9
+ key?: string
10
+ label: string
11
+ /** A second line, e.g. an email address or a domain. */
12
+ description?: string
13
+ /** An icon before the text (or use the leading slot, e.g. for an Avatar). */
14
+ icon?: Component
15
+ /** Go somewhere through `linkComponent` (e.g. RouterLink), which receives `to`. */
16
+ to?: unknown
17
+ /** Go to a URL. */
18
+ href?: string
19
+ disabled?: boolean
20
+ /** The one you're on now (aria-current). */
21
+ current?: boolean
22
+ /** Show a spinner in place of the arrow, e.g. while switching to it. */
23
+ loading?: boolean
24
+ }
25
+
26
+ /**
27
+ * A list of things to pick one of and go to: accounts to switch to,
28
+ * organisations, connected apps. Each row is a single button or link with an
29
+ * optional icon or avatar, a name and a line under it, and an arrow, in a
30
+ * bordered box with lines between rows.
31
+ *
32
+ * Rows without `to` / `href` are buttons that emit `select`.
33
+ *
34
+ * Slots, each given `{ item }`: leading (before the text, e.g. an Avatar),
35
+ * badges (after the name, e.g. "Current"), after (beside the row, outside
36
+ * the button, e.g. a Leave button).
37
+ */
38
+ export interface ActionListProps {
39
+ items: ActionListItem[]
40
+ /** What the list is, for screen readers, e.g. "Signed-in accounts". */
41
+ label: string
42
+ /** Component used for rows with `to`, e.g. RouterLink. */
43
+ linkComponent?: string | Component
44
+ class?: HTMLAttributes['class']
45
+ }
46
+
47
+ const props = defineProps<ActionListProps>()
48
+ const emit = defineEmits<{ select: [item: ActionListItem] }>()
49
+
50
+ defineSlots<{
51
+ leading?(props: { item: ActionListItem }): unknown
52
+ badges?(props: { item: ActionListItem }): unknown
53
+ after?(props: { item: ActionListItem }): unknown
54
+ }>()
55
+
56
+ function tagFor(item: ActionListItem) {
57
+ if (item.disabled) return 'button'
58
+ if (item.to !== undefined) return props.linkComponent ?? 'a'
59
+ if (item.href !== undefined) return 'a'
60
+ return 'button'
61
+ }
62
+
63
+ function attrsFor(item: ActionListItem) {
64
+ if (item.disabled) return { type: 'button', disabled: true }
65
+ if (item.to !== undefined) return { to: item.to }
66
+ if (item.href !== undefined) return { href: item.href }
67
+ return { type: 'button' }
68
+ }
69
+
70
+ function onClick(item: ActionListItem) {
71
+ if (!item.disabled && item.to === undefined && item.href === undefined) emit('select', item)
72
+ }
73
+
74
+ // The whole row presses; hover and press tint it, focus rings it inside the box.
75
+ const row = [
76
+ 'flex min-w-0 flex-1 cursor-pointer items-center gap-3 px-3 py-3 text-left text-ink no-underline transition-colors',
77
+ 'not-disabled:hover:bg-hover not-disabled:active:bg-pressed aria-[current=true]:bg-selected',
78
+ 'outline-2 -outline-offset-2 outline-transparent focus-visible:outline-focus',
79
+ 'disabled:cursor-not-allowed disabled:opacity-40'
80
+ ]
81
+ </script>
82
+
83
+ <template>
84
+ <!-- overflow-hidden: the rows' tint follows the rounded corners. -->
85
+ <ul
86
+ :aria-label="label"
87
+ :class="cn('flex flex-col divide-y divide-ink/10 overflow-hidden rounded-lg border border-ink/10 bg-surface shadow-xs', props.class)"
88
+ >
89
+ <li v-for="item in items" :key="item.key ?? item.label" class="flex items-center">
90
+ <component
91
+ :is="tagFor(item)"
92
+ v-bind="attrsFor(item)"
93
+ :aria-current="item.current ? 'true' : undefined"
94
+ :aria-busy="item.loading || undefined"
95
+ :class="row"
96
+ @click="onClick(item)"
97
+ >
98
+ <slot name="leading" :item="item">
99
+ <component :is="item.icon" v-if="item.icon" class="size-5 shrink-0 text-ink/65" aria-hidden="true" />
100
+ </slot>
101
+ <span class="flex min-w-0 flex-1 flex-col">
102
+ <span class="flex min-w-0 items-center gap-2">
103
+ <span class="truncate text-sm font-bold">{{ item.label }}</span>
104
+ <slot name="badges" :item="item" />
105
+ </span>
106
+ <span v-if="item.description" class="truncate text-xs text-ink/65">{{ item.description }}</span>
107
+ </span>
108
+ <Spinner v-if="item.loading" size="sm" />
109
+ <ChevronRight v-else-if="!item.disabled" class="size-4 shrink-0 text-ink/55" aria-hidden="true" />
110
+ </component>
111
+ <div v-if="$slots.after" class="shrink-0 pr-3 empty:hidden">
112
+ <slot name="after" :item="item" />
113
+ </div>
114
+ </li>
115
+ </ul>
116
+ </template>
@@ -0,0 +1,65 @@
1
+ import type { Meta, StoryObj } from '@storybook/vue3-vite'
2
+ import { LifeBuoy } from '@lucide/vue'
3
+ import Alert from './Alert.vue'
4
+ import Button from '../Button/Button.vue'
5
+
6
+ const meta = {
7
+ title: 'Components/Element/Alert',
8
+ component: Alert,
9
+ tags: ['autodocs'],
10
+ args: {
11
+ type: 'info',
12
+ title: 'Term dates have changed',
13
+ description: 'The autumn term now starts on Wednesday 3 September.',
14
+ appearance: 'card',
15
+ dismissible: false,
16
+ live: false,
17
+ },
18
+ decorators: [() => ({ template: '<div class="max-w-2xl"><story /></div>' })],
19
+ } satisfies Meta<typeof Alert>
20
+
21
+ export default meta
22
+ type Story = StoryObj<typeof meta>
23
+
24
+ export const Default: Story = {}
25
+
26
+ /** The four types, each with its own icon and colour, and its name for screen readers. */
27
+ export const Types: Story = {
28
+ render: () => ({
29
+ components: { Alert },
30
+ template: `
31
+ <div class="flex flex-col gap-3">
32
+ <Alert type="info" title="Term dates have changed" description="The autumn term now starts on Wednesday 3 September." />
33
+ <Alert type="success" title="All services are running" />
34
+ <Alert type="warning" title="Keston Admin is slow at the moment" description="We're looking into it. Your changes are still saved." />
35
+ <Alert type="error" title="3 students couldn't be moved" description="Their year group is missing. Add it, then try again." />
36
+ </div>`,
37
+ }),
38
+ }
39
+
40
+ /** With actions on the right (under the text on narrow screens). */
41
+ export const WithActions: Story = {
42
+ render: () => ({
43
+ components: { Alert, Button },
44
+ template: `
45
+ <Alert type="warning" title="Your password expires in 7 days" description="Choose a new one before it does.">
46
+ <template #actions><Button size="sm">Change password</Button></template>
47
+ </Alert>`,
48
+ }),
49
+ }
50
+
51
+ /** Banner: full width, under the Header, for something about the whole app. */
52
+ export const Banner: Story = {
53
+ decorators: [() => ({ template: '<div><story /></div>' })],
54
+ render: () => ({
55
+ components: { Alert, Button },
56
+ setup: () => ({ LifeBuoy }),
57
+ template: `
58
+ <Alert appearance="banner" :icon="LifeBuoy">
59
+ You're using a Keston support account in <b>The Keston School</b> until 17:30.
60
+ <template #actions><Button size="sm">End session</Button></template>
61
+ </Alert>`,
62
+ }),
63
+ }
64
+
65
+ export const Dismissible: Story = { args: { dismissible: true } }
@@ -0,0 +1,110 @@
1
+ <script setup lang="ts">
2
+ import { computed, type Component, type HTMLAttributes } from 'vue'
3
+ import { useI18n } from 'vue-i18n'
4
+ import { CircleCheck, CircleX, Info, TriangleAlert, X } from '@lucide/vue'
5
+ import { cn } from '../../lib/cn'
6
+ import Button from '../Button/Button.vue'
7
+
8
+ /**
9
+ * A message that stays on the page until it's dealt with: a problem to fix,
10
+ * a warning, something that went well, or information people need here.
11
+ * For something that has just happened, use a toast Notification instead.
12
+ *
13
+ * Two appearances:
14
+ * - card (default): a rounded box in the page's flow, e.g. above a form.
15
+ * - banner: full width, no corners, a line under it, e.g. under the Header
16
+ * for something that affects the whole app ("You're using a support
17
+ * account").
18
+ *
19
+ * Slots: default (the description, for links or bold text), actions (Buttons
20
+ * on the right; under the text on narrow screens), icon (replaces the icon).
21
+ */
22
+ export interface AlertProps {
23
+ type?: 'info' | 'success' | 'warning' | 'error'
24
+ /** The message, in a few words. Bold. */
25
+ title?: string
26
+ /** More detail under the title. Or use the default slot. */
27
+ description?: string
28
+ appearance?: 'card' | 'banner'
29
+ /** A different icon from the type's own. */
30
+ icon?: Component
31
+ /** Show × to close it; emits `dismiss`. */
32
+ dismissible?: boolean
33
+ /**
34
+ * Announce it to screen readers when it appears (or changes). For alerts
35
+ * shown in response to something, e.g. a save that failed. Not for ones
36
+ * that are on the page from the start.
37
+ */
38
+ live?: boolean
39
+ class?: HTMLAttributes['class']
40
+ }
41
+
42
+ const { t } = useI18n()
43
+ const props = withDefaults(defineProps<AlertProps>(), {
44
+ type: 'info',
45
+ appearance: 'card'
46
+ })
47
+
48
+ const emit = defineEmits<{ dismiss: [] }>()
49
+
50
+ defineSlots<{
51
+ default?(): unknown
52
+ actions?(): unknown
53
+ icon?(): unknown
54
+ }>()
55
+
56
+ // The colour is a tint of the type's text colour, so it reads in both modes.
57
+ const types = {
58
+ info: { icon: Info, text: 'text-blue-fg', box: 'border-blue-fg/30 bg-blue-fg/8' },
59
+ success: { icon: CircleCheck, text: 'text-green-fg', box: 'border-green-fg/30 bg-green-fg/8' },
60
+ warning: { icon: TriangleAlert, text: 'text-yellow-fg', box: 'border-yellow-fg/40 bg-yellow-fg/10' },
61
+ error: { icon: CircleX, text: 'text-red-fg', box: 'border-red-fg/30 bg-red-fg/8' }
62
+ } as const
63
+
64
+ const look = computed(() => types[props.type])
65
+
66
+ // Errors interrupt (role=alert); everything else waits its turn (status).
67
+ const role = computed(() => (props.live ? (props.type === 'error' ? 'alert' : 'status') : undefined))
68
+
69
+ const appearances = {
70
+ card: 'rounded-lg border p-4',
71
+ banner: 'border-b px-4 py-3 sm:px-6'
72
+ }
73
+ </script>
74
+
75
+ <template>
76
+ <div
77
+ :role="role"
78
+ :class="cn('flex flex-wrap items-start gap-x-3 gap-y-2 text-sm text-ink', appearances[appearance], look.box, props.class)"
79
+ >
80
+ <span :class="cn('flex h-5 shrink-0 items-center', look.text)">
81
+ <slot name="icon">
82
+ <component :is="icon ?? look.icon" class="size-4" aria-hidden="true" />
83
+ </slot>
84
+ </span>
85
+ <div class="flex min-w-0 flex-1 basis-48 flex-col gap-0.5">
86
+ <!-- The type in words for screen readers, as the colour and icon don't say it. -->
87
+ <p v-if="title" class="font-bold">
88
+ <span class="sr-only">{{ t(`ui.alert.${type}`) }}: </span>{{ title }}
89
+ </p>
90
+ <div v-if="description || $slots.default" :class="title ? 'text-ink/80' : ''">
91
+ <span v-if="!title" class="sr-only">{{ t(`ui.alert.${type}`) }}: </span>
92
+ <slot>{{ description }}</slot>
93
+ </div>
94
+ </div>
95
+ <div v-if="$slots.actions" class="flex shrink-0 flex-wrap items-center gap-2 self-center">
96
+ <slot name="actions" />
97
+ </div>
98
+ <Button
99
+ v-if="dismissible"
100
+ variant="ghost"
101
+ size="sm"
102
+ square
103
+ :aria-label="t('ui.alert.dismiss')"
104
+ class="-my-1 -mr-1 shrink-0"
105
+ @click="emit('dismiss')"
106
+ >
107
+ <X />
108
+ </Button>
109
+ </div>
110
+ </template>
@@ -0,0 +1,62 @@
1
+ import type { Meta, StoryObj } from '@storybook/vue3-vite'
2
+ import BarChart from './BarChart.vue'
3
+ import Card from '../Card/Card.vue'
4
+
5
+ const week = [
6
+ { label: 'Mon', fullLabel: 'Monday 24 November', values: { success: 182, failed: 6 } },
7
+ { label: 'Tue', fullLabel: 'Tuesday 25 November', values: { success: 201, failed: 3 } },
8
+ { label: 'Wed', fullLabel: 'Wednesday 26 November', values: { success: 176, failed: 14 } },
9
+ { label: 'Thu', fullLabel: 'Thursday 27 November', values: { success: 190, failed: 2 } },
10
+ { label: 'Fri', fullLabel: 'Friday 28 November', values: { success: 158, failed: 4 } },
11
+ { label: 'Sat', fullLabel: 'Saturday 29 November', values: { success: 12, failed: 0 } },
12
+ { label: 'Sun', fullLabel: 'Sunday 30 November', values: { success: 9, failed: 1 } },
13
+ ]
14
+
15
+ const meta = {
16
+ title: 'Components/Data/BarChart',
17
+ component: BarChart,
18
+ tags: ['autodocs'],
19
+ args: {
20
+ label: 'Sign-ins this week',
21
+ categoryLabel: 'Date',
22
+ data: week,
23
+ series: [
24
+ { key: 'success', label: 'Succeeded' },
25
+ { key: 'failed', label: 'Failed', color: 8 },
26
+ ],
27
+ stacked: true,
28
+ },
29
+ render: (args) => ({
30
+ components: { BarChart, Card },
31
+ setup: () => ({ args }),
32
+ template: `
33
+ <Card title="Sign-ins this week" description="Succeeded and failed, by day." class="max-w-2xl">
34
+ <BarChart v-bind="args" />
35
+ </Card>`,
36
+ }),
37
+ } satisfies Meta<typeof BarChart>
38
+
39
+ export default meta
40
+ type Story = StoryObj<typeof meta>
41
+
42
+ /** Stacked: the parts of each day's total. Hover a column for its numbers; "Show as table" for all of them. */
43
+ export const Default: Story = {}
44
+
45
+ /** Side by side, to compare the series rather than see the total. */
46
+ export const SideBySide: Story = { args: { stacked: false } }
47
+
48
+ /** One series: no legend, as the Card's title says what it is. */
49
+ export const OneSeries: Story = {
50
+ args: {
51
+ label: 'Applications by year group',
52
+ categoryLabel: 'Year group',
53
+ data: [
54
+ { label: 'Y7', fullLabel: 'Year 7', values: { count: 142 } },
55
+ { label: 'Y8', fullLabel: 'Year 8', values: { count: 18 } },
56
+ { label: 'Y9', fullLabel: 'Year 9', values: { count: 23 } },
57
+ { label: 'Y10', fullLabel: 'Year 10', values: { count: 11 } },
58
+ { label: 'Y12', fullLabel: 'Year 12', values: { count: 87 } },
59
+ ],
60
+ series: [{ key: 'count', label: 'Applications' }],
61
+ },
62
+ }
@@ -0,0 +1,153 @@
1
+ <script setup lang="ts">
2
+ import { computed, type HTMLAttributes } from 'vue'
3
+ import { useI18n } from 'vue-i18n'
4
+ import { cn } from '../../lib/cn'
5
+ import { niceScale, seriesColor, valueOf, type ChartPoint, type ChartSeries } from '../../lib/chart'
6
+ import ChartData from './ChartData.vue'
7
+
8
+ /**
9
+ * Amounts compared across a few categories or days, as columns: sign-ins per
10
+ * day, applications per year group. One series, or several stacked (the
11
+ * parts of a whole, e.g. succeeded and failed) or side by side.
12
+ *
13
+ * Every chart carries its numbers as a table: always there for screen
14
+ * readers, and shown to anyone with "Show as table". Hovering a column shows
15
+ * its values. Put it in a Card whose title says what it shows.
16
+ *
17
+ * Colours come from the chart palette (chart-1 to chart-8), in order unless a
18
+ * series sets its own `color`.
19
+ */
20
+ export interface BarChartProps {
21
+ /** What the chart shows, e.g. "Sign-ins this week". The table's caption; not shown (the Card's title says it). */
22
+ label: string
23
+ /** One point per column, in order. */
24
+ data: ChartPoint[]
25
+ /** The numbers in each point. One series needs no legend; two or more get one. */
26
+ series: ChartSeries[]
27
+ /** With several series: stack them in one column (default), or put them side by side. */
28
+ stacked?: boolean
29
+ /** Heading of the table's first column, e.g. "Date". Default "Category". */
30
+ categoryLabel?: string
31
+ /** Height of the plot in px. */
32
+ height?: number
33
+ /** How values are written on the axis, in tooltips and the table. Default: whole numbers in the app's language. */
34
+ format?: (value: number) => string
35
+ class?: HTMLAttributes['class']
36
+ }
37
+
38
+ const { t, locale } = useI18n()
39
+ const props = withDefaults(defineProps<BarChartProps>(), {
40
+ stacked: true,
41
+ height: 160
42
+ })
43
+
44
+ const numberFormat = computed(() => new Intl.NumberFormat(locale.value))
45
+ const fmt = (value: number) => (props.format ? props.format(value) : numberFormat.value.format(value))
46
+
47
+ // The tallest column decides the axis: the stack's total, or the biggest bar.
48
+ const largest = computed(() =>
49
+ Math.max(
50
+ 0,
51
+ ...props.data.map((point) =>
52
+ props.stacked
53
+ ? props.series.reduce((sum, s) => sum + valueOf(point, s.key), 0)
54
+ : Math.max(0, ...props.series.map((s) => valueOf(point, s.key)))
55
+ )
56
+ )
57
+ )
58
+ const scale = computed(() => niceScale(largest.value))
59
+ const percent = (value: number) => (value / scale.value.max) * 100
60
+
61
+ // Each column's bars (or stacked parts), bottom first, leaving out zeros.
62
+ const columns = computed(() =>
63
+ props.data.map((point) => ({
64
+ point,
65
+ parts: props.series
66
+ .map((s, i) => ({ series: s, value: valueOf(point, s.key), color: seriesColor(s, i) }))
67
+ .filter((part) => part.value > 0)
68
+ }))
69
+ )
70
+
71
+ const total = (parts: { value: number }[]) => parts.reduce((sum, p) => sum + p.value, 0)
72
+ </script>
73
+
74
+ <template>
75
+ <div :class="cn('flex flex-col gap-3', props.class)">
76
+ <!-- The plot, hidden from screen readers: ChartData's table has the same numbers. -->
77
+ <div class="flex gap-2" aria-hidden="true">
78
+ <!-- Value axis: clean ticks, right-aligned, level with their gridlines. -->
79
+ <div class="relative w-8 shrink-0 text-right text-xs text-ink/65 tabular-nums" :style="{ height: `${height}px` }">
80
+ <span
81
+ v-for="tick in scale.ticks"
82
+ :key="tick"
83
+ class="absolute right-0 translate-y-1/2 leading-none"
84
+ :style="{ bottom: `${percent(tick)}%` }"
85
+ >{{ fmt(tick) }}</span>
86
+ </div>
87
+
88
+ <div class="flex min-w-0 flex-1 flex-col gap-1.5">
89
+ <div class="relative" :style="{ height: `${height}px` }">
90
+ <!-- Gridlines: hairlines, recessive. -->
91
+ <div
92
+ v-for="tick in scale.ticks"
93
+ :key="tick"
94
+ class="absolute inset-x-0 h-px bg-ink/10"
95
+ :style="{ bottom: `${percent(tick)}%` }"
96
+ />
97
+ <div class="absolute inset-0 flex">
98
+ <!-- A column: the whole slot is the hover target; the bars sit in its middle. -->
99
+ <div
100
+ v-for="(column, c) in columns"
101
+ :key="c"
102
+ class="group/column relative isolate hover:z-20 flex h-full min-w-0 flex-1 items-end justify-center px-0.5"
103
+ >
104
+ <!-- Stacked: one bar, parts 2px apart (the surface showing through). -->
105
+ <div v-if="stacked" class="flex w-full max-w-6 flex-col-reverse gap-0.5" :style="{ height: `${percent(total(column.parts))}%` }">
106
+ <div
107
+ v-for="(part, p) in column.parts"
108
+ :key="part.series.key"
109
+ :class="cn('w-full min-h-px', p === column.parts.length - 1 && 'rounded-t-sm')"
110
+ :style="{ flexGrow: part.value, flexBasis: 0, background: part.color }"
111
+ />
112
+ </div>
113
+ <!-- Side by side: a bar each, 2px apart. -->
114
+ <div v-else class="flex h-full w-full items-end justify-center gap-0.5">
115
+ <div
116
+ v-for="part in column.parts"
117
+ :key="part.series.key"
118
+ class="w-full max-w-6 rounded-t-sm"
119
+ :style="{ height: `${percent(part.value)}%`, background: part.color }"
120
+ />
121
+ </div>
122
+
123
+ <!-- Tooltip on hover: the full label and every series' value. -->
124
+ <div
125
+ class="pointer-events-none absolute bottom-full left-1/2 z-10 mb-1 hidden w-max max-w-56 -translate-x-1/2 flex-col gap-1 rounded-md border border-ink/10 bg-surface-elevated px-2.5 py-1.5 text-xs text-ink shadow-md group-hover/column:flex"
126
+ >
127
+ <span class="font-bold">{{ column.point.fullLabel ?? column.point.label }}</span>
128
+ <span v-for="(s, i) in series" :key="s.key" class="flex items-center gap-1.5">
129
+ <span class="size-2 rounded-sm" :style="{ background: seriesColor(s, i) }" />
130
+ {{ s.label }}<span class="ml-auto pl-3 tabular-nums">{{ fmt(valueOf(column.point, s.key)) }}</span>
131
+ </span>
132
+ </div>
133
+ <!-- Hover highlight behind the bars. -->
134
+ <div class="absolute inset-0 -z-10 rounded-sm group-hover/column:bg-hover" />
135
+ </div>
136
+ </div>
137
+ </div>
138
+ <!-- Category labels under their columns. -->
139
+ <div class="flex text-xs text-ink/65">
140
+ <span v-for="(column, c) in columns" :key="c" class="min-w-0 flex-1 truncate px-0.5 text-center">{{ column.point.label }}</span>
141
+ </div>
142
+ </div>
143
+ </div>
144
+
145
+ <ChartData
146
+ :label="label"
147
+ :category-label="categoryLabel ?? t('ui.chart.category')"
148
+ :series="series"
149
+ :data="data"
150
+ :format="fmt"
151
+ />
152
+ </div>
153
+ </template>