Select.vue
10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<script setup>
/**
* 下拉选择。自绘 trigger + listbox 浮层,不用原生 `<select>`(它的面板由操作系统绘制,没法跟主题一致)。
*
* 浮层 teleport 到 body:卡片有 `overflow: hidden`,不 teleport 会被剪掉。
*
* @typedef {'sm'|'md'|'lg'} SelectSize
* @typedef {{ value: string|number, label: string }} SelectOption
*/
import { computed, nextTick, onBeforeUnmount, ref, watch } from 'vue'
const props = defineProps({
modelValue: { type: [String, Number], default: '' },
/** @type {import('vue').PropType<SelectOption[]>} */
options: { type: Array, default: () => [] },
/** 未选中时 trigger 上显示的文案。 */
placeholder: { type: String, default: '' },
ariaLabel: { type: String, default: '' },
disabled: { type: Boolean, default: false },
invalid: { type: Boolean, default: false },
/** @type {import('vue').PropType<SelectSize>} */
size: { type: String, default: 'md' },
/** 撑满父容器。默认按内容宽度。 */
block: { type: Boolean, default: false },
/** 窄屏下将选项改为从底部弹出的选择面板。 */
mobileSheet: { type: Boolean, default: false },
})
const emit = defineEmits(['update:modelValue'])
const open = ref(false)
const triggerEl = ref(null)
const panelEl = ref(null)
const activeIndex = ref(-1)
const panelStyle = ref({})
const selected = computed(() => props.options.find((o) => o.value === props.modelValue) ?? null)
const triggerLabel = computed(() => selected.value?.label ?? props.placeholder)
function place() {
const rect = triggerEl.value.getBoundingClientRect()
const below = window.innerHeight - rect.bottom
const flip = below < 240 && rect.top > below
panelStyle.value = {
left: `${rect.left}px`,
minWidth: `${rect.width}px`,
...(flip ? { bottom: `${window.innerHeight - rect.top + 4}px` } : { top: `${rect.bottom + 4}px` }),
}
}
async function toggle() {
if (props.disabled) return
open.value = !open.value
if (!open.value) return
activeIndex.value = props.options.findIndex((o) => o.value === props.modelValue)
place()
await nextTick()
panelEl.value?.querySelector('[data-active="true"]')?.scrollIntoView({ block: 'nearest' })
}
function choose(option) {
emit('update:modelValue', option.value)
open.value = false
triggerEl.value?.focus()
}
function onKeydown(event) {
if (props.disabled) return
if (!open.value) {
if (['Enter', ' ', 'ArrowDown', 'ArrowUp'].includes(event.key)) {
event.preventDefault()
toggle()
}
return
}
if (event.key === 'Escape') {
event.preventDefault()
open.value = false
triggerEl.value?.focus()
} else if (event.key === 'ArrowDown') {
event.preventDefault()
activeIndex.value = Math.min(activeIndex.value + 1, props.options.length - 1)
} else if (event.key === 'ArrowUp') {
event.preventDefault()
activeIndex.value = Math.max(activeIndex.value - 1, 0)
} else if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault()
const option = props.options[activeIndex.value]
if (option) choose(option)
}
}
function onPointerDown(event) {
if (triggerEl.value?.contains(event.target) || panelEl.value?.contains(event.target)) return
open.value = false
}
watch(open, (isOpen) => {
const method = isOpen ? 'addEventListener' : 'removeEventListener'
document[method]('pointerdown', onPointerDown, true)
window[method]('resize', place)
window[method]('scroll', place, true)
})
onBeforeUnmount(() => {
document.removeEventListener('pointerdown', onPointerDown, true)
window.removeEventListener('resize', place)
window.removeEventListener('scroll', place, true)
})
</script>
<template>
<button
ref="triggerEl"
type="button"
class="drama-select"
:class="[`drama-select--${size}`, { 'is-invalid': invalid, 'is-open': open, 'is-block': block, 'is-placeholder': !selected }]"
:disabled="disabled"
:aria-label="ariaLabel || undefined"
:aria-expanded="open"
:aria-invalid="invalid ? 'true' : 'false'"
aria-haspopup="listbox"
@click="toggle"
@keydown="onKeydown"
>
<span class="drama-select__label">{{ triggerLabel }}</span>
<svg
class="drama-select__chevron"
width="16"
height="16"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
>
<path d="m6 9 6 6 6-6" />
</svg>
</button>
<Teleport to="body">
<button
v-if="open && mobileSheet"
class="drama-select__sheet-dismiss"
type="button"
aria-label="Close"
@click="open = false"
></button>
<div
v-if="open"
ref="panelEl"
class="drama-select__panel"
:class="[`drama-select__panel--${size}`, { 'is-mobile-sheet': mobileSheet }]"
:style="panelStyle"
role="listbox"
>
<button
v-for="(option, index) in options"
:key="option.value"
type="button"
class="drama-select__option"
role="option"
:aria-selected="option.value === modelValue"
:data-active="index === activeIndex"
@click="choose(option)"
@mousemove="activeIndex = index"
>
<span class="drama-select__option-label">{{ option.label }}</span>
<svg
v-if="option.value === modelValue"
width="14"
height="14"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2.5"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
>
<path d="M20 6 9 17l-5-5" />
</svg>
</button>
</div>
</Teleport>
</template>
<style scoped>
.drama-select {
box-sizing: border-box;
display: inline-flex;
align-items: center;
justify-content: space-between;
gap: var(--drama-space-sm);
border: 1px solid var(--drama-color-border);
border-radius: var(--drama-radius-sm);
background-color: var(--drama-color-surface);
color: var(--drama-color-text);
font-family: var(--drama-font-family-body);
line-height: var(--drama-line-height-tight);
cursor: pointer;
outline: none;
transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.drama-select.is-block {
display: flex;
width: 100%;
}
.drama-select:hover:not(:disabled):not(.is-open) {
border-color: var(--drama-color-border-strong);
}
.drama-select:focus-visible,
.drama-select.is-open {
border-color: var(--drama-color-primary);
box-shadow: 0 0 0 2px color-mix(in srgb, var(--drama-color-primary) 18%, transparent);
}
.drama-select:disabled {
opacity: 0.55;
cursor: not-allowed;
}
.drama-select.is-invalid {
border-color: var(--drama-color-danger);
}
.drama-select.is-placeholder .drama-select__label {
color: var(--drama-color-text-muted);
}
.drama-select__label {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.drama-select__chevron {
flex-shrink: 0;
color: var(--drama-color-text-muted);
stroke-width: 1.75;
transition: transform 0.15s ease;
}
/* 箭头是提示,不是内容:32px 高的控件里塞一个 16px、粗描边的箭头,它比值还抢眼。 */
.drama-select--sm .drama-select__chevron {
width: 14px;
height: 14px;
}
.drama-select.is-open .drama-select__chevron {
transform: rotate(180deg);
}
/* 高度取 control token,和同尺寸的 Input / SegmentedControl / Button 等高。 */
.drama-select--sm {
height: var(--drama-control-height-sm);
padding: 0 var(--drama-space-md);
font-size: var(--drama-font-size-sm);
}
.drama-select--md {
height: var(--drama-control-height-md);
padding: 0 var(--drama-space-md);
font-size: var(--drama-font-size-md);
}
.drama-select--lg {
height: var(--drama-control-height-lg);
padding: 0 var(--drama-space-lg);
font-size: var(--drama-font-size-lg);
}
.drama-select__panel {
position: fixed;
z-index: 1000;
max-height: 280px;
overflow-y: auto;
padding: var(--drama-space-xs);
background-color: var(--drama-color-surface);
border: 1px solid var(--drama-color-border);
border-radius: var(--drama-radius-md);
box-shadow: var(--drama-shadow-md);
}
.drama-select__sheet-dismiss {
display: none;
}
/* 面板的字号和行高跟着 trigger 走。写死一个尺寸,sm 的 trigger 下面就会掉出一张字更大、行更高的
列表 —— 同一个控件的两半对不上。 */
.drama-select__panel--sm {
font-size: var(--drama-font-size-sm);
--drama-select-option-height: var(--drama-control-height-sm);
}
.drama-select__panel--md {
font-size: var(--drama-font-size-md);
--drama-select-option-height: var(--drama-control-height-md);
}
.drama-select__panel--lg {
font-size: var(--drama-font-size-lg);
--drama-select-option-height: var(--drama-control-height-lg);
}
.drama-select__option {
display: flex;
align-items: center;
justify-content: space-between;
gap: var(--drama-space-md);
width: 100%;
height: var(--drama-select-option-height);
padding: 0 var(--drama-space-md);
border: 0;
border-radius: var(--drama-radius-sm);
background: transparent;
color: var(--drama-color-text);
font-family: var(--drama-font-family-body);
font-size: inherit;
line-height: var(--drama-line-height-tight);
text-align: left;
cursor: pointer;
}
/* 键盘/指针停在哪一行 —— 一层很淡的底色,不是一条压过去的大色块。 */
.drama-select__option[data-active='true'] {
background-color: color-mix(in srgb, var(--drama-color-primary) 8%, transparent);
}
/* 选中项靠主色文字 + 右侧对勾表明,不再叠一层填充:填充是「光标在这行」,对勾才是「选的是这个」。 */
.drama-select__option[aria-selected='true'] {
color: var(--drama-color-primary);
font-weight: var(--drama-font-weight-medium);
}
.drama-select__option-label {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
@media (max-width: 680px) {
.drama-select__sheet-dismiss {
position: fixed;
z-index: 1001;
inset: 0;
display: block;
padding: 0;
border: 0;
background: var(--drama-color-overlay);
}
.drama-select__panel.is-mobile-sheet {
position: fixed;
z-index: 1002;
top: auto !important;
right: 0;
bottom: 0 !important;
left: 0 !important;
width: 100%;
min-width: 0 !important;
max-height: min(70dvh, 520px);
padding: 18px 12px calc(12px + env(safe-area-inset-bottom));
border-right: 0;
border-bottom: 0;
border-left: 0;
border-radius: 18px 18px 0 0;
box-shadow: var(--drama-shadow-lg);
}
.drama-select__panel.is-mobile-sheet::before {
position: absolute;
top: 7px;
left: 50%;
width: 34px;
height: 3px;
border-radius: var(--drama-radius-full);
background: var(--drama-color-border-strong);
content: '';
transform: translateX(-50%);
}
.drama-select__panel.is-mobile-sheet .drama-select__option {
height: 46px;
padding-inline: 14px;
border-radius: 8px;
font-size: 13px;
}
}
</style>