|
| 1 | +import { useBoundProp } from '@json-render/vue' |
| 2 | +import { defineComponent, h, ref, watchEffect } from 'vue' |
| 3 | +import { getIconifySvg } from '../../utils/iconify' |
| 4 | +import { colors, primary, surfaceSubtle } from './tokens' |
| 5 | +import { registryProps } from './types' |
| 6 | + |
| 7 | +export interface TabDescriptor { |
| 8 | + value: string |
| 9 | + label: string |
| 10 | + icon?: string |
| 11 | + badge?: string |
| 12 | + badgeVariant?: 'default' | 'info' | 'success' | 'warning' | 'danger' |
| 13 | +} |
| 14 | + |
| 15 | +export interface TabsProps { |
| 16 | + /** `children[i]` renders when `tabs[i]` is active — the two arrays are positional. */ |
| 17 | + tabs: TabDescriptor[] |
| 18 | + /** Two-way bindable via `{ $bindState: '...' }`; otherwise the tab switches local, uncontrolled state. */ |
| 19 | + value?: string |
| 20 | + /** Seeds the uncontrolled case only — ignored once `value` is bound. */ |
| 21 | + defaultValue?: string |
| 22 | + orientation?: 'horizontal' | 'vertical' |
| 23 | +} |
| 24 | + |
| 25 | +export const Tabs = defineComponent({ |
| 26 | + name: 'JrTabs', |
| 27 | + props: registryProps<'Tabs', TabsProps>(), |
| 28 | + setup(ctx, { slots }) { |
| 29 | + /** Local fallback for when `value` has no `$bindState` binding — `useBoundProp`'s setter is a no-op without one. */ |
| 30 | + const uncontrolledValue = ref<string | undefined>(ctx.element.props.defaultValue ?? ctx.element.props.tabs?.[0]?.value) |
| 31 | + |
| 32 | + /** Icon SVGs keyed by name, resolved like `Icon.ts` — one tab's icon changing shouldn't refetch the others. */ |
| 33 | + const iconSvgs = ref<Record<string, string>>({}) |
| 34 | + watchEffect(async () => { |
| 35 | + const names = (ctx.element.props.tabs ?? []) |
| 36 | + .map(tab => tab.icon) |
| 37 | + .filter((name): name is string => !!name && !(name in iconSvgs.value)) |
| 38 | + for (const name of names) { |
| 39 | + const match = name.match(/^(?:i-)?([\w-]+):([\w-]+)$/) |
| 40 | + if (match?.[1] && match[2]) { |
| 41 | + const svg = await getIconifySvg(match[1], match[2]) |
| 42 | + if (svg) |
| 43 | + iconSvgs.value = { ...iconSvgs.value, [name]: svg } |
| 44 | + } |
| 45 | + } |
| 46 | + }) |
| 47 | + |
| 48 | + return () => { |
| 49 | + const tabs: TabDescriptor[] = ctx.element.props.tabs ?? [] |
| 50 | + const orientation: 'horizontal' | 'vertical' = ctx.element.props.orientation === 'vertical' ? 'vertical' : 'horizontal' |
| 51 | + const isVertical = orientation === 'vertical' |
| 52 | + |
| 53 | + const [boundValue, setBoundValue] = useBoundProp<string>(ctx.element.props.value, ctx.bindings?.value) |
| 54 | + const controlled = ctx.bindings?.value != null |
| 55 | + const activeValue = controlled ? boundValue : uncontrolledValue.value |
| 56 | + const change = ctx.on('change') |
| 57 | + const setActive = (next: string) => { |
| 58 | + if (controlled) |
| 59 | + setBoundValue(next) |
| 60 | + else uncontrolledValue.value = next |
| 61 | + change.emit() |
| 62 | + } |
| 63 | + |
| 64 | + /** Roving tabindex per WAI-ARIA — arrow keys move focus and selection together. */ |
| 65 | + const move = (fromIndex: number, delta: number, container: HTMLElement) => { |
| 66 | + if (tabs.length === 0) |
| 67 | + return |
| 68 | + const nextIndex = (fromIndex + delta + tabs.length) % tabs.length |
| 69 | + const nextTab = tabs[nextIndex]! |
| 70 | + setActive(nextTab.value) |
| 71 | + requestAnimationFrame(() => { |
| 72 | + (container.querySelectorAll('[role="tab"]')[nextIndex] as HTMLElement | undefined)?.focus() |
| 73 | + }) |
| 74 | + } |
| 75 | + |
| 76 | + const tabButtons = tabs.map((tab, index) => { |
| 77 | + const active = tab.value === activeValue |
| 78 | + return h('button', { |
| 79 | + 'type': 'button', |
| 80 | + 'role': 'tab', |
| 81 | + 'aria-selected': active ? 'true' : 'false', |
| 82 | + 'tabindex': active ? '0' : '-1', |
| 83 | + 'class': 'jr-tab', |
| 84 | + 'style': { |
| 85 | + display: 'inline-flex', |
| 86 | + alignItems: 'center', |
| 87 | + gap: '6px', |
| 88 | + padding: '6px 10px', |
| 89 | + fontSize: '12px', |
| 90 | + fontWeight: active ? '600' : '400', |
| 91 | + color: active ? primary : 'inherit', |
| 92 | + background: 'none', |
| 93 | + border: 'none', |
| 94 | + borderBottom: !isVertical ? `2px solid ${active ? primary : 'transparent'}` : undefined, |
| 95 | + borderLeft: isVertical ? `2px solid ${active ? primary : 'transparent'}` : undefined, |
| 96 | + // Rounds the corners away from the active-tab indicator: top for the horizontal underline, right for the vertical rail. |
| 97 | + borderRadius: isVertical ? '0 4px 4px 0' : '4px 4px 0 0', |
| 98 | + cursor: 'pointer', |
| 99 | + whiteSpace: 'nowrap', |
| 100 | + transition: 'background-color 0.15s ease', |
| 101 | + }, |
| 102 | + 'onClick': () => setActive(tab.value), |
| 103 | + 'onMouseenter': (e: MouseEvent) => { |
| 104 | + if (!active) |
| 105 | + (e.currentTarget as HTMLElement).style.backgroundColor = surfaceSubtle |
| 106 | + }, |
| 107 | + 'onMouseleave': (e: MouseEvent) => { (e.currentTarget as HTMLElement).style.backgroundColor = '' }, |
| 108 | + 'onKeydown': (e: KeyboardEvent) => { |
| 109 | + const container = (e.currentTarget as HTMLElement).parentElement |
| 110 | + if (!container) |
| 111 | + return |
| 112 | + const forward = isVertical ? 'ArrowDown' : 'ArrowRight' |
| 113 | + const backward = isVertical ? 'ArrowUp' : 'ArrowLeft' |
| 114 | + if (e.key === forward) { |
| 115 | + e.preventDefault() |
| 116 | + move(index, 1, container) |
| 117 | + } |
| 118 | + else if (e.key === backward) { |
| 119 | + e.preventDefault() |
| 120 | + move(index, -1, container) |
| 121 | + } |
| 122 | + else if (e.key === 'Home') { |
| 123 | + e.preventDefault() |
| 124 | + move(index, -index, container) |
| 125 | + } |
| 126 | + else if (e.key === 'End') { |
| 127 | + e.preventDefault() |
| 128 | + move(index, tabs.length - 1 - index, container) |
| 129 | + } |
| 130 | + }, |
| 131 | + }, [ |
| 132 | + tab.icon && h('span', { |
| 133 | + style: { display: 'inline-flex', width: '14px', height: '14px', lineHeight: '1' }, |
| 134 | + innerHTML: iconSvgs.value[tab.icon] || '', |
| 135 | + }), |
| 136 | + h('span', tab.label), |
| 137 | + tab.badge && h('span', { |
| 138 | + class: `jr-badge jr-badge-${tab.badgeVariant ?? 'default'}`, |
| 139 | + style: { |
| 140 | + display: 'inline-block', |
| 141 | + padding: '1px 6px', |
| 142 | + borderRadius: '9px', |
| 143 | + fontSize: '10px', |
| 144 | + fontWeight: '500', |
| 145 | + backgroundColor: (colors[tab.badgeVariant ?? 'default'] ?? colors.default).bg, |
| 146 | + color: (colors[tab.badgeVariant ?? 'default'] ?? colors.default).fg, |
| 147 | + }, |
| 148 | + }, tab.badge), |
| 149 | + ]) |
| 150 | + }) |
| 151 | + |
| 152 | + const panels = slots.default?.() ?? [] |
| 153 | + const activeIndex = tabs.findIndex(tab => tab.value === activeValue) |
| 154 | + const activePanel = activeIndex >= 0 ? panels[activeIndex] : undefined |
| 155 | + |
| 156 | + return h('div', { |
| 157 | + style: { display: 'flex', flexDirection: isVertical ? 'row' : 'column', gap: '8px' }, |
| 158 | + }, [ |
| 159 | + h('div', { |
| 160 | + 'role': 'tablist', |
| 161 | + 'aria-orientation': orientation, |
| 162 | + 'style': { |
| 163 | + display: 'flex', |
| 164 | + flexDirection: isVertical ? 'column' : 'row', |
| 165 | + gap: '2px', |
| 166 | + borderBottom: !isVertical ? '1px solid var(--jr-border, rgba(128,128,128,0.2))' : undefined, |
| 167 | + borderRight: isVertical ? '1px solid var(--jr-border, rgba(128,128,128,0.2))' : undefined, |
| 168 | + flexShrink: '0', |
| 169 | + }, |
| 170 | + }, tabButtons), |
| 171 | + h('div', { role: 'tabpanel', style: { flex: '1', minWidth: '0' } }, activePanel ? [activePanel] : []), |
| 172 | + ]) |
| 173 | + } |
| 174 | + }, |
| 175 | +}) |
0 commit comments