fix: handle ColorSwatchPicker activation lifecycle and dynamic focus
This commit is contained in:
@@ -92,7 +92,7 @@ const registry = useCollectionRegistry<ColorSwatchPickerMember>();
|
||||
let sequence = 0;
|
||||
const nextKey = () => ++sequence;
|
||||
const ordered = () => registry.ordered();
|
||||
const enabled = () => registry.available();
|
||||
const enabled = () => (state.disabled.value ? [] : registry.available());
|
||||
|
||||
const rootElement = shallowRef<HTMLElement>();
|
||||
const focused = shallowRef<ColorSwatchPickerKey>();
|
||||
@@ -270,17 +270,28 @@ function onRootKeydown(event: KeyboardEvent) {
|
||||
}
|
||||
}
|
||||
|
||||
// A removed or disabled item cannot stay focused; focus falls back to the
|
||||
// listbox entry so it is not lost to the document.
|
||||
// A removed or disabled item cannot stay focused; the listbox keeps one legal
|
||||
// tab entry (the root or a usable item) and never drops focus to the document.
|
||||
watch(
|
||||
() => registry.ordered(),
|
||||
() =>
|
||||
[
|
||||
state.disabled.value,
|
||||
registry
|
||||
.ordered()
|
||||
.map((member) => `${String(member.key())}:${member.disabled()}`)
|
||||
.join(","),
|
||||
].join("|"),
|
||||
() => {
|
||||
if (focused.value === undefined) return;
|
||||
const member = enabled().find((item) => item.key() === focused.value);
|
||||
if (member) return;
|
||||
const usable = (key: ColorSwatchPickerKey | undefined) =>
|
||||
key !== undefined &&
|
||||
enabled().some((item) => item.key() === key && !itemDisabled(key));
|
||||
if (pressed.value !== undefined && !usable(pressed.value))
|
||||
pressed.value = undefined;
|
||||
if (focused.value === undefined || usable(focused.value)) return;
|
||||
focused.value = undefined;
|
||||
if (rootElement.value?.contains(document.activeElement))
|
||||
rootElement.value.focus();
|
||||
focusVisible.value = false;
|
||||
const root = rootElement.value;
|
||||
if (root?.contains(document.activeElement)) root.focus();
|
||||
},
|
||||
);
|
||||
|
||||
@@ -328,6 +339,9 @@ function rootAttrs() {
|
||||
onMousedown: () => {
|
||||
focusVisible.value = false;
|
||||
},
|
||||
onMouseup: (event: MouseEvent) => {
|
||||
if (event.button === 0) pressed.value = undefined;
|
||||
},
|
||||
onKeyupCapture: () => {
|
||||
pressed.value = undefined;
|
||||
},
|
||||
@@ -358,9 +372,10 @@ provide(colorSwatchPickerKey, {
|
||||
focused.value = key;
|
||||
},
|
||||
noteBlur: (key) => {
|
||||
// Press release is owned by the item's mouse/leave handlers: a blur caused
|
||||
// by a new press must not clear the new press state.
|
||||
if (focused.value !== key) return;
|
||||
focusVisible.value = false;
|
||||
pressed.value = undefined;
|
||||
},
|
||||
focusItem,
|
||||
activate,
|
||||
|
||||
@@ -91,16 +91,45 @@ const state = computed<ColorSwatchPickerItemRenderProps>(() => {
|
||||
|
||||
provide(colorSwatchPickerItemKey, { key, state });
|
||||
|
||||
/** The compatibility click of an already activated press must not re-propose. */
|
||||
const pressActivated = shallowRef(false);
|
||||
|
||||
function activateFrom(event: Event) {
|
||||
picker.activate(key, event);
|
||||
element.value?.focus({ preventScroll: true });
|
||||
picker.noteFocus(key);
|
||||
}
|
||||
|
||||
function onMousedown(event: MouseEvent) {
|
||||
// Only the primary button activates; secondary buttons keep their native
|
||||
// behaviour (context menu, autoscroll).
|
||||
if (event.button !== 0) return;
|
||||
pressActivated.value = true;
|
||||
if (state.value.isDisabled) {
|
||||
// Keep focus from moving onto a disabled option.
|
||||
event.preventDefault();
|
||||
return;
|
||||
}
|
||||
picker.pressed.value = key;
|
||||
picker.activate(key, event);
|
||||
element.value?.focus({ preventScroll: true });
|
||||
picker.noteFocus(key);
|
||||
activateFrom(event);
|
||||
}
|
||||
|
||||
function onClick(event: MouseEvent) {
|
||||
// Real pointer clicks already activated on press (detail > 0). Click-only
|
||||
// activations from assistive technology arrive without a press and with
|
||||
// detail 0, and still honour cancelable clicks and rejection retries.
|
||||
if (pressActivated.value) {
|
||||
pressActivated.value = false;
|
||||
return;
|
||||
}
|
||||
if (event.detail > 0) return;
|
||||
if (state.value.isDisabled) return;
|
||||
picker.pressed.value = undefined;
|
||||
activateFrom(event);
|
||||
}
|
||||
|
||||
function releasePress() {
|
||||
if (picker.pressed.value === key) picker.pressed.value = undefined;
|
||||
}
|
||||
|
||||
function itemAttrs() {
|
||||
@@ -136,14 +165,24 @@ function itemAttrs() {
|
||||
: ((node as { $el?: HTMLElement } | null)?.$el ?? undefined);
|
||||
},
|
||||
onFocus: () => picker.noteFocus(key),
|
||||
onBlur: () => picker.noteBlur(key),
|
||||
onBlur: () => {
|
||||
pressActivated.value = false;
|
||||
releasePress();
|
||||
picker.noteBlur(key);
|
||||
},
|
||||
onMousedown,
|
||||
onClick,
|
||||
onMouseup: (event: MouseEvent) => {
|
||||
if (event.button !== 0) return;
|
||||
releasePress();
|
||||
},
|
||||
onMouseenter: () => {
|
||||
hovered.value = true;
|
||||
},
|
||||
onMouseleave: () => {
|
||||
hovered.value = false;
|
||||
picker.pressed.value = undefined;
|
||||
pressActivated.value = false;
|
||||
releasePress();
|
||||
},
|
||||
} as Record<string, unknown>);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user