docs/.vitepress/theme/components/tabs/PluginTabs.vue

107 lines · 2979 bytes

  1<script setup lang="ts">
  2import { computed, ref, toRef, useId, useSlots } from 'vue'
  3import { useIsPrint, useStabilizeScrollPosition } from './composables'
  4import { provideTabsSingleState, useTabsSelectedState } from './state'
  5
  6defineOptions({ name: 'PluginTabs' })
  7
  8const props = defineProps<{ sharedStateKey?: string }>()
  9
 10interface TabItem {
 11  anchor: string
 12  label: string
 13}
 14
 15const isPrint = useIsPrint()
 16const slots = useSlots()
 17const tabItems = computed<TabItem[]>(() => {
 18  const defaultSlot = slots.default?.()
 19  if (!defaultSlot) return []
 20
 21  return defaultSlot
 22    .filter((vnode) => {
 23      if (!vnode.props) return false
 24      if (typeof vnode.type !== 'object') return false
 25
 26      const type = vnode.type as { __name?: string; name?: string }
 27      return type.__name === 'PluginTabsTab' || type.name === 'PluginTabsTab'
 28    })
 29    .map((vnode) => ({
 30      anchor: String(vnode.props?.anchor ?? ''),
 31      label: String(vnode.props?.label ?? '')
 32    }))
 33    .filter((item) => item.anchor && item.label)
 34})
 35const tabLabels = computed(() => tabItems.value.map((item) => item.label))
 36const { selected, select } = useTabsSelectedState(
 37  tabLabels,
 38  toRef(props, 'sharedStateKey')
 39)
 40const tablist = ref<HTMLElement>()
 41const { stabilizeScrollPosition } = useStabilizeScrollPosition(tablist)
 42const selectStable = stabilizeScrollPosition(select)
 43const buttonRefs = ref<HTMLButtonElement[]>([])
 44const uid = useId()
 45
 46provideTabsSingleState({ uid, selected })
 47
 48function getTabButtonId(item: TabItem) {
 49  return `tab-${item.anchor}-${uid}`
 50}
 51
 52function getTabPanelId(item: TabItem) {
 53  return `panel-${item.anchor}-${uid}`
 54}
 55
 56async function onKeydown(e: KeyboardEvent) {
 57  const currentIndex = tabItems.value.findIndex(
 58    (item) => item.label === selected.value
 59  )
 60  let selectIndex: number | undefined
 61
 62  if (e.key === 'ArrowLeft') {
 63    selectIndex = currentIndex >= 1
 64      ? currentIndex - 1
 65      : tabItems.value.length - 1
 66  } else if (e.key === 'ArrowRight') {
 67    selectIndex = currentIndex < tabItems.value.length - 1
 68      ? currentIndex + 1
 69      : 0
 70  }
 71
 72  if (selectIndex === undefined || !tabItems.value[selectIndex]) return
 73
 74  e.preventDefault()
 75  await selectStable(tabItems.value[selectIndex].label)
 76  buttonRefs.value[selectIndex]?.focus()
 77}
 78</script>
 79
 80<template>
 81  <div class="plugin-tabs">
 82    <div
 83      ref="tablist"
 84      class="plugin-tabs--tab-list"
 85      role="tablist"
 86      @keydown="onKeydown"
 87    >
 88      <button
 89        v-for="item in tabItems"
 90        :id="getTabButtonId(item)"
 91        ref="buttonRefs"
 92        :key="item.anchor"
 93        type="button"
 94        role="tab"
 95        class="plugin-tabs--tab"
 96        :data-tab-anchor="item.anchor"
 97        :aria-selected="item.label === selected && !isPrint"
 98        :aria-controls="getTabPanelId(item)"
 99        :tabindex="item.label === selected ? 0 : -1"
100        @click="selectStable(item.label)"
101      >
102        {{ item.label }}
103      </button>
104    </div>
105    <slot />
106  </div>
107</template>