@@ -15,9 +15,9 @@
|
|||||||
value: any;
|
value: any;
|
||||||
orientation: "vertical" | "horizontal";
|
orientation: "vertical" | "horizontal";
|
||||||
|
|
||||||
labelRef: HTMLLabelElement | null;
|
labelRef: Writable<HTMLLabelElement | null>;
|
||||||
buttonRef: HTMLButtonElement | null;
|
buttonRef: Writable<HTMLButtonElement | null>;
|
||||||
optionsRef: HTMLDivElement | null;
|
optionsRef: Writable<HTMLElement | null>;
|
||||||
|
|
||||||
disabled: boolean;
|
disabled: boolean;
|
||||||
options: { id: string; dataRef: ListboxOptionDataRef }[];
|
options: { id: string; dataRef: ListboxOptionDataRef }[];
|
||||||
@@ -71,15 +71,9 @@
|
|||||||
const dispatch = createEventDispatcher();
|
const dispatch = createEventDispatcher();
|
||||||
|
|
||||||
let listboxState = ListboxStates.Closed;
|
let listboxState = ListboxStates.Closed;
|
||||||
let labelStore = writable(null);
|
let labelRef: StateDefinition["labelRef"] = writable(null);
|
||||||
setContext("labelStore", labelStore);
|
let buttonRef: StateDefinition["buttonRef"] = writable(null);
|
||||||
$: labelRef = $labelStore;
|
let optionsRef: StateDefinition["optionsRef"] = writable(null);
|
||||||
|
|
||||||
let buttonRef: Writable<StateDefinition["buttonRef"]> = writable(null);
|
|
||||||
setContext("buttonStore", buttonRef);
|
|
||||||
|
|
||||||
let optionsRef: Writable<StateDefinition["optionsRef"]> = writable(null);
|
|
||||||
setContext("optionsStore", optionsRef);
|
|
||||||
|
|
||||||
let options: StateDefinition["options"] = [];
|
let options: StateDefinition["options"] = [];
|
||||||
let searchQuery: StateDefinition["searchQuery"] = "";
|
let searchQuery: StateDefinition["searchQuery"] = "";
|
||||||
@@ -87,10 +81,10 @@
|
|||||||
|
|
||||||
let api: Writable<StateDefinition> = writable({
|
let api: Writable<StateDefinition> = writable({
|
||||||
listboxState,
|
listboxState,
|
||||||
labelRef,
|
|
||||||
value,
|
value,
|
||||||
buttonRef: $buttonRef,
|
labelRef,
|
||||||
optionsRef: $optionsRef,
|
buttonRef,
|
||||||
|
optionsRef,
|
||||||
options,
|
options,
|
||||||
searchQuery,
|
searchQuery,
|
||||||
activeOptionIndex,
|
activeOptionIndex,
|
||||||
@@ -190,10 +184,7 @@
|
|||||||
return {
|
return {
|
||||||
...obj,
|
...obj,
|
||||||
listboxState,
|
listboxState,
|
||||||
labelRef,
|
|
||||||
value,
|
value,
|
||||||
buttonRef: $buttonRef,
|
|
||||||
optionsRef: $optionsRef,
|
|
||||||
options,
|
options,
|
||||||
searchQuery,
|
searchQuery,
|
||||||
activeOptionIndex,
|
activeOptionIndex,
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { getContext, tick } from "svelte";
|
import { tick } from "svelte";
|
||||||
import { ListboxStates, useListboxContext } from "./Listbox.svelte";
|
import { ListboxStates, useListboxContext } from "./Listbox.svelte";
|
||||||
import { useId } from "$lib/hooks/use-id";
|
import { useId } from "$lib/hooks/use-id";
|
||||||
import { Keys } from "$lib/utils/keyboard";
|
import { Keys } from "$lib/utils/keyboard";
|
||||||
@@ -7,7 +7,8 @@
|
|||||||
|
|
||||||
let api = useListboxContext("ListboxButton");
|
let api = useListboxContext("ListboxButton");
|
||||||
let id = `headlessui-listbox-button-${useId()}`;
|
let id = `headlessui-listbox-button-${useId()}`;
|
||||||
let buttonStore: SvelteStore<HTMLButtonElement> = getContext("buttonStore");
|
let buttonRef = $api.buttonRef;
|
||||||
|
let optionsRef = $api.optionsRef;
|
||||||
|
|
||||||
async function handleKeyDown(event: KeyboardEvent) {
|
async function handleKeyDown(event: KeyboardEvent) {
|
||||||
switch (event.key) {
|
switch (event.key) {
|
||||||
@@ -18,7 +19,7 @@
|
|||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
$api.openListbox();
|
$api.openListbox();
|
||||||
await tick();
|
await tick();
|
||||||
$api.optionsRef?.focus({ preventScroll: true });
|
$optionsRef?.focus({ preventScroll: true });
|
||||||
if (!$api.value) $api.goToOption(Focus.First);
|
if (!$api.value) $api.goToOption(Focus.First);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -26,7 +27,7 @@
|
|||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
$api.openListbox();
|
$api.openListbox();
|
||||||
await tick();
|
await tick();
|
||||||
$api.optionsRef?.focus({ preventScroll: true });
|
$optionsRef?.focus({ preventScroll: true });
|
||||||
if (!$api.value) $api.goToOption(Focus.Last);
|
if (!$api.value) $api.goToOption(Focus.Last);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -48,20 +49,20 @@
|
|||||||
if ($api.listboxState === ListboxStates.Open) {
|
if ($api.listboxState === ListboxStates.Open) {
|
||||||
$api.closeListbox();
|
$api.closeListbox();
|
||||||
await tick();
|
await tick();
|
||||||
$api.buttonRef?.focus({ preventScroll: true });
|
$buttonRef?.focus({ preventScroll: true });
|
||||||
} else {
|
} else {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
$api.openListbox();
|
$api.openListbox();
|
||||||
await tick();
|
await tick();
|
||||||
$api.optionsRef?.focus({ preventScroll: true });
|
$optionsRef?.focus({ preventScroll: true });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$: propsWeControl = {
|
$: propsWeControl = {
|
||||||
id,
|
id,
|
||||||
"aria-haspopup": true,
|
"aria-haspopup": true,
|
||||||
"aria-controls": $api?.optionsRef?.id,
|
"aria-controls": $optionsRef?.id,
|
||||||
"aria-expanded": $api?.disabled
|
"aria-expanded": $api.disabled
|
||||||
? undefined
|
? undefined
|
||||||
: $api?.listboxState === ListboxStates.Open,
|
: $api?.listboxState === ListboxStates.Open,
|
||||||
"aria-labelledby": $api?.labelRef
|
"aria-labelledby": $api?.labelRef
|
||||||
@@ -74,7 +75,7 @@
|
|||||||
<button
|
<button
|
||||||
{...$$restProps}
|
{...$$restProps}
|
||||||
{...propsWeControl}
|
{...propsWeControl}
|
||||||
bind:this={$buttonStore}
|
bind:this={$buttonRef}
|
||||||
on:click={handleClick}
|
on:click={handleClick}
|
||||||
on:keydown={handleKeyDown}
|
on:keydown={handleKeyDown}
|
||||||
on:keyup={handleKeyUp}
|
on:keyup={handleKeyUp}
|
||||||
|
|||||||
@@ -1,18 +1,19 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { getContext } from "svelte";
|
|
||||||
import { ListboxStates, useListboxContext } from "./Listbox.svelte";
|
import { ListboxStates, useListboxContext } from "./Listbox.svelte";
|
||||||
import { useId } from "$lib/hooks/use-id";
|
import { useId } from "$lib/hooks/use-id";
|
||||||
let api = useListboxContext("ListboxLabel");
|
|
||||||
let id = `headlessui-listbox-label-${useId()}`;
|
let id = `headlessui-listbox-label-${useId()}`;
|
||||||
let labelStore: SvelteStore<HTMLLabelElement> = getContext("labelStore");
|
let api = useListboxContext("ListboxLabel");
|
||||||
|
|
||||||
function handleClick() {
|
let labelRef = $api.labelRef;
|
||||||
$api.buttonRef?.focus({ preventScroll: true });
|
let buttonRef = $api.buttonRef;
|
||||||
|
|
||||||
|
function handleClick(): void {
|
||||||
|
$buttonRef?.focus({ preventScroll: true });
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<!-- svelte-ignore a11y-label-has-associated-control -->
|
<!-- svelte-ignore a11y-label-has-associated-control -->
|
||||||
<label {...$$restProps} {id} bind:this={$labelStore} on:click={handleClick}>
|
<label {...$$restProps} {id} bind:this={$labelRef} on:click={handleClick}>
|
||||||
<slot
|
<slot
|
||||||
open={$api.listboxState === ListboxStates.Open}
|
open={$api.listboxState === ListboxStates.Open}
|
||||||
disabled={$api.disabled}
|
disabled={$api.disabled}
|
||||||
|
|||||||
@@ -8,6 +8,8 @@
|
|||||||
let api = useListboxContext("ListboxOption");
|
let api = useListboxContext("ListboxOption");
|
||||||
let id = `headlessui-listbox-option-${useId()}`;
|
let id = `headlessui-listbox-option-${useId()}`;
|
||||||
|
|
||||||
|
let buttonRef = $api.buttonRef;
|
||||||
|
|
||||||
$: active =
|
$: active =
|
||||||
$api?.activeOptionIndex !== null
|
$api?.activeOptionIndex !== null
|
||||||
? $api?.options[$api.activeOptionIndex].id === id
|
? $api?.options[$api.activeOptionIndex].id === id
|
||||||
@@ -62,7 +64,7 @@
|
|||||||
$api.select(value);
|
$api.select(value);
|
||||||
$api.closeListbox();
|
$api.closeListbox();
|
||||||
await tick();
|
await tick();
|
||||||
$api.buttonRef?.focus({ preventScroll: true });
|
$buttonRef?.focus({ preventScroll: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleFocus() {
|
function handleFocus() {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { getContext, tick } from "svelte";
|
import { tick } from "svelte";
|
||||||
import { ListboxStates, useListboxContext } from "./Listbox.svelte";
|
import { ListboxStates, useListboxContext } from "./Listbox.svelte";
|
||||||
import { useId } from "$lib/hooks/use-id";
|
import { useId } from "$lib/hooks/use-id";
|
||||||
import { match } from "$lib/utils/match";
|
import { match } from "$lib/utils/match";
|
||||||
@@ -9,7 +9,9 @@
|
|||||||
|
|
||||||
let api = useListboxContext("ListboxOptions");
|
let api = useListboxContext("ListboxOptions");
|
||||||
let id = `headlessui-listbox-options-${useId()}`;
|
let id = `headlessui-listbox-options-${useId()}`;
|
||||||
let optionsStore: SvelteStore<HTMLUListElement> = getContext("optionsStore");
|
let optionsRef = $api.optionsRef;
|
||||||
|
let buttonRef = $api.buttonRef;
|
||||||
|
let labelRef = $api.labelRef;
|
||||||
|
|
||||||
let searchDebounce: ReturnType<typeof setTimeout> | null = null;
|
let searchDebounce: ReturnType<typeof setTimeout> | null = null;
|
||||||
async function handleKeyDown(event: KeyboardEvent) {
|
async function handleKeyDown(event: KeyboardEvent) {
|
||||||
@@ -34,7 +36,7 @@
|
|||||||
}
|
}
|
||||||
$api.closeListbox();
|
$api.closeListbox();
|
||||||
await tick();
|
await tick();
|
||||||
$api.buttonRef?.focus({ preventScroll: true });
|
$buttonRef?.focus({ preventScroll: true });
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case match($api.orientation, {
|
case match($api.orientation, {
|
||||||
@@ -70,7 +72,7 @@
|
|||||||
event.stopPropagation();
|
event.stopPropagation();
|
||||||
$api.closeListbox();
|
$api.closeListbox();
|
||||||
await tick();
|
await tick();
|
||||||
$api.buttonRef?.focus({ preventScroll: true });
|
$buttonRef?.focus({ preventScroll: true });
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Keys.Tab:
|
case Keys.Tab:
|
||||||
@@ -92,7 +94,7 @@
|
|||||||
$api?.activeOptionIndex === null
|
$api?.activeOptionIndex === null
|
||||||
? undefined
|
? undefined
|
||||||
: $api?.options[$api.activeOptionIndex]?.id,
|
: $api?.options[$api.activeOptionIndex]?.id,
|
||||||
"aria-labelledby": $api?.labelRef?.id ?? $api?.buttonRef?.id,
|
"aria-labelledby": $labelRef?.id ?? $buttonRef?.id,
|
||||||
"aria-orientation": $api?.orientation,
|
"aria-orientation": $api?.orientation,
|
||||||
id,
|
id,
|
||||||
role: "listbox",
|
role: "listbox",
|
||||||
@@ -108,7 +110,7 @@
|
|||||||
|
|
||||||
{#if visible}
|
{#if visible}
|
||||||
<ul
|
<ul
|
||||||
bind:this={$optionsStore}
|
bind:this={$optionsRef}
|
||||||
on:keydown={handleKeyDown}
|
on:keydown={handleKeyDown}
|
||||||
{...$$restProps}
|
{...$$restProps}
|
||||||
{...propsWeControl}
|
{...propsWeControl}
|
||||||
|
|||||||
Reference in New Issue
Block a user