@bjj/nuxt-layer-shadcn-ui 0.14.0 → 0.15.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.
@@ -0,0 +1,141 @@
1
+ <script setup lang="ts">
2
+ import type { SidebarLayoutMenuItem, SidebarLayoutProps } from './types'
3
+ import SidebarMenus from './SidebarMenus.vue'
4
+ import { sidebarMenuItemKey } from './utils'
5
+ import {
6
+ Sidebar,
7
+ SidebarContent,
8
+ SidebarFooter,
9
+ SidebarHeader,
10
+ SidebarInset,
11
+ SidebarProvider,
12
+ SidebarRail,
13
+ SidebarTrigger,
14
+ } from '../../shadcn/sidebar'
15
+ import { provideSidebarContext, SIDEBAR_WIDTH } from '../../shadcn/sidebar/utils'
16
+ import { TooltipProvider } from '../../shadcn/tooltip'
17
+
18
+ const props = withDefaults(defineProps<SidebarLayoutProps>(), {
19
+ active: undefined,
20
+ emptyText: undefined,
21
+ variant: 'sidebar',
22
+ collapsible: 'icon',
23
+ sidebarWidth: SIDEBAR_WIDTH,
24
+ contentClass: undefined,
25
+ class: undefined,
26
+ })
27
+
28
+ const emit = defineEmits<{
29
+ 'update:active': [key: string]
30
+ 'select': [item: SidebarLayoutMenuItem]
31
+ }>()
32
+
33
+ defineSlots<{
34
+ default?: () => any
35
+ header?: () => any
36
+ footer?: () => any
37
+ }>()
38
+
39
+ // The context `Sidebar` requires, without nesting `SidebarProvider` — whose
40
+ // viewport wrapper, state cookie and Cmd/Ctrl+B toggle are page-level behaviour.
41
+ // In page mode the provider mounts anyway and shadows this.
42
+ const noop = () => {}
43
+ provideSidebarContext({
44
+ state: computed(() => 'expanded' as const),
45
+ open: ref(true),
46
+ setOpen: noop,
47
+ isMobile: ref(false),
48
+ openMobile: ref(false),
49
+ setOpenMobile: noop,
50
+ toggleSidebar: noop,
51
+ })
52
+
53
+ const rootComponent = computed(() => props.embedded ? 'div' : SidebarProvider)
54
+
55
+ const rootStyle = computed(() => ({ '--sidebar-width': props.sidebarWidth }))
56
+
57
+ const rootClass = computed(() =>
58
+ cn(props.embedded && 'min-h-0 flex size-full overflow-hidden', props.class),
59
+ )
60
+
61
+ function onSelect (item: SidebarLayoutMenuItem) {
62
+ emit('select', item)
63
+ emit('update:active', sidebarMenuItemKey(item))
64
+ }
65
+ </script>
66
+
67
+ <template>
68
+ <component
69
+ :is="rootComponent"
70
+ :style="rootStyle"
71
+ :class="rootClass"
72
+ >
73
+ <!-- Menu-button tooltips need a provider, which embedded mode has none of. -->
74
+ <TooltipProvider>
75
+ <!-- `shrink-0`: wide pane content scrolls instead of squeezing the menu. -->
76
+ <Sidebar
77
+ :variant="variant"
78
+ :collapsible="embedded ? 'none' : collapsible"
79
+ :class="embedded ? 'shrink-0 border-r' : undefined"
80
+ >
81
+ <SidebarHeader v-if="$slots.header">
82
+ <slot name="header" />
83
+ </SidebarHeader>
84
+
85
+ <SidebarContent class="overflow-hidden">
86
+ <ScrollArea
87
+ fadeMask
88
+ class="min-h-0 flex-1"
89
+ >
90
+ <SidebarMenus
91
+ :menus="menus"
92
+ :active="active"
93
+ :emptyText="emptyText"
94
+ @select="onSelect"
95
+ />
96
+ </ScrollArea>
97
+ </SidebarContent>
98
+
99
+ <SidebarFooter v-if="$slots.footer">
100
+ <slot name="footer" />
101
+ </SidebarFooter>
102
+
103
+ <SidebarRail v-if="!embedded" />
104
+ </Sidebar>
105
+
106
+ <!-- The nav keeps the page ground, so the content takes the raised surface —
107
+ embedded, the container owns it (a modal is `bg-popover`).
108
+ Top padding on mobile reserves a safe area for the floating trigger. -->
109
+ <SidebarInset
110
+ :class="[
111
+ 'min-w-0',
112
+ embedded ? 'bg-transparent' : 'bg-card',
113
+ !embedded && `
114
+ pt-14
115
+ md:pt-0
116
+ `,
117
+ ]"
118
+ >
119
+ <!-- Mobile-only trigger: the desktop toggle lives in the sidebar header. -->
120
+ <SidebarTrigger
121
+ v-if="!embedded"
122
+ class="
123
+ left-3 top-3 size-9 rounded-md bg-background/80 shadow-sm
124
+ backdrop-blur-sm
125
+ md:hidden
126
+ absolute z-30 cursor-pointer border
127
+ "
128
+ />
129
+
130
+ <ScrollArea
131
+ fadeMask
132
+ class="min-h-0 flex-1"
133
+ >
134
+ <div :class="contentClass">
135
+ <slot />
136
+ </div>
137
+ </ScrollArea>
138
+ </SidebarInset>
139
+ </TooltipProvider>
140
+ </component>
141
+ </template>
@@ -0,0 +1,31 @@
1
+ import type { Component } from 'vue'
2
+
3
+ export interface SidebarLayoutMenuItem {
4
+ label: string
5
+ /** Identity used for key-based selection. Falls back to `href`, then `label`. */
6
+ key?: string
7
+ icon?: string | Component
8
+ href?: string
9
+ command?: () => void
10
+ group?: string
11
+ expanded?: boolean
12
+ children?: SidebarLayoutMenuItem[]
13
+ actions?: SidebarLayoutMenuItem[]
14
+ }
15
+
16
+ export interface SidebarLayoutProps {
17
+ menus: SidebarLayoutMenuItem[]
18
+ /** Key of the selected item. Omitted → the active item is matched by route. */
19
+ active?: string
20
+ /** Placeholder shown when `menus` is empty. */
21
+ emptyText?: string
22
+ /** Fill the parent instead of the viewport: never collapses, no rail or mobile trigger. */
23
+ embedded?: boolean
24
+ variant?: 'sidebar' | 'floating' | 'inset'
25
+ /** Collapse behaviour of the sidebar. Ignored while `embedded`. */
26
+ collapsible?: 'offcanvas' | 'icon' | 'none'
27
+ /** Width of the sidebar, as a CSS length. */
28
+ sidebarWidth?: string
29
+ contentClass?: ClassValue
30
+ class?: ClassValue
31
+ }
@@ -0,0 +1,29 @@
1
+ import type { SidebarLayoutMenuItem } from './types'
2
+
3
+ /** Identity for key-based selection; an explicit `key` is only needed when neither fallback is unique. */
4
+ export function sidebarMenuItemKey (item: SidebarLayoutMenuItem): string {
5
+ return item.key ?? item.href ?? item.label
6
+ }
7
+
8
+ export function findSidebarMenuItem (
9
+ menus: SidebarLayoutMenuItem[],
10
+ key: string,
11
+ ): SidebarLayoutMenuItem | undefined {
12
+ for (const item of menus) {
13
+ if (sidebarMenuItemKey(item) === key) return item
14
+ const child = item.children && findSidebarMenuItem(item.children, key)
15
+ if (child) return child
16
+ }
17
+ return undefined
18
+ }
19
+
20
+ export function firstSidebarMenuItemKey (
21
+ menus: SidebarLayoutMenuItem[],
22
+ ): string | undefined {
23
+ for (const item of menus) {
24
+ if (!item.children?.length) return sidebarMenuItemKey(item)
25
+ const key = firstSidebarMenuItemKey(item.children)
26
+ if (key) return key
27
+ }
28
+ return undefined
29
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "noResults": "No results",
3
+ "searchPlaceholder": "Search"
4
+ }
@@ -0,0 +1,368 @@
1
+ import type { Meta, StoryObj } from '@storybook/vue3'
2
+ import type { SidebarLayoutMenuItem } from '../SidebarLayout/types'
3
+ import EventLog from '#storybook/EventLog.vue'
4
+ import { useArgsModel } from '#storybook/argsModel'
5
+ import Button from '../Button/index.vue'
6
+ import Icon from '../Icon/index.vue'
7
+ import Input from '../Input/index.vue'
8
+ import Switch from '../Switch/index.vue'
9
+ import SidebarModal from './index.vue'
10
+
11
+ const menus: SidebarLayoutMenuItem[] = [
12
+ { key: 'general', label: 'General', icon: 'settings', group: 'Settings' },
13
+ { key: 'account', label: 'Account', icon: 'circle-user', group: 'Settings' },
14
+ { key: 'usage', label: 'Usage', icon: 'chart-column', group: 'Settings' },
15
+ { key: 'capabilities', label: 'Capabilities', icon: 'briefcase-business', group: 'Settings' },
16
+ { key: 'appearance', label: 'Appearance', icon: 'monitor', group: 'Desktop app' },
17
+ { key: 'extensions', label: 'Extensions', icon: 'blocks', group: 'Desktop app' },
18
+ { key: 'developer', label: 'Developer', icon: 'wrench', group: 'Desktop app' },
19
+ { key: 'skills', label: 'Skills', icon: 'scroll-text', group: 'Customize' },
20
+ { key: 'connectors', label: 'Connectors', icon: 'plug', group: 'Customize' },
21
+ ]
22
+
23
+ const nestedMenus: SidebarLayoutMenuItem[] = [
24
+ { key: 'general', label: 'General', icon: 'settings' },
25
+ {
26
+ label: 'Workspace',
27
+ icon: 'building-2',
28
+ expanded: true,
29
+ children: [
30
+ { key: 'members', label: 'Members' },
31
+ { key: 'billing', label: 'Billing' },
32
+ ],
33
+ },
34
+ ]
35
+
36
+ const scrollingMenus: SidebarLayoutMenuItem[] = [
37
+ ...menus,
38
+ ...Array.from({ length: 12 }, (_, index) => ({
39
+ key: `plugin-${index + 1}`,
40
+ label: `Plugin ${index + 1}`,
41
+ icon: 'puzzle',
42
+ group: 'Plugins',
43
+ })),
44
+ ]
45
+
46
+ const generalPane = `
47
+ <template #general>
48
+ <div class="space-y-4">
49
+ <h2 class="text-lg font-semibold">Profile</h2>
50
+ <div class="divide-y divide-border">
51
+ <div class="flex items-center justify-between gap-4 py-4">
52
+ <span class="text-sm">Full name</span>
53
+ <Input class="max-w-64" modelValue="b.jj" />
54
+ </div>
55
+ <div class="flex items-center justify-between gap-4 py-4">
56
+ <span class="text-sm">Email</span>
57
+ <Input class="max-w-64" modelValue="b.jj@example.com" />
58
+ </div>
59
+ <div class="flex items-center justify-between gap-4 py-4">
60
+ <span class="text-sm">Email notifications</span>
61
+ <Switch :modelValue="true" />
62
+ </div>
63
+ </div>
64
+ </div>
65
+ </template>
66
+ `
67
+
68
+ const accountPane = `
69
+ <template #account>
70
+ <div class="space-y-4">
71
+ <h2 class="text-lg font-semibold">Account</h2>
72
+ <p class="text-sm text-muted-foreground">Manage the credentials used to sign in.</p>
73
+ <Button variant="outline" icon="lock-keyhole">Change password</Button>
74
+ </div>
75
+ </template>
76
+ `
77
+
78
+ const fallbackPane = `
79
+ <template #default="{ active, item }">
80
+ <div class="space-y-2">
81
+ <h2 class="text-lg font-semibold">{{ item?.label }}</h2>
82
+ <p class="text-sm text-muted-foreground">No pane is declared for "{{ active }}" yet.</p>
83
+ </div>
84
+ </template>
85
+ `
86
+
87
+ const scrollingPane = `
88
+ <template #general>
89
+ <div class="space-y-4">
90
+ <h2 class="text-lg font-semibold">Preferences</h2>
91
+ <div class="divide-y divide-border">
92
+ <div
93
+ v-for="i in 20"
94
+ :key="i"
95
+ class="flex items-center justify-between gap-4 py-4"
96
+ >
97
+ <span class="text-sm">Preference {{ i }}</span>
98
+ <Switch :modelValue="i % 2 === 0" />
99
+ </div>
100
+ </div>
101
+ </div>
102
+ </template>
103
+ `
104
+
105
+ const meta = {
106
+ title: 'UI/SidebarModal',
107
+ component: SidebarModal,
108
+ argTypes: {
109
+ menus: { control: 'object' },
110
+ visible: { control: 'boolean' },
111
+ active: { control: 'text' },
112
+ searchable: { control: 'boolean' },
113
+ searchPlaceholder: { control: 'text' },
114
+ title: { control: 'text' },
115
+ modal: { control: 'boolean' },
116
+ showClose: { control: 'boolean' },
117
+ closeOnClickOutside: { control: 'boolean' },
118
+ sidebarWidth: { control: 'text' },
119
+ contentClass: { control: 'text' },
120
+ },
121
+ args: {
122
+ menus,
123
+ visible: false,
124
+ active: 'general',
125
+ searchable: true,
126
+ searchPlaceholder: '',
127
+ title: 'Settings',
128
+ modal: true,
129
+ showClose: true,
130
+ closeOnClickOutside: false,
131
+ sidebarWidth: '',
132
+ contentClass: '',
133
+ },
134
+ render: args => {
135
+ const onUpdateVisible = useArgsModel('visible')
136
+ const onUpdateActive = useArgsModel('active')
137
+ return {
138
+ components: { SidebarModal, Button, Input, Switch },
139
+ setup: () => ({ args, onUpdateVisible, onUpdateActive }),
140
+ // Opening through `#trigger` keeps the demo alive on the Docs page, where
141
+ // only the primary story's args round-trip; the emits still sync Controls.
142
+ template: `
143
+ <SidebarModal
144
+ v-bind="args"
145
+ @update:visible="onUpdateVisible"
146
+ @update:active="onUpdateActive"
147
+ >
148
+ <template #trigger>
149
+ <Button>Open Settings</Button>
150
+ </template>
151
+
152
+ ${generalPane}
153
+ ${accountPane}
154
+ ${fallbackPane}
155
+ </SidebarModal>
156
+ `,
157
+ }
158
+ },
159
+ } satisfies Meta<typeof SidebarModal>
160
+
161
+ export default meta
162
+ type Story = StoryObj<typeof meta>
163
+
164
+ const noControls = { controls: { disable: true }} satisfies Story['parameters']
165
+
166
+ export const Default: Story = {}
167
+
168
+ export const WithoutSearch: Story = {
169
+ parameters: noControls,
170
+ args: {
171
+ searchable: false,
172
+ },
173
+ }
174
+
175
+ export const SearchPlaceholder: Story = {
176
+ parameters: noControls,
177
+ args: {
178
+ searchPlaceholder: 'Find a setting',
179
+ },
180
+ }
181
+
182
+ // A search flattens matched children so none stays hidden behind a closed parent.
183
+ export const NestedMenus: Story = {
184
+ parameters: noControls,
185
+ args: {
186
+ menus: nestedMenus,
187
+ active: 'members',
188
+ },
189
+ }
190
+
191
+ export const Sizing: Story = {
192
+ parameters: noControls,
193
+ args: {
194
+ sidebarWidth: '12rem',
195
+ contentClass: 'px-8 py-6',
196
+ class: 'sm:max-w-2xl h-[560px]',
197
+ },
198
+ }
199
+
200
+ // Both columns outgrow the dialog: the height stays capped and each side
201
+ // scrolls on its own, with the search field and the pane header staying put.
202
+ export const Scrolling: Story = {
203
+ parameters: {
204
+ ...noControls,
205
+ docs: {
206
+ source: {
207
+ code: `
208
+ <template>
209
+ <SidebarModal
210
+ :menus="menus"
211
+ title="Settings"
212
+ searchable
213
+ >
214
+ <template #trigger>
215
+ <Button>Open Settings</Button>
216
+ </template>
217
+
218
+ <template #general>
219
+ <div class="divide-y divide-border">
220
+ <div
221
+ v-for="i in 20"
222
+ :key="i"
223
+ class="flex items-center justify-between gap-4 py-4"
224
+ >
225
+ <span class="text-sm">Preference {{ i }}</span>
226
+ <Switch :modelValue="i % 2 === 0" />
227
+ </div>
228
+ </div>
229
+ </template>
230
+ </SidebarModal>
231
+ </template>
232
+ `.trim(),
233
+ },
234
+ },
235
+ },
236
+ render: () => ({
237
+ components: { SidebarModal, Button, Switch },
238
+ setup: () => ({ menus: scrollingMenus }),
239
+ template: `
240
+ <SidebarModal :menus="menus" title="Settings" searchable>
241
+ <template #trigger>
242
+ <Button>Open Settings</Button>
243
+ </template>
244
+
245
+ ${scrollingPane}
246
+
247
+ <template #default="{ item }">
248
+ <h2 class="text-lg font-semibold">{{ item?.label }}</h2>
249
+ </template>
250
+ </SidebarModal>
251
+ `,
252
+ }),
253
+ }
254
+
255
+ export const ClickOutsideToClose: Story = {
256
+ parameters: noControls,
257
+ args: {
258
+ showClose: false,
259
+ closeOnClickOutside: true,
260
+ },
261
+ }
262
+
263
+ export const NonModal: Story = {
264
+ parameters: noControls,
265
+ args: {
266
+ modal: false,
267
+ },
268
+ }
269
+
270
+ export const CustomHeader: Story = {
271
+ parameters: {
272
+ ...noControls,
273
+ docs: {
274
+ source: {
275
+ code: `
276
+ <template>
277
+ <SidebarModal
278
+ v-model:visible="visible"
279
+ :menus="menus"
280
+ title="Settings"
281
+ >
282
+ <template #header>
283
+ <div class="flex items-center gap-2 px-2 py-1">
284
+ <Icon name="building-2" />
285
+ <span class="text-base font-semibold">Acme Inc.</span>
286
+ </div>
287
+ </template>
288
+
289
+ <template #footer>
290
+ <Button
291
+ variant="ghost"
292
+ icon="log-out"
293
+ class="justify-start"
294
+ >
295
+ Sign out
296
+ </Button>
297
+ </template>
298
+
299
+ <template #default="{ item }">
300
+ <h2 class="text-lg font-semibold">{{ item?.label }}</h2>
301
+ </template>
302
+ </SidebarModal>
303
+ </template>
304
+ `.trim(),
305
+ },
306
+ },
307
+ },
308
+ render: () => ({
309
+ components: { SidebarModal, Button, Icon },
310
+ setup () {
311
+ const visible = ref(false)
312
+ return { visible, menus }
313
+ },
314
+ template: `
315
+ <div>
316
+ <Button @click="visible = true">Open Settings</Button>
317
+ <SidebarModal v-model:visible="visible" :menus="menus" title="Settings">
318
+ <template #header>
319
+ <div class="flex items-center gap-2 px-2 py-1">
320
+ <Icon name="building-2" />
321
+ <span class="text-base font-semibold">Acme Inc.</span>
322
+ </div>
323
+ </template>
324
+
325
+ <template #footer>
326
+ <Button variant="ghost" icon="log-out" class="justify-start">Sign out</Button>
327
+ </template>
328
+
329
+ <template #default="{ item }">
330
+ <h2 class="text-lg font-semibold">{{ item?.label }}</h2>
331
+ </template>
332
+ </SidebarModal>
333
+ </div>
334
+ `,
335
+ }),
336
+ }
337
+
338
+ export const EventHandling: Story = {
339
+ parameters: noControls,
340
+ render: () => ({
341
+ components: { SidebarModal, Button, EventLog },
342
+ setup () {
343
+ const visible = ref(false)
344
+ return { visible, menus }
345
+ },
346
+ template: `
347
+ <EventLog v-slot="{ record }">
348
+ <Button @click="visible = true">Open Settings</Button>
349
+ <SidebarModal
350
+ v-model:visible="visible"
351
+ :menus="menus"
352
+ title="Settings"
353
+ searchable
354
+ @open="record('open')"
355
+ @close="record('close')"
356
+ @closed="record('closed')"
357
+ @select="(item) => record('select', item.label)"
358
+ @update:active="(key) => record('update:active', key)"
359
+ @update:visible="(value) => record('update:visible', value)"
360
+ >
361
+ <template #default="{ item }">
362
+ <h2 class="text-lg font-semibold">{{ item?.label }}</h2>
363
+ </template>
364
+ </SidebarModal>
365
+ </EventLog>
366
+ `,
367
+ }),
368
+ }