Add <Render> usage to <Listbox>
This commit is contained in:
@@ -61,6 +61,17 @@
|
|||||||
import { Readable, writable, Writable } from "svelte/store";
|
import { Readable, writable, Writable } from "svelte/store";
|
||||||
import { match } from "$lib/utils/match";
|
import { match } from "$lib/utils/match";
|
||||||
import { State, useOpenClosedProvider } from "$lib/internal/open-closed";
|
import { State, useOpenClosedProvider } from "$lib/internal/open-closed";
|
||||||
|
import { forwardEventsBuilder } from "$lib/internal/forwardEventsBuilder";
|
||||||
|
import { get_current_component } from "svelte/internal";
|
||||||
|
import type { SupportedAs } from "$lib/internal/elements";
|
||||||
|
import type { HTMLActionArray } from "$lib/hooks/use-actions";
|
||||||
|
import Render from "$lib/utils/Render.svelte";
|
||||||
|
const forwardEvents = forwardEventsBuilder(get_current_component(), [
|
||||||
|
"change",
|
||||||
|
]);
|
||||||
|
export let as: SupportedAs = "div";
|
||||||
|
export let use: HTMLActionArray = [];
|
||||||
|
|
||||||
export let disabled = false;
|
export let disabled = false;
|
||||||
export let horizontal = false;
|
export let horizontal = false;
|
||||||
export let value: StateDefinition["value"];
|
export let value: StateDefinition["value"];
|
||||||
@@ -205,7 +216,16 @@
|
|||||||
$buttonRef?.focus({ preventScroll: true });
|
$buttonRef?.focus({ preventScroll: true });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
$: slot = { open: listboxState === ListboxStates.Open };
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<svelte:window on:mousedown={handleMousedown} />
|
<svelte:window on:mousedown={handleMousedown} />
|
||||||
<slot open={listboxState === ListboxStates.Open} />
|
<Render
|
||||||
|
{...$$restProps}
|
||||||
|
{as}
|
||||||
|
{slot}
|
||||||
|
use={[...use, forwardEvents]}
|
||||||
|
name={"Listbox"}
|
||||||
|
>
|
||||||
|
<slot {...slot} />
|
||||||
|
</Render>
|
||||||
|
|||||||
@@ -4,6 +4,15 @@
|
|||||||
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";
|
||||||
import { Focus } from "$lib/utils/calculate-active-index";
|
import { Focus } from "$lib/utils/calculate-active-index";
|
||||||
|
import { forwardEventsBuilder } from "$lib/internal/forwardEventsBuilder";
|
||||||
|
import type { SupportedAs } from "$lib/internal/elements";
|
||||||
|
import type { HTMLActionArray } from "$lib/hooks/use-actions";
|
||||||
|
import { get_current_component } from "svelte/internal";
|
||||||
|
import Render from "$lib/utils/Render.svelte";
|
||||||
|
|
||||||
|
const forwardEvents = forwardEventsBuilder(get_current_component());
|
||||||
|
export let as: SupportedAs = "button";
|
||||||
|
export let use: HTMLActionArray = [];
|
||||||
|
|
||||||
let api = useListboxContext("ListboxButton");
|
let api = useListboxContext("ListboxButton");
|
||||||
let id = `headlessui-listbox-button-${useId()}`;
|
let id = `headlessui-listbox-button-${useId()}`;
|
||||||
@@ -11,7 +20,8 @@
|
|||||||
let optionsRef = $api.optionsRef;
|
let optionsRef = $api.optionsRef;
|
||||||
let labelRef = $api.labelRef;
|
let labelRef = $api.labelRef;
|
||||||
|
|
||||||
async function handleKeyDown(event: KeyboardEvent) {
|
async function handleKeyDown(e: CustomEvent) {
|
||||||
|
let event = e as any as KeyboardEvent;
|
||||||
switch (event.key) {
|
switch (event.key) {
|
||||||
// Ref: https://www.w3.org/TR/wai-aria-practices-1.2/#keyboard-interaction-13
|
// Ref: https://www.w3.org/TR/wai-aria-practices-1.2/#keyboard-interaction-13
|
||||||
case Keys.Space:
|
case Keys.Space:
|
||||||
@@ -34,7 +44,8 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleKeyUp(event: KeyboardEvent) {
|
function handleKeyUp(e: CustomEvent) {
|
||||||
|
let event = e as any as KeyboardEvent;
|
||||||
switch (event.key) {
|
switch (event.key) {
|
||||||
case Keys.Space:
|
case Keys.Space:
|
||||||
// Required for firefox, event.preventDefault() in handleKeyDown for
|
// Required for firefox, event.preventDefault() in handleKeyDown for
|
||||||
@@ -45,7 +56,8 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleClick(event: MouseEvent) {
|
async function handleClick(e: CustomEvent) {
|
||||||
|
let event = e as any as MouseEvent;
|
||||||
if ($api.disabled) return;
|
if ($api.disabled) return;
|
||||||
if ($api.listboxState === ListboxStates.Open) {
|
if ($api.listboxState === ListboxStates.Open) {
|
||||||
$api.closeListbox();
|
$api.closeListbox();
|
||||||
@@ -69,18 +81,24 @@
|
|||||||
"aria-labelledby": $labelRef ? [$labelRef?.id, id].join(" ") : undefined,
|
"aria-labelledby": $labelRef ? [$labelRef?.id, id].join(" ") : undefined,
|
||||||
disabled: $api.disabled === true ? true : undefined,
|
disabled: $api.disabled === true ? true : undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
$: slot = {
|
||||||
|
open: $api.listboxState === ListboxStates.Open,
|
||||||
|
disabled: $api.disabled,
|
||||||
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<button
|
<Render
|
||||||
{...$$restProps}
|
{...$$restProps}
|
||||||
{...propsWeControl}
|
{...propsWeControl}
|
||||||
bind:this={$buttonRef}
|
{as}
|
||||||
|
{slot}
|
||||||
|
use={[...use, forwardEvents]}
|
||||||
|
name={"ListboxButton"}
|
||||||
|
bind:el={$buttonRef}
|
||||||
on:click={handleClick}
|
on:click={handleClick}
|
||||||
on:keydown={handleKeyDown}
|
on:keydown={handleKeyDown}
|
||||||
on:keyup={handleKeyUp}
|
on:keyup={handleKeyUp}
|
||||||
>
|
>
|
||||||
<slot
|
<slot {...slot} />
|
||||||
open={$api.listboxState === ListboxStates.Open}
|
</Render>
|
||||||
disabled={$api.disabled}
|
|
||||||
/>
|
|
||||||
</button>
|
|
||||||
|
|||||||
@@ -1,6 +1,16 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
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 Render from "$lib/utils/Render.svelte";
|
||||||
|
import { forwardEventsBuilder } from "$lib/internal/forwardEventsBuilder";
|
||||||
|
import type { SupportedAs } from "$lib/internal/elements";
|
||||||
|
import type { HTMLActionArray } from "$lib/hooks/use-actions";
|
||||||
|
import { get_current_component } from "svelte/internal";
|
||||||
|
|
||||||
|
const forwardEvents = forwardEventsBuilder(get_current_component());
|
||||||
|
export let as: SupportedAs = "label";
|
||||||
|
export let use: HTMLActionArray = [];
|
||||||
|
|
||||||
let id = `headlessui-listbox-label-${useId()}`;
|
let id = `headlessui-listbox-label-${useId()}`;
|
||||||
let api = useListboxContext("ListboxLabel");
|
let api = useListboxContext("ListboxLabel");
|
||||||
|
|
||||||
@@ -10,12 +20,22 @@
|
|||||||
function handleClick(): void {
|
function handleClick(): void {
|
||||||
$buttonRef?.focus({ preventScroll: true });
|
$buttonRef?.focus({ preventScroll: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$: slot = {
|
||||||
|
open: $api.listboxState === ListboxStates.Open,
|
||||||
|
disabled: $api.disabled,
|
||||||
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<!-- svelte-ignore a11y-label-has-associated-control -->
|
<Render
|
||||||
<label {...$$restProps} {id} bind:this={$labelRef} on:click={handleClick}>
|
{...$$restProps}
|
||||||
<slot
|
{id}
|
||||||
open={$api.listboxState === ListboxStates.Open}
|
{as}
|
||||||
disabled={$api.disabled}
|
{slot}
|
||||||
/>
|
use={[...use, forwardEvents]}
|
||||||
</label>
|
name={"ListboxLabel"}
|
||||||
|
bind:el={$labelRef}
|
||||||
|
on:click={handleClick}
|
||||||
|
>
|
||||||
|
<slot {...slot} />
|
||||||
|
</Render>
|
||||||
|
|||||||
@@ -3,6 +3,16 @@
|
|||||||
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 { Focus } from "$lib/utils/calculate-active-index";
|
import { Focus } from "$lib/utils/calculate-active-index";
|
||||||
|
import Render from "$lib/utils/Render.svelte";
|
||||||
|
import { forwardEventsBuilder } from "$lib/internal/forwardEventsBuilder";
|
||||||
|
import type { SupportedAs } from "$lib/internal/elements";
|
||||||
|
import { get_current_component } from "svelte/internal";
|
||||||
|
import type { HTMLActionArray } from "$lib/hooks/use-actions";
|
||||||
|
|
||||||
|
const forwardEvents = forwardEventsBuilder(get_current_component());
|
||||||
|
export let as: SupportedAs = "li";
|
||||||
|
export let use: HTMLActionArray = [];
|
||||||
|
|
||||||
export let value: unknown;
|
export let value: unknown;
|
||||||
export let disabled = false;
|
export let disabled = false;
|
||||||
let api = useListboxContext("ListboxOption");
|
let api = useListboxContext("ListboxOption");
|
||||||
@@ -59,7 +69,8 @@
|
|||||||
}
|
}
|
||||||
$: updateFocus($api.listboxState, selected, active);
|
$: updateFocus($api.listboxState, selected, active);
|
||||||
|
|
||||||
async function handleClick(event: MouseEvent) {
|
async function handleClick(e: CustomEvent) {
|
||||||
|
let event = e as any as MouseEvent;
|
||||||
if (disabled) return event.preventDefault();
|
if (disabled) return event.preventDefault();
|
||||||
$api.select(value);
|
$api.select(value);
|
||||||
$api.closeListbox();
|
$api.closeListbox();
|
||||||
@@ -92,17 +103,16 @@
|
|||||||
"aria-selected": selected === true ? selected : undefined,
|
"aria-selected": selected === true ? selected : undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
$: classStyle = $$props.class
|
$: slot = { active, selected, disabled };
|
||||||
? typeof $$props.class === "function"
|
|
||||||
? $$props.class({ active, selected, disabled })
|
|
||||||
: $$props.class
|
|
||||||
: "";
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<li
|
<Render
|
||||||
{...$$restProps}
|
{...$$restProps}
|
||||||
class={classStyle}
|
|
||||||
{...propsWeControl}
|
{...propsWeControl}
|
||||||
|
{as}
|
||||||
|
{slot}
|
||||||
|
use={[...use, forwardEvents]}
|
||||||
|
name={"ListboxOption"}
|
||||||
on:click={handleClick}
|
on:click={handleClick}
|
||||||
on:focus={handleFocus}
|
on:focus={handleFocus}
|
||||||
on:pointermove={handleMove}
|
on:pointermove={handleMove}
|
||||||
@@ -110,5 +120,5 @@
|
|||||||
on:pointerleave={handleLeave}
|
on:pointerleave={handleLeave}
|
||||||
on:mouseleave={handleLeave}
|
on:mouseleave={handleLeave}
|
||||||
>
|
>
|
||||||
<slot {active} {selected} {disabled} />
|
<slot {...slot} />
|
||||||
</li>
|
</Render>
|
||||||
|
|||||||
@@ -6,6 +6,15 @@
|
|||||||
import { Keys } from "$lib/utils/keyboard";
|
import { Keys } from "$lib/utils/keyboard";
|
||||||
import { Focus } from "$lib/utils/calculate-active-index";
|
import { Focus } from "$lib/utils/calculate-active-index";
|
||||||
import { State, useOpenClosed } from "$lib/internal/open-closed";
|
import { State, useOpenClosed } from "$lib/internal/open-closed";
|
||||||
|
import Render from "$lib/utils/Render.svelte";
|
||||||
|
import { forwardEventsBuilder } from "$lib/internal/forwardEventsBuilder";
|
||||||
|
import type { SupportedAs } from "$lib/internal/elements";
|
||||||
|
import type { HTMLActionArray } from "$lib/hooks/use-actions";
|
||||||
|
import { get_current_component } from "svelte/internal";
|
||||||
|
|
||||||
|
const forwardEvents = forwardEventsBuilder(get_current_component());
|
||||||
|
export let as: SupportedAs = "ul";
|
||||||
|
export let use: HTMLActionArray = [];
|
||||||
|
|
||||||
let api = useListboxContext("ListboxOptions");
|
let api = useListboxContext("ListboxOptions");
|
||||||
let id = `headlessui-listbox-options-${useId()}`;
|
let id = `headlessui-listbox-options-${useId()}`;
|
||||||
@@ -14,7 +23,8 @@
|
|||||||
let labelRef = $api.labelRef;
|
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(e: CustomEvent) {
|
||||||
|
let event = e as any as KeyboardEvent;
|
||||||
if (searchDebounce) clearTimeout(searchDebounce);
|
if (searchDebounce) clearTimeout(searchDebounce);
|
||||||
|
|
||||||
switch (event.key) {
|
switch (event.key) {
|
||||||
@@ -106,15 +116,21 @@
|
|||||||
usesOpenClosedState !== undefined
|
usesOpenClosedState !== undefined
|
||||||
? $usesOpenClosedState === State.Open
|
? $usesOpenClosedState === State.Open
|
||||||
: $api.listboxState === ListboxStates.Open;
|
: $api.listboxState === ListboxStates.Open;
|
||||||
|
|
||||||
|
$: slot = { open: $api.listboxState === ListboxStates.Open };
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{#if visible}
|
{#if visible}
|
||||||
<ul
|
<Render
|
||||||
bind:this={$optionsRef}
|
|
||||||
on:keydown={handleKeyDown}
|
|
||||||
{...$$restProps}
|
{...$$restProps}
|
||||||
{...propsWeControl}
|
{...propsWeControl}
|
||||||
|
{as}
|
||||||
|
{slot}
|
||||||
|
use={[...use, forwardEvents]}
|
||||||
|
name={"ListboxOptions"}
|
||||||
|
bind:el={$optionsRef}
|
||||||
|
on:keydown={handleKeyDown}
|
||||||
>
|
>
|
||||||
<slot open={$api.listboxState === ListboxStates.Open} />
|
<slot {...slot} />
|
||||||
</ul>
|
</Render>
|
||||||
{/if}
|
{/if}
|
||||||
|
|||||||
Reference in New Issue
Block a user