|
1 | 1 | <script setup lang="ts"> |
2 | 2 | const selectedPM = useSelectedPackageManager() |
| 3 | +
|
| 4 | +const tablistNavigationKeys = new Set(['ArrowRight', 'ArrowLeft', 'Home', 'End']) |
| 5 | +
|
| 6 | +function onTabListKeydown(event: KeyboardEvent) { |
| 7 | + if (!tablistNavigationKeys.has(event.key)) return |
| 8 | + const tablist = event.currentTarget as HTMLElement | null |
| 9 | + if (!tablist) return |
| 10 | +
|
| 11 | + const tabs = Array.from(tablist.querySelectorAll<HTMLElement>('[role="tab"]')) |
| 12 | + const count = Math.min(tabs.length, packageManagers.length) |
| 13 | + if (!count) return |
| 14 | +
|
| 15 | + event.preventDefault() |
| 16 | +
|
| 17 | + let activeIndex = packageManagers.findIndex(pm => pm.id === selectedPM.value) |
| 18 | + if (activeIndex < 0) activeIndex = 0 |
| 19 | +
|
| 20 | + let nextIndex = activeIndex |
| 21 | + if (event.key === 'ArrowRight') nextIndex = (activeIndex + 1) % count |
| 22 | + if (event.key === 'ArrowLeft') nextIndex = (activeIndex - 1 + count) % count |
| 23 | + if (event.key === 'Home') nextIndex = 0 |
| 24 | + if (event.key === 'End') nextIndex = count - 1 |
| 25 | +
|
| 26 | + const nextTab = tabs[nextIndex] |
| 27 | + const nextId = packageManagers[nextIndex]?.id |
| 28 | + if (nextId && nextId !== selectedPM.value) { |
| 29 | + selectedPM.value = nextId |
| 30 | + } |
| 31 | +
|
| 32 | + nextTick(() => nextTab?.focus()) |
| 33 | +} |
3 | 34 | </script> |
4 | 35 |
|
5 | 36 | <template> |
6 | 37 | <div |
7 | 38 | class="flex items-center gap-1 p-0.5 bg-bg-subtle border border-border-subtle rounded-md overflow-x-auto" |
8 | 39 | role="tablist" |
9 | 40 | :aria-label="$t('package.get_started.pm_label')" |
| 41 | + @keydown="onTabListKeydown" |
10 | 42 | > |
11 | 43 | <button |
12 | 44 | v-for="pm in packageManagers" |
13 | 45 | :key="pm.id" |
| 46 | + :id="`pm-tab-${pm.id}`" |
14 | 47 | role="tab" |
15 | 48 | :data-pm-tab="pm.id" |
16 | 49 | :aria-selected="selectedPM === pm.id" |
| 50 | + :aria-controls="`pm-panel-${pm.id}`" |
| 51 | + :tabindex="selectedPM === pm.id ? 0 : -1" |
| 52 | + type="button" |
17 | 53 | class="pm-tab px-2 py-1.5 font-mono text-xs rounded transition-colors duration-150 border border-solid focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fg/50 inline-flex items-center gap-1.5 hover:text-fg" |
18 | 54 | @click="selectedPM = pm.id" |
19 | 55 | > |
|
0 commit comments