Convert Menu to <Render>
This commit is contained in:
@@ -7,7 +7,12 @@
|
|||||||
import { Readable, writable, Writable } from "svelte/store";
|
import { Readable, writable, Writable } from "svelte/store";
|
||||||
import { State, useOpenClosedProvider } from "$lib/internal/open-closed";
|
import { State, useOpenClosedProvider } from "$lib/internal/open-closed";
|
||||||
import { match } from "$lib/utils/match";
|
import { match } from "$lib/utils/match";
|
||||||
import { ActionArray, useActions } from "$lib/hooks/use-actions";
|
import type { HTMLActionArray } from "$lib/hooks/use-actions";
|
||||||
|
import type { SupportedAs } from "$lib/internal/elements";
|
||||||
|
import { forwardEventsBuilder } from "$lib/internal/forwardEventsBuilder";
|
||||||
|
import { get_current_component } from "svelte/internal";
|
||||||
|
import Render from "$lib/utils/Render.svelte";
|
||||||
|
|
||||||
export enum MenuStates {
|
export enum MenuStates {
|
||||||
Open,
|
Open,
|
||||||
Closed,
|
Closed,
|
||||||
@@ -50,7 +55,9 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
export let use: ActionArray = [];
|
export let use: HTMLActionArray = [];
|
||||||
|
export let as: SupportedAs = "div";
|
||||||
|
const forwardEvents = forwardEventsBuilder(get_current_component());
|
||||||
let menuState: StateDefinition["menuState"] = MenuStates.Closed;
|
let menuState: StateDefinition["menuState"] = MenuStates.Closed;
|
||||||
let buttonStore: StateDefinition["buttonStore"] = writable(null);
|
let buttonStore: StateDefinition["buttonStore"] = writable(null);
|
||||||
let itemsStore: StateDefinition["itemsStore"] = writable(null);
|
let itemsStore: StateDefinition["itemsStore"] = writable(null);
|
||||||
@@ -158,6 +165,6 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<svelte:window on:mousedown={handleWindowMousedown} />
|
<svelte:window on:mousedown={handleWindowMousedown} />
|
||||||
<div use:useActions={use} {...$$restProps}>
|
<Render {...$$restProps} use={[...use, forwardEvents]} {as} name={"Menu"}>
|
||||||
<slot />
|
<slot />
|
||||||
</div>
|
</Render>
|
||||||
|
|||||||
@@ -4,15 +4,23 @@
|
|||||||
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 { tick } from "svelte";
|
import { tick } from "svelte";
|
||||||
import { ActionArray, useActions } from "$lib/hooks/use-actions";
|
import type { HTMLActionArray } from "$lib/hooks/use-actions";
|
||||||
export let use: ActionArray = [];
|
import Render from "$lib/utils/Render.svelte";
|
||||||
|
import type { SupportedAs } from "$lib/internal/elements";
|
||||||
|
import { forwardEventsBuilder } from "$lib/internal/forwardEventsBuilder";
|
||||||
|
import { get_current_component } from "svelte/internal";
|
||||||
|
const forwardEvents = forwardEventsBuilder(get_current_component());
|
||||||
|
export let as: SupportedAs = "button";
|
||||||
|
export let use: HTMLActionArray = [];
|
||||||
|
|
||||||
export let disabled = false;
|
export let disabled = false;
|
||||||
const api = useMenuContext("MenuButton");
|
const api = useMenuContext("MenuButton");
|
||||||
const id = `headlessui-menu-button-${useId()}`;
|
const id = `headlessui-menu-button-${useId()}`;
|
||||||
|
|
||||||
$: buttonStore = $api.buttonStore;
|
$: buttonStore = $api.buttonStore;
|
||||||
$: itemsStore = $api.itemsStore;
|
$: itemsStore = $api.itemsStore;
|
||||||
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
|
||||||
|
|
||||||
@@ -38,7 +46,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
|
||||||
@@ -49,7 +58,8 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleClick(event: MouseEvent) {
|
async function handleClick(e: CustomEvent) {
|
||||||
|
let event = e as any as MouseEvent;
|
||||||
if (disabled) return;
|
if (disabled) return;
|
||||||
if ($api.menuState === MenuStates.Open) {
|
if ($api.menuState === MenuStates.Open) {
|
||||||
$api.closeMenu();
|
$api.closeMenu();
|
||||||
@@ -70,15 +80,20 @@
|
|||||||
"aria-controls": $itemsStore?.id,
|
"aria-controls": $itemsStore?.id,
|
||||||
"aria-expanded": disabled ? undefined : $api.menuState === MenuStates.Open,
|
"aria-expanded": disabled ? undefined : $api.menuState === MenuStates.Open,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
$: slot = { open: $api.menuState === MenuStates.Open };
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<button
|
<Render
|
||||||
{...{ ...$$restProps, ...propsWeControl }}
|
{...{ ...$$restProps, ...propsWeControl }}
|
||||||
bind:this={$buttonStore}
|
{as}
|
||||||
use:useActions={use}
|
{slot}
|
||||||
|
use={[...use, forwardEvents]}
|
||||||
|
name={"MenuButton"}
|
||||||
|
bind:el={$buttonStore}
|
||||||
on:click={handleClick}
|
on:click={handleClick}
|
||||||
on:keydown={handleKeyDown}
|
on:keydown={handleKeyDown}
|
||||||
on:keyup={handleKeyUp}
|
on:keyup={handleKeyUp}
|
||||||
>
|
>
|
||||||
<slot open={$api.menuState === MenuStates.Open} />
|
<slot {...slot} />
|
||||||
</button>
|
</Render>
|
||||||
|
|||||||
@@ -3,8 +3,14 @@
|
|||||||
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 { afterUpdate, onDestroy, onMount, tick } from "svelte";
|
import { afterUpdate, onDestroy, onMount, tick } from "svelte";
|
||||||
import { ActionArray, useActions } from "$lib/hooks/use-actions";
|
import Render from "$lib/utils/Render.svelte";
|
||||||
export let use: ActionArray = [];
|
import type { SupportedAs } from "$lib/internal/elements";
|
||||||
|
import { forwardEventsBuilder } from "$lib/internal/forwardEventsBuilder";
|
||||||
|
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 = "a";
|
||||||
|
export let use: HTMLActionArray = [];
|
||||||
export let disabled = false;
|
export let disabled = false;
|
||||||
const api = useMenuContext("MenuItem");
|
const api = useMenuContext("MenuItem");
|
||||||
const id = `headlessui-menu-item-${useId()}`;
|
const id = `headlessui-menu-item-${useId()}`;
|
||||||
@@ -16,7 +22,7 @@
|
|||||||
|
|
||||||
$: buttonStore = $api.buttonStore;
|
$: buttonStore = $api.buttonStore;
|
||||||
|
|
||||||
let elementRef: HTMLDivElement | undefined;
|
let elementRef: HTMLElement | undefined;
|
||||||
$: textValue = elementRef?.textContent?.toLowerCase().trim();
|
$: textValue = elementRef?.textContent?.toLowerCase().trim();
|
||||||
$: data = { disabled, textValue } as MenuItemData;
|
$: data = { disabled, textValue } as MenuItemData;
|
||||||
|
|
||||||
@@ -36,7 +42,7 @@
|
|||||||
elementRef?.scrollIntoView?.({ block: "nearest" });
|
elementRef?.scrollIntoView?.({ block: "nearest" });
|
||||||
});
|
});
|
||||||
|
|
||||||
async function handleClick(event: MouseEvent) {
|
async function handleClick(event: CustomEvent) {
|
||||||
if (disabled) return event.preventDefault();
|
if (disabled) return event.preventDefault();
|
||||||
$api.closeMenu();
|
$api.closeMenu();
|
||||||
$buttonStore?.focus({ preventScroll: true });
|
$buttonStore?.focus({ preventScroll: true });
|
||||||
@@ -65,12 +71,17 @@
|
|||||||
tabIndex: disabled === true ? undefined : -1,
|
tabIndex: disabled === true ? undefined : -1,
|
||||||
"aria-disabled": disabled === true ? true : undefined,
|
"aria-disabled": disabled === true ? true : undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
$: slot = { active, disabled };
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div
|
<Render
|
||||||
{...{ ...$$restProps, ...propsWeControl }}
|
{...{ ...$$restProps, ...propsWeControl }}
|
||||||
bind:this={elementRef}
|
use={[...use, forwardEvents]}
|
||||||
use:useActions={use}
|
{as}
|
||||||
|
{slot}
|
||||||
|
name={"MenuItem"}
|
||||||
|
bind:el={elementRef}
|
||||||
on:click={handleClick}
|
on:click={handleClick}
|
||||||
on:focus={handleFocus}
|
on:focus={handleFocus}
|
||||||
on:pointermove={handleMove}
|
on:pointermove={handleMove}
|
||||||
@@ -78,5 +89,5 @@
|
|||||||
on:pointerleave={handleLeave}
|
on:pointerleave={handleLeave}
|
||||||
on:mouseleave={handleLeave}
|
on:mouseleave={handleLeave}
|
||||||
>
|
>
|
||||||
<slot {active} {disabled} />
|
<slot {...slot} />
|
||||||
</div>
|
</Render>
|
||||||
|
|||||||
@@ -6,8 +6,15 @@
|
|||||||
import { treeWalker } from "$lib/hooks/use-tree-walker";
|
import { treeWalker } from "$lib/hooks/use-tree-walker";
|
||||||
import { State, useOpenClosed } from "$lib/internal/open-closed";
|
import { State, useOpenClosed } from "$lib/internal/open-closed";
|
||||||
import { tick } from "svelte";
|
import { tick } from "svelte";
|
||||||
import { ActionArray, useActions } from "$lib/hooks/use-actions";
|
import type { HTMLActionArray } from "$lib/hooks/use-actions";
|
||||||
export let use: ActionArray = [];
|
import type { SupportedAs } from "$lib/internal/elements";
|
||||||
|
import Render from "$lib/utils/Render.svelte";
|
||||||
|
import { forwardEventsBuilder } from "$lib/internal/forwardEventsBuilder";
|
||||||
|
import { get_current_component } from "svelte/internal";
|
||||||
|
|
||||||
|
const forwardEvents = forwardEventsBuilder(get_current_component());
|
||||||
|
export let as: SupportedAs = "div";
|
||||||
|
export let use: HTMLActionArray = [];
|
||||||
const api = useMenuContext("MenuButton");
|
const api = useMenuContext("MenuButton");
|
||||||
const id = `headlessui-menu-items-${useId()}`;
|
const id = `headlessui-menu-items-${useId()}`;
|
||||||
let searchDebounce: ReturnType<typeof setTimeout> | null = null;
|
let searchDebounce: ReturnType<typeof setTimeout> | null = null;
|
||||||
@@ -36,8 +43,9 @@
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
async function handleKeyDown(event: KeyboardEvent) {
|
async function handleKeyDown(e: CustomEvent) {
|
||||||
if (searchDebounce) clearTimeout(searchDebounce);
|
if (searchDebounce) clearTimeout(searchDebounce);
|
||||||
|
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-12
|
// Ref: https://www.w3.org/TR/wai-aria-practices-1.2/#keyboard-interaction-12
|
||||||
@@ -105,7 +113,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
|
||||||
@@ -126,16 +135,20 @@
|
|||||||
role: "menu",
|
role: "menu",
|
||||||
tabIndex: 0,
|
tabIndex: 0,
|
||||||
};
|
};
|
||||||
|
$: slot = { open: $api.menuState === MenuStates.Open };
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{#if visible}
|
{#if visible}
|
||||||
<div
|
<Render
|
||||||
{...{ ...$$restProps, ...propsWeControl }}
|
{...{ ...$$restProps, ...propsWeControl }}
|
||||||
bind:this={$itemsStore}
|
{as}
|
||||||
use:useActions={use}
|
{slot}
|
||||||
|
use={[...use, forwardEvents]}
|
||||||
|
bind:el={$itemsStore}
|
||||||
|
name={"MenuItems"}
|
||||||
on:keydown={handleKeyDown}
|
on:keydown={handleKeyDown}
|
||||||
on:keyup={handleKeyUp}
|
on:keyup={handleKeyUp}
|
||||||
>
|
>
|
||||||
<slot open={$api.menuState === MenuStates.Open} />
|
<slot {...slot} />
|
||||||
</div>
|
</Render>
|
||||||
{/if}
|
{/if}
|
||||||
|
|||||||
Reference in New Issue
Block a user