Skip to content

Commit 713a284

Browse files
focus highlighted only
1 parent c15490b commit 713a284

File tree

4 files changed

+13
-20
lines changed

4 files changed

+13
-20
lines changed

packages/kit-headless/src/components/select/select-context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export type SelectContext = {
1414
listboxRef: Signal<HTMLUListElement | undefined>;
1515
groupRef: Signal<HTMLDivElement | undefined>;
1616
labelRef: Signal<HTMLDivElement | undefined>;
17-
firstEnabledItemRef: Signal<HTMLLIElement | undefined>;
17+
highlightedItemRef: Signal<HTMLLIElement | undefined>;
1818

1919
// core state
2020
itemsMapSig: Readonly<Signal<TItemsMap>>;

packages/kit-headless/src/components/select/select-item.tsx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ export const HSelectItem = component$<SelectItemProps>((props) => {
3535
const itemRef = useSignal<HTMLLIElement>();
3636
const localIndexSig = useSignal<number | null>(null);
3737
const itemId = `${context.localId}-${_index}`;
38-
const isInitialFocusSig = useSignal<boolean>(true);
3938

4039
const { selectionManager$, getNextEnabledItemIndex$, getPrevEnabledItemIndex$ } =
4140
useSelect();
@@ -61,12 +60,6 @@ export const HSelectItem = component$<SelectItemProps>((props) => {
6160
throw Error('Qwik UI: Select component item cannot find its proper index.');
6261

6362
localIndexSig.value = _index;
64-
65-
// update the context with the first enabled item ref
66-
const firstEnabledIndex = await getNextEnabledItemIndex$(-1);
67-
if (localIndexSig.value === firstEnabledIndex) {
68-
context.firstEnabledItemRef = itemRef;
69-
}
7063
});
7164

7265
useTask$(async function scrollableTask({ track, cleanup }) {
@@ -98,7 +91,10 @@ export const HSelectItem = component$<SelectItemProps>((props) => {
9891
}
9992
}
10093

101-
if (!isInitialFocusSig.value) return;
94+
// update the context with the highlighted item ref
95+
if (localIndexSig.value === context.highlightedIndexSig.value) {
96+
context.highlightedItemRef = itemRef;
97+
}
10298
});
10399

104100
const handleClick$ = $(async () => {

packages/kit-headless/src/components/select/select-root.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ export const HSelectImpl = component$<SelectProps<boolean> & InternalSelectProps
161161
const currDisplayValueSig = useSignal<string | string[]>();
162162

163163
const initialLoadSig = useSignal<boolean>(true);
164-
const firstEnabledItemRef = useSignal<HTMLLIElement>();
164+
const highlightedItemRef = useSignal<HTMLLIElement>();
165165

166166
const context: SelectContext = {
167167
itemsMapSig,
@@ -171,7 +171,7 @@ export const HSelectImpl = component$<SelectProps<boolean> & InternalSelectProps
171171
listboxRef,
172172
labelRef,
173173
groupRef,
174-
firstEnabledItemRef,
174+
highlightedItemRef,
175175
localId,
176176
highlightedIndexSig,
177177
selectedIndexSetSig,

packages/kit-headless/src/components/select/select-trigger.tsx

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,13 @@ export const HSelectTrigger = component$<SelectTriggerProps>((props) => {
100100

101101
/** When initially opening the listbox, we want to grab the first enabled option index */
102102
if (context.highlightedIndexSig.value === null) {
103-
const firstItem = await getNextEnabledItemIndex$(-1);
104-
context.highlightedIndexSig.value = firstItem;
105-
106-
// Wait for the popover code to be executed
107-
while (!(context.firstEnabledItemRef.value === document.activeElement)) {
108-
await new Promise((resolve) => setTimeout(resolve, 5));
109-
context.firstEnabledItemRef.value?.focus();
110-
}
103+
context.highlightedIndexSig.value = await getNextEnabledItemIndex$(-1);
104+
}
111105

112-
return;
106+
// Wait for the popover code to be executed
107+
while (context.highlightedItemRef.value !== document.activeElement) {
108+
await new Promise((resolve) => setTimeout(resolve, 5));
109+
context.highlightedItemRef.value?.focus();
113110
}
114111
});
115112

0 commit comments

Comments
 (0)