|
| 1 | +<script setup lang="ts"> |
| 2 | +import { computed, shallowRef, useAttrs } from 'vue' |
| 3 | +import { useTabsContext } from './Tabs.ts' |
| 4 | +import { makeContentId, makeTriggerId } from './utils.ts' |
| 5 | +import type { TabsTriggerProps } from './TabsTrigger.ts' |
| 6 | +import { Primitive } from '~/primitive/index.ts' |
| 7 | +import { RovingFocusItem } from '~/roving-focus/index.ts' |
| 8 | +import { composeEventHandlers } from '~/utils/composeEventHandlers.ts' |
| 9 | +
|
| 10 | +defineOptions({ |
| 11 | + name: 'TabsTrigger', |
| 12 | + inheritAttrs: false, |
| 13 | +}) |
| 14 | +
|
| 15 | +const props = withDefaults(defineProps<TabsTriggerProps>(), { |
| 16 | + as: 'button', |
| 17 | +}) |
| 18 | +const attrs = useAttrs() |
| 19 | +
|
| 20 | +const elRef = shallowRef<HTMLElement>() |
| 21 | +
|
| 22 | +const context = useTabsContext() |
| 23 | +const triggerId = computed(() => makeTriggerId(context.baseId, props.value)) |
| 24 | +const contentId = computed(() => makeContentId(context.baseId, props.value)) |
| 25 | +const isSelected = computed(() => context.value.value === props.value) |
| 26 | +
|
| 27 | +const onMousedown = composeEventHandlers<MouseEvent>((event) => { |
| 28 | + (attrs.onMousedown as Function | undefined)?.(event) |
| 29 | +}, (event) => { |
| 30 | + // only call handler if it's the left button (mousedown gets triggered by all mouse buttons) |
| 31 | + // but not when the control key is pressed (avoiding MacOS right click) |
| 32 | + if (!attrs.disabled && event.button === 0 && event.ctrlKey === false) { |
| 33 | + context.onValueChange(props.value) |
| 34 | + } |
| 35 | + else { |
| 36 | + // prevent focus to avoid accidental activation |
| 37 | + event.preventDefault() |
| 38 | + } |
| 39 | +}) |
| 40 | +
|
| 41 | +const onKeydown = composeEventHandlers<KeyboardEvent>((event) => { |
| 42 | + (attrs.onKeydown as Function | undefined)?.(event) |
| 43 | +}, (event) => { |
| 44 | + if ([' ', 'Enter'].includes(event.key)) |
| 45 | + context.onValueChange(props.value) |
| 46 | +}) |
| 47 | +
|
| 48 | +const onFocus = composeEventHandlers<FocusEvent>((event) => { |
| 49 | + (attrs.onFocus as Function | undefined)?.(event) |
| 50 | +}, () => { |
| 51 | + // handle "automatic" activation if necessary |
| 52 | + // ie. activate tab following focus |
| 53 | + const isAutomaticActivation = context.activationMode !== 'manual' |
| 54 | + if (!isSelected.value && !attrs.disabled && isAutomaticActivation) { |
| 55 | + context.onValueChange(props.value) |
| 56 | + } |
| 57 | +}) |
| 58 | +
|
| 59 | +defineExpose({ |
| 60 | + $el: elRef, |
| 61 | +}) |
| 62 | +</script> |
| 63 | + |
| 64 | +<template> |
| 65 | + <RovingFocusItem as-child :focusable="!attrs.disabled" :active="isSelected"> |
| 66 | + <Primitive |
| 67 | + :id="triggerId" |
| 68 | + :ref="(el: any) => { |
| 69 | + const node = (el?.$el ?? el) |
| 70 | + elRef = node?.hasAttribute ? node : undefined |
| 71 | + }" |
| 72 | + :as="as" |
| 73 | + :as-child="asChild" |
| 74 | + v-bind="{ |
| 75 | + ...attrs, |
| 76 | + onMousedown, |
| 77 | + onKeydown, |
| 78 | + onFocus, |
| 79 | + }" |
| 80 | + type="button" |
| 81 | + role="tab" |
| 82 | + :aria-selected="isSelected" |
| 83 | + :aria-controls="contentId" |
| 84 | + :data-state="isSelected ? 'active' : 'inactive'" |
| 85 | + :data-disabled="attrs.disabled ? '' : undefined" |
| 86 | + :disabled="attrs.disabled" |
| 87 | + > |
| 88 | + <slot /> |
| 89 | + </Primitive> |
| 90 | + </RovingFocusItem> |
| 91 | +</template> |
0 commit comments