Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(Selector): the option the mouse moves onto after keyboard input shows no focus ring (#10049)

Cindy Zhangb988598bd9ed
diff --git a/.changeset/selector-active-option-ring.md b/.changeset/selector-active-option-ring.md
index 0fd4e33dde..5a308c5a17 100644
--- a/.changeset/selector-active-option-ring.md
+++ b/.changeset/selector-active-option-ring.md
@@ -2,6 +2,6 @@
'@astryxdesign/core': patch
---
-[fix] Selector: after keyboard input, the active option draws the standard focus ring inside its edge, so people moving through the list with the keyboard can see where they are. The overlay fill alone measured about 1.09:1 against the listbox in light mode and 1.15:1 in dark, under the 3:1 a state indicator needs; with the stock theme the ring measures 5.39:1 (light) and 3.10:1 (dark). The ring takes its width, style and color from the `--focus-outline-*` tokens, like every other focus ring. After pointer input the active option keeps the fill alone, as a focused DropdownMenu row does.
+[fix] Selector: after keyboard input, the active option draws the standard focus ring inside its edge, so people moving through the list with the keyboard can see where they are. The overlay fill alone measured about 1.09:1 against the listbox in light mode and 1.15:1 in dark, under the 3:1 a state indicator needs; with the stock theme the ring measures 5.39:1 (light) and 3.10:1 (dark). The ring takes its width, style and color from the `--focus-outline-*` tokens, like every other focus ring. After pointer input the active option keeps the fill alone, as a focused DropdownMenu row does. This includes a mouse that moves onto another option after keyboard input, without a click.
@cixzhang
diff --git a/packages/core/src/Selector/Selector.activeOption.test.tsx b/packages/core/src/Selector/Selector.activeOption.test.tsx
index b21280df03..f77b972c02 100644
--- a/packages/core/src/Selector/Selector.activeOption.test.tsx
+++ b/packages/core/src/Selector/Selector.activeOption.test.tsx
@@ -1,7 +1,7 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.
import {describe, expect, it} from 'vitest';
-import {render, screen} from '@testing-library/react';
+import {fireEvent, render, screen} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import {Selector} from './Selector';
import {compositeOver, contrastRatio} from '../theme/contrast';
@@ -94,6 +94,43 @@ describe('Selector active option indicator', () => {
);
});
+ it('moves to the fill alone when the mouse takes the highlight after keyboard input', async () => {
+ const user = userEvent.setup();
+ const field = renderSelector();
+ field.focus();
+ await user.keyboard('{ArrowDown}');
+ await user.keyboard('{ArrowDown}');
+ expect(hasRing(activeOption(field))).toBe(true);
+
+ // A hover without a press: the shared modality still reads keyboard.
+ await user.hover(option('Cherry'));
+ const row = activeOption(field);
+ expect(row).toHaveTextContent('Cherry');
+ expect(hasRing(row)).toBe(false);
+ expect(hasRing(option('Banana'))).toBe(false);
+
+ await user.keyboard('{ArrowUp}');
+ const back = activeOption(field);
+ expect(back).toHaveTextContent('Banana');
+ expect(hasRing(back)).toBe(true);
+ });
+
+ it('keeps the ring when a key brings the active option under a still mouse', async () => {
+ const user = userEvent.setup();
+ const field = renderSelector();
+ field.focus();
+ await user.keyboard('{ArrowDown}');
+ await user.keyboard('{ArrowDown}');
+ const row = activeOption(field);
+ expect(row).toHaveTextContent('Banana');
+
+ // The browser reports the row under a still pointer as entered when a
+ // key scrolls the list. No pointer move happens.
+ fireEvent.mouseEnter(row);
+ expect(activeOption(field)).toBe(row);
+ expect(hasRing(row)).toBe(true);
+ });
+
// WCAG 2.2 1.4.11: a state indicator needs 3:1 against the listbox it sits
// on. The overlay fill alone measures about 1.09:1 (light) and 1.15:1
// (dark).
diff --git a/packages/core/src/Selector/Selector.tsx b/packages/core/src/Selector/Selector.tsx
index b1e92a2311..0b1fdf7b14 100644
--- a/packages/core/src/Selector/Selector.tsx
+++ b/packages/core/src/Selector/Selector.tsx
@@ -939,7 +939,12 @@ interface SelectorOptionRowProps {
renderOption: ((option: SelectorOptionData) => ReactNode) | undefined;
SelectionMark: IndicatorComponent<IndicatorMap['check']>;
onSelect: (item: SelectorOptionData) => void;
- onHover: (item: SelectorOptionData, flatIndex: number) => void;
+ /** `isActive`: the option was already the highlighted one when entered. */
+ onHover: (
+ item: SelectorOptionData,
+ flatIndex: number,
+ isActive: boolean,
+ ) => void;
}
/**
@@ -993,7 +998,7 @@ const SelectorOptionRow = memo(function SelectorOptionRow({
aria-selected={isSelected}
aria-disabled={item.disabled}
onClick={() => onSelect(item)}
- onMouseEnter={() => onHover(item, flatIndex)}
+ onMouseEnter={() => onHover(item, flatIndex, isHighlighted)}
{...mergeProps(
// Stable theme target on the option row itself, mirroring
// `multi-selector-option`: it carries the row's size and runtime
@@ -1521,6 +1526,27 @@ export function Selector<T extends SelectorOptionType>(
// Only after keyboard input does the active option draw the focus ring; a
// pointer highlight keeps the fill alone, as a focused menu row does.
+ // Modality changes only on a key or a press, so a hover alone still reads
+ // as keyboard. The option the mouse enters therefore draws no ring until a
+ // key moves the highlight again. Entering the option that is already
+ // active, as when a key scrolls it under a still mouse, changes nothing.
+ const [hoveredIndex, setHoveredIndex] = useState<number | null>(null);
+ const keyDownAfterHover = useCallback(
+ (e: React.KeyboardEvent) => {
+ setHoveredIndex(null);
+ comboboxKeyDown(e);
+ },
+ [comboboxKeyDown],
+ );
+ const mouseEnterOption = useCallback(
+ (item: SelectorOptionData, index: number, isActive: boolean) => {
+ if (!isActive) {
+ setHoveredIndex(index);
+ }
+ comboboxItemMouseEnter(item, index);
+ },
+ [comboboxItemMouseEnter],
+ );
const {
isKeyboardHighlight: isHighlightFromKeyboard,
noteHighlightMove,
@@ -1528,8 +1554,8 @@ export function Selector<T extends SelectorOptionType>(
onItemMouseEnter,
} = useKeyboardHighlight(
triggerModality.readModality,
- comboboxKeyDown,
- comboboxItemMouseEnter,
+ keyDownAfterHover,
+ mouseEnterOption,
);
const onTriggerClick = useCallback(() => {
comboboxTriggerClick();
@@ -1595,7 +1621,8 @@ export function Selector<T extends SelectorOptionType>(
const index = optionIndexFromRow(row);
const item = filteredItems[index];
if (item != null) {
- onItemMouseEnter(item, index);
+ // A press is pointer input, even on the active option.
+ onItemMouseEnter(item, index, false);
}
},
onActivate: row => {
@@ -1613,6 +1640,7 @@ export function Selector<T extends SelectorOptionType>(
return;
}
noteHighlightMove();
+ setHoveredIndex(null);
// With hasSearch the query input owns typing, so type-to-select is off.
if (!hasSearch && typeahead.onKeyDown(e)) {
e.preventDefault();
@@ -1770,7 +1798,9 @@ export function Selector<T extends SelectorOptionType>(
id={getItemId(flatIndex)}
isHighlighted={flatIndex === highlightedIndex}
isKeyboardHighlighted={
- flatIndex === highlightedIndex && isHighlightFromKeyboard
+ flatIndex === highlightedIndex &&
+ isHighlightFromKeyboard &&
+ flatIndex !== hoveredIndex
}
isSelected={item.value === normalizedValue}
size={size}
@@ -1786,6 +1816,7 @@ export function Selector<T extends SelectorOptionType>(
indicatorPosition,
highlightedIndex,
isHighlightFromKeyboard,
+ hoveredIndex,
size,
normalizedValue,
getItemId,