@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,192 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from "@storybook/vue3-vite";
|
|
2
|
+
import {
|
|
3
|
+
BookOpen,
|
|
4
|
+
Building2,
|
|
5
|
+
CalendarCog,
|
|
6
|
+
GraduationCap,
|
|
7
|
+
Layers,
|
|
8
|
+
CalendarDays,
|
|
9
|
+
ClipboardList,
|
|
10
|
+
House,
|
|
11
|
+
LifeBuoy,
|
|
12
|
+
MessageSquare,
|
|
13
|
+
Settings,
|
|
14
|
+
Users,
|
|
15
|
+
} from "@lucide/vue";
|
|
16
|
+
import { computed, ref } from "vue";
|
|
17
|
+
import Sidebar, { type SidebarItem, type SidebarSection } from "./Sidebar.vue";
|
|
18
|
+
|
|
19
|
+
const items: SidebarItem[] = [
|
|
20
|
+
{ label: "Home", icon: House, href: "#", active: true },
|
|
21
|
+
{ label: "Students", icon: Users, href: "#" },
|
|
22
|
+
{ label: "Timetable", icon: CalendarDays, href: "#" },
|
|
23
|
+
{ label: "Messages", icon: MessageSquare, href: "#", badge: 3 },
|
|
24
|
+
{ label: "Homework", icon: BookOpen, href: "#", badge: 2, badgeColor: "red" },
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
const sections: SidebarSection[] = [
|
|
28
|
+
{
|
|
29
|
+
label: "Teaching",
|
|
30
|
+
items: [
|
|
31
|
+
{ label: "Registers", icon: ClipboardList, href: "#" },
|
|
32
|
+
{ label: "Lessons", icon: BookOpen, href: "#" },
|
|
33
|
+
],
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
label: "School",
|
|
37
|
+
items: [
|
|
38
|
+
{ label: "Settings", icon: Settings, href: "#" },
|
|
39
|
+
{ label: "Archive", icon: BookOpen, href: "#", disabled: true },
|
|
40
|
+
],
|
|
41
|
+
},
|
|
42
|
+
];
|
|
43
|
+
|
|
44
|
+
const Logo = `
|
|
45
|
+
<span class="flex size-7 shrink-0 items-center justify-center rounded bg-primary text-xs font-bold text-white" aria-hidden="true">K</span>
|
|
46
|
+
<span class="truncate text-base font-bold transition-opacity duration-150" :class="collapsed && 'opacity-0'">Keston</span>`;
|
|
47
|
+
|
|
48
|
+
const meta = {
|
|
49
|
+
title: "Components/Navigation/Sidebar",
|
|
50
|
+
component: Sidebar,
|
|
51
|
+
tags: ["autodocs"],
|
|
52
|
+
parameters: { layout: "fullscreen" },
|
|
53
|
+
args: {
|
|
54
|
+
items,
|
|
55
|
+
sections,
|
|
56
|
+
label: "Main",
|
|
57
|
+
collapsible: true,
|
|
58
|
+
},
|
|
59
|
+
render: (args) => ({
|
|
60
|
+
components: { Sidebar, LifeBuoy },
|
|
61
|
+
setup: () => ({ args }),
|
|
62
|
+
template: `
|
|
63
|
+
<div class="flex h-screen bg-background">
|
|
64
|
+
<Sidebar v-bind="args">
|
|
65
|
+
<template #header="{ collapsed }">${Logo}</template>
|
|
66
|
+
</Sidebar>
|
|
67
|
+
</div>`,
|
|
68
|
+
}),
|
|
69
|
+
} satisfies Meta<typeof Sidebar>;
|
|
70
|
+
|
|
71
|
+
export default meta;
|
|
72
|
+
type Story = StoryObj<typeof meta>;
|
|
73
|
+
|
|
74
|
+
/** Playground: use the controls below. The button at the bottom collapses it to icons. */
|
|
75
|
+
export const Default: Story = {};
|
|
76
|
+
|
|
77
|
+
/** Narrow, icons only. Labels are kept for screen readers and shown as a tooltip on hover. */
|
|
78
|
+
export const Collapsed: Story = {
|
|
79
|
+
args: { collapsed: true },
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
/** Clicking an item makes it the current page. */
|
|
83
|
+
export const Interactive: Story = {
|
|
84
|
+
args: { sections: [] },
|
|
85
|
+
render: (args) => ({
|
|
86
|
+
components: { Sidebar },
|
|
87
|
+
setup() {
|
|
88
|
+
const current = ref("Home");
|
|
89
|
+
const navItems = computed<SidebarItem[]>(() =>
|
|
90
|
+
items.map((item) => ({
|
|
91
|
+
...item,
|
|
92
|
+
href: undefined,
|
|
93
|
+
active: item.label === current.value,
|
|
94
|
+
onSelect: () => (current.value = item.label),
|
|
95
|
+
})),
|
|
96
|
+
);
|
|
97
|
+
return { args, navItems, current };
|
|
98
|
+
},
|
|
99
|
+
template: `
|
|
100
|
+
<div class="flex h-screen bg-background">
|
|
101
|
+
<Sidebar v-bind="args" :items="navItems" />
|
|
102
|
+
<main class="flex-1 p-6 text-sm">Current page: <strong>{{ current }}</strong></main>
|
|
103
|
+
</div>`,
|
|
104
|
+
}),
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
/** Something at the bottom, e.g. help or a profile, in the footer slot. */
|
|
108
|
+
export const WithFooter: Story = {
|
|
109
|
+
args: { collapsible: false },
|
|
110
|
+
render: (args) => ({
|
|
111
|
+
components: { Sidebar, LifeBuoy },
|
|
112
|
+
setup: () => ({ args }),
|
|
113
|
+
template: `
|
|
114
|
+
<div class="flex h-screen bg-background">
|
|
115
|
+
<Sidebar v-bind="args">
|
|
116
|
+
<template #header="{ collapsed }">${Logo}</template>
|
|
117
|
+
<template #footer>
|
|
118
|
+
<div class="flex items-center gap-3 px-3 py-2 text-sm">
|
|
119
|
+
<span class="flex size-7 shrink-0 items-center justify-center rounded-full bg-black/10 text-xs font-bold" aria-hidden="true">BS</span>
|
|
120
|
+
<span class="flex min-w-0 flex-col">
|
|
121
|
+
<span class="truncate font-bold">Ben Smith</span>
|
|
122
|
+
<span class="truncate text-xs text-black/65">Head of Year 9</span>
|
|
123
|
+
</span>
|
|
124
|
+
</div>
|
|
125
|
+
</template>
|
|
126
|
+
</Sidebar>
|
|
127
|
+
</div>`,
|
|
128
|
+
}),
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
// School › School Structure › New School Year Setup, plus siblings at each level.
|
|
132
|
+
const schoolTree = (current: string, go: (label: string) => void): SidebarItem[] => {
|
|
133
|
+
const page = (label: string, icon?: SidebarItem["icon"]): SidebarItem => ({
|
|
134
|
+
label,
|
|
135
|
+
icon,
|
|
136
|
+
active: label === current,
|
|
137
|
+
onSelect: () => go(label),
|
|
138
|
+
});
|
|
139
|
+
return [
|
|
140
|
+
page("Home", House),
|
|
141
|
+
page("Students", Users),
|
|
142
|
+
{
|
|
143
|
+
label: "School",
|
|
144
|
+
icon: Building2,
|
|
145
|
+
children: [
|
|
146
|
+
page("Overview", House),
|
|
147
|
+
{
|
|
148
|
+
label: "School Structure",
|
|
149
|
+
icon: Layers,
|
|
150
|
+
children: [
|
|
151
|
+
page("New School Year Setup", CalendarCog),
|
|
152
|
+
page("Year Groups", GraduationCap),
|
|
153
|
+
page("Houses", Users),
|
|
154
|
+
],
|
|
155
|
+
},
|
|
156
|
+
page("Term Dates", CalendarDays),
|
|
157
|
+
page("Settings", Settings),
|
|
158
|
+
],
|
|
159
|
+
},
|
|
160
|
+
page("Messages", MessageSquare),
|
|
161
|
+
];
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Items with `children` open a level instead of navigating, with a back row
|
|
166
|
+
* (‹ School) at the top. It opens at the current page's level, and the
|
|
167
|
+
* parents above the current page are shown in bold.
|
|
168
|
+
*/
|
|
169
|
+
export const Nested: Story = {
|
|
170
|
+
args: { sections: [] },
|
|
171
|
+
render: (args) => ({
|
|
172
|
+
components: { Sidebar },
|
|
173
|
+
setup() {
|
|
174
|
+
const current = ref("New School Year Setup");
|
|
175
|
+
const navItems = computed(() => schoolTree(current.value, (label) => (current.value = label)));
|
|
176
|
+
return { args, navItems, current };
|
|
177
|
+
},
|
|
178
|
+
template: `
|
|
179
|
+
<div class="flex h-screen bg-background">
|
|
180
|
+
<Sidebar v-bind="args" :items="navItems" />
|
|
181
|
+
<main class="flex-1 p-6 text-sm">Current page: <strong>{{ current }}</strong></main>
|
|
182
|
+
</div>`,
|
|
183
|
+
}),
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
/** Many items: the middle scrolls, the header and footer stay put. */
|
|
187
|
+
export const LongList: Story = {
|
|
188
|
+
args: {
|
|
189
|
+
items: Array.from({ length: 40 }, (_, n) => ({ label: `Class ${n + 1}`, icon: Users, href: "#" })),
|
|
190
|
+
sections: [],
|
|
191
|
+
},
|
|
192
|
+
};
|
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { computed, nextTick, ref, useId, watch, type Component, type HTMLAttributes } from 'vue'
|
|
3
|
+
import { ChevronLeft, ChevronRight, PanelLeftClose, PanelLeftOpen } from '@lucide/vue'
|
|
4
|
+
import { cn } from '../../lib/cn'
|
|
5
|
+
import Badge, { type BadgeProps } from '../Badge/Badge.vue'
|
|
6
|
+
import Button from '../Button/Button.vue'
|
|
7
|
+
import { sidebarTrail } from './sidebar'
|
|
8
|
+
|
|
9
|
+
export interface SidebarItem {
|
|
10
|
+
label: string
|
|
11
|
+
/** A Lucide (or any) icon component. Needed for the item to make sense when collapsed. */
|
|
12
|
+
icon?: Component
|
|
13
|
+
/** Link with a plain <a href>. */
|
|
14
|
+
href?: string
|
|
15
|
+
/** Link through `linkComponent` (e.g. RouterLink), which receives `to`. */
|
|
16
|
+
to?: unknown
|
|
17
|
+
/** The current page. Shown highlighted, and announced as aria-current="page". */
|
|
18
|
+
active?: boolean
|
|
19
|
+
/** A count or short note on the right, e.g. unread messages. */
|
|
20
|
+
badge?: string | number
|
|
21
|
+
/** Colour of the badge (default basic), e.g. red for something overdue. */
|
|
22
|
+
badgeColor?: BadgeProps['color']
|
|
23
|
+
disabled?: boolean
|
|
24
|
+
/** Called when the item is chosen (for items that aren't links, or as well as navigating). */
|
|
25
|
+
onSelect?: (event: MouseEvent) => void
|
|
26
|
+
/**
|
|
27
|
+
* Sub-pages. Choosing this item shows them in place of the list, with a
|
|
28
|
+
* back row (‹ and this item's label) at the top, instead of navigating.
|
|
29
|
+
* e.g. School › School Structure › New School Year Setup.
|
|
30
|
+
*/
|
|
31
|
+
children?: SidebarItem[]
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface SidebarSection {
|
|
35
|
+
/** Heading above the section's items. */
|
|
36
|
+
label?: string
|
|
37
|
+
items: SidebarItem[]
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The app's main navigation, down the left of the page. Items can be links
|
|
42
|
+
* (`href`, or `to` with a router link component) or buttons (`onSelect`), and
|
|
43
|
+
* can be grouped into labelled sections, and nested (`children`): choosing
|
|
44
|
+
* an item with children shows them, with a back row to return. Opens at the
|
|
45
|
+
* level of the current page. Can collapse to a narrow strip of icons.
|
|
46
|
+
*
|
|
47
|
+
* For a Breadcrumb that follows it, use `sidebarTrail(items, sections)`.
|
|
48
|
+
*
|
|
49
|
+
* On small screens, put it inside a Slideover (`side="left"`) opened from the
|
|
50
|
+
* Header's menu button. See Examples/App shell.
|
|
51
|
+
*
|
|
52
|
+
* Slots: header (top, e.g. a logo), footer (bottom, e.g. settings or a
|
|
53
|
+
* profile), item (replaces each item's content: `{ item, active, collapsed }`).
|
|
54
|
+
*/
|
|
55
|
+
export interface SidebarProps {
|
|
56
|
+
/** Items in a single section. Use `sections` for headings. */
|
|
57
|
+
items?: SidebarItem[]
|
|
58
|
+
/** Groups of items, each with an optional heading. Shown after `items`. */
|
|
59
|
+
sections?: SidebarSection[]
|
|
60
|
+
/** Accessible name of the navigation landmark. */
|
|
61
|
+
label?: string
|
|
62
|
+
/** Show the collapse / expand button in the footer. */
|
|
63
|
+
collapsible?: boolean
|
|
64
|
+
/** Component used for items with `to`, e.g. RouterLink or NuxtLink. */
|
|
65
|
+
linkComponent?: string | Component
|
|
66
|
+
class?: HTMLAttributes['class']
|
|
67
|
+
/** Extra classes for the parts. */
|
|
68
|
+
ui?: {
|
|
69
|
+
header?: HTMLAttributes['class']
|
|
70
|
+
body?: HTMLAttributes['class']
|
|
71
|
+
footer?: HTMLAttributes['class']
|
|
72
|
+
item?: HTMLAttributes['class']
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const props = withDefaults(defineProps<SidebarProps>(), {
|
|
77
|
+
items: () => [],
|
|
78
|
+
sections: () => [],
|
|
79
|
+
label: 'Main',
|
|
80
|
+
collapsible: false,
|
|
81
|
+
linkComponent: 'a'
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
const emit = defineEmits<{
|
|
85
|
+
/** An item was chosen (not emitted for disabled items). */
|
|
86
|
+
select: [item: SidebarItem, event: MouseEvent]
|
|
87
|
+
}>()
|
|
88
|
+
|
|
89
|
+
defineSlots<{
|
|
90
|
+
header?(props: { collapsed: boolean }): unknown
|
|
91
|
+
footer?(props: { collapsed: boolean }): unknown
|
|
92
|
+
item?(props: { item: SidebarItem; active: boolean; collapsed: boolean }): unknown
|
|
93
|
+
}>()
|
|
94
|
+
|
|
95
|
+
/** Narrow, icons only. Optional: use v-model:collapsed to control it. */
|
|
96
|
+
const collapsed = defineModel<boolean>('collapsed', { default: false })
|
|
97
|
+
|
|
98
|
+
// Unique per sidebar, so two on one page (e.g. inline and in a Slideover) don't clash.
|
|
99
|
+
const uid = useId()
|
|
100
|
+
|
|
101
|
+
const topSections = computed<SidebarSection[]>(() => [
|
|
102
|
+
...(props.items.length ? [{ items: props.items }] : []),
|
|
103
|
+
...props.sections
|
|
104
|
+
])
|
|
105
|
+
|
|
106
|
+
// ── Nesting ─────────────────────────────────────────────────────────────────
|
|
107
|
+
// `trail` is the labels of the parents opened, top first; empty at the top.
|
|
108
|
+
// Labels (not objects) so it survives the app rebuilding its items.
|
|
109
|
+
const trail = ref<string[]>([])
|
|
110
|
+
|
|
111
|
+
// The parent items along the trail. Stops early if one has gone away.
|
|
112
|
+
const opened = computed(() => {
|
|
113
|
+
const found: SidebarItem[] = []
|
|
114
|
+
let level = topSections.value.flatMap((section) => section.items)
|
|
115
|
+
for (const label of trail.value) {
|
|
116
|
+
const item = level.find((i) => i.label === label && i.children?.length)
|
|
117
|
+
if (!item) break
|
|
118
|
+
found.push(item)
|
|
119
|
+
level = item.children!
|
|
120
|
+
}
|
|
121
|
+
return found
|
|
122
|
+
})
|
|
123
|
+
const current = computed(() => opened.value[opened.value.length - 1])
|
|
124
|
+
|
|
125
|
+
// What the list shows: the top-level sections, or the open item's children.
|
|
126
|
+
const allSections = computed<SidebarSection[]>(() =>
|
|
127
|
+
current.value ? [{ items: current.value.children! }] : topSections.value
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
// Parents of the current page, so the list opens where it is, and so those
|
|
131
|
+
// parents can be marked (they contain the current page).
|
|
132
|
+
const activeTrail = computed(() => sidebarTrail(props.items, props.sections))
|
|
133
|
+
const activeParents = computed(() => new Set(activeTrail.value.slice(0, -1).map((item) => item.label)))
|
|
134
|
+
|
|
135
|
+
// Follow the current page: when it changes (or on first show), open its level.
|
|
136
|
+
watch(
|
|
137
|
+
() => activeTrail.value.map((item) => item.label).join('\u0000'),
|
|
138
|
+
() => {
|
|
139
|
+
trail.value = activeTrail.value.slice(0, -1).map((item) => item.label)
|
|
140
|
+
},
|
|
141
|
+
{ immediate: true }
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
// ── Sliding between levels ──────────────────────────────────────────────────
|
|
145
|
+
// Each level is keyed by its trail, so moving between levels swaps the list
|
|
146
|
+
// with a slide: deeper comes in from the right, back from the left. The
|
|
147
|
+
// level leaving is taken out of the layout (absolute) and made inert, so it
|
|
148
|
+
// can't be clicked or focused while it slides away.
|
|
149
|
+
const levelKey = computed(() => opened.value.map((item) => item.label).join('\u0000'))
|
|
150
|
+
const direction = ref<'forward' | 'back'>('forward')
|
|
151
|
+
watch(
|
|
152
|
+
() => opened.value.length,
|
|
153
|
+
(now, before) => {
|
|
154
|
+
direction.value = now < before ? 'back' : 'forward'
|
|
155
|
+
},
|
|
156
|
+
{ flush: 'sync' }
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
const slide = computed(() => {
|
|
160
|
+
const forward = direction.value === 'forward'
|
|
161
|
+
return {
|
|
162
|
+
enterActiveClass: 'transition duration-300 ease-out motion-reduce:transition-none',
|
|
163
|
+
leaveActiveClass: 'absolute inset-x-2 top-2 transition duration-300 ease-out motion-reduce:transition-none',
|
|
164
|
+
enterFromClass: cn('opacity-0', forward ? 'translate-x-full' : '-translate-x-full'),
|
|
165
|
+
leaveToClass: cn('opacity-0', forward ? '-translate-x-1/3' : 'translate-x-1/3')
|
|
166
|
+
}
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
function onLeave(el: Element) {
|
|
170
|
+
;(el as HTMLElement).inert = true
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const navRef = ref<HTMLElement>()
|
|
174
|
+
|
|
175
|
+
// After moving between levels, focus the first item (or, going back, the
|
|
176
|
+
// item just left), so keyboard users carry on in the new list.
|
|
177
|
+
async function focusAfterMove(label?: string) {
|
|
178
|
+
await nextTick()
|
|
179
|
+
// The level coming in, not the one sliding out.
|
|
180
|
+
const nav = navRef.value?.querySelector<HTMLElement>(`[data-level="${CSS.escape(levelKey.value)}"]`)
|
|
181
|
+
if (!nav) return
|
|
182
|
+
navRef.value!.scrollTop = 0
|
|
183
|
+
const target =
|
|
184
|
+
(label && nav.querySelector<HTMLElement>(`[data-parent="${CSS.escape(label)}"]`)) ||
|
|
185
|
+
nav.querySelector<HTMLElement>('li [data-sidebar-item]:not([disabled]):not([aria-disabled="true"])')
|
|
186
|
+
target?.focus()
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function openItem(item: SidebarItem) {
|
|
190
|
+
trail.value = [...opened.value.map((i) => i.label), item.label]
|
|
191
|
+
focusAfterMove()
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function goBack() {
|
|
195
|
+
const from = current.value?.label
|
|
196
|
+
trail.value = opened.value.slice(0, -1).map((i) => i.label)
|
|
197
|
+
focusAfterMove(from)
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// Links render as <a> (href) or the link component (to); everything else is
|
|
201
|
+
// a button. Disabled links lose their href, so they can't be followed.
|
|
202
|
+
function tagFor(item: SidebarItem) {
|
|
203
|
+
if (item.children?.length) return 'button'
|
|
204
|
+
if (item.disabled) return item.href || item.to !== undefined ? 'a' : 'button'
|
|
205
|
+
if (item.to !== undefined) return props.linkComponent
|
|
206
|
+
if (item.href) return 'a'
|
|
207
|
+
return 'button'
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function attrsFor(item: SidebarItem) {
|
|
211
|
+
const tag = tagFor(item)
|
|
212
|
+
if (tag === 'button') return { type: 'button', disabled: item.disabled || undefined }
|
|
213
|
+
if (item.disabled) return { role: 'link', 'aria-disabled': true }
|
|
214
|
+
if (item.to !== undefined) return { to: item.to }
|
|
215
|
+
return { href: item.href }
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function select(item: SidebarItem, event: MouseEvent) {
|
|
219
|
+
if (item.disabled) {
|
|
220
|
+
event.preventDefault()
|
|
221
|
+
return
|
|
222
|
+
}
|
|
223
|
+
if (item.children?.length) {
|
|
224
|
+
openItem(item)
|
|
225
|
+
return
|
|
226
|
+
}
|
|
227
|
+
item.onSelect?.(event)
|
|
228
|
+
emit('select', item, event)
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// ── Collapsing ──────────────────────────────────────────────────────────────
|
|
232
|
+
// Only the width animates. Nothing else changes position or size between
|
|
233
|
+
// expanded and collapsed, so nothing jumps while the width changes:
|
|
234
|
+
// - icons stay where they are: in the 56px strip, an item's 12px padding
|
|
235
|
+
// already centres a 16px icon in its 40px width.
|
|
236
|
+
// - labels and badges stay in the layout and fade out; the item clips them
|
|
237
|
+
// (overflow-hidden), and screen readers still read them.
|
|
238
|
+
// - section headings fade into a divider of the same height.
|
|
239
|
+
const fade = 'transition-opacity duration-150 motion-reduce:transition-none'
|
|
240
|
+
|
|
241
|
+
// The same height, radius, focus ring and hover / press tints as a md ghost
|
|
242
|
+
// Button, so items and buttons in the footer line up.
|
|
243
|
+
const itemBase = [
|
|
244
|
+
'flex h-9.5 w-full items-center gap-3 overflow-hidden rounded px-3 text-left text-sm whitespace-nowrap select-none',
|
|
245
|
+
'cursor-pointer transition [&_svg]:size-4 [&_svg]:shrink-0',
|
|
246
|
+
// Inset, so the ring isn't clipped by the scrolling body.
|
|
247
|
+
'outline-2 -outline-offset-2 outline-transparent forced-colors:not-focus-visible:outline-0',
|
|
248
|
+
'focus-visible:outline-focus',
|
|
249
|
+
'not-aria-disabled:not-disabled:hover:bg-black/10 not-aria-disabled:not-disabled:active:bg-black/15',
|
|
250
|
+
'disabled:cursor-not-allowed disabled:opacity-40 aria-disabled:cursor-not-allowed aria-disabled:opacity-40'
|
|
251
|
+
]
|
|
252
|
+
|
|
253
|
+
const itemActive = 'bg-primary/10 font-bold text-primary not-aria-disabled:not-disabled:hover:bg-primary/15'
|
|
254
|
+
|
|
255
|
+
// A parent of the current page: bold primary text, no fill, so it reads as
|
|
256
|
+
// "you are in here" without looking like the page itself.
|
|
257
|
+
const itemActiveParent = 'font-bold text-primary'
|
|
258
|
+
</script>
|
|
259
|
+
|
|
260
|
+
<template>
|
|
261
|
+
<aside
|
|
262
|
+
:data-collapsed="collapsed || undefined"
|
|
263
|
+
:class="cn(
|
|
264
|
+
'flex h-full shrink-0 flex-col border-r border-black/15 bg-white text-black',
|
|
265
|
+
'w-64 transition-[width] duration-300 ease-out motion-reduce:transition-none',
|
|
266
|
+
collapsed && 'w-14',
|
|
267
|
+
props.class
|
|
268
|
+
)"
|
|
269
|
+
>
|
|
270
|
+
<div
|
|
271
|
+
v-if="$slots.header"
|
|
272
|
+
:class="cn('flex h-12 shrink-0 items-center gap-2 overflow-hidden border-b border-black/15 px-4', ui?.header)"
|
|
273
|
+
>
|
|
274
|
+
<slot name="header" :collapsed="collapsed" />
|
|
275
|
+
</div>
|
|
276
|
+
|
|
277
|
+
<nav
|
|
278
|
+
ref="navRef"
|
|
279
|
+
:aria-label="label"
|
|
280
|
+
:class="cn(
|
|
281
|
+
'relative flex-1 overflow-x-hidden overflow-y-auto p-2 [scrollbar-width:thin]',
|
|
282
|
+
// Collapsed, a scrollbar would take a third of the 40px strip and cut
|
|
283
|
+
// the icons off, so it's hidden (the list still scrolls).
|
|
284
|
+
collapsed && '[scrollbar-width:none] [&::-webkit-scrollbar]:hidden',
|
|
285
|
+
ui?.body
|
|
286
|
+
)"
|
|
287
|
+
>
|
|
288
|
+
<Transition v-bind="slide" @leave="onLeave">
|
|
289
|
+
<div :key="levelKey" :data-level="levelKey" class="flex flex-col gap-4">
|
|
290
|
+
<!-- Inside a nested item: ‹ and its label, back to the level above.
|
|
291
|
+
The divider under it keeps the list's gap-4 rhythm with sections. -->
|
|
292
|
+
<div v-if="current" class="-mb-2 flex flex-col gap-1 border-b border-black/15 pb-2">
|
|
293
|
+
<button
|
|
294
|
+
type="button"
|
|
295
|
+
data-back
|
|
296
|
+
:title="collapsed ? `Back from ${current.label}` : undefined"
|
|
297
|
+
:class="cn(itemBase, 'font-bold', ui?.item)"
|
|
298
|
+
@click="goBack"
|
|
299
|
+
>
|
|
300
|
+
<ChevronLeft aria-hidden="true" />
|
|
301
|
+
<span :class="cn('min-w-0 flex-1 truncate', fade, collapsed && 'opacity-0')">
|
|
302
|
+
<span class="sr-only">Back from </span>{{ current.label }}
|
|
303
|
+
</span>
|
|
304
|
+
</button>
|
|
305
|
+
</div>
|
|
306
|
+
|
|
307
|
+
<div v-for="(section, s) in allSections" :key="s" class="flex flex-col gap-1">
|
|
308
|
+
<!-- Section headings fade into a divider when collapsed, so the groups
|
|
309
|
+
stay visible without the text. Same height either way. -->
|
|
310
|
+
<div v-if="section.label" class="relative flex h-6 items-center px-3">
|
|
311
|
+
<div
|
|
312
|
+
:id="`${uid}-section-${s}`"
|
|
313
|
+
:class="cn('truncate text-xs font-bold text-black/65 uppercase', fade, collapsed && 'opacity-0')"
|
|
314
|
+
>
|
|
315
|
+
{{ section.label }}
|
|
316
|
+
</div>
|
|
317
|
+
<div
|
|
318
|
+
aria-hidden="true"
|
|
319
|
+
:class="cn('absolute inset-x-2 top-1/2 border-t border-black/15', fade, !collapsed && 'opacity-0')"
|
|
320
|
+
/>
|
|
321
|
+
</div>
|
|
322
|
+
|
|
323
|
+
<ul class="flex flex-col gap-1" :aria-labelledby="section.label ? `${uid}-section-${s}` : undefined">
|
|
324
|
+
<li v-for="(item, i) in section.items" :key="`${item.label}-${i}`">
|
|
325
|
+
<component
|
|
326
|
+
:is="tagFor(item)"
|
|
327
|
+
v-bind="attrsFor(item)"
|
|
328
|
+
data-sidebar-item
|
|
329
|
+
:data-parent="item.children?.length ? item.label : undefined"
|
|
330
|
+
:aria-current="item.active ? 'page' : undefined"
|
|
331
|
+
:title="collapsed ? item.label : undefined"
|
|
332
|
+
:class="cn(itemBase, item.active && itemActive, activeParents.has(item.label) && itemActiveParent, ui?.item)"
|
|
333
|
+
@click="select(item, $event)"
|
|
334
|
+
>
|
|
335
|
+
<slot name="item" :item="item" :active="!!item.active" :collapsed="collapsed">
|
|
336
|
+
<component :is="item.icon" v-if="item.icon" aria-hidden="true" />
|
|
337
|
+
<span :class="cn('min-w-0 flex-1 truncate', fade, collapsed && 'opacity-0')">
|
|
338
|
+
{{ item.label }}<span v-if="item.children?.length" class="sr-only">, more options</span>
|
|
339
|
+
</span>
|
|
340
|
+
<Badge
|
|
341
|
+
v-if="item.badge !== undefined && item.badge !== ''"
|
|
342
|
+
size="sm"
|
|
343
|
+
:color="item.badgeColor"
|
|
344
|
+
:class="cn(fade, collapsed && 'opacity-0')"
|
|
345
|
+
>
|
|
346
|
+
{{ item.badge }}
|
|
347
|
+
</Badge>
|
|
348
|
+
<!-- Parents: an arrow into their sub-pages. -->
|
|
349
|
+
<ChevronRight
|
|
350
|
+
v-if="item.children?.length"
|
|
351
|
+
aria-hidden="true"
|
|
352
|
+
:class="cn('opacity-70', fade, collapsed && 'opacity-0')"
|
|
353
|
+
/>
|
|
354
|
+
</slot>
|
|
355
|
+
</component>
|
|
356
|
+
</li>
|
|
357
|
+
</ul>
|
|
358
|
+
</div>
|
|
359
|
+
</div>
|
|
360
|
+
</Transition>
|
|
361
|
+
</nav>
|
|
362
|
+
|
|
363
|
+
<div
|
|
364
|
+
v-if="$slots.footer || collapsible"
|
|
365
|
+
:class="cn('flex shrink-0 flex-col gap-1 overflow-hidden border-t border-black/15 p-2', ui?.footer)"
|
|
366
|
+
>
|
|
367
|
+
<slot name="footer" :collapsed="collapsed" />
|
|
368
|
+
<Button
|
|
369
|
+
v-if="collapsible"
|
|
370
|
+
variant="ghost"
|
|
371
|
+
square
|
|
372
|
+
:aria-label="collapsed ? 'Expand sidebar' : 'Collapse sidebar'"
|
|
373
|
+
:aria-expanded="!collapsed"
|
|
374
|
+
class="self-start"
|
|
375
|
+
@click="collapsed = !collapsed"
|
|
376
|
+
>
|
|
377
|
+
<PanelLeftOpen v-if="collapsed" />
|
|
378
|
+
<PanelLeftClose v-else />
|
|
379
|
+
</Button>
|
|
380
|
+
</div>
|
|
381
|
+
</aside>
|
|
382
|
+
</template>
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { SidebarItem, SidebarSection } from './Sidebar.vue'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The path to the current page (the item marked `active`) through nested
|
|
5
|
+
* items, top level first, e.g. [School, School Structure, New School Year
|
|
6
|
+
* Setup]. Empty when no item is active. Takes the same `items` and
|
|
7
|
+
* `sections` as the Sidebar, so a Breadcrumb can follow it:
|
|
8
|
+
*
|
|
9
|
+
* <Breadcrumb :items="sidebarTrail(items, sections)" />
|
|
10
|
+
*/
|
|
11
|
+
export function sidebarTrail(items: SidebarItem[] = [], sections: SidebarSection[] = []): SidebarItem[] {
|
|
12
|
+
for (const item of [...items, ...sections.flatMap((section) => section.items)]) {
|
|
13
|
+
if (item.active) return [item]
|
|
14
|
+
if (item.children?.length) {
|
|
15
|
+
const below = sidebarTrail(item.children)
|
|
16
|
+
if (below.length) return [item, ...below]
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return []
|
|
20
|
+
}
|