Skip to content

Commit b365206

Browse files
thibaudcolaslaymonage
authored andcommitted
Switch block previews from hover + toggle to toggle only
1 parent 0193cad commit b365206

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

client/src/components/ComboBox/ComboBox.scss

+9
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,15 @@ $width: clamp(300px, 75vw, 1000px);
137137
&:hover {
138138
color: theme('colors.icon-secondary-hover');
139139
}
140+
141+
&[aria-expanded='true'] {
142+
color: theme('colors.text-link-default');
143+
144+
@media (forced-colors: active) {
145+
background: Highlight;
146+
color: HighlightText;
147+
}
148+
}
140149
}
141150

142151
.w-combobox__option-row--col1 {

client/src/components/ComboBox/ComboBox.tsx

+10-13
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export default function ComboBox<ComboBoxOption extends ComboBoxItem>({
6868
(category) => category.items || [],
6969
);
7070
const [inputItems, setInputItems] = useState<ComboBoxOption[]>(flatItems);
71+
const [previewedIndex, setPreviewedIndex] = useState<number>(-1);
7172
// Re-create the categories so the two-column layout flows as expected.
7273
const categories = items.reduce<ComboBoxCategory<ComboBoxOption>[]>(
7374
(cats, cat, index) => {
@@ -88,7 +89,6 @@ export default function ComboBox<ComboBoxOption extends ComboBoxItem>({
8889
getMenuProps,
8990
getInputProps,
9091
getItemProps,
91-
highlightedIndex,
9292
setHighlightedIndex,
9393
setInputValue,
9494
openMenu,
@@ -153,12 +153,11 @@ export default function ComboBox<ComboBoxOption extends ComboBoxItem>({
153153
setInputItems(filtered);
154154
// Always reset the first item to highlighted on filtering, to speed up selection.
155155
setHighlightedIndex(0);
156+
// Hide any preview when the user starts typing.
157+
setPreviewedIndex(-1);
156158
},
157159
});
158160

159-
const [lastHighlightedIndex, setLastHighlightedIndex] =
160-
useState(highlightedIndex);
161-
162161
useEffect(() => {
163162
if (inputValue) {
164163
openMenu();
@@ -178,15 +177,8 @@ export default function ComboBox<ComboBoxOption extends ComboBoxItem>({
178177
}
179178
}, [inputValue]);
180179

181-
if (
182-
inputItems[highlightedIndex] &&
183-
highlightedIndex !== lastHighlightedIndex
184-
) {
185-
setLastHighlightedIndex(highlightedIndex);
186-
}
187-
188180
const previewedBlock =
189-
inputItems[highlightedIndex] || inputItems[lastHighlightedIndex];
181+
previewedIndex >= 0 ? inputItems[previewedIndex] : null;
190182

191183
return (
192184
<div className="w-combobox-container">
@@ -283,8 +275,13 @@ export default function ComboBox<ComboBoxOption extends ComboBoxItem>({
283275
<button
284276
className="w-combobox__option-preview"
285277
aria-label={comboBoxPreviewLabel}
278+
aria-expanded={previewedIndex === itemIndex}
286279
type="button"
287-
onClick={() => setHighlightedIndex(itemIndex)}
280+
onClick={() =>
281+
setPreviewedIndex(
282+
previewedIndex === itemIndex ? -1 : itemIndex,
283+
)
284+
}
288285
>
289286
<Icon name="view" />
290287
</button>

0 commit comments

Comments
 (0)