|
| 1 | +<script setup lang="ts" generic="T"> |
| 2 | +import { computed, nextTick, ref } from 'vue' |
| 3 | +import { navEnabled, useRemoteNav } from '../composables/useRemoteNav' |
| 4 | +
|
| 5 | +// 遥控/手柄友好的下拉框: |
| 6 | +// - 鼠标模式(navEnabled 为假):直接渲染原生 <select>,行为与体验完全不变。 |
| 7 | +// - 遥控模式:渲染触发按钮 —— 选中后按「确定」弹出选项列表,上下键在列表内移动, |
| 8 | +// 再按「确定」才切换选项(而非原生 select 那样左右键即时改值,不符合遥控操作习惯)。 |
| 9 | +// 弹层挂 priority=20 的 useRemoteNav(高于模态的 10),打开时独占按键;BACK/确定后回落。 |
| 10 | +
|
| 11 | +interface Option { |
| 12 | + value: T |
| 13 | + label: string |
| 14 | +} |
| 15 | +
|
| 16 | +const props = defineProps<{ options: Option[] }>() |
| 17 | +const model = defineModel<T>() |
| 18 | +
|
| 19 | +const open = ref(false) |
| 20 | +const triggerRef = ref<HTMLElement | null>(null) |
| 21 | +const popupRef = ref<HTMLElement | null>(null) |
| 22 | +// 弹层固定定位坐标(打开时按触发按钮的矩形计算,避免被面板 overflow 裁剪)。 |
| 23 | +const popupStyle = ref<Record<string, string>>({}) |
| 24 | +
|
| 25 | +const currentLabel = computed(() => { |
| 26 | + const hit = props.options.find((o) => o.value === model.value) |
| 27 | + return hit ? hit.label : (props.options[0]?.label ?? '') |
| 28 | +}) |
| 29 | +
|
| 30 | +function openPopup(): void { |
| 31 | + const trigger = triggerRef.value |
| 32 | + if (!trigger) return |
| 33 | + const r = trigger.getBoundingClientRect() |
| 34 | + // 优先向下展开;下方空间不足则向上。宽度不小于触发按钮。 |
| 35 | + const belowSpace = window.innerHeight - r.bottom |
| 36 | + const style: Record<string, string> = { |
| 37 | + position: 'fixed', |
| 38 | + left: `${r.left}px`, |
| 39 | + minWidth: `${r.width}px`, |
| 40 | + maxHeight: `${Math.max(belowSpace, r.top) - 16}px`, |
| 41 | + } |
| 42 | + if (belowSpace >= 220 || belowSpace >= r.top) { |
| 43 | + style.top = `${r.bottom + 4}px` |
| 44 | + } else { |
| 45 | + style.bottom = `${window.innerHeight - r.top + 4}px` |
| 46 | + } |
| 47 | + popupStyle.value = style |
| 48 | + open.value = true |
| 49 | + // 弹层渲染后把焦点落到当前选中项(便于上下微调),供遥控/键盘导航起点。 |
| 50 | + nextTick(() => { |
| 51 | + const el = |
| 52 | + popupRef.value?.querySelector<HTMLElement>('.rs-option[data-selected="true"]') ?? |
| 53 | + popupRef.value?.querySelector<HTMLElement>('.rs-option') |
| 54 | + el?.focus() |
| 55 | + }) |
| 56 | +} |
| 57 | +
|
| 58 | +function closePopup(): void { |
| 59 | + if (!open.value) return |
| 60 | + open.value = false |
| 61 | + // 关闭后焦点回到触发按钮,导航链路不断。 |
| 62 | + nextTick(() => triggerRef.value?.focus()) |
| 63 | +} |
| 64 | +
|
| 65 | +function toggle(): void { |
| 66 | + if (open.value) closePopup() |
| 67 | + else openPopup() |
| 68 | +} |
| 69 | +
|
| 70 | +function pick(opt: Option): void { |
| 71 | + model.value = opt.value |
| 72 | + closePopup() |
| 73 | +} |
| 74 | +
|
| 75 | +// 弹层内的焦点导航:上下选、确定切换(见各选项 @click)、BACK 关闭。 |
| 76 | +useRemoteNav({ |
| 77 | + container: popupRef, |
| 78 | + active: computed(() => navEnabled.value && open.value), |
| 79 | + onBack: closePopup, |
| 80 | + priority: 20, |
| 81 | +}) |
| 82 | +</script> |
| 83 | + |
| 84 | +<template> |
| 85 | + <!-- 鼠标模式:原生下拉,体验不变 --> |
| 86 | + <select v-if="!navEnabled" v-model="model" class="remote-select"> |
| 87 | + <option v-for="opt in options" :key="String(opt.value)" :value="opt.value"> |
| 88 | + {{ opt.label }} |
| 89 | + </option> |
| 90 | + </select> |
| 91 | + |
| 92 | + <!-- 遥控模式:触发按钮 + 弹出列表 --> |
| 93 | + <button |
| 94 | + v-else |
| 95 | + ref="triggerRef" |
| 96 | + type="button" |
| 97 | + class="remote-select rs-trigger" |
| 98 | + :aria-expanded="open" |
| 99 | + @click="toggle" |
| 100 | + > |
| 101 | + <span class="rs-value">{{ currentLabel }}</span> |
| 102 | + <span class="rs-arrow" aria-hidden="true">▾</span> |
| 103 | + </button> |
| 104 | + |
| 105 | + <Teleport v-if="open" to="body"> |
| 106 | + <div class="rs-backdrop" @click="closePopup" /> |
| 107 | + <div ref="popupRef" class="rs-popup" role="listbox" :style="popupStyle"> |
| 108 | + <button |
| 109 | + v-for="opt in options" |
| 110 | + :key="String(opt.value)" |
| 111 | + type="button" |
| 112 | + class="rs-option" |
| 113 | + role="option" |
| 114 | + :data-selected="opt.value === model" |
| 115 | + :aria-selected="opt.value === model" |
| 116 | + @click="pick(opt)" |
| 117 | + > |
| 118 | + {{ opt.label }} |
| 119 | + </button> |
| 120 | + </div> |
| 121 | + </Teleport> |
| 122 | +</template> |
| 123 | + |
| 124 | +<style scoped> |
| 125 | +.remote-select { |
| 126 | + height: 34px; |
| 127 | + border: 1px solid #434a54; |
| 128 | + border-radius: 6px; |
| 129 | + background: #171a1f; |
| 130 | + color: #eee; |
| 131 | + padding: 0 10px; |
| 132 | + font-size: 14px; |
| 133 | +} |
| 134 | +.rs-trigger { |
| 135 | + display: inline-flex; |
| 136 | + align-items: center; |
| 137 | + justify-content: space-between; |
| 138 | + gap: 8px; |
| 139 | + cursor: pointer; |
| 140 | + text-align: left; |
| 141 | +} |
| 142 | +.rs-value { |
| 143 | + overflow: hidden; |
| 144 | + text-overflow: ellipsis; |
| 145 | + white-space: nowrap; |
| 146 | +} |
| 147 | +.rs-arrow { |
| 148 | + color: #9aa2ad; |
| 149 | + font-size: 12px; |
| 150 | + flex: none; |
| 151 | +} |
| 152 | +.rs-backdrop { |
| 153 | + position: fixed; |
| 154 | + inset: 0; |
| 155 | + z-index: 200; |
| 156 | + background: transparent; |
| 157 | +} |
| 158 | +.rs-popup { |
| 159 | + position: fixed; |
| 160 | + z-index: 201; |
| 161 | + display: flex; |
| 162 | + flex-direction: column; |
| 163 | + padding: 4px; |
| 164 | + gap: 2px; |
| 165 | + background: #23272e; |
| 166 | + border: 1px solid #434a54; |
| 167 | + border-radius: 8px; |
| 168 | + box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5); |
| 169 | + overflow-y: auto; |
| 170 | +} |
| 171 | +.rs-option { |
| 172 | + border: none; |
| 173 | + background: transparent; |
| 174 | + color: #ddd; |
| 175 | + padding: 8px 12px; |
| 176 | + border-radius: 5px; |
| 177 | + font-size: 14px; |
| 178 | + text-align: left; |
| 179 | + white-space: nowrap; |
| 180 | + cursor: pointer; |
| 181 | +} |
| 182 | +.rs-option:hover, |
| 183 | +.rs-option:focus { |
| 184 | + background: #2f6f9f; |
| 185 | + color: #fff; |
| 186 | + outline: none; |
| 187 | +} |
| 188 | +.rs-option[data-selected='true'] { |
| 189 | + color: #6db3e6; |
| 190 | +} |
| 191 | +.rs-option[data-selected='true']::before { |
| 192 | + content: '✓ '; |
| 193 | +} |
| 194 | +</style> |
0 commit comments