@bwui/vue 0.1.13 → 0.1.15

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/src/chrome.ts ADDED
@@ -0,0 +1,385 @@
1
+ /**
2
+ * Vue mirrors for Tabs (H01), Avatar (A05) and Breadcrumbs (H02).
3
+ * Class and a11y contracts match packages/react/src/chrome.tsx as far as
4
+ * practical for Vue 3 (provide/inject, update:value).
5
+ */
6
+ import {
7
+ defineComponent,
8
+ h,
9
+ provide,
10
+ inject,
11
+ ref,
12
+ onBeforeUnmount,
13
+ type PropType,
14
+ type InjectionKey,
15
+ type Ref,
16
+ type VNodeRef,
17
+ } from 'vue'
18
+ import { cn } from './cn'
19
+
20
+ export type TabsVariant = 'underline' | 'pill'
21
+
22
+ type TabsContext = {
23
+ id: () => string
24
+ value: Ref<string>
25
+ onValueChange: (value: string) => void
26
+ variant: () => TabsVariant
27
+ registerTab: (value: string, el: HTMLButtonElement | null) => void
28
+ tabOrder: Ref<string[]>
29
+ tabEls: Map<string, HTMLButtonElement>
30
+ }
31
+
32
+ const tabsKey: InjectionKey<TabsContext> = Symbol('bw-tabs')
33
+
34
+ function useTabs(): TabsContext {
35
+ const ctx = inject(tabsKey, null)
36
+ if (!ctx) throw new Error('Tabs parts must be used within <Tabs>')
37
+ return ctx
38
+ }
39
+
40
+ export const Tabs = defineComponent({
41
+ name: 'BwTabs',
42
+ inheritAttrs: false,
43
+ props: {
44
+ id: { type: String, default: 'bw-tabs' },
45
+ value: { type: String, required: true },
46
+ variant: { type: String as PropType<TabsVariant>, default: 'underline' },
47
+ },
48
+ emits: ['update:value'],
49
+ setup(props, { slots, attrs, emit }) {
50
+ const tabEls = new Map<string, HTMLButtonElement>()
51
+ const tabOrder = ref<string[]>([])
52
+ const valueRef = ref(props.value)
53
+
54
+ const registerTab = (tabValue: string, el: HTMLButtonElement | null) => {
55
+ if (el) {
56
+ tabEls.set(tabValue, el)
57
+ if (!tabOrder.value.includes(tabValue)) {
58
+ tabOrder.value = [...tabOrder.value, tabValue]
59
+ }
60
+ } else {
61
+ tabEls.delete(tabValue)
62
+ tabOrder.value = tabOrder.value.filter((v) => v !== tabValue)
63
+ }
64
+ }
65
+
66
+ const onValueChange = (next: string) => {
67
+ valueRef.value = next
68
+ emit('update:value', next)
69
+ }
70
+
71
+ const onListKeyDown = (event: KeyboardEvent) => {
72
+ const keys = tabOrder.value
73
+ if (!keys.length) return
74
+ const active = document.activeElement
75
+ let currentIndex = -1
76
+ for (let i = 0; i < keys.length; i++) {
77
+ if (tabEls.get(keys[i]) === active) {
78
+ currentIndex = i
79
+ break
80
+ }
81
+ }
82
+ if (currentIndex < 0) currentIndex = Math.max(0, keys.indexOf(valueRef.value))
83
+ const rtl =
84
+ (typeof document !== 'undefined' &&
85
+ (document.documentElement.getAttribute('dir') === 'rtl' ||
86
+ document.body.getAttribute('dir') === 'rtl')) ||
87
+ false
88
+ let next = currentIndex
89
+ if (event.key === 'ArrowRight') {
90
+ next = rtl ? (currentIndex - 1 + keys.length) % keys.length : (currentIndex + 1) % keys.length
91
+ } else if (event.key === 'ArrowLeft') {
92
+ next = rtl ? (currentIndex + 1) % keys.length : (currentIndex - 1 + keys.length) % keys.length
93
+ } else if (event.key === 'Home') {
94
+ next = 0
95
+ } else if (event.key === 'End') {
96
+ next = keys.length - 1
97
+ } else {
98
+ return
99
+ }
100
+ event.preventDefault()
101
+ tabEls.get(keys[next])?.focus()
102
+ // Manual activation: arrows move focus only; Enter/Space/click select.
103
+ }
104
+
105
+ provide(tabsKey, {
106
+ id: () => props.id,
107
+ value: valueRef,
108
+ onValueChange,
109
+ variant: () => props.variant,
110
+ registerTab,
111
+ tabOrder,
112
+ tabEls,
113
+ })
114
+
115
+ return () => {
116
+ valueRef.value = props.value
117
+ const { class: _class, ...rest } = attrs as Record<string, unknown>
118
+ return h(
119
+ 'div',
120
+ {
121
+ ...rest,
122
+ class: cn('bw-tabs', `bw-tabs--${props.variant}`, String(attrs.class || '')),
123
+ 'data-bw-tabs-id': props.id,
124
+ onKeydown: onListKeyDown,
125
+ },
126
+ slots.default?.(),
127
+ )
128
+ }
129
+ },
130
+ })
131
+
132
+ export const TabList = defineComponent({
133
+ name: 'BwTabList',
134
+ inheritAttrs: false,
135
+ setup(_, { slots, attrs }) {
136
+ return () => {
137
+ const { class: _class, ...rest } = attrs as Record<string, unknown>
138
+ return h(
139
+ 'div',
140
+ {
141
+ ...rest,
142
+ class: cn('bw-tabs__list', String(attrs.class || '')),
143
+ role: 'tablist',
144
+ },
145
+ slots.default?.(),
146
+ )
147
+ }
148
+ },
149
+ })
150
+
151
+ export const Tab = defineComponent({
152
+ name: 'BwTab',
153
+ inheritAttrs: false,
154
+ props: {
155
+ value: { type: String, required: true },
156
+ badge: { type: [String, Number, Boolean] as PropType<string | number | boolean>, default: undefined },
157
+ },
158
+ setup(props, { slots, attrs }) {
159
+ const tabs = useTabs()
160
+ const elRef = ref<HTMLButtonElement | null>(null)
161
+ const setRef: VNodeRef = (el) => {
162
+ elRef.value = (el as HTMLButtonElement | null) ?? null
163
+ tabs.registerTab(props.value, elRef.value)
164
+ }
165
+ onBeforeUnmount(() => tabs.registerTab(props.value, null))
166
+ return () => {
167
+ const selected = tabs.value.value === props.value
168
+ const tabId = `bw-tab-${tabs.id()}-${props.value}`
169
+ const panelId = `bw-tabpanel-${tabs.id()}-${props.value}`
170
+ const badgeContent = slots.badge?.() ?? (props.badge != null && props.badge !== false ? props.badge : null)
171
+ const { class: _class, ...rest } = attrs as Record<string, unknown>
172
+ return h(
173
+ 'button',
174
+ {
175
+ ...rest,
176
+ ref: setRef,
177
+ type: 'button',
178
+ role: 'tab',
179
+ id: tabId,
180
+ class: cn('bw-tabs__tab', selected && 'bw-tabs__tab--active', String(attrs.class || '')),
181
+ 'aria-selected': selected,
182
+ 'aria-controls': panelId,
183
+ tabindex: selected ? 0 : -1,
184
+ ...(selected ? { 'data-bw-active': '' } : {}),
185
+ onClick: () => tabs.onValueChange(props.value),
186
+ onKeydown: (event: KeyboardEvent) => {
187
+ if (event.key === 'Enter' || event.key === ' ') {
188
+ event.preventDefault()
189
+ tabs.onValueChange(props.value)
190
+ }
191
+ },
192
+ },
193
+ [
194
+ h('span', { class: 'bw-tabs__label' }, slots.default?.()),
195
+ badgeContent != null
196
+ ? h('span', { class: 'bw-tabs__badge' }, badgeContent)
197
+ : null,
198
+ ],
199
+ )
200
+ }
201
+ },
202
+ })
203
+
204
+ export const TabPanel = defineComponent({
205
+ name: 'BwTabPanel',
206
+ inheritAttrs: false,
207
+ props: {
208
+ value: { type: String, required: true },
209
+ },
210
+ setup(props, { slots, attrs }) {
211
+ const tabs = useTabs()
212
+ return () => {
213
+ const selected = tabs.value.value === props.value
214
+ const panelId = `bw-tabpanel-${tabs.id()}-${props.value}`
215
+ const tabId = `bw-tab-${tabs.id()}-${props.value}`
216
+ const { class: _class, ...rest } = attrs as Record<string, unknown>
217
+ return h(
218
+ 'div',
219
+ {
220
+ ...rest,
221
+ id: panelId,
222
+ role: 'tabpanel',
223
+ class: cn('bw-tabs__panel', String(attrs.class || '')),
224
+ 'aria-labelledby': tabId,
225
+ hidden: !selected,
226
+ tabindex: selected ? 0 : undefined,
227
+ },
228
+ slots.default?.(),
229
+ )
230
+ }
231
+ },
232
+ })
233
+
234
+ /** ADR-125 size steps for Avatar (AVR-003). */
235
+ export type AvatarSize = 'xs' | 'sm' | 'md' | 'lg' | 'xl'
236
+
237
+ /** Avatar corner treatment (AVR-004). */
238
+ export type AvatarShape = 'circle' | 'square'
239
+
240
+ /**
241
+ * Avatar (A05 / R10). Pass exactly one content shape: `src`+`alt`, `initials`,
242
+ * or `overflow`. Matches React `Avatar` class and a11y carriers (AVR-070).
243
+ */
244
+ export const Avatar = defineComponent({
245
+ name: 'BwAvatar',
246
+ inheritAttrs: false,
247
+ props: {
248
+ size: { type: String as PropType<AvatarSize>, default: 'md' },
249
+ shape: { type: String as PropType<AvatarShape>, default: 'circle' },
250
+ src: { type: String, default: undefined },
251
+ alt: { type: String, default: undefined },
252
+ initials: { type: String, default: undefined },
253
+ overflow: { type: Number, default: undefined },
254
+ },
255
+ setup(props, { attrs }) {
256
+ return () => {
257
+ const rootClass = (...parts: Array<string | false | null | undefined>) =>
258
+ cn(
259
+ 'bw-avatar',
260
+ `bw-avatar--size-${props.size}`,
261
+ `bw-avatar--shape-${props.shape}`,
262
+ ...parts,
263
+ String(attrs.class || ''),
264
+ )
265
+ const ariaLabel =
266
+ (attrs['aria-label'] as string | undefined) ??
267
+ (attrs.ariaLabel as string | undefined)
268
+
269
+ if (props.src != null) {
270
+ const { class: _c, 'aria-label': _a, ariaLabel: _al, ...rest } = attrs as Record<
271
+ string,
272
+ unknown
273
+ >
274
+ // Vue SSR collapses alt="" to a boolean attribute; keep the empty
275
+ // string in the VNode so the DOM still treats the image as decorative.
276
+ const alt = props.alt ?? ''
277
+ return h('span', { ...rest, class: rootClass() }, [
278
+ h('img', {
279
+ class: 'bw-avatar__image',
280
+ src: props.src,
281
+ alt,
282
+ }),
283
+ ])
284
+ }
285
+
286
+ if (props.overflow !== undefined) {
287
+ const { class: _c, 'aria-label': _a, ariaLabel: _al, ...rest } = attrs as Record<
288
+ string,
289
+ unknown
290
+ >
291
+ return h(
292
+ 'span',
293
+ {
294
+ ...rest,
295
+ class: rootClass('bw-avatar--overflow'),
296
+ ...(ariaLabel ? { role: 'img', 'aria-label': ariaLabel } : {}),
297
+ },
298
+ [
299
+ h(
300
+ 'span',
301
+ {
302
+ class: 'bw-avatar__initials',
303
+ ...(ariaLabel ? { 'aria-hidden': true } : {}),
304
+ },
305
+ `+${props.overflow}`,
306
+ ),
307
+ ],
308
+ )
309
+ }
310
+
311
+ const { class: _c, 'aria-label': _a, ariaLabel: _al, ...rest } = attrs as Record<
312
+ string,
313
+ unknown
314
+ >
315
+ const label = ariaLabel ?? props.initials
316
+ return h(
317
+ 'span',
318
+ {
319
+ ...rest,
320
+ class: rootClass('bw-avatar--initials'),
321
+ role: 'img',
322
+ 'aria-label': label,
323
+ },
324
+ [h('span', { class: 'bw-avatar__initials', 'aria-hidden': true }, props.initials)],
325
+ )
326
+ }
327
+ },
328
+ })
329
+
330
+ export type BreadcrumbItem = {
331
+ /** Intermediate crumbs with href render as links. The last crumb is never a link. */
332
+ href?: string
333
+ label: string
334
+ }
335
+
336
+ /**
337
+ * Breadcrumbs (H02 / R57): trail of ancestors ending in the current page.
338
+ * Last crumb is always unlinked with aria-current="page", even if href is set.
339
+ * Empty items renders nothing so a host :empty rule can collapse the region.
340
+ */
341
+ export const Breadcrumbs = defineComponent({
342
+ name: 'BwBreadcrumbs',
343
+ inheritAttrs: false,
344
+ props: {
345
+ items: { type: Array as PropType<BreadcrumbItem[]>, required: true },
346
+ /** Accessible name for the nav. Override for locales; default matches the Django shell. */
347
+ label: { type: String, default: 'Breadcrumb' },
348
+ },
349
+ setup(props, { attrs }) {
350
+ return () => {
351
+ if (!props.items.length) return null
352
+ const { class: _class, ...rest } = attrs as Record<string, unknown>
353
+ return h(
354
+ 'nav',
355
+ {
356
+ ...rest,
357
+ class: cn('bw-breadcrumbs', String(attrs.class || '')),
358
+ 'aria-label': props.label,
359
+ },
360
+ [
361
+ h(
362
+ 'ol',
363
+ { class: 'bw-breadcrumbs__list' },
364
+ props.items.map((item, i) => {
365
+ const last = i === props.items.length - 1
366
+ let child
367
+ if (item.href && !last) {
368
+ child = h('a', { class: 'bw-breadcrumbs__link', href: item.href }, item.label)
369
+ } else if (last) {
370
+ child = h(
371
+ 'span',
372
+ { class: 'bw-breadcrumbs__current', 'aria-current': 'page' },
373
+ item.label,
374
+ )
375
+ } else {
376
+ child = h('span', { class: 'bw-breadcrumbs__text' }, item.label)
377
+ }
378
+ return h('li', { key: `${item.label}-${i}`, class: 'bw-breadcrumbs__item' }, [child])
379
+ }),
380
+ ),
381
+ ],
382
+ )
383
+ }
384
+ },
385
+ })