2025-12-04 10:04:21 +08:00

241 lines
5.3 KiB
Vue

<template>
<div class="u-tabs" role="tablist" :aria-label="ariaLabel">
<!-- tabs -->
<button
v-for="(t, i) in tabsArr"
:key="(t.value ?? t.label ?? i) as any"
class="u-tab"
:class="{ active: i === currentIndex }"
role="tab"
:aria-selected="i === currentIndex"
:tabindex="i === currentIndex ? 0 : -1"
@click="setActive(i)"
@keydown.left.prevent="focusPrev"
@keydown.right.prevent="focusNext"
:ref="(el) => assignTabRef(el, i)"
>
{{ t.label }}
</button>
<!-- 活动下划线 -->
<span
class="u-underline"
:style="{ width: underline.w + 'px', transform: `translateX(${underline.x}px)` }"
aria-hidden="true"
/>
</div>
<!-- 面板(可选) -->
<div v-if="usePanels" class="u-panels">
<div
v-for="(t, i) in tabsArr"
:key="(t.value ?? t.label ?? i) as any"
v-show="i === currentIndex"
role="tabpanel"
class="u-panel"
>
<slot :name="t.slot || 'panel'" :tab="t" :index="i">
{{ t.content ?? '' }}
</slot>
</div>
</div>
</template>
<script setup lang="ts">
import {
ref, watch, onMounted, nextTick, computed, onBeforeUnmount,
type ComponentPublicInstance
} from 'vue'
type TabItem = Readonly<{
label: string
value?: string | number
slot?: string
content?: string
}>
interface Props {
tabs: ReadonlyArray<TabItem>
modelValue?: string | number | null
defaultIndex?: number
usePanels?: boolean
ariaLabel?: string
}
const props = withDefaults(defineProps<Props>(), {
modelValue: null,
defaultIndex: 0,
usePanels: false,
ariaLabel: ''
})
const emit = defineEmits<{
(e: 'update:modelValue', v: string | number): void
(e: 'change', payload: { index: number; tab: TabItem }): void
}>()
const tabsArr = computed(() => props.tabs ?? [])
/** 用数组存每个 tab 的原生按钮元素 */
const tabRefs = ref<Array<HTMLButtonElement | null>>([])
/** 兼容 Vue 的 VNodeRef 签名 */
function assignTabRef(
el: Element | ComponentPublicInstance | null,
i: number
) {
// 这里只会接收到原生 <button>,安全断言为 HTMLButtonElement
tabRefs.value[i] = (el as HTMLButtonElement) ?? null
}
const currentIndex = ref(0)
function valueToIndex(v: string | number | null) {
return tabsArr.value.findIndex(t => (t?.value ?? t?.label) === v)
}
watch(
() => props.modelValue,
(v) => {
if (v == null) return
const idx = valueToIndex(v)
if (idx >= 0) {
currentIndex.value = idx
nextTick(updateUnderline)
}
},
{ immediate: true }
)
onMounted(() => {
if (props.modelValue == null) {
currentIndex.value = Math.min(
props.defaultIndex ?? 0,
Math.max(tabsArr.value.length - 1, 0)
)
}
nextTick(() => {
updateUnderline()
window.addEventListener('resize', handleResize)
})
})
onBeforeUnmount(() => {
window.removeEventListener('resize', handleResize)
})
/** 下划线位置与宽度 */
const underline = ref({ x: 0, w: 0 })
function updateUnderline() {
const el = tabRefs.value[currentIndex.value]
if (!el) return
const parent = el.parentElement
if (!parent) return
const parentRect = parent.getBoundingClientRect()
const rect = el.getBoundingClientRect()
underline.value = { x: rect.left - parentRect.left, w: rect.width }
}
function handleResize() {
updateUnderline()
}
function setActive(i: number) {
if (i < 0 || i >= tabsArr.value.length) return
const tab = tabsArr.value[i]!
currentIndex.value = i
updateUnderline()
const v = (tab.value ?? tab.label) as string | number
emit('update:modelValue', v)
emit('change', { index: i, tab })
}
function focusPrev() {
if (!tabsArr.value.length) return
const i = (currentIndex.value - 1 + tabsArr.value.length) % tabsArr.value.length
setActive(i)
tabRefs.value[i]?.focus?.()
}
function focusNext() {
if (!tabsArr.value.length) return
const i = (currentIndex.value + 1) % tabsArr.value.length
setActive(i)
tabRefs.value[i]?.focus?.()
}
watch(currentIndex, () => nextTick(updateUnderline))
</script>
<style scoped>
.u-tabs {
--tabs-gap: 48px;
--tabs-color: #4b5563; /* 灰700 */
--tabs-active: #0f172a; /* 石板900 */
--tabs-underline: #0f172a;
--tabs-font: 16px;
position: relative;
display: flex;
align-items: center;
gap: var(--tabs-gap);
padding-inline: 8px;
}
.u-tab {
position: relative;
appearance: none;
border: 0;
background: transparent;
padding: 8px 0;
font-size: var(--tabs-font);
color: var(--tabs-color);
cursor: pointer;
transition: color 0.18s ease, transform 0.18s ease;
outline: none;
}
.u-tab:hover {
color: var(--tabs-active);
}
.u-tab.active {
color: var(--tabs-active);
font-weight: 700;
}
.u-tab:focus-visible {
outline: 2px solid rgba(99, 102, 241, 0.35);
outline-offset: 2px;
border-radius: 6px;
}
/* 下划线 */
.u-underline {
position: absolute;
bottom: 0;
height: 3px;
left: 0;
background: var(--tabs-underline);
border-radius: 3px;
transition:
transform 0.22s cubic-bezier(0.22, 1, 0.36, 1),
width 0.22s cubic-bezier(0.22, 1, 0.36, 1);
pointer-events: none;
}
/* 面板(可选) */
.u-panels {
margin-top: 12px;
}
.u-panel {
padding: 8px 0;
}
/* 小屏收紧间距 */
@media (max-width: 640px) {
.u-tabs {
--tabs-gap: 28px;
--tabs-font: 15px;
}
.u-underline {
height: 2px;
}
}
</style>