@thekestongroup/ui 0.15.0 → 0.18.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 (78) hide show
  1. package/dist/components/ActionList/ActionList.vue.d.ts +74 -0
  2. package/dist/components/Alert/Alert.vue.d.ts +55 -0
  3. package/dist/components/AsyncState/AsyncState.vue.d.ts +53 -0
  4. package/dist/components/AvatarGroup/AvatarGroup.vue.d.ts +1 -1
  5. package/dist/components/BarChart/BarChart.vue.d.ts +37 -0
  6. package/dist/components/BarChart/ChartData.vue.d.ts +20 -0
  7. package/dist/components/Button/Button.vue.d.ts +1 -1
  8. package/dist/components/Collapsible/Collapsible.vue.d.ts +48 -0
  9. package/dist/components/ConfirmDialog/ConfirmDialog.vue.d.ts +62 -0
  10. package/dist/components/FileUpload/FileUpload.vue.d.ts +55 -0
  11. package/dist/components/Footer/Footer.vue.d.ts +11 -1
  12. package/dist/components/Header/Header.vue.d.ts +1 -1
  13. package/dist/components/InputRichText/InputRichText.vue.d.ts +12 -6
  14. package/dist/components/LineChart/LineChart.vue.d.ts +37 -0
  15. package/dist/components/Notification/Notification.vue.d.ts +2 -2
  16. package/dist/components/Progress/Progress.vue.d.ts +14 -1
  17. package/dist/components/Prose/Prose.vue.d.ts +36 -0
  18. package/dist/components/ResultState/ResultState.vue.d.ts +48 -0
  19. package/dist/components/Slideover/Slideover.vue.d.ts +7 -5
  20. package/dist/components/Spinner/Spinner.vue.d.ts +1 -1
  21. package/dist/components/Steps/Steps.vue.d.ts +16 -5
  22. package/dist/components/ThemeSwitcher/ThemeSwitcher.vue.d.ts +18 -0
  23. package/dist/i18n/en.d.ts +43 -0
  24. package/dist/index.d.ts +25 -0
  25. package/dist/keston-ui.js +5287 -4028
  26. package/dist/lib/chart.d.ts +32 -0
  27. package/dist/lib/initials.d.ts +6 -1
  28. package/dist/lib/theme.d.ts +23 -0
  29. package/package.json +10 -3
  30. package/src/components/ActionList/ActionList.stories.ts +92 -0
  31. package/src/components/ActionList/ActionList.vue +138 -0
  32. package/src/components/Alert/Alert.stories.ts +65 -0
  33. package/src/components/Alert/Alert.vue +110 -0
  34. package/src/components/AsyncState/AsyncState.stories.ts +46 -0
  35. package/src/components/AsyncState/AsyncState.vue +78 -0
  36. package/src/components/BarChart/BarChart.stories.ts +62 -0
  37. package/src/components/BarChart/BarChart.vue +153 -0
  38. package/src/components/BarChart/ChartData.vue +64 -0
  39. package/src/components/Button/Button.vue +4 -4
  40. package/src/components/Calendar/Calendar.vue +3 -2
  41. package/src/components/CardSelect/CardSelect.vue +1 -1
  42. package/src/components/Checkbox/Checkbox.vue +1 -1
  43. package/src/components/CheckboxGroup/CheckboxGroup.vue +1 -1
  44. package/src/components/Collapsible/Collapsible.stories.ts +32 -0
  45. package/src/components/Collapsible/Collapsible.vue +68 -0
  46. package/src/components/ConfirmDialog/ConfirmDialog.stories.ts +54 -0
  47. package/src/components/ConfirmDialog/ConfirmDialog.vue +119 -0
  48. package/src/components/FileUpload/FileUpload.stories.ts +38 -0
  49. package/src/components/FileUpload/FileUpload.vue +150 -0
  50. package/src/components/Footer/Footer.stories.ts +10 -2
  51. package/src/components/Footer/Footer.vue +22 -4
  52. package/src/components/Header/Header.stories.ts +20 -0
  53. package/src/components/InputRichText/InputRichText.stories.ts +19 -0
  54. package/src/components/InputRichText/InputRichText.vue +129 -13
  55. package/src/components/LineChart/LineChart.stories.ts +47 -0
  56. package/src/components/LineChart/LineChart.vue +173 -0
  57. package/src/components/Notification/Toaster.vue +3 -1
  58. package/src/components/Progress/Progress.stories.ts +18 -0
  59. package/src/components/Progress/Progress.vue +57 -0
  60. package/src/components/Prose/Prose.stories.ts +46 -0
  61. package/src/components/Prose/Prose.vue +54 -0
  62. package/src/components/RadioGroup/RadioGroup.vue +2 -2
  63. package/src/components/ResultState/ResultState.stories.ts +44 -0
  64. package/src/components/ResultState/ResultState.vue +76 -0
  65. package/src/components/Select/Select.vue +6 -4
  66. package/src/components/Slideover/Slideover.vue +107 -31
  67. package/src/components/Steps/Steps.stories.ts +18 -0
  68. package/src/components/Steps/Steps.vue +28 -8
  69. package/src/components/ThemeSwitcher/ThemeSwitcher.stories.ts +22 -0
  70. package/src/components/ThemeSwitcher/ThemeSwitcher.vue +48 -0
  71. package/src/components/Tooltip/Tooltip.vue +1 -1
  72. package/src/i18n/en.ts +29 -7
  73. package/src/index.ts +25 -0
  74. package/src/lib/chart.ts +47 -0
  75. package/src/lib/initials.test.ts +19 -0
  76. package/src/lib/initials.ts +12 -3
  77. package/src/lib/theme.ts +75 -0
  78. package/src/styles/theme.css +34 -2
@@ -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>
@@ -0,0 +1,64 @@
1
+ <script setup lang="ts">
2
+ import { ref } from 'vue'
3
+ import { useI18n } from 'vue-i18n'
4
+ import { seriesColor, valueOf, type ChartPoint, type ChartSeries } from '../../lib/chart'
5
+
6
+ /**
7
+ * The parts every Keston chart shares, under the plot: the legend (for two
8
+ * or more series), a button to see the numbers as a table, and the table.
9
+ * The table is always there for screen readers; the button shows it to
10
+ * everyone (keyboard users can't hover for tooltips). Internal: used by
11
+ * BarChart and LineChart.
12
+ */
13
+ const props = defineProps<{
14
+ /** The chart's name: the table's caption. */
15
+ label: string
16
+ /** Heading of the table's first column, e.g. "Date". */
17
+ categoryLabel: string
18
+ series: ChartSeries[]
19
+ data: ChartPoint[]
20
+ format: (value: number) => string
21
+ }>()
22
+
23
+ const { t } = useI18n()
24
+ const showTable = ref(false)
25
+ </script>
26
+
27
+ <template>
28
+ <div class="flex flex-col gap-2">
29
+ <div class="flex flex-wrap items-center gap-x-4 gap-y-1">
30
+ <!-- The legend: hidden from screen readers, which get the table's headings. -->
31
+ <ul v-if="series.length > 1" class="flex flex-wrap gap-x-4 gap-y-1 text-xs text-ink/65" aria-hidden="true">
32
+ <li v-for="(s, i) in series" :key="s.key" class="flex items-center gap-1.5">
33
+ <span class="size-2.5 rounded-sm" :style="{ background: seriesColor(s, i) }" />{{ s.label }}
34
+ </li>
35
+ </ul>
36
+ <button
37
+ type="button"
38
+ class="ml-auto cursor-pointer rounded-sm text-xs text-ink/65 underline underline-offset-2 hover:text-ink outline-2 outline-offset-1 outline-transparent focus-visible:outline-focus"
39
+ :aria-expanded="showTable"
40
+ @click="showTable = !showTable"
41
+ >
42
+ {{ showTable ? t('ui.chart.hideTable') : t('ui.chart.showTable') }}
43
+ </button>
44
+ </div>
45
+ <!-- The numbers. For screen readers always; for everyone with the button. -->
46
+ <div :class="showTable ? 'overflow-x-auto' : 'sr-only'">
47
+ <table class="w-full text-left text-xs tabular-nums">
48
+ <caption class="sr-only">{{ props.label }}</caption>
49
+ <thead class="text-ink/65">
50
+ <tr class="border-b border-ink/10">
51
+ <th scope="col" class="py-1.5 pr-4 font-normal">{{ categoryLabel }}</th>
52
+ <th v-for="s in series" :key="s.key" scope="col" class="py-1.5 pr-4 text-right font-normal">{{ s.label }}</th>
53
+ </tr>
54
+ </thead>
55
+ <tbody>
56
+ <tr v-for="point in data" :key="point.label" class="border-b border-ink/10 last:border-b-0">
57
+ <th scope="row" class="py-1.5 pr-4 font-normal">{{ point.fullLabel ?? point.label }}</th>
58
+ <td v-for="s in series" :key="s.key" class="py-1.5 pr-4 text-right">{{ format(valueOf(point, s.key)) }}</td>
59
+ </tr>
60
+ </tbody>
61
+ </table>
62
+ </div>
63
+ </div>
64
+ </template>
@@ -164,16 +164,16 @@ const base = [
164
164
  // Hover / pressed only apply when the button is usable. The theme's state
165
165
  // colours (theme.css, "States") keep this the same as every other component:
166
166
  // - coloured fills (primary, danger) darken
167
- // - basic (surface fill) takes the solid surface-hover / -pressed colours,
168
- // which lighten in dark mode rather than darken
167
+ // - basic takes the control colours: a step above whatever it sits on (a
168
+ // card, a menu, the page), lightening in dark mode rather than darkening
169
169
  // - ghost has no fill, so it takes the see-through hover / pressed tints. Its
170
170
  // border stays (transparent) so it lines up with the other variants.
171
171
  const interactive = 'not-disabled:not-aria-disabled:hover:brightness-90 not-disabled:not-aria-disabled:active:brightness-80'
172
- const surfaceInteractive = 'not-disabled:not-aria-disabled:hover:bg-surface-hover not-disabled:not-aria-disabled:active:bg-surface-pressed'
172
+ const controlInteractive = 'not-disabled:not-aria-disabled:hover:bg-control-hover not-disabled:not-aria-disabled:active:bg-control-pressed'
173
173
  const ghostInteractive = 'not-disabled:not-aria-disabled:hover:bg-hover not-disabled:not-aria-disabled:active:bg-pressed'
174
174
 
175
175
  const variants: Record<NonNullable<ButtonProps['variant']>, string> = {
176
- basic: `bg-surface text-ink shadow-xs ${surfaceInteractive}`,
176
+ basic: `bg-control text-ink shadow-xs ${controlInteractive}`,
177
177
  primary: `bg-primary text-white ${interactive}`,
178
178
  ghost: `bg-transparent text-ink border-transparent ${ghostInteractive}`,
179
179
  danger: `bg-red text-white ${interactive}`
@@ -237,6 +237,7 @@ const day = [
237
237
  :initial-focus="initialFocus"
238
238
  :week-starts-on="1"
239
239
  fixed-weeks
240
+ :data-disabled="disabled || undefined"
240
241
  :class="cn('w-fit text-ink', disabled && 'opacity-40', props.class)"
241
242
  @update:model-value="setValue"
242
243
  @update:placeholder="onPlaceholderChange"
@@ -263,7 +264,7 @@ const day = [
263
264
  v-if="view === 'days'"
264
265
  type="button"
265
266
  :class="headingButton"
266
- :aria-label="t('ui.calendar.chooseMonth')"
267
+ :aria-label="t('ui.calendar.chooseMonth', { month: monthLongName })"
267
268
  :disabled="disabled"
268
269
  @click="showMonths"
269
270
  >
@@ -273,7 +274,7 @@ const day = [
273
274
  v-if="view !== 'years'"
274
275
  type="button"
275
276
  :class="headingButton"
276
- :aria-label="t('ui.calendar.chooseYear')"
277
+ :aria-label="t('ui.calendar.chooseYear', { year: placeholder.year })"
277
278
  :disabled="disabled"
278
279
  @click="showYears"
279
280
  >
@@ -102,7 +102,7 @@ const card = [
102
102
  </script>
103
103
 
104
104
  <template>
105
- <div :class="cn('flex flex-col gap-2 text-ink', disabled && 'opacity-40', props.class)">
105
+ <div :data-disabled="disabled || undefined" :class="cn('flex flex-col gap-2 text-ink', disabled && 'opacity-40', props.class)">
106
106
  <div :class="cn('flex flex-col gap-0.5', hideLabel && 'sr-only')">
107
107
  <div class="flex items-center gap-1">
108
108
  <span :id="labelId" class="text-sm">{{ label }}</span>
@@ -71,7 +71,7 @@ const box = [
71
71
  </script>
72
72
 
73
73
  <template>
74
- <div :class="cn('flex flex-col gap-1.5 text-ink', disabled && 'opacity-40', props.class)">
74
+ <div :data-disabled="disabled || undefined" :class="cn('flex flex-col gap-1.5 text-ink', disabled && 'opacity-40', props.class)">
75
75
  <div class="flex items-start gap-2">
76
76
  <CheckboxRoot
77
77
  :id="controlId"
@@ -61,7 +61,7 @@ function toggle(value: CheckboxGroupValue, on: boolean) {
61
61
  </script>
62
62
 
63
63
  <template>
64
- <div :class="cn('flex flex-col gap-1.5 text-ink', props.class)">
64
+ <div :data-disabled="disabled || undefined" :class="cn('flex flex-col gap-1.5 text-ink', props.class)">
65
65
  <div :class="cn('flex flex-col gap-0.5', hideLabel && 'sr-only')">
66
66
  <div class="flex items-center gap-1">
67
67
  <span :id="labelId" class="text-sm">{{ label }}</span>
@@ -0,0 +1,32 @@
1
+ import type { Meta, StoryObj } from '@storybook/vue3-vite'
2
+ import Collapsible from './Collapsible.vue'
3
+ import Input from '../Input/Input.vue'
4
+ import Switch from '../Switch/Switch.vue'
5
+
6
+ const meta = {
7
+ title: 'Components/Element/Collapsible',
8
+ component: Collapsible,
9
+ tags: ['autodocs'],
10
+ args: { title: 'Advanced options', defaultOpen: false, divided: false, disabled: false },
11
+ render: (args) => ({
12
+ components: { Collapsible, Input, Switch },
13
+ setup: () => ({ args }),
14
+ template: `
15
+ <div class="max-w-xl">
16
+ <Collapsible v-bind="args">
17
+ <Input label="Issuer name" helper="Shown in people's authenticator apps." />
18
+ <Switch label="Include backup codes" />
19
+ </Collapsible>
20
+ </div>`,
21
+ }),
22
+ } satisfies Meta<typeof Collapsible>
23
+
24
+ export default meta
25
+ type Story = StoryObj<typeof meta>
26
+
27
+ export const Default: Story = {}
28
+
29
+ export const Open: Story = { args: { defaultOpen: true } }
30
+
31
+ /** At the end of a form, with a line above it. */
32
+ export const Divided: Story = { args: { divided: true, title: 'Customise rules' } }
@@ -0,0 +1,68 @@
1
+ <script setup lang="ts">
2
+ import type { HTMLAttributes } from 'vue'
3
+ import { CollapsibleContent, CollapsibleRoot, CollapsibleTrigger } from 'reka-ui'
4
+ import { ChevronRight } from '@lucide/vue'
5
+ import { cn } from '../../lib/cn'
6
+
7
+ /**
8
+ * A section that opens and closes: "Advanced options", "Customise rules",
9
+ * "How does this work?". The title row is a button; the content shows under
10
+ * it. Closed by default; use `defaultOpen` or `v-model:open`.
11
+ *
12
+ * Built on Reka UI's Collapsible, so the button says whether it's open
13
+ * (aria-expanded) and points at what it opens (aria-controls).
14
+ *
15
+ * Slots: default (the content), title (replaces the title text).
16
+ */
17
+ export interface CollapsibleProps {
18
+ /** The title row's text, e.g. "Advanced options". */
19
+ title?: string
20
+ /** Start open (when not using v-model:open). */
21
+ defaultOpen?: boolean
22
+ /** A line between it and what's above, e.g. at the end of a Card's fields. */
23
+ divided?: boolean
24
+ disabled?: boolean
25
+ class?: HTMLAttributes['class']
26
+ /** Extra classes for the parts. */
27
+ ui?: {
28
+ trigger?: HTMLAttributes['class']
29
+ content?: HTMLAttributes['class']
30
+ }
31
+ }
32
+
33
+ const props = defineProps<CollapsibleProps>()
34
+ const open = defineModel<boolean | undefined>('open', { default: undefined })
35
+
36
+ defineSlots<{
37
+ default?(): unknown
38
+ title?(): unknown
39
+ }>()
40
+ </script>
41
+
42
+ <template>
43
+ <CollapsibleRoot
44
+ v-model:open="open"
45
+ :default-open="defaultOpen"
46
+ :disabled="disabled"
47
+ :class="cn('flex flex-col', divided && 'border-t border-ink/10', props.class)"
48
+ >
49
+ <!-- The row: a chevron that turns to point down when open, then the title. -->
50
+ <CollapsibleTrigger
51
+ :class="cn(
52
+ 'group/collapsible -mx-2 flex items-center gap-2 rounded px-2 py-3 text-left text-sm font-bold text-ink transition-colors',
53
+ 'cursor-pointer not-disabled:hover:bg-hover not-disabled:active:bg-pressed disabled:cursor-not-allowed disabled:opacity-40',
54
+ 'outline-2 -outline-offset-2 outline-transparent focus-visible:outline-focus',
55
+ ui?.trigger
56
+ )"
57
+ >
58
+ <ChevronRight
59
+ class="size-4 shrink-0 text-ink/65 transition-transform duration-150 group-data-[state=open]/collapsible:rotate-90 motion-reduce:transition-none"
60
+ aria-hidden="true"
61
+ />
62
+ <slot name="title">{{ title }}</slot>
63
+ </CollapsibleTrigger>
64
+ <CollapsibleContent :class="cn('flex flex-col gap-4 pb-3', ui?.content)">
65
+ <slot />
66
+ </CollapsibleContent>
67
+ </CollapsibleRoot>
68
+ </template>
@@ -0,0 +1,54 @@
1
+ import type { Meta, StoryObj } from '@storybook/vue3-vite'
2
+ import { ref } from 'vue'
3
+ import { Trash2 } from '@lucide/vue'
4
+ import ConfirmDialog from './ConfirmDialog.vue'
5
+ import Button from '../Button/Button.vue'
6
+ import type { ButtonStatus } from '../Button/Button.vue'
7
+
8
+ const meta = {
9
+ title: 'Components/Overlay/ConfirmDialog',
10
+ component: ConfirmDialog,
11
+ tags: ['autodocs'],
12
+ args: {
13
+ title: 'Delete 3 applications?',
14
+ description: "They're removed for everyone at the school. This can't be undone.",
15
+ confirmLabel: 'Delete applications',
16
+ danger: true,
17
+ },
18
+ render: (args) => ({
19
+ components: { ConfirmDialog, Button },
20
+ setup() {
21
+ const open = ref(false)
22
+ const status = ref<ButtonStatus>('idle')
23
+ // Pretend to delete for a second, then close.
24
+ function confirm() {
25
+ status.value = 'loading'
26
+ setTimeout(() => {
27
+ status.value = 'idle'
28
+ open.value = false
29
+ }, 1000)
30
+ }
31
+ return { args, open, status, confirm, Trash2 }
32
+ },
33
+ template: `
34
+ <ConfirmDialog v-bind="args" v-model:open="open" :status="status" loading-text="Deleting…" @confirm="confirm">
35
+ <Button variant="danger"><template #leading><component :is="Trash2" /></template>Delete</Button>
36
+ </ConfirmDialog>`,
37
+ }),
38
+ } satisfies Meta<typeof ConfirmDialog>
39
+
40
+ export default meta
41
+ type Story = StoryObj<typeof meta>
42
+
43
+ /** Something that can't be undone: a danger action, focus starting on Cancel. */
44
+ export const Default: Story = {}
45
+
46
+ /** Not destructive, e.g. leaving an organisation you can rejoin: a primary action. */
47
+ export const NotDangerous: Story = {
48
+ args: {
49
+ title: 'Leave Keston Prep?',
50
+ description: "You'll lose access to its apps. An admin there can invite you again.",
51
+ confirmLabel: 'Leave',
52
+ danger: false,
53
+ },
54
+ }
@@ -0,0 +1,119 @@
1
+ <script setup lang="ts">
2
+ import { computed, ref, type HTMLAttributes } from 'vue'
3
+ import { useI18n } from 'vue-i18n'
4
+ import {
5
+ AlertDialogCancel,
6
+ AlertDialogContent,
7
+ AlertDialogDescription,
8
+ AlertDialogOverlay,
9
+ AlertDialogPortal,
10
+ AlertDialogRoot,
11
+ AlertDialogTitle,
12
+ AlertDialogTrigger
13
+ } from 'reka-ui'
14
+ import { cn } from '../../lib/cn'
15
+ import Button, { type ButtonStatus } from '../Button/Button.vue'
16
+
17
+ /**
18
+ * "Are you sure?" before something that can't easily be undone: deleting,
19
+ * leaving, removing access. A small panel in the middle of the screen with a
20
+ * title that asks the question, what will happen, and Cancel / the action.
21
+ *
22
+ * Focus starts on Cancel, so pressing Enter by accident doesn't do it.
23
+ * Escape and Cancel close it; clicking outside doesn't (it's a decision).
24
+ * Built on Reka UI's AlertDialog, so it's announced as an alert dialog.
25
+ *
26
+ * Open it with `v-model:open`, or put the button that opens it in the
27
+ * default slot. While `status` is loading, the action shows a spinner and
28
+ * the dialog can't be closed; set it back to idle (and `open` to false) when
29
+ * the work is done.
30
+ *
31
+ * Slots: default (the trigger), description (instead of `description`, for
32
+ * bold text or a list of what will be lost).
33
+ */
34
+ export interface ConfirmDialogProps {
35
+ /** The question, e.g. "Delete 3 applications?" */
36
+ title: string
37
+ /** What will happen, and whether it can be undone. */
38
+ description?: string
39
+ /** The action's text: a verb, e.g. "Delete applications". Default "Confirm". */
40
+ confirmLabel?: string
41
+ /** Default "Cancel". */
42
+ cancelLabel?: string
43
+ /** Destroys something: the action is a danger Button. */
44
+ danger?: boolean
45
+ /** Progress of the action, e.g. loading while it runs. */
46
+ status?: ButtonStatus
47
+ /** Text beside the spinner while loading, e.g. "Deleting…". */
48
+ loadingText?: string
49
+ class?: HTMLAttributes['class']
50
+ }
51
+
52
+ const { t } = useI18n()
53
+ const props = withDefaults(defineProps<ConfirmDialogProps>(), {
54
+ status: 'idle'
55
+ })
56
+ const open = defineModel<boolean>('open', { default: false })
57
+ const emit = defineEmits<{
58
+ /** The action was pressed. Close it yourself (`open = false`) when it's done. */
59
+ confirm: []
60
+ }>()
61
+
62
+ defineSlots<{
63
+ default?(): unknown
64
+ description?(): unknown
65
+ }>()
66
+
67
+ const busy = computed(() => props.status === 'loading')
68
+ const cancelButton = ref<{ $el?: HTMLElement }>()
69
+
70
+ // Can't be closed while the action runs.
71
+ function onOpenChange(value: boolean) {
72
+ if (!value && busy.value) return
73
+ open.value = value
74
+ }
75
+ </script>
76
+
77
+ <template>
78
+ <AlertDialogRoot :open="open" @update:open="onOpenChange">
79
+ <AlertDialogTrigger v-if="$slots.default" as-child>
80
+ <slot />
81
+ </AlertDialogTrigger>
82
+ <AlertDialogPortal>
83
+ <AlertDialogOverlay class="fixed inset-0 z-50 bg-black/40 data-[state=open]:animate-fade-in data-[state=closed]:animate-fade-out" />
84
+ <AlertDialogContent
85
+ :class="cn(
86
+ 'fixed top-1/2 left-1/2 z-50 flex w-[calc(100vw-2rem)] max-w-md -translate-x-1/2 -translate-y-1/2 flex-col gap-4',
87
+ 'rounded-lg border border-ink/10 bg-surface-elevated p-6 text-ink shadow-xl outline-none',
88
+ 'data-[state=open]:animate-fade-in data-[state=closed]:animate-fade-out',
89
+ props.class
90
+ )"
91
+ @escape-key-down="busy && $event.preventDefault()"
92
+ @open-auto-focus.prevent="cancelButton?.$el?.focus()"
93
+ >
94
+ <div class="flex flex-col gap-2">
95
+ <AlertDialogTitle class="text-lg font-bold">{{ title }}</AlertDialogTitle>
96
+ <AlertDialogDescription v-if="description || $slots.description" as="div" class="text-sm text-ink/65">
97
+ <slot name="description">{{ description }}</slot>
98
+ </AlertDialogDescription>
99
+ </div>
100
+ <!-- Cancel on the left, the action on the right; stacked on narrow
101
+ screens with the action on top. -->
102
+ <div class="flex flex-col-reverse gap-2 sm:flex-row sm:justify-end">
103
+ <AlertDialogCancel as-child>
104
+ <Button ref="cancelButton" :disabled="busy">{{ cancelLabel ?? t('ui.confirmDialog.cancel') }}</Button>
105
+ </AlertDialogCancel>
106
+ <!-- Not AlertDialogAction's own close: it stays open while the work runs. -->
107
+ <Button
108
+ :variant="danger ? 'danger' : 'primary'"
109
+ :status="status"
110
+ :loading-text="loadingText"
111
+ @click="emit('confirm')"
112
+ >
113
+ {{ confirmLabel ?? t('ui.confirmDialog.confirm') }}
114
+ </Button>
115
+ </div>
116
+ </AlertDialogContent>
117
+ </AlertDialogPortal>
118
+ </AlertDialogRoot>
119
+ </template>