Skip to content

Commit 3c34774

Browse files
fix: select does not wake up core on page load
1 parent 7e0db1a commit 3c34774

File tree

3 files changed

+27
-22
lines changed

3 files changed

+27
-22
lines changed

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import {
1313
import { isBrowser } from '@builder.io/qwik/build';
1414
import SelectContextId, { type SelectContext } from './select-context';
1515
import { Opt } from './select-inline';
16-
import { getActiveDescendant } from './utils';
1716
import { HiddenSelect } from './hidden-select';
17+
import { useSelect } from './use-select';
1818

1919
export type InternalSelectProps = {
2020
/** Our source of truth for the options. We get this at pre-render time in the inline component, that way we do not need to call native methods such as textContent.
@@ -88,6 +88,8 @@ export const SelectImpl = component$<SelectProps & InternalSelectProps>(
8888
...rest
8989
} = props;
9090

91+
const { getActiveDescendant } = useSelect();
92+
9193
// refs
9294
const rootRef = useSignal<HTMLDivElement>();
9395
const triggerRef = useSignal<HTMLButtonElement>();
@@ -148,6 +150,18 @@ export const SelectImpl = component$<SelectProps & InternalSelectProps>(
148150
}
149151
});
150152

153+
const activeDescendantSig = useComputed$(() => {
154+
if (isListboxOpenSig.value) {
155+
return getActiveDescendant(
156+
highlightedIndexSig.value ?? -1,
157+
optionsSig.value,
158+
localId,
159+
);
160+
} else {
161+
return '';
162+
}
163+
});
164+
151165
const context: SelectContext = {
152166
triggerRef,
153167
popoverRef,
@@ -173,15 +187,7 @@ export const SelectImpl = component$<SelectProps & InternalSelectProps>(
173187
aria-controls={listboxId}
174188
aria-expanded={context.isListboxOpenSig.value}
175189
aria-haspopup="listbox"
176-
aria-activedescendant={
177-
context.isListboxOpenSig.value
178-
? getActiveDescendant(
179-
context.highlightedIndexSig.value ?? -1,
180-
context.optionsSig.value,
181-
context.localId,
182-
)
183-
: ''
184-
}
190+
aria-activedescendant={activeDescendantSig.value}
185191
{...rest}
186192
>
187193
<Slot />

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,17 @@ export function useSelect() {
5151
return index;
5252
});
5353

54-
return { getNextEnabledOptionIndex, getPrevEnabledOptionIndex };
54+
const getActiveDescendant = $((index: number, options: Opt[], localId: string) => {
55+
const option = options[index];
56+
57+
if (index === -1 || option?.isDisabled) {
58+
return '';
59+
}
60+
61+
return `${localId}-${index}`;
62+
});
63+
64+
return { getNextEnabledOptionIndex, getPrevEnabledOptionIndex, getActiveDescendant };
5565
}
5666

5767
export function useTypeahead() {

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

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)