@bjj/nuxt-layer-shadcn-ui 0.15.0 → 0.15.2

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.
@@ -71,10 +71,8 @@ function hasActiveChild (item: SidebarLayoutMenuItem): boolean {
71
71
  return item.children?.some(isActive) ?? false
72
72
  }
73
73
 
74
- // A linked item navigates through its `WebLink`, so `command` is for the rest.
75
74
  function handleSelect (item: SidebarLayoutMenuItem) {
76
75
  emit('select', item)
77
- if (!isLink(item)) item.command?.()
78
76
  }
79
77
  </script>
80
78
 
@@ -160,6 +160,28 @@ export const Embedded: Story = {
160
160
  },
161
161
  }
162
162
 
163
+ // Embedded on a phone viewport: the nav stacks above the content as a strip.
164
+ export const Stacked: Story = {
165
+ parameters: {
166
+ ...noControls,
167
+ viewport: {
168
+ options: {
169
+ mobile: {
170
+ name: 'Mobile',
171
+ styles: { width: '390px', height: '844px' },
172
+ type: 'mobile',
173
+ },
174
+ },
175
+ },
176
+ },
177
+ globals: {
178
+ viewport: { value: 'mobile', isRotated: false },
179
+ },
180
+ args: {
181
+ embedded: true,
182
+ },
183
+ }
184
+
163
185
  export const SidebarWidth: Story = {
164
186
  parameters: noControls,
165
187
  args: {
@@ -1,7 +1,12 @@
1
1
  <script setup lang="ts">
2
+ import type { TabsItem } from '../Tabs/types'
2
3
  import type { SidebarLayoutMenuItem, SidebarLayoutProps } from './types'
3
4
  import SidebarMenus from './SidebarMenus.vue'
4
- import { sidebarMenuItemKey } from './utils'
5
+ import {
6
+ findSidebarMenuItem,
7
+ flattenSidebarMenuItems,
8
+ sidebarMenuItemKey,
9
+ } from './utils'
5
10
  import {
6
11
  Sidebar,
7
12
  SidebarContent,
@@ -50,17 +55,42 @@ provideSidebarContext({
50
55
  toggleSidebar: noop,
51
56
  })
52
57
 
58
+ const { isMobile } = useDevice()
59
+
60
+ // A 16rem column would eat a phone's width, so an embedded layout stacks its nav
61
+ // above the content there. Page mode keeps the vendored drawer instead.
62
+ const stacked = computed(() => props.embedded && isMobile.value)
63
+
64
+ const tabItems = computed<TabsItem[]>(() =>
65
+ flattenSidebarMenuItems(props.menus).map(item => ({
66
+ value: sidebarMenuItemKey(item),
67
+ title: item.label,
68
+ icon: item.icon,
69
+ })),
70
+ )
71
+
53
72
  const rootComponent = computed(() => props.embedded ? 'div' : SidebarProvider)
54
73
 
55
74
  const rootStyle = computed(() => ({ '--sidebar-width': props.sidebarWidth }))
56
75
 
57
76
  const rootClass = computed(() =>
58
- cn(props.embedded && 'min-h-0 flex size-full overflow-hidden', props.class),
77
+ cn(
78
+ props.embedded && 'min-h-0 flex size-full overflow-hidden',
79
+ stacked.value && 'flex-col',
80
+ props.class,
81
+ ),
59
82
  )
60
83
 
84
+ // A linked item navigates through its `WebLink`, so `command` is for the rest.
61
85
  function onSelect (item: SidebarLayoutMenuItem) {
62
86
  emit('select', item)
63
87
  emit('update:active', sidebarMenuItemKey(item))
88
+ if (!item.href) item.command?.()
89
+ }
90
+
91
+ function onTabSelect (key: string) {
92
+ const item = findSidebarMenuItem(props.menus, key)
93
+ if (item) onSelect(item)
64
94
  }
65
95
  </script>
66
96
 
@@ -72,8 +102,41 @@ function onSelect (item: SidebarLayoutMenuItem) {
72
102
  >
73
103
  <!-- Menu-button tooltips need a provider, which embedded mode has none of. -->
74
104
  <TooltipProvider>
105
+ <!-- Stacked: the menu becomes a scrollable strip, groups flattened away. -->
106
+ <template v-if="stacked">
107
+ <div
108
+ v-if="$slots.header"
109
+ class="gap-2 p-2 flex flex-col"
110
+ >
111
+ <slot name="header" />
112
+ </div>
113
+
114
+ <div
115
+ v-if="emptyText && !menus.length"
116
+ class="px-4 py-3 text-sm text-muted-foreground border-b text-center"
117
+ >
118
+ {{ emptyText }}
119
+ </div>
120
+ <Tabs
121
+ v-else
122
+ :items="tabItems"
123
+ :modelValue="active"
124
+ listClass="
125
+ h-auto w-full gap-1 rounded-none border-b bg-transparent p-2
126
+ justify-start overflow-x-auto
127
+ "
128
+ triggerClass="
129
+ data-[state=active]:bg-accent
130
+ data-[state=active]:shadow-none
131
+ flex-none
132
+ "
133
+ @update:modelValue="onTabSelect"
134
+ />
135
+ </template>
136
+
75
137
  <!-- `shrink-0`: wide pane content scrolls instead of squeezing the menu. -->
76
138
  <Sidebar
139
+ v-else
77
140
  :variant="variant"
78
141
  :collapsible="embedded ? 'none' : collapsible"
79
142
  :class="embedded ? 'shrink-0 border-r' : undefined"
@@ -108,7 +171,7 @@ function onSelect (item: SidebarLayoutMenuItem) {
108
171
  Top padding on mobile reserves a safe area for the floating trigger. -->
109
172
  <SidebarInset
110
173
  :class="[
111
- 'min-w-0',
174
+ 'min-w-0 min-h-0',
112
175
  embedded ? 'bg-transparent' : 'bg-card',
113
176
  !embedded && `
114
177
  pt-14
@@ -19,8 +19,13 @@ export interface SidebarLayoutProps {
19
19
  active?: string
20
20
  /** Placeholder shown when `menus` is empty. */
21
21
  emptyText?: string
22
- /** Fill the parent instead of the viewport: never collapses, no rail or mobile trigger. */
22
+ /** Fill the parent instead of the viewport: never collapses, no rail. */
23
23
  embedded?: boolean
24
+ /**
25
+ * Stack the nav above the content as a scrollable strip instead of placing it
26
+ * in a column. The caller decides when — the layout holds no breakpoint.
27
+ */
28
+ stacked?: boolean
24
29
  variant?: 'sidebar' | 'floating' | 'inset'
25
30
  /** Collapse behaviour of the sidebar. Ignored while `embedded`. */
26
31
  collapsible?: 'offcanvas' | 'icon' | 'none'
@@ -17,13 +17,18 @@ export function findSidebarMenuItem (
17
17
  return undefined
18
18
  }
19
19
 
20
+ /** Selectable (childless) items in listing order, with grouping flattened away. */
21
+ export function flattenSidebarMenuItems (
22
+ menus: SidebarLayoutMenuItem[],
23
+ ): SidebarLayoutMenuItem[] {
24
+ return menus.flatMap(item =>
25
+ item.children?.length ? flattenSidebarMenuItems(item.children) : [ item ],
26
+ )
27
+ }
28
+
20
29
  export function firstSidebarMenuItemKey (
21
30
  menus: SidebarLayoutMenuItem[],
22
31
  ): 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
32
+ const [ first ] = flattenSidebarMenuItems(menus)
33
+ return first && sidebarMenuItemKey(first)
29
34
  }
@@ -1,5 +1,6 @@
1
1
  import type { Meta, StoryObj } from '@storybook/vue3'
2
2
  import type { SidebarLayoutMenuItem } from '../SidebarLayout/types'
3
+ import type { SidebarModalFilter } from './types'
3
4
  import EventLog from '#storybook/EventLog.vue'
4
5
  import { useArgsModel } from '#storybook/argsModel'
5
6
  import Button from '../Button/index.vue'
@@ -43,6 +44,31 @@ const scrollingMenus: SidebarLayoutMenuItem[] = [
43
44
  })),
44
45
  ]
45
46
 
47
+ // The rows each pane renders, doubling as the index a business search would hit.
48
+ const paneContent: Record<string, string[]> = {
49
+ general: [ 'full name', 'email', 'notifications' ],
50
+ account: [ 'password', 'two-factor', 'sessions' ],
51
+ usage: [ 'quota', 'billing period' ],
52
+ capabilities: [ 'beta features' ],
53
+ appearance: [ 'theme', 'dark mode', 'font size' ],
54
+ extensions: [ 'browser extension' ],
55
+ developer: [ 'api key', 'webhooks' ],
56
+ skills: [ 'custom instructions' ],
57
+ connectors: [ 'google drive', 'slack' ],
58
+ }
59
+
60
+ const searchByContent: SidebarModalFilter = (items, keyword) => {
61
+ if (!keyword) return items
62
+ const needle = keyword.toLowerCase()
63
+ return items.filter(item => {
64
+ const key = item.key ?? item.label
65
+ return [ item.label, item.group, ...(paneContent[key] ?? []) ]
66
+ .join(' ')
67
+ .toLowerCase()
68
+ .includes(needle)
69
+ })
70
+ }
71
+
46
72
  const generalPane = `
47
73
  <template #general>
48
74
  <div class="space-y-4">
@@ -110,6 +136,7 @@ const meta = {
110
136
  visible: { control: 'boolean' },
111
137
  active: { control: 'text' },
112
138
  searchable: { control: 'boolean' },
139
+ search: { control: 'text' },
113
140
  searchPlaceholder: { control: 'text' },
114
141
  title: { control: 'text' },
115
142
  modal: { control: 'boolean' },
@@ -123,6 +150,7 @@ const meta = {
123
150
  visible: false,
124
151
  active: 'general',
125
152
  searchable: true,
153
+ search: '',
126
154
  searchPlaceholder: '',
127
155
  title: 'Settings',
128
156
  modal: true,
@@ -134,9 +162,10 @@ const meta = {
134
162
  render: args => {
135
163
  const onUpdateVisible = useArgsModel('visible')
136
164
  const onUpdateActive = useArgsModel('active')
165
+ const onUpdateSearch = useArgsModel('search')
137
166
  return {
138
167
  components: { SidebarModal, Button, Input, Switch },
139
- setup: () => ({ args, onUpdateVisible, onUpdateActive }),
168
+ setup: () => ({ args, onUpdateVisible, onUpdateActive, onUpdateSearch }),
140
169
  // Opening through `#trigger` keeps the demo alive on the Docs page, where
141
170
  // only the primary story's args round-trip; the emits still sync Controls.
142
171
  template: `
@@ -144,6 +173,7 @@ const meta = {
144
173
  v-bind="args"
145
174
  @update:visible="onUpdateVisible"
146
175
  @update:active="onUpdateActive"
176
+ @update:search="onUpdateSearch"
147
177
  >
148
178
  <template #trigger>
149
179
  <Button>Open Settings</Button>
@@ -180,6 +210,85 @@ export const SearchPlaceholder: Story = {
180
210
  }
181
211
 
182
212
  // A search flattens matched children so none stays hidden behind a closed parent.
213
+ // A `searchable` function owns the listing, so the keyword can be matched against
214
+ // pane content: "dark" surfaces Appearance even though no label contains it.
215
+ export const CustomSearch: Story = {
216
+ parameters: {
217
+ ...noControls,
218
+ docs: {
219
+ source: {
220
+ code: `
221
+ <template>
222
+ <SidebarModal
223
+ :menus="menus"
224
+ :searchable="searchByContent"
225
+ title="Settings"
226
+ >
227
+ <template #trigger>
228
+ <Button>Open Settings</Button>
229
+ </template>
230
+
231
+ <template #default="{ active, item }">
232
+ <h2 class="text-lg font-semibold">{{ item?.label }}</h2>
233
+ <div
234
+ v-for="row in paneContent[active]"
235
+ :key="row"
236
+ class="py-3 text-sm"
237
+ >
238
+ {{ row }}
239
+ </div>
240
+ </template>
241
+ </SidebarModal>
242
+ </template>
243
+
244
+ <script setup lang="ts">
245
+ // One source of truth: the rows a pane renders are the rows search matches.
246
+ const searchByContent: SidebarModalFilter = (items, keyword) => {
247
+ if (!keyword) return items
248
+ const needle = keyword.toLowerCase()
249
+ return items.filter(item => [ item.label, item.group, ...paneContent[item.key] ?? [] ]
250
+ .join(' ')
251
+ .toLowerCase()
252
+ .includes(needle))
253
+ }
254
+ </script>
255
+ `.trim(),
256
+ },
257
+ },
258
+ },
259
+ render: () => ({
260
+ components: { SidebarModal, Button },
261
+ setup: () => ({ menus, searchByContent, paneContent }),
262
+ template: `
263
+ <SidebarModal
264
+ :menus="menus"
265
+ :searchable="searchByContent"
266
+ title="Settings"
267
+ searchPlaceholder="Search settings or content"
268
+ >
269
+ <template #trigger>
270
+ <Button>Open Settings</Button>
271
+ </template>
272
+
273
+ <template #default="{ active, item }">
274
+ <div class="space-y-4">
275
+ <h2 class="text-lg font-semibold">{{ item?.label }}</h2>
276
+ <div class="divide-y divide-border">
277
+ <div
278
+ v-for="row in paneContent[active] || []"
279
+ :key="row"
280
+ class="py-3 text-sm capitalize"
281
+ >
282
+ {{ row }}
283
+ </div>
284
+ </div>
285
+ </div>
286
+ </template>
287
+ </SidebarModal>
288
+ `,
289
+ }),
290
+ }
291
+
183
292
  export const NestedMenus: Story = {
184
293
  parameters: noControls,
185
294
  args: {
@@ -252,6 +361,25 @@ export const Scrolling: Story = {
252
361
  }),
253
362
  }
254
363
 
364
+ // Phone viewport: the menu becomes a scrollable strip under a visible title row.
365
+ export const Mobile: Story = {
366
+ parameters: {
367
+ ...noControls,
368
+ viewport: {
369
+ options: {
370
+ mobile: {
371
+ name: 'Mobile',
372
+ styles: { width: '390px', height: '844px' },
373
+ type: 'mobile',
374
+ },
375
+ },
376
+ },
377
+ },
378
+ globals: {
379
+ viewport: { value: 'mobile', isRotated: false },
380
+ },
381
+ }
382
+
255
383
  export const ClickOutsideToClose: Story = {
256
384
  parameters: noControls,
257
385
  args: {
@@ -267,6 +395,7 @@ export const NonModal: Story = {
267
395
  },
268
396
  }
269
397
 
398
+ // The `#header` slot sits above the built-in search field rather than replacing it.
270
399
  export const CustomHeader: Story = {
271
400
  parameters: {
272
401
  ...noControls,
@@ -278,6 +407,7 @@ export const CustomHeader: Story = {
278
407
  v-model:visible="visible"
279
408
  :menus="menus"
280
409
  title="Settings"
410
+ searchable
281
411
  >
282
412
  <template #header>
283
413
  <div class="flex items-center gap-2 px-2 py-1">
@@ -314,7 +444,7 @@ export const CustomHeader: Story = {
314
444
  template: `
315
445
  <div>
316
446
  <Button @click="visible = true">Open Settings</Button>
317
- <SidebarModal v-model:visible="visible" :menus="menus" title="Settings">
447
+ <SidebarModal v-model:visible="visible" :menus="menus" title="Settings" searchable>
318
448
  <template #header>
319
449
  <div class="flex items-center gap-2 px-2 py-1">
320
450
  <Icon name="building-2" />
@@ -16,6 +16,7 @@ defineOptions({ inheritAttrs: false })
16
16
  const props = withDefaults(defineProps<SidebarModalProps>(), {
17
17
  visible: undefined,
18
18
  active: undefined,
19
+ search: undefined,
19
20
  searchPlaceholder: undefined,
20
21
  title: undefined,
21
22
  modal: true,
@@ -29,6 +30,7 @@ const props = withDefaults(defineProps<SidebarModalProps>(), {
29
30
  const emit = defineEmits<{
30
31
  'update:visible': [value: boolean]
31
32
  'update:active': [key: string]
33
+ 'update:search': [keyword: string]
32
34
  'select': [item: SidebarLayoutMenuItem]
33
35
  'open': []
34
36
  'close': []
@@ -49,7 +51,7 @@ const T = useTranslations('components.ui.SidebarModal')
49
51
 
50
52
  const dialogOpen = ref(props.visible ?? false)
51
53
  const activeKey = ref(props.active ?? '')
52
- const searchQuery = ref<string>()
54
+ const searchQuery = ref(props.search)
53
55
 
54
56
  const activeItem = computed(() => findSidebarMenuItem(props.menus, activeKey.value))
55
57
 
@@ -58,24 +60,35 @@ const paneSlotProps = computed<SidebarModalPaneSlotProps>(() => ({
58
60
  item: activeItem.value,
59
61
  }))
60
62
 
61
- const searchKeyword = computed(() => searchQuery.value?.trim().toLowerCase() ?? '')
63
+ const searchKeyword = computed(() => searchQuery.value?.trim() ?? '')
62
64
 
63
- const filteredMenus = computed(() =>
64
- searchKeyword.value ? filterMenus(props.menus, searchKeyword.value) : props.menus,
65
- )
65
+ const filteredMenus = computed(() => {
66
+ // A custom filter runs for every keyword, empty included, so it fully owns the
67
+ // listing; reading its own reactive state in there also covers async results.
68
+ if (typeof props.searchable === 'function') {
69
+ return props.searchable(props.menus, searchKeyword.value)
70
+ }
71
+ if (!searchKeyword.value) return props.menus
72
+ return filterMenus(props.menus, searchKeyword.value.toLowerCase())
73
+ })
66
74
 
67
75
  const dialogClass = computed(() =>
68
76
  cn(
69
77
  `
70
78
  gap-0 bg-popover p-0
71
- sm:max-w-4xl
72
- flex h-[min(80vh,42rem)] flex-col overflow-hidden
79
+ md:max-w-4xl md:h-[min(80vh,42rem)]
80
+ flex h-[85vh] flex-col overflow-hidden
73
81
  `,
74
82
  props.class,
75
83
  ),
76
84
  )
77
85
 
78
- const paneClass = computed(() => cn('px-6 py-5', props.contentClass))
86
+ const paneClass = computed(() =>
87
+ cn(`
88
+ px-4 py-4
89
+ md:px-6 md:py-5
90
+ `, props.contentClass),
91
+ )
79
92
 
80
93
  watch(() => props.visible, value => {
81
94
  if (value !== undefined) dialogOpen.value = value
@@ -92,6 +105,12 @@ watch(dialogOpen, value => {
92
105
  searchQuery.value = undefined
93
106
  })
94
107
 
108
+ watch(() => props.search, keyword => {
109
+ if (keyword !== undefined) searchQuery.value = keyword
110
+ })
111
+
112
+ watch(searchQuery, keyword => emit('update:search', keyword ?? ''))
113
+
95
114
  watch(() => props.active, key => {
96
115
  if (key !== undefined) activeKey.value = key
97
116
  })
@@ -153,7 +172,13 @@ function onPointerDownOutside (event: Event) {
153
172
  @closeAutoFocus="emit('closed')"
154
173
  >
155
174
  <!-- A11y: reka-ui requires a title and a description inside the dialog. -->
156
- <DialogTitle class="sr-only">
175
+ <DialogTitle
176
+ class="
177
+ px-4 pt-4 text-base font-semibold
178
+ md:sr-only md:p-0
179
+ shrink-0
180
+ "
181
+ >
157
182
  {{ title }}
158
183
  </DialogTitle>
159
184
  <DialogDescription class="sr-only" />
@@ -171,16 +196,17 @@ function onPointerDownOutside (event: Event) {
171
196
  v-if="searchable || $slots.header"
172
197
  #header
173
198
  >
174
- <slot name="header">
175
- <Input
176
- v-model="searchQuery"
177
- :placeholder="searchPlaceholder || T('searchPlaceholder')"
178
- >
179
- <template #prefix>
180
- <Icon name="search" />
181
- </template>
182
- </Input>
183
- </slot>
199
+ <slot name="header" />
200
+
201
+ <Input
202
+ v-if="searchable"
203
+ v-model="searchQuery"
204
+ :placeholder="searchPlaceholder || T('searchPlaceholder')"
205
+ >
206
+ <template #prefix>
207
+ <Icon name="search" />
208
+ </template>
209
+ </Input>
184
210
  </template>
185
211
 
186
212
  <template
@@ -6,13 +6,28 @@ export interface SidebarModalPaneSlotProps {
6
6
  item?: SidebarLayoutMenuItem
7
7
  }
8
8
 
9
+ /**
10
+ * Menus to list for the current keyword. Receives the keyword as typed (trimmed),
11
+ * so a business search can look beyond labels — pane content, a remote index —
12
+ * and return whatever entries should show.
13
+ */
14
+ export type SidebarModalFilter = (
15
+ menus: SidebarLayoutMenuItem[],
16
+ keyword: string,
17
+ ) => SidebarLayoutMenuItem[]
18
+
9
19
  export interface SidebarModalProps {
10
20
  menus: SidebarLayoutMenuItem[]
11
21
  visible?: boolean
12
22
  /** Key of the selected menu item. Omitted → the first selectable item. */
13
23
  active?: string
14
- /** Render a search field above the menu, filtering items by label and group. */
15
- searchable?: boolean
24
+ /**
25
+ * Render a search field between `#header` and the menu. `true`: filter menus by
26
+ * label and group; function: custom filter (replaces the built-in one).
27
+ */
28
+ searchable?: boolean | SidebarModalFilter
29
+ /** Search keyword. Use `v-model:search` to read or drive it from the outside. */
30
+ search?: string
16
31
  searchPlaceholder?: string
17
32
  /** Accessible name of the dialog, rendered for screen readers only. */
18
33
  title?: string
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bjj/nuxt-layer-shadcn-ui",
3
- "version": "0.15.0",
3
+ "version": "0.15.2",
4
4
  "description": "Nuxt layer providing shadcn-vue based UI components",
5
5
  "type": "module",
6
6
  "main": "./nuxt.config.ts",
@@ -42,5 +42,5 @@
42
42
  "vue-i18n": "^11",
43
43
  "vue-router": "^4 || ^5"
44
44
  },
45
- "gitHead": "8b94a7bdf44027021baccd353801079740f84b3e"
45
+ "gitHead": "44db021855f40b5899bc164cfcea07d75791956b"
46
46
  }