fix(collection): close the six review gaps in filtering and virtual focus
- Keep the composite input focused on real pointer presses: cancel the default focus transfer for plain row presses only, leaving nested controls and already cancelled callers alone. - Drive the virtual window, total height, reveal index and aria positions from the visible subset, and render the empty channel when filtering removes every row. Non-virtualized rows stay mounted and are hidden, so their text and accepted selection survive. - Scroll mounted rows for reveal/focus outside virtualization, wait for a virtualized row to mount (frame-based, bounded), and drop reveal requests that filtering, closing or unmounting made obsolete. - Stop overwriting caller `hidden`: mergeProps deletes an earlier key when a later value is undefined, so the filter now only adds `hidden` when the caller did not set it. - Share keyboard modality through an optional `keyboardIntent` ref so a pointer press no longer leaves the active row without data-focus-visible. - Give the verification harness instance-unique ids and derive aria-activedescendant from open state, mounted id and filter visibility. Adds the reviewer probe plus eight regressions, and a virtualized harness instance for the browser smoke of filtering plus window/height/positions.
This commit is contained in:
@@ -16,6 +16,12 @@ export interface CollectionView {
|
||||
virtualFocus?: () => boolean;
|
||||
/** Active key for virtual focus; the owning composite drives it. */
|
||||
activeKey?: Ref<CollectionKey | undefined>;
|
||||
/**
|
||||
* Keyboard modality shared with the composite that holds DOM focus. The
|
||||
* collection clears it on pointer presses; the composite sets it on keys, so
|
||||
* there is one source instead of a second copy.
|
||||
*/
|
||||
keyboardIntent?: Ref<boolean>;
|
||||
}
|
||||
export interface OwnedCollection {
|
||||
items: () => CollectionRecord[];
|
||||
|
||||
@@ -185,7 +185,8 @@ const selection = useCollectionSelection({
|
||||
const focused = ref<ListBoxKey>();
|
||||
const lastFocused = ref<ListBoxKey>();
|
||||
const pressed = ref<ListBoxKey>();
|
||||
const keyboard = ref(true);
|
||||
// Pointer presses clear keyboard intent; the composite sets it again on keys.
|
||||
const keyboard = owner?.keyboardIntent ?? ref(true);
|
||||
const active = computed(() => {
|
||||
if (virtualFocus.value && owner?.activeKey) return owner.activeKey.value;
|
||||
if (!mounted.value) return undefined;
|
||||
@@ -197,7 +198,18 @@ const active = computed(() => {
|
||||
items[0]?.key
|
||||
);
|
||||
});
|
||||
const source = computed(() => (props.items ? [...props.items] : []));
|
||||
/**
|
||||
* Items as the caller passed them, restricted to the visible subset. The full
|
||||
* data set stays available through `known()` for text and accepted values.
|
||||
*/
|
||||
const visibleItems = computed(() => {
|
||||
const items = props.items ? [...props.items] : [];
|
||||
const filter = ownerFilter.value;
|
||||
if (!items.length || !filter) return items;
|
||||
const visible = visibleKeys.value;
|
||||
return items.filter((item) => visible.has(item.id));
|
||||
});
|
||||
const source = computed(() => visibleItems.value);
|
||||
// VueUse owns measurement, scroll range and cleanup; one logical collection
|
||||
// remains available for select-all/typeahead even while most rows are unmounted.
|
||||
const {
|
||||
@@ -231,28 +243,54 @@ function optionId(key: ListBoxKey): string | undefined {
|
||||
?.element();
|
||||
return element?.id || undefined;
|
||||
}
|
||||
function scrollRowIntoView(key: ListBoxKey) {
|
||||
if (!props.virtualized) return;
|
||||
const index = source.value.findIndex((item) => item.id === key);
|
||||
if (index < 0) return;
|
||||
const rowTop = index * props.rowHeight;
|
||||
const element = root.value;
|
||||
if (
|
||||
element &&
|
||||
(rowTop < element.scrollTop ||
|
||||
rowTop + props.rowHeight > element.scrollTop + element.clientHeight)
|
||||
)
|
||||
scrollTo(index);
|
||||
const nextFrame = (): Promise<void> =>
|
||||
typeof requestAnimationFrame === "function"
|
||||
? new Promise((resolve) => requestAnimationFrame(() => resolve()))
|
||||
: nextTick();
|
||||
/** A virtualized row mounts after the window moves; wait for it, bounded. */
|
||||
async function waitForMountedRow(key: ListBoxKey) {
|
||||
for (let attempt = 0; attempt < 3; attempt++) {
|
||||
if (registry.ordered().some((member) => member.key() === key)) return;
|
||||
await nextFrame();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Bring a row into view without taking DOM focus. Virtualized lists move the
|
||||
* window by index; ordinary lists scroll the mounted row (the row itself may
|
||||
* sit inside any scroll container supplied by the caller).
|
||||
*/
|
||||
async function revealRow(key: ListBoxKey) {
|
||||
if (props.virtualized) {
|
||||
const index = source.value.findIndex((item) => item.id === key);
|
||||
if (index < 0) return;
|
||||
const rowTop = index * props.rowHeight;
|
||||
const element = root.value;
|
||||
if (
|
||||
element &&
|
||||
(rowTop < element.scrollTop ||
|
||||
rowTop + props.rowHeight > element.scrollTop + element.clientHeight)
|
||||
)
|
||||
scrollTo(index);
|
||||
await waitForMountedRow(key);
|
||||
return;
|
||||
}
|
||||
await nextTick();
|
||||
const element = registry
|
||||
.ordered()
|
||||
.find((member) => member.key() === key)
|
||||
?.element();
|
||||
element?.scrollIntoView?.({ block: "nearest" });
|
||||
}
|
||||
let revealRequest = 0;
|
||||
/** Reveal a row without taking DOM focus (virtual focus / activedescendant). */
|
||||
async function reveal(key: ListBoxKey) {
|
||||
if (disposing || disabled.value) return;
|
||||
if (!eligible().some((item) => item.key === key)) return;
|
||||
const request = ++revealRequest;
|
||||
lastFocused.value = key;
|
||||
scrollRowIntoView(key);
|
||||
// Wait one tick so a virtualized row is mounted before the consumer reads
|
||||
// its element id for aria-activedescendant.
|
||||
await nextTick();
|
||||
await revealRow(key);
|
||||
// A newer request, filtering, close or unmount makes this one obsolete.
|
||||
if (disposing || request !== revealRequest) return;
|
||||
}
|
||||
async function focus(key: ListBoxKey) {
|
||||
if (
|
||||
@@ -263,14 +301,15 @@ async function focus(key: ListBoxKey) {
|
||||
return;
|
||||
const request = ++focusRequest;
|
||||
lastFocused.value = key;
|
||||
scrollRowIntoView(key);
|
||||
if (virtualFocus.value) {
|
||||
if (owner?.activeKey && owner.activeKey.value !== key)
|
||||
owner.activeKey.value = key;
|
||||
await nextTick();
|
||||
const revealToken = ++revealRequest;
|
||||
await revealRow(key);
|
||||
if (disposing || revealToken !== revealRequest) return;
|
||||
return;
|
||||
}
|
||||
await nextTick();
|
||||
await revealRow(key);
|
||||
if (disposing || request !== focusRequest || disabled.value) return;
|
||||
const element = registry
|
||||
.available()
|
||||
@@ -493,9 +532,9 @@ provide(listBoxKey, {
|
||||
restoreFocus,
|
||||
position: (key) => {
|
||||
if (!props.virtualized || !data.value) return undefined;
|
||||
const items = ownerFilter.value ? visibleKnown() : data.value;
|
||||
const items = visibleItems.value;
|
||||
return {
|
||||
index: items.findIndex((item) => item.key === key) + 1,
|
||||
index: items.findIndex((item) => item.id === key) + 1,
|
||||
size: items.length,
|
||||
};
|
||||
},
|
||||
@@ -574,24 +613,28 @@ defineExpose({
|
||||
<template>
|
||||
<component :is="as" ref="root" v-bind="rootAttrs()">
|
||||
<slot v-if="!items" :item="undefined!" :index="-1" />
|
||||
<slot v-else-if="!items.length" name="empty" />
|
||||
<div
|
||||
v-else-if="virtualized"
|
||||
v-bind="wrapperProps"
|
||||
role="presentation"
|
||||
data-slot="list-box-virtual-content"
|
||||
>
|
||||
<template v-else>
|
||||
<!-- Filtered-empty still needs a visible channel; non-virtualized rows
|
||||
keep their DOM and are only hidden by data-filtered. -->
|
||||
<slot v-if="!visibleItems.length" name="empty" />
|
||||
<div
|
||||
v-for="entry in windowItems"
|
||||
:key="entry.data.id"
|
||||
v-if="virtualized"
|
||||
v-bind="wrapperProps"
|
||||
role="presentation"
|
||||
:style="{ height: `${rowHeight}px` }"
|
||||
data-slot="list-box-virtual-content"
|
||||
>
|
||||
<slot :item="entry.data" :index="entry.index" />
|
||||
<div
|
||||
v-for="entry in windowItems"
|
||||
:key="entry.data.id"
|
||||
role="presentation"
|
||||
:style="{ height: `${rowHeight}px` }"
|
||||
>
|
||||
<slot :item="entry.data" :index="entry.index" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<template v-else v-for="(item, index) in items" :key="item.id">
|
||||
<slot :item="item" :index="index" />
|
||||
<template v-else v-for="(item, index) in items" :key="item.id">
|
||||
<slot :item="item" :index="index" />
|
||||
</template>
|
||||
</template>
|
||||
</component>
|
||||
</template>
|
||||
|
||||
@@ -139,6 +139,23 @@ export default defineComponent({
|
||||
else context.restoreFocus();
|
||||
}
|
||||
});
|
||||
/**
|
||||
* Native presses move focus to the pressed element (or body for a
|
||||
* non-focusable row). Under virtual focus the composite's input must keep
|
||||
* focus, so the default focus transfer is cancelled here — but only for a
|
||||
* plain row press: nested controls keep their own focus behaviour and a
|
||||
* caller that already cancelled is respected.
|
||||
*/
|
||||
function keepInputFocus(event: MouseEvent | PointerEvent) {
|
||||
if (!context.virtual() || event.defaultPrevented) return;
|
||||
if (event instanceof MouseEvent && event.button !== 0) return;
|
||||
const target = event.target as Element | null;
|
||||
const nested = target?.closest?.(
|
||||
'button, a, input, select, textarea, [contenteditable="true"]',
|
||||
);
|
||||
if (nested && nested !== element.value) return;
|
||||
event.preventDefault();
|
||||
}
|
||||
function click(event: MouseEvent | KeyboardEvent) {
|
||||
if (event.defaultPrevented || disabled.value || context.readOnly.value)
|
||||
return;
|
||||
@@ -156,6 +173,10 @@ export default defineComponent({
|
||||
const current = state();
|
||||
const position = context.position(props.id);
|
||||
const visible = context.visible(props.id);
|
||||
// Never emit an undefined-valued key over the caller's attribute:
|
||||
// mergeProps removes the earlier key when a later value is undefined.
|
||||
const hideFiltered =
|
||||
!visible && (attrs as Record<string, unknown>).hidden === undefined;
|
||||
const children = text.prepare(slots.default?.(current) ?? [], true);
|
||||
return h(
|
||||
props.as,
|
||||
@@ -172,7 +193,6 @@ export default defineComponent({
|
||||
context.active.value === props.id
|
||||
? 0
|
||||
: -1,
|
||||
hidden: visible ? undefined : true,
|
||||
"aria-selected":
|
||||
context.mode.value === "none" ? undefined : current.isSelected,
|
||||
"aria-disabled": disabled.value || undefined,
|
||||
@@ -186,6 +206,10 @@ export default defineComponent({
|
||||
text.describedBy(),
|
||||
),
|
||||
"data-slot": "list-box-item",
|
||||
// Filtered rows stay mounted and are hidden, not removed, so their
|
||||
// text and accepted selection survive. `hidden` is only added when the
|
||||
// caller did not set it; the stylesheet also hides data-filtered.
|
||||
...(hideFiltered ? { hidden: true } : {}),
|
||||
"data-filtered": visible ? undefined : true,
|
||||
"data-disabled": disabled.value || undefined,
|
||||
"data-selected": current.isSelected || undefined,
|
||||
@@ -209,6 +233,7 @@ export default defineComponent({
|
||||
hovered.value = false;
|
||||
},
|
||||
onPointerdown: (event: PointerEvent) => {
|
||||
keepInputFocus(event);
|
||||
if (
|
||||
!event.defaultPrevented &&
|
||||
!disabled.value &&
|
||||
@@ -216,6 +241,11 @@ export default defineComponent({
|
||||
)
|
||||
pressed.value = true;
|
||||
},
|
||||
onMousedown: (event: MouseEvent) => {
|
||||
// Chromium transfers focus on mousedown even when pointerdown was
|
||||
// cancelled, so the same rule is applied to both events.
|
||||
keepInputFocus(event);
|
||||
},
|
||||
}),
|
||||
children,
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user