@thekestongroup/ui 0.9.2 → 0.11.1

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 (28) hide show
  1. package/README.md +8 -12
  2. package/dist/components/AppShell/AppShell.vue.d.ts +4 -1
  3. package/dist/components/CardSelect/CardSelect.vue.d.ts +59 -0
  4. package/dist/components/ChangeIndicator/ChangeIndicator.vue.d.ts +20 -0
  5. package/dist/components/ChangeIndicator/changeIndicator.d.ts +5 -0
  6. package/dist/components/Header/Header.vue.d.ts +6 -1
  7. package/dist/components/StatTile/StatTile.vue.d.ts +10 -1
  8. package/dist/components/UnsavedChangesBar/UnsavedChangesBar.vue.d.ts +46 -0
  9. package/dist/i18n/en.d.ts +8 -0
  10. package/dist/index.d.ts +6 -0
  11. package/dist/keston-ui.js +698 -441
  12. package/package.json +1 -1
  13. package/src/components/AppShell/AppShell.vue +3 -1
  14. package/src/components/CardSelect/CardSelect.stories.ts +88 -0
  15. package/src/components/CardSelect/CardSelect.vue +151 -0
  16. package/src/components/ChangeIndicator/ChangeIndicator.stories.ts +32 -0
  17. package/src/components/ChangeIndicator/ChangeIndicator.vue +42 -0
  18. package/src/components/ChangeIndicator/changeIndicator.test.ts +21 -0
  19. package/src/components/ChangeIndicator/changeIndicator.ts +11 -0
  20. package/src/components/Header/Header.stories.ts +29 -0
  21. package/src/components/Header/Header.vue +14 -9
  22. package/src/components/StatTile/StatTile.stories.ts +10 -1
  23. package/src/components/StatTile/StatTile.vue +43 -6
  24. package/src/components/UnsavedChangesBar/UnsavedChangesBar.stories.ts +62 -0
  25. package/src/components/UnsavedChangesBar/UnsavedChangesBar.vue +87 -0
  26. package/src/i18n/en.ts +6 -0
  27. package/src/i18n/i18n.test.ts +10 -0
  28. package/src/index.ts +6 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thekestongroup/ui",
3
- "version": "0.9.2",
3
+ "version": "0.11.1",
4
4
  "description": "Keston UI — Vue 3 + Reka UI + Tailwind CSS component library",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "repository": {
@@ -29,7 +29,7 @@ import Slideover from '../Slideover/Slideover.vue'
29
29
  * account-settings, switch-account, log-out (and sign-in when signed out).
30
30
  *
31
31
  * Slots: default (the page), header-leading, header-middle, header-trailing,
32
- * account, account-menu (passed to Header), sidebar-header and sidebar-footer (passed to Sidebar;
32
+ * account, account-menu and organisation (passed to Header), sidebar-header and sidebar-footer (passed to Sidebar;
33
33
  * sidebar-header is only shown on wide screens, as the Slideover has its own
34
34
  * title).
35
35
  */
@@ -86,6 +86,7 @@ defineSlots<{
86
86
  'header-trailing'?(): unknown
87
87
  account?(): unknown
88
88
  'account-menu'?(props: { close: () => void }): unknown
89
+ organisation?(props: { close: () => void }): unknown
89
90
  'sidebar-header'?(props: { collapsed: boolean }): unknown
90
91
  'sidebar-footer'?(props: { collapsed: boolean; mobile: boolean }): unknown
91
92
  }>()
@@ -166,6 +167,7 @@ const sidebarId = `${uid}-sidebar`
166
167
  <template v-if="$slots['header-trailing']" #trailing><slot name="header-trailing" /></template>
167
168
  <template v-if="$slots.account" #account><slot name="account" /></template>
168
169
  <template v-if="$slots['account-menu']" #account-menu="slotProps"><slot name="account-menu" v-bind="slotProps" /></template>
170
+ <template v-if="$slots.organisation" #organisation="slotProps"><slot name="organisation" v-bind="slotProps" /></template>
169
171
  </Header>
170
172
 
171
173
  <div class="flex flex-1">
@@ -0,0 +1,88 @@
1
+ import type { Meta, StoryObj } from '@storybook/vue3-vite'
2
+ import { ref } from 'vue'
3
+ import { Gauge, ShieldCheck, ShieldAlert, SlidersHorizontal } from '@lucide/vue'
4
+ import CardSelect from './CardSelect.vue'
5
+
6
+ const presets = [
7
+ { title: 'Baseline', value: 'baseline', description: '8 characters. Nothing extra.' },
8
+ { title: 'Recommended', value: 'recommended', description: '12 characters and a number.' },
9
+ { title: 'Strict', value: 'strict', description: '14 characters with every kind of character.' },
10
+ { title: 'Custom', value: 'custom', description: 'Set each rule yourself.' },
11
+ ]
12
+
13
+ const meta = {
14
+ title: 'Components/Form/CardSelect',
15
+ component: CardSelect,
16
+ tags: ['autodocs'],
17
+ argTypes: {
18
+ helperPlacement: { options: ['below', 'icon'], control: { type: 'inline-radio' } },
19
+ columns: { options: [undefined, 1, 2, 3, 4], control: { type: 'inline-radio' } },
20
+ },
21
+ args: {
22
+ label: 'Preset',
23
+ options: presets,
24
+ hideLabel: false,
25
+ helper: '',
26
+ helperPlacement: 'below',
27
+ errorText: '',
28
+ disabled: false,
29
+ },
30
+ render: (args) => ({
31
+ components: { CardSelect },
32
+ setup: () => ({ args, value: ref<string | number | undefined>('recommended') }),
33
+ template: '<CardSelect v-bind="args" v-model="value" />',
34
+ }),
35
+ } satisfies Meta<typeof CardSelect>
36
+
37
+ export default meta
38
+ type Story = StoryObj<typeof meta>
39
+
40
+ /** Playground. Tab into the group, then use the arrow keys to choose. Nothing moves when the choice changes. */
41
+ export const Default: Story = {}
42
+
43
+ /** Icons before the titles. */
44
+ export const WithIcons: Story = {
45
+ args: {
46
+ options: [
47
+ { title: 'Baseline', value: 'baseline', description: '8 characters.', icon: ShieldAlert },
48
+ { title: 'Recommended', value: 'recommended', description: '12 characters and a number.', icon: ShieldCheck },
49
+ { title: 'Strict', value: 'strict', description: '14 characters, every kind.', icon: Gauge },
50
+ { title: 'Custom', value: 'custom', description: 'Set each rule yourself.', icon: SlidersHorizontal },
51
+ ],
52
+ },
53
+ }
54
+
55
+ /** Titles only. */
56
+ export const TitlesOnly: Story = {
57
+ args: {
58
+ label: 'Layout',
59
+ options: [
60
+ { title: 'List', value: 'list' },
61
+ { title: 'Grid', value: 'grid' },
62
+ ],
63
+ },
64
+ }
65
+
66
+ export const Nothing: Story = {
67
+ render: (args) => ({
68
+ components: { CardSelect },
69
+ setup: () => ({ args, value: ref<string | number | undefined>() }),
70
+ template: '<CardSelect v-bind="args" v-model="value" />',
71
+ }),
72
+ }
73
+
74
+ export const HelperBelowLabel: Story = { args: { helper: 'You can change any rule afterwards.' } }
75
+ export const HelperBehindIcon: Story = { args: { helper: 'You can change any rule afterwards.', helperPlacement: 'icon' } }
76
+ export const Error: Story = {
77
+ args: { errorText: 'Choose a preset' },
78
+ render: (args) => ({
79
+ components: { CardSelect },
80
+ setup: () => ({ args, value: ref<string | number | undefined>() }),
81
+ template: '<CardSelect v-bind="args" v-model="value" />',
82
+ }),
83
+ }
84
+ export const DisabledOption: Story = {
85
+ args: { options: presets.map((o) => (o.value === 'strict' ? { ...o, disabled: true } : o)) },
86
+ }
87
+ export const Disabled: Story = { args: { disabled: true } }
88
+ export const TwoColumns: Story = { args: { columns: 2 } }
@@ -0,0 +1,151 @@
1
+ <script setup lang="ts">
2
+ import { useI18n } from 'vue-i18n'
3
+ import { computed, useId, type Component, type HTMLAttributes } from 'vue'
4
+ import { RadioGroupItem, RadioGroupRoot } from 'reka-ui'
5
+ import { cn } from '../../lib/cn'
6
+ import InfoTip from '../InfoTip/InfoTip.vue'
7
+ import FieldMessage from '../FormField/FieldMessage.vue'
8
+
9
+ /**
10
+ * One choice from a few, shown as side-by-side cards, e.g. a preset ("Baseline",
11
+ * "Recommended", "Strict", "Custom"). Behaves as a radio group: Tab moves into
12
+ * it, the arrow keys move and choose. Choosing never moves anything: every
13
+ * card keeps the same size, border width and padding whether chosen or not,
14
+ * and the space for the round indicator is always reserved.
15
+ *
16
+ * For a plain list of options with small text, use RadioGroup.
17
+ */
18
+ export type CardSelectValue = string | number
19
+
20
+ export interface CardSelectOption {
21
+ /** The card's heading, e.g. "Recommended". */
22
+ title: string
23
+ value: CardSelectValue
24
+ /** A line or two under the title. */
25
+ description?: string
26
+ /** A Lucide icon (or any component) shown before the title. */
27
+ icon?: Component
28
+ disabled?: boolean
29
+ }
30
+
31
+ export interface CardSelectProps {
32
+ /** The question. Also the group's accessible name. */
33
+ label: string
34
+ options: CardSelectOption[]
35
+ /** Visually hide the label (it stays the accessible name). */
36
+ hideLabel?: boolean
37
+ /** Extra guidance about the choice. */
38
+ helper?: string
39
+ /** Show helper text under the label, or behind a `?` icon next to it. */
40
+ helperPlacement?: 'below' | 'icon'
41
+ /** Error message. */
42
+ errorText?: string
43
+ /**
44
+ * Cards per row on wide screens (1–4). Always one or two per row on
45
+ * narrow screens. By default as many as fit, at least 10rem each.
46
+ */
47
+ columns?: 1 | 2 | 3 | 4
48
+ /** Name for native form submission. */
49
+ name?: string
50
+ required?: boolean
51
+ disabled?: boolean
52
+ class?: HTMLAttributes['class']
53
+ }
54
+
55
+ const { t } = useI18n()
56
+ const props = withDefaults(defineProps<CardSelectProps>(), {
57
+ helperPlacement: 'below'
58
+ })
59
+
60
+ /** The chosen card's value; undefined until one is chosen. */
61
+ const model = defineModel<CardSelectValue | undefined>()
62
+
63
+ const id = useId()
64
+ const labelId = `${id}-label`
65
+ const helperId = `${id}-helper`
66
+ const statusId = `${id}-status`
67
+
68
+ const describedBy = computed(
69
+ () => [props.helper && helperId, props.errorText && statusId].filter(Boolean).join(' ') || undefined
70
+ )
71
+
72
+ const grid = computed(() => {
73
+ switch (props.columns) {
74
+ case 1:
75
+ return 'grid-cols-1'
76
+ case 2:
77
+ return 'grid-cols-1 sm:grid-cols-2'
78
+ case 3:
79
+ return 'grid-cols-1 sm:grid-cols-3'
80
+ case 4:
81
+ return 'grid-cols-2 md:grid-cols-4'
82
+ default:
83
+ return 'grid-cols-[repeat(auto-fit,minmax(min(10rem,100%),1fr))]'
84
+ }
85
+ })
86
+
87
+ // Chosen and not chosen have the same 1px border and padding. The chosen
88
+ // card only changes colours and gets a second, inset 1px line drawn with a
89
+ // box-shadow, which takes no space. Text keeps the same weight. Nothing is
90
+ // added or removed in the card, so nothing can move.
91
+ const card = [
92
+ 'group relative flex w-full cursor-pointer items-start gap-3 rounded-lg border border-black/15 bg-white p-3 text-left text-black transition-colors',
93
+ 'outline-2 outline-offset-2 outline-transparent focus-visible:outline-focus',
94
+ 'not-disabled:not-data-[state=checked]:hover:bg-surface-elevated',
95
+ 'data-[state=checked]:border-primary data-[state=checked]:bg-primary/6 data-[state=checked]:shadow-[inset_0_0_0_1px_var(--color-primary)]',
96
+ 'disabled:cursor-not-allowed disabled:opacity-40',
97
+ 'motion-reduce:transition-none'
98
+ ]
99
+ </script>
100
+
101
+ <template>
102
+ <div :class="cn('flex flex-col gap-2 text-black', disabled && 'opacity-40', props.class)">
103
+ <div :class="cn('flex flex-col gap-0.5', hideLabel && 'sr-only')">
104
+ <div class="flex items-center gap-1">
105
+ <span :id="labelId" class="text-sm">{{ label }}</span>
106
+ <InfoTip v-if="helper && helperPlacement === 'icon'" :text="helper" :label="t('ui.formField.about', { label })" />
107
+ </div>
108
+ <p v-if="helper" :id="helperId" :hidden="helperPlacement === 'icon'" class="text-xs text-black/65">
109
+ {{ helper }}
110
+ </p>
111
+ </div>
112
+
113
+ <RadioGroupRoot
114
+ v-model="model"
115
+ :name="name"
116
+ :required="required"
117
+ :disabled="disabled"
118
+ :aria-labelledby="labelId"
119
+ :aria-describedby="describedBy"
120
+ :aria-invalid="errorText ? true : undefined"
121
+ :class="cn('grid gap-2', grid)"
122
+ >
123
+ <RadioGroupItem
124
+ v-for="(option, i) in options"
125
+ :id="`${id}-${i}`"
126
+ :key="String(option.value)"
127
+ :value="option.value"
128
+ :disabled="option.disabled"
129
+ :aria-describedby="option.description ? `${id}-${i}-description` : undefined"
130
+ :class="cn(card, errorText && 'not-data-[state=checked]:border-red')"
131
+ >
132
+ <component :is="option.icon" v-if="option.icon" aria-hidden="true" class="mt-0.5 size-4 shrink-0 text-black/65 group-data-[state=checked]:text-primary" />
133
+ <span class="flex min-w-0 flex-1 flex-col gap-0.5">
134
+ <span class="text-sm font-bold">{{ option.title }}</span>
135
+ <span v-if="option.description" :id="`${id}-${i}-description`" class="text-xs text-black/65">
136
+ {{ option.description }}
137
+ </span>
138
+ </span>
139
+ <!-- Always there, so the card is the same size either way: the dot only fades and scales in. -->
140
+ <span
141
+ aria-hidden="true"
142
+ class="mt-0.5 flex size-4 shrink-0 items-center justify-center rounded-full border border-black/55 bg-white group-data-[state=checked]:border-primary"
143
+ >
144
+ <span class="size-2 scale-0 rounded-full bg-primary transition-transform group-data-[state=checked]:scale-100 motion-reduce:transition-none" />
145
+ </span>
146
+ </RadioGroupItem>
147
+ </RadioGroupRoot>
148
+
149
+ <FieldMessage :id="statusId" :status="errorText ? 'error' : undefined" :text="errorText" />
150
+ </div>
151
+ </template>
@@ -0,0 +1,32 @@
1
+ import type { Meta, StoryObj } from '@storybook/vue3-vite'
2
+ import ChangeIndicator from './ChangeIndicator.vue'
3
+
4
+ const meta = {
5
+ title: 'Components/Element/ChangeIndicator',
6
+ component: ChangeIndicator,
7
+ tags: ['autodocs'],
8
+ args: { from: 15, to: 5 },
9
+ } satisfies Meta<typeof ChangeIndicator>
10
+
11
+ export default meta
12
+ type Story = StoryObj<typeof meta>
13
+
14
+ export const Default: Story = {}
15
+
16
+ /** The unit follows the new value only: `3 → 5 phrases`. */
17
+ export const WithUnit: Story = { args: { from: 3, to: 5, unit: 'phrases' } }
18
+
19
+ /** Text values work too. */
20
+ export const Text: Story = { args: { from: 'Recommended', to: 'Custom' } }
21
+
22
+ /** Nothing is shown when the value is back to what was saved. */
23
+ export const Unchanged: Story = { args: { from: 8, to: 8 } }
24
+
25
+ /** Next to a setting's label, while it has unsaved edits. */
26
+ export const BesideALabel: Story = {
27
+ render: (args) => ({
28
+ components: { ChangeIndicator },
29
+ setup: () => ({ args }),
30
+ template: '<p class="flex items-center gap-2 text-sm">Minimum length <ChangeIndicator v-bind="args" /></p>',
31
+ }),
32
+ }
@@ -0,0 +1,42 @@
1
+ <script setup lang="ts">
2
+ import { useI18n } from 'vue-i18n'
3
+ import { computed, type HTMLAttributes } from 'vue'
4
+ import { ArrowRight } from '@lucide/vue'
5
+ import { cn } from '../../lib/cn'
6
+ import { hasChanged, withUnit, type ChangeValue } from './changeIndicator'
7
+
8
+ /**
9
+ * Shows that a setting has been edited and not yet saved, as old value → new
10
+ * value, e.g. `15 → 5` or `3 → 5 phrases`. Put it next to the setting's label.
11
+ * It renders nothing when `from` and `to` are the same, so it can sit in the
12
+ * template permanently and appear only while the setting is edited.
13
+ */
14
+ export interface ChangeIndicatorProps {
15
+ /** The saved value. */
16
+ from: ChangeValue
17
+ /** The edited value. */
18
+ to: ChangeValue
19
+ /** Unit shown after the new value only, e.g. "phrases". */
20
+ unit?: string
21
+ class?: HTMLAttributes['class']
22
+ }
23
+
24
+ const { t } = useI18n()
25
+ const props = defineProps<ChangeIndicatorProps>()
26
+
27
+ const changed = computed(() => hasChanged(props.from, props.to))
28
+ // Screen readers get one sentence; the symbols are hidden from them.
29
+ const label = computed(() => t('ui.changeIndicator.changed', { from: props.from, to: withUnit(props.to, props.unit) }))
30
+ </script>
31
+
32
+ <template>
33
+ <span
34
+ v-if="changed"
35
+ :class="cn('inline-flex items-center gap-1 whitespace-nowrap rounded-full border border-blue/30 bg-blue/10 px-2 text-xs font-bold text-blue', props.class)"
36
+ >
37
+ <span class="sr-only">{{ label }}</span>
38
+ <span aria-hidden="true" class="font-normal line-through decoration-blue/60">{{ from }}</span>
39
+ <ArrowRight aria-hidden="true" class="size-3 shrink-0" />
40
+ <span aria-hidden="true">{{ to }}<template v-if="unit"> {{ unit }}</template></span>
41
+ </span>
42
+ </template>
@@ -0,0 +1,21 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { hasChanged, withUnit } from './changeIndicator'
3
+
4
+ describe('withUnit', () => {
5
+ it('puts the unit after the value', () => {
6
+ expect(withUnit(5, 'phrases')).toBe('5 phrases')
7
+ })
8
+ it('is just the value without a unit', () => {
9
+ expect(withUnit(5)).toBe('5')
10
+ })
11
+ })
12
+
13
+ describe('hasChanged', () => {
14
+ it('is false for the same value, even as number and string', () => {
15
+ expect(hasChanged(8, 8)).toBe(false)
16
+ expect(hasChanged(8, '8')).toBe(false)
17
+ })
18
+ it('is true when different', () => {
19
+ expect(hasChanged(15, 5)).toBe(true)
20
+ })
21
+ })
@@ -0,0 +1,11 @@
1
+ export type ChangeValue = string | number
2
+
3
+ /** A value with its unit, e.g. "5 phrases"; just the value when there is no unit. */
4
+ export function withUnit(value: ChangeValue, unit?: string): string {
5
+ return unit ? `${value} ${unit}` : String(value)
6
+ }
7
+
8
+ /** True when there is something to show: the value really changed. */
9
+ export function hasChanged(from: ChangeValue, to: ChangeValue): boolean {
10
+ return String(from) !== String(to)
11
+ }
@@ -94,6 +94,35 @@ export const CustomUserMenu: Story = {
94
94
  }),
95
95
  };
96
96
 
97
+ /**
98
+ * The organisation slot replaces the organisation heading at the top of the
99
+ * account menu, e.g. with a row that switches organisation. A divider sits
100
+ * under it.
101
+ */
102
+ export const OrganisationSwitcher: Story = {
103
+ render: (args) => ({
104
+ components: { Header, Building2, ChevronRight },
105
+ setup: () => ({ args }),
106
+ template: `
107
+ <Header v-bind="args">
108
+ <template #organisation="{ close }">
109
+ <button
110
+ type="button"
111
+ class="flex w-full items-center gap-2 px-4 py-3 text-left text-sm hover:bg-black/5 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus"
112
+ @click="close"
113
+ >
114
+ <Building2 class="size-4 shrink-0" aria-hidden="true" />
115
+ <span class="flex min-w-0 flex-1 flex-col">
116
+ <span class="truncate font-bold">The Keston School</span>
117
+ <span class="text-xs text-black/65">Switch organisation</span>
118
+ </span>
119
+ <ChevronRight class="size-4 shrink-0" aria-hidden="true" />
120
+ </button>
121
+ </template>
122
+ </Header>`,
123
+ }),
124
+ };
125
+
97
126
  /** The whole account area replaced with the account slot. */
98
127
  export const CustomUser: Story = {
99
128
  render: (args) => ({
@@ -30,7 +30,9 @@ export type HeaderUserAction = 'account-settings' | 'switch-account' | 'log-out'
30
30
  * fills the space between title and trailing), trailing (actions before the
31
31
  * account menu), account (replaces the account menu, or the Sign in button),
32
32
  * account-menu (extra content in the account menu, above Switch account /
33
- * Log out: `{ close }`).
33
+ * Log out: `{ close }`), organisation (replaces the organisation heading at the
34
+ * very top of the account menu, e.g. with a row that switches organisation:
35
+ * `{ close }`).
34
36
  */
35
37
  export interface HeaderProps {
36
38
  /** Name of the app or area, shown next to the menu button. */
@@ -108,6 +110,7 @@ defineSlots<{
108
110
  trailing?(): unknown
109
111
  account?(): unknown
110
112
  'account-menu'?(props: { close: () => void }): unknown
113
+ organisation?(props: { close: () => void }): unknown
111
114
  }>()
112
115
 
113
116
  // "Ben Smith" → "BS", "Ben" → "B".
@@ -192,20 +195,22 @@ function runAction(action: HeaderUserAction, close: () => void) {
192
195
  <slot name="account">
193
196
  <!-- Account menu: the user's avatar opens a panel with who is signed
194
197
  in and the account actions. -->
195
- <Popover v-if="user" :content="{ align: 'end' }" :ui="{ content: cn('w-72', ui?.userMenu) }">
198
+ <Popover v-if="user" :content="{ align: 'end' }" :ui="{ content: cn('w-80', ui?.userMenu) }">
196
199
  <Button variant="ghost" square :aria-label="accountLabel" class="-mr-1.5 rounded-full">
197
200
  <Avatar :user="user" size="sm" decorative />
198
201
  </Button>
199
202
 
200
203
  <template #content="{ close }">
201
204
  <div class="flex flex-col">
202
- <p
203
- v-if="user.organisation"
204
- class="truncate px-4 pt-3 text-xs font-bold text-black/65 uppercase"
205
- :title="user.organisation"
206
- >
207
- {{ user.organisation }}
208
- </p>
205
+ <!-- The organisation at the top, with a divider under it. The
206
+ organisation slot replaces the plain heading. -->
207
+ <div v-if="user.organisation || $slots.organisation" class="border-b border-black/15">
208
+ <slot name="organisation" :close="close">
209
+ <p class="truncate px-4 py-3 text-xs font-bold text-black/65 uppercase" :title="user.organisation">
210
+ {{ user.organisation }}
211
+ </p>
212
+ </slot>
213
+ </div>
209
214
  <!-- Two columns: the avatar at the top left, beside the name and
210
215
  email; Account settings sits under them in the text column,
211
216
  without moving the avatar. -->
@@ -45,12 +45,21 @@ export const Grid: Story = {
45
45
  <div class="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-4">
46
46
  <StatTile label="Students on roll" value="1,204" :icon="Users" change="+38" change-label="since September" trend="up" />
47
47
  <StatTile label="Open applications" value="48" :icon="FileText" change="+12%" change-label="on last year" trend="up" />
48
- <StatTile label="Attendance" value="95.8%" :icon="CalendarCheck" change="0.0 pts" change-label="on last week" trend="flat" />
48
+ <StatTile label="Attendance" value="95.8%" :icon="CalendarCheck" change="0.0 pts" change-label="on last week" trend="flat" href="#attendance" />
49
49
  <StatTile label="Persistent absence" value="7.4%" :icon="UserX" change="+0.9 pts" change-label="this term" trend="up" sentiment="negative" />
50
50
  </div>`,
51
51
  }),
52
52
  };
53
53
 
54
+ /**
55
+ * With an `href` (or `as` for a router link) the whole tile is one link: an
56
+ * arrow in the corner shows it's clickable, the border darkens on hover and
57
+ * the background on press. Tab to it to see the focus ring.
58
+ */
59
+ export const Clickable: Story = {
60
+ args: { href: "#open-applications", icon: FileText, description: "Waiting for an interview or a decision." },
61
+ };
62
+
54
63
  /** With a description and a footer link. */
55
64
  export const WithDescriptionAndFooter: Story = {
56
65
  args: { description: "Waiting for an interview or a decision.", change: undefined, changeLabel: undefined, trend: undefined },
@@ -1,6 +1,7 @@
1
1
  <script setup lang="ts">
2
2
  import { useI18n } from 'vue-i18n'
3
- import { computed, type Component, type HTMLAttributes } from 'vue'
3
+ import { computed, watchEffect, type Component, type HTMLAttributes } from 'vue'
4
+ import { Primitive, type PrimitiveProps } from 'reka-ui'
4
5
  import { ArrowDownRight, ArrowRight, ArrowUpRight } from '@lucide/vue'
5
6
  import { cn } from '../../lib/cn'
6
7
  import Card from '../Card/Card.vue'
@@ -10,8 +11,12 @@ import Card from '../Card/Card.vue'
10
11
  * Put several in a grid. Optionally with how it's changed ("+12% on last
11
12
  * year") and a short description.
12
13
  *
14
+ * Give it an `href` (or `as`, e.g. a router link, with its own `to`) and the
15
+ * whole tile becomes one link: an arrow in the corner shows it's clickable,
16
+ * and it darkens its border on hover and its background when pressed.
17
+ *
13
18
  * Slots: value (e.g. a value with a unit in smaller text), description,
14
- * footer (e.g. a "View all" link).
19
+ * footer (e.g. a "View all" link; not in a tile that is itself a link).
15
20
  */
16
21
  export interface StatTileProps {
17
22
  /** What the number is, e.g. "Open applications". */
@@ -34,18 +39,30 @@ export interface StatTileProps {
34
39
  sentiment?: 'positive' | 'negative' | 'neutral'
35
40
  /** Grey text under the value. */
36
41
  description?: string
42
+ /** Makes the whole tile a link to here. */
43
+ href?: string
44
+ /** Element or component to render the link as, e.g. RouterLink (then pass its `to`). Makes the tile a link. */
45
+ as?: PrimitiveProps['as']
37
46
  class?: HTMLAttributes['class']
38
47
  }
39
48
 
40
49
  const { t } = useI18n()
41
50
  const props = defineProps<StatTileProps>()
42
51
 
43
- defineSlots<{
52
+ const slots = defineSlots<{
44
53
  value?(): unknown
45
54
  description?(): unknown
46
55
  footer?(): unknown
47
56
  }>()
48
57
 
58
+ const isLink = computed(() => Boolean(props.href || props.as))
59
+ // A link can't hold another link or button, so the footer is for static tiles.
60
+ watchEffect(() => {
61
+ if (import.meta.env.DEV && isLink.value && slots.footer) {
62
+ console.warn('[StatTile] The footer slot is ignored when the tile is a link (href or as).')
63
+ }
64
+ })
65
+
49
66
  const tone = computed(() => {
50
67
  if (props.sentiment) return props.sentiment
51
68
  if (props.trend === 'up') return 'positive'
@@ -62,8 +79,27 @@ const trendWords = computed(() => ({ up: t('ui.statTile.up'), down: t('ui.statTi
62
79
  </script>
63
80
 
64
81
  <template>
65
- <Card padding="sm" :class="props.class" :ui="{ body: 'gap-1' }">
66
- <p class="flex items-center gap-2 text-sm text-black/65">
82
+ <!-- A link wraps the card; a static tile is just the card (as="template" hands the attributes to it). -->
83
+ <Primitive
84
+ :as="isLink ? (as ?? 'a') : 'template'"
85
+ :href="isLink ? href : undefined"
86
+ :class="isLink && 'group block h-full cursor-pointer rounded-lg outline-2 outline-offset-2 outline-transparent focus-visible:outline-focus'"
87
+ >
88
+ <Card
89
+ padding="sm"
90
+ :class="
91
+ cn(
92
+ isLink &&
93
+ 'relative h-full transition-[border-color,box-shadow,background-color] duration-150 motion-reduce:transition-none group-hover:border-black/40 group-hover:shadow-md group-active:bg-black/1',
94
+ props.class
95
+ )
96
+ "
97
+ :ui="{ body: 'gap-1' }"
98
+ >
99
+ <!-- Still, decorative: the link role is what screen readers get. -->
100
+ <ArrowUpRight v-if="isLink" class="absolute end-4 top-4 size-4 text-black/65" aria-hidden="true" />
101
+
102
+ <p :class="cn('flex items-center gap-2 text-sm text-black/65', isLink && 'pe-6')">
67
103
  <component :is="icon" v-if="icon" class="size-4 shrink-0" aria-hidden="true" />
68
104
  {{ label }}
69
105
  </p>
@@ -84,8 +120,9 @@ const trendWords = computed(() => ({ up: t('ui.statTile.up'), down: t('ui.statTi
84
120
  <slot name="description">{{ description }}</slot>
85
121
  </p>
86
122
 
87
- <div v-if="$slots.footer" class="mt-2 text-sm">
123
+ <div v-if="$slots.footer && !isLink" class="mt-2 text-sm">
88
124
  <slot name="footer" />
89
125
  </div>
90
126
  </Card>
127
+ </Primitive>
91
128
  </template>
@@ -0,0 +1,62 @@
1
+ import type { Meta, StoryObj } from '@storybook/vue3-vite'
2
+ import { ref } from 'vue'
3
+ import Button from '../Button/Button.vue'
4
+ import UnsavedChangesBar from './UnsavedChangesBar.vue'
5
+
6
+ const meta = {
7
+ title: 'Components/Overlay/UnsavedChangesBar',
8
+ component: UnsavedChangesBar,
9
+ tags: ['autodocs'],
10
+ parameters: { layout: 'fullscreen' },
11
+ args: { open: true, message: '', discardLabel: '', saveLabel: '', status: 'idle', reserveSpace: true },
12
+ argTypes: { status: { options: ['idle', 'loading', 'success', 'error'], control: { type: 'inline-radio' } } },
13
+ } satisfies Meta<typeof UnsavedChangesBar>
14
+
15
+ export default meta
16
+ type Story = StoryObj<typeof meta>
17
+
18
+ /** Shown with its defaults: "Unsaved changes", Discard and Save. */
19
+ export const Default: Story = {
20
+ render: (args) => ({
21
+ components: { UnsavedChangesBar },
22
+ setup: () => ({ args }),
23
+ template: `
24
+ <div class="p-6"><p class="text-sm">Page content. The bar is fixed to the bottom of the screen.</p>
25
+ <UnsavedChangesBar v-bind="args" :message="args.message || undefined" :discard-label="args.discardLabel || undefined" :save-label="args.saveLabel || undefined" /></div>`,
26
+ }),
27
+ }
28
+
29
+ /** Edit to open it; it slides up, and slides back down on Discard or Save. Save shows a spinner for a moment. */
30
+ export const SlidesInAndOut: Story = {
31
+ render: () => ({
32
+ components: { UnsavedChangesBar, Button },
33
+ setup() {
34
+ const open = ref(false)
35
+ const status = ref<'idle' | 'loading' | 'success' | 'error'>('idle')
36
+ function save() {
37
+ status.value = 'loading'
38
+ setTimeout(() => {
39
+ status.value = 'idle'
40
+ open.value = false
41
+ }, 1200)
42
+ }
43
+ return { open, status, save }
44
+ },
45
+ template: `
46
+ <div class="flex flex-col items-start gap-4 p-6">
47
+ <p class="text-sm">The bar leaves a gap at the end of the page, so nothing is covered.</p>
48
+ <Button variant="primary" @click="open = true">Make a change</Button>
49
+ <div class="h-[60vh] w-full rounded-lg bg-surface" />
50
+ <p class="text-sm">Last line of the page.</p>
51
+ <UnsavedChangesBar :open="open" :status="status" @discard="open = false" @save="save" />
52
+ </div>`,
53
+ }),
54
+ }
55
+
56
+ /** Words that fit the page, e.g. "Unsaved password policy". */
57
+ export const CustomWords: Story = {
58
+ args: { message: 'Unsaved password policy', discardLabel: 'Undo', saveLabel: 'Apply' },
59
+ render: Default.render,
60
+ }
61
+
62
+ export const Saving: Story = { args: { status: 'loading' }, render: Default.render }