Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/purple-games-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@qwik-ui/headless': patch
---

fix: select can now be reactively disabled
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
width: 100%;
height: 100%;
border: 2px dotted hsla(var(--primary) / 1);
border-radius: calc(var (--border-radius) / 2);
min-height: 44px;
max-width: var(--select-width);
padding-block: 0.5rem;
Expand Down Expand Up @@ -39,7 +38,6 @@
background-color: hsl(var(--background));
padding: 0.5rem;
border: 2px dotted hsla(var(--foreground) / 0.6);
border-radius: calc(var(--border-radius) / 2);
max-width: var(--select-width);
color: hsl(var(--foreground));
}
Expand Down Expand Up @@ -93,7 +91,6 @@
[data-highlighted] {
background-color: hsla(var(--primary) / 0.08);
outline: 2px dotted hsla(var(--primary) / 1);
border-radius: calc(var(--border-radius) / 2);
}

[data-disabled] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const HHiddenNativeSelect = component$(
multiple={context.multiple}
tabIndex={-1}
autocomplete={autoComplete}
disabled={context.disabled}
disabled={context.isDisabledSig.value ? true : undefined}
required={context.required}
name={context.name}
// height is determined by its children
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type SelectContext = {
highlightedIndexSig: Signal<number | null>;
currDisplayValueSig: Signal<string | string[] | undefined>;
isListboxOpenSig: Signal<boolean>;
isDisabledSig: Signal<boolean>;
localId: string;

// user configurable
Expand All @@ -38,11 +39,6 @@ export type SelectContext = {
* Specifies that the user must select a value before submitting the form. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select#required).
*/
required?: boolean;

/**
* If `true`, prevents the user from interacting with the select.
*/
disabled?: boolean;
};

export const groupContextId = createContextId<GroupContext>('Select-Group');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ export const HSelectDescription = component$((props: SelectDescriptionProps) =>
const descriptionId = `${context.localId}-description`;

return (
<div id={descriptionId} data-disabled={context.disabled ? '' : undefined} {...props}>
<div
id={descriptionId}
data-disabled={context.isDisabledSig.value ? '' : undefined}
{...props}
>
<Slot />
</div>
);
Expand Down
4 changes: 2 additions & 2 deletions packages/kit-headless/src/components/select/select-label.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const HSelectLabel = component$((props: PropsOf<'div'>) => {
const labelId = `${context.localId}-label`;

const handleClick$ = $(() => {
if (context.disabled) return;
if (context.isDisabledSig.value) return;

context.triggerRef.value?.focus();
});
Expand All @@ -17,7 +17,7 @@ export const HSelectLabel = component$((props: PropsOf<'div'>) => {

return (
<div
data-disabled={context.disabled ? '' : undefined}
data-disabled={context.isDisabledSig.value ? '' : undefined}
ref={context.labelRef}
id={labelId}
onClick$={[handleClick$, props.onClick$]}
Expand Down
7 changes: 4 additions & 3 deletions packages/kit-headless/src/components/select/select-root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ export const HSelectImpl = component$<SelectProps<boolean> & InternalSelectProps

const initialLoadSig = useSignal<boolean>(true);
const highlightedItemRef = useSignal<HTMLLIElement>();
const isDisabledSig = useSignal<boolean>(disabled ?? false);

const context: SelectContext = {
itemsMapSig,
Expand All @@ -181,7 +182,7 @@ export const HSelectImpl = component$<SelectProps<boolean> & InternalSelectProps
multiple,
name,
required,
disabled,
isDisabledSig,
};

useContextProvider(SelectContextId, context);
Expand Down Expand Up @@ -281,7 +282,7 @@ export const HSelectImpl = component$<SelectProps<boolean> & InternalSelectProps
});

useTask$(({ track }) => {
context.disabled = track(() => disabled);
isDisabledSig.value = track(() => disabled ?? false);
});

return (
Expand All @@ -290,7 +291,7 @@ export const HSelectImpl = component$<SelectProps<boolean> & InternalSelectProps
ref={rootRef}
data-open={context.isListboxOpenSig.value ? '' : undefined}
data-closed={!context.isListboxOpenSig.value ? '' : undefined}
data-disabled={context.disabled ? '' : undefined}
data-disabled={isDisabledSig.value ? '' : undefined}
aria-controls={listboxId}
aria-expanded={context.isListboxOpenSig.value}
aria-haspopup="listbox"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ export const HSelectTrigger = component$<SelectTriggerProps>((props) => {
onKeyDown$={[handleKeyDownSync$, handleKeyDown$, props.onKeyDown$]}
data-open={context.isListboxOpenSig.value ? '' : undefined}
data-closed={!context.isListboxOpenSig.value ? '' : undefined}
data-disabled={context.disabled ? '' : undefined}
data-disabled={context.isDisabledSig.value ? '' : undefined}
aria-expanded={context.isListboxOpenSig.value}
aria-labelledby={labelId}
aria-describedby={descriptionId}
disabled={context.disabled}
disabled={context.isDisabledSig.value ? true : undefined}
preventdefault:blur
>
<Slot />
Expand Down