-
-
Notifications
You must be signed in to change notification settings - Fork 7.1k
feat(VList): add navigation-strategy to control focused item
#22328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Co-authored-by: John Leider <[email protected]>
|
At which point do we supplement visual feedback? I have a feeling it should be a part of this PR. watch(navigationIndex, v => {
activateSingle(v)
// ...and scroll
})// nested.ts
// naive implementation, without testing with expandable and nested lists... but maybe it is fine?
activateSingle: (index: number) => {
const targetValue = [...nodeIds].at(index)
activated.value = new Set(targetValue !== undefined ? [targetValue] : [])
},Playground (from one of my oldest demos)<template>
<v-app theme="dark">
<div class="d-flex justify-space-around">
<v-menu v-model="open" :close-on-content-click="false">
<template #activator="{ props }">
<v-btn color="primary" v-bind="props">{{ selection[0] }}</v-btn>
</template>
<v-card>
<v-text-field
ref="searchField"
v-model="search"
autocomplete="off"
class="ma-1"
density="compact"
min-width="200"
variant="outlined"
hide-details
@keydown.down="moveDown"
@keydown.enter="select()"
@keydown.up="moveUp"
/>
<v-list
v-model:navigation-index="listNavIndex"
:items="visibleItems"
class="py-0"
density="compact"
elevation="0"
max-height="300"
navigation-strategy="track"
activatable
item-props
mandatory
>
<template #item="{ props }">
<v-list-item
v-bind="props"
:append-icon="selection.includes(props.value) ? '$complete' : ''"
@click="select(props.value)"
>
<template v-if="search" #title>
<span>{{ props.title.split(searchRegex, 1)[0] }}</span>
<span class="font-weight-bold">{{ props.title.match(searchRegex)[0] }}</span>
<span>{{
props.title.substring(props.title.split(searchRegex)[0].length
+ search.length) }}</span>
</template>
</v-list-item>
</template>
</v-list>
</v-card>
</v-menu>
</div>
</v-app>
</template>
<script setup lang="ts">
import { computed, ref, watch } from 'vue'
const selection = ref(['Apple'])
const search = ref('')
const searchField = ref()
const listNavIndex = ref<number>()
function moveUp () {
listNavIndex.value = Math.max(0, (listNavIndex.value ?? visibleItems.value.length) - 1)
}
function moveDown () {
listNavIndex.value = Math.min(visibleItems.value.length, (listNavIndex.value ?? -1) + 1)
}
const open = ref(false)
const items = [
{ title: 'Apple', value: 'Apple' },
{ title: 'Orange', value: 'Orange' },
{ title: 'Banana', value: 'Banana' },
{ title: 'Grapefruit', value: 'Grapefruit' },
].concat(
[...new Array(50)].map((_, i) => ({
title: `item #${i}`,
value: `item #${i}`,
}))
)
const searchRegex = computed(() => {
const regEscape = v => v.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&')
return new RegExp(regEscape(search.value), 'i')
})
const visibleItems = computed(() => {
return search.value
? items.filter(x => searchRegex.value.test(x.title))
: items
})
function select (v?: any) {
if (v !== undefined) {
selection.value = [v]
open.value = false
return
}
if (!visibleItems.value.length) return
const index = Math.max(0, listNavIndex.value ?? 0)
if (index < visibleItems.value.length) {
selection.value = [visibleItems.value[index].value]
open.value = false
}
}
watch(open, v => {
if (v) {
setTimeout(() => searchField.value.focus(), 200)
} else {
setTimeout(() => search.value = '', 300)
}
})
</script> |
e9e80ae to
97e8682
Compare
|
I can |
|
Something broke the demo with |
- index not passed to item slot in VListChildren.tsx - Non-unique IDs across multiple lists
|
Minor defect, would be great if we could iron out: Steps:
Problem: it is still selected, but was not visible. If a user would click "enter" they would apply a selection that they don't see. Suggestion:
The first one is probably something that would work in general, but some may require some extra thought - can we support navigation in the list that loads items progressively (i.e. only appends new items)? |
|
@J-Sek LMK if you think the most recent change addresses your feedback! |
|
@MatthewAry it solves the problem 👍🏼 The only nitpick would be that it might feel a bit inconsistent to keep index while typing. VCombobox, just drops the selection and scroll to the top of the list. Is it intentional or some kind of a side-effect? |
|
@J-Sek Not intentional, rather, it's an oversight. I think VCombobox's behavior makes sense. |
J-Sek
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One last suggestion for naming and it is 🟢 from me.
navigation-strategy to control focused item
Cause it's gone through enough
https://bin.vuetifyjs.com/bins/l_ScLQ
Add VList "Strategy" Mode with Index Tracking
The Problem (Context)
VList's keyboard navigation assumes: focus movement to items
VCommandPalette needs: index tracking without focus movement
Currently VCommandPalette works around this by:
tabindex=-1on items (breaks accessibility)The Solution
Add two keyboard navigation modes to VList:
Mode 1: 'focus' (default, current behavior)
focusChild()utilityMode 2: 'track' (new, for VCommandPalette)
Why This Works