vael-ui v0.5.7

Used with a component · Tabs

useTabs

The keyboard engine behind <Tabs>: selection, roving focus and arrow key navigation from the ARIA Authoring Practices Guide (APG) tabs pattern. It renders nothing. You render the role="tab" elements and bind onKeydown on the list. <Tabs> adds the markup, ids, panel wiring and the sliding indicators on top. Use the composable when your tabs need markup the #item slot can't give you.

Install

import { useTabs } from 'vael-ui'

Examples

<script setup lang="ts">
import { shallowRef, useTemplateRef } from 'vue'
import { useTabs } from 'vael-ui'

const tabs = ['general', 'billing', 'team'] as const
const active = shallowRef<(typeof tabs)[number]>('general')
const listEl = useTemplateRef<HTMLElement>('list')
const { select, onKeydown } = useTabs(active, { items: tabs, listEl })
</script>

<template>
  <div ref="list" role="tablist" @keydown="onKeydown">
    <button
      v-for="tab in tabs"
      :key="tab"
      role="tab"
      :data-tab-value="tab"
      :aria-selected="active === tab"
      :tabindex="active === tab ? 0 : -1"
      @click="select(tab)"
    >
      {{ tab }}
    </button>
  </div>
</template>

Parameters

NameTypeDescription
activeRef<T>The selected item (first argument). select() and the arrow keys write to it.
itemsMaybeRefOrGetter<readonly T[]>Every tab, in order. Use strings or numbers: a tab element is matched by data-tab-value="String(item)", then by its position among the role="tab" elements.
listElRef<HTMLElement | null>The tab list. Its role="tab" descendants are the tabs. It skips a tab with disabled or aria-disabled="true".
orientationMaybeRefOrGetter<'horizontal' | 'vertical'>The arrow keys it uses: Left and Right for horizontal tabs, Up and Down for vertical ones. Default horizontal.
activationMaybeRefOrGetter<'automatic' | 'manual'>When a tab gets selected. Default automatic: the arrow keys select as they move focus. With manual, the arrow keys only move focus, and Enter or Space selects the focused tab. Use manual when switching tabs is expensive, such as a tab that loads data.
onChange(item: T) => voidFires when the selection changes, by click or keyboard.

Returns

NameTypeDescription
select(item: T) => voidSelects item and calls onChange. Does nothing when item is already selected. Bind it to each tab's click.
onKeydown(event: KeyboardEvent) => voidBind it on the list. The arrow keys move to the next enabled tab and wrap around the ends. Home and End jump to the first and last enabled tab. Focus follows after the next render.
focusedRef<T>The tab that holds keyboard focus. It differs from active only in manual mode, where you give it tabindex="0" instead of the selected tab.