|
| 1 | +<script setup lang="ts" generic="T extends AccordionType"> |
| 2 | +import { computed, shallowRef, toRef, useAttrs } from 'vue' |
| 3 | +import { ACCORDION_KEYS, type AccordionEmits, type AccordionProps, type AccordionType, Collection, provideAccordionContext, useCollection } from './Accordion.ts' |
| 4 | +import { useDirection } from '~/direction/Direction.ts' |
| 5 | +import { useControllableState } from '~/hooks/useControllableState.ts' |
| 6 | +import { Primitive } from '~/primitive/index.ts' |
| 7 | +import { composeEventHandlers } from '~/utils/composeEventHandlers.ts' |
| 8 | +import { arrayify } from '~/utils/array.ts' |
| 9 | +
|
| 10 | +defineOptions({ |
| 11 | + name: 'Accordion', |
| 12 | + inheritAttrs: false, |
| 13 | +}) |
| 14 | +
|
| 15 | +const props = withDefaults(defineProps<AccordionProps<T>>(), { |
| 16 | + disabled: false, |
| 17 | + orientation: 'vertical', |
| 18 | + collapsible: false, |
| 19 | +}) |
| 20 | +const emit = defineEmits<AccordionEmits<T>>() |
| 21 | +const attrs = useAttrs() |
| 22 | +
|
| 23 | +const elRef = shallowRef<HTMLElement>() |
| 24 | +
|
| 25 | +const direction = useDirection(() => props.dir) |
| 26 | +
|
| 27 | +const value = useControllableState(props, emit, 'value', props.defaultValue) |
| 28 | +const TYPE_SINGLE = 'single' as const satisfies AccordionType |
| 29 | +type SingleValue = NonNullable<AccordionProps<'single'>['value']> |
| 30 | +type MultipleValue = NonNullable<AccordionProps<'multiple'>['value']> |
| 31 | +type Value = T extends 'single' ? SingleValue : MultipleValue |
| 32 | +
|
| 33 | +const collectionContext = Collection.provideCollectionContext(elRef) |
| 34 | +const getItems = useCollection(collectionContext) |
| 35 | +const handleKeydown = composeEventHandlers<KeyboardEvent>(() => { |
| 36 | + ;(attrs.onKeydown as Function | undefined)?.() |
| 37 | +}, (event) => { |
| 38 | + if (!ACCORDION_KEYS.includes(event.key)) |
| 39 | + return |
| 40 | + const target = event.target as HTMLElement |
| 41 | + const triggerCollection = getItems().filter(item => !item.ref?.disabled) |
| 42 | + const triggerIndex = triggerCollection.findIndex(item => item.ref === target) |
| 43 | + const triggerCount = triggerCollection.length |
| 44 | +
|
| 45 | + if (triggerIndex === -1) |
| 46 | + return |
| 47 | +
|
| 48 | + // Prevents page scroll while user is navigating |
| 49 | + event.preventDefault() |
| 50 | +
|
| 51 | + let nextIndex = triggerIndex |
| 52 | + const homeIndex = 0 |
| 53 | + const endIndex = triggerCount - 1 |
| 54 | +
|
| 55 | + const moveNext = () => { |
| 56 | + nextIndex = triggerIndex + 1 |
| 57 | + if (nextIndex > endIndex) { |
| 58 | + nextIndex = homeIndex |
| 59 | + } |
| 60 | + } |
| 61 | +
|
| 62 | + const movePrev = () => { |
| 63 | + nextIndex = triggerIndex - 1 |
| 64 | + if (nextIndex < homeIndex) { |
| 65 | + nextIndex = endIndex |
| 66 | + } |
| 67 | + } |
| 68 | +
|
| 69 | + switch (event.key) { |
| 70 | + case 'Home': |
| 71 | + nextIndex = homeIndex |
| 72 | + break |
| 73 | + case 'End': |
| 74 | + nextIndex = endIndex |
| 75 | + break |
| 76 | + case 'ArrowRight': |
| 77 | + if (props.orientation === 'horizontal') { |
| 78 | + if (direction.value === 'ltr') { |
| 79 | + moveNext() |
| 80 | + } |
| 81 | + else { |
| 82 | + movePrev() |
| 83 | + } |
| 84 | + } |
| 85 | + break |
| 86 | + case 'ArrowDown': |
| 87 | + if (props.orientation === 'vertical') { |
| 88 | + moveNext() |
| 89 | + } |
| 90 | + break |
| 91 | + case 'ArrowLeft': |
| 92 | + if (props.orientation === 'horizontal') { |
| 93 | + if (direction.value === 'ltr') { |
| 94 | + movePrev() |
| 95 | + } |
| 96 | + else { |
| 97 | + moveNext() |
| 98 | + } |
| 99 | + } |
| 100 | + break |
| 101 | + case 'ArrowUp': |
| 102 | + if (props.orientation === 'vertical') { |
| 103 | + movePrev() |
| 104 | + } |
| 105 | + break |
| 106 | + } |
| 107 | +
|
| 108 | + const clampedIndex = nextIndex % triggerCount |
| 109 | + triggerCollection[clampedIndex]?.ref?.focus() |
| 110 | +}) |
| 111 | +
|
| 112 | +provideAccordionContext({ |
| 113 | + collapsible: props.collapsible, |
| 114 | +
|
| 115 | + disabled: toRef(props, 'disabled'), |
| 116 | + direction, |
| 117 | + orientation: props.orientation, |
| 118 | + value: computed(() => { |
| 119 | + if (props.type === TYPE_SINGLE) |
| 120 | + return typeof value.value === 'string' ? [value.value] : [] |
| 121 | + return Array.isArray(value.value) ? value.value : [] |
| 122 | + }), |
| 123 | + onItemOpen(itemValue) { |
| 124 | + if (props.type === TYPE_SINGLE) { |
| 125 | + value.value = itemValue as Value |
| 126 | + } |
| 127 | + else { |
| 128 | + value.value = [...arrayify<SingleValue>(value.value || []), itemValue] as Value |
| 129 | + } |
| 130 | + }, |
| 131 | + onItemClose(itemValue) { |
| 132 | + if (props.type === TYPE_SINGLE) { |
| 133 | + if (props.collapsible) { |
| 134 | + value.value = '' as Value |
| 135 | + } |
| 136 | + } |
| 137 | + else { |
| 138 | + value.value = arrayify<SingleValue>(value.value || []).filter(value => value !== itemValue) as Value |
| 139 | + } |
| 140 | + }, |
| 141 | +}) |
| 142 | +</script> |
| 143 | + |
| 144 | +<template> |
| 145 | + <Primitive |
| 146 | + :ref="(el: any) => { |
| 147 | + const node = (el?.$el ?? el) |
| 148 | + elRef = node?.hasAttribute ? node : undefined |
| 149 | + }" |
| 150 | + :as="as" |
| 151 | + :as-child="asChild" |
| 152 | + v-bind="{ |
| 153 | + ...attrs, |
| 154 | + onKeydown(e: KeyboardEvent) { |
| 155 | + if (!props.disabled) |
| 156 | + handleKeydown(e) |
| 157 | + }, |
| 158 | + }" |
| 159 | + :data-orientation="props.orientation" |
| 160 | + > |
| 161 | + <slot /> |
| 162 | + </Primitive> |
| 163 | +</template> |
0 commit comments