89 lines
3.2 KiB
Svelte
89 lines
3.2 KiB
Svelte
<!-- This Source Code Form is subject to the terms of the Mozilla Public
|
|
- License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
- file, You can obtain one at https://mozilla.org/MPL/2.0/. -->
|
|
<script lang="ts" module>
|
|
import type { IconName } from "@ianitor/ui";
|
|
export interface Item {
|
|
label: string;
|
|
icon: IconName;
|
|
danger?: boolean;
|
|
reason?: string | null;
|
|
divider?: boolean;
|
|
run: () => void;
|
|
}
|
|
</script>
|
|
|
|
<script lang="ts">
|
|
// Handoff §2.6: the row's ⋯ menu (WAI-ARIA menu button).
|
|
import { tick } from "svelte";
|
|
import { Icon } from "@ianitor/ui";
|
|
|
|
let { hostname, items }: { hostname: string; items: Item[] } = $props();
|
|
|
|
let open = $state(false);
|
|
let btn: HTMLButtonElement | undefined = $state();
|
|
let list: HTMLUListElement | undefined = $state();
|
|
const id = "menu-" + Math.random().toString(36).slice(2, 9);
|
|
|
|
const nodes = () => Array.from(list?.querySelectorAll<HTMLElement>('[role="menuitem"]') ?? []);
|
|
|
|
async function show(focus: "first" | "last" = "first") {
|
|
open = true;
|
|
await tick();
|
|
const n = nodes();
|
|
(focus === "first" ? n[0] : n[n.length - 1])?.focus();
|
|
}
|
|
|
|
function close(back = true) {
|
|
open = false;
|
|
if (back) btn?.focus();
|
|
}
|
|
|
|
function key(e: KeyboardEvent) {
|
|
const n = nodes();
|
|
const i = n.indexOf(document.activeElement as HTMLElement);
|
|
if (e.key === "ArrowDown") { e.preventDefault(); n[(i + 1) % n.length]?.focus(); }
|
|
else if (e.key === "ArrowUp") { e.preventDefault(); n[(i - 1 + n.length) % n.length]?.focus(); }
|
|
else if (e.key === "Home") { e.preventDefault(); n[0]?.focus(); }
|
|
else if (e.key === "End") { e.preventDefault(); n[n.length - 1]?.focus(); }
|
|
else if (e.key === "Escape") { e.preventDefault(); close(); }
|
|
else if (e.key === "Tab") close(false);
|
|
}
|
|
|
|
function pick(it: Item) {
|
|
if (it.reason) return;
|
|
// Focus back on More first: a dialog the item opens returns focus there when it closes
|
|
// (§2.9), not to a menu item that no longer exists.
|
|
close(true);
|
|
it.run();
|
|
}
|
|
|
|
function outside(e: MouseEvent) {
|
|
if (open && !(e.target instanceof Node && (list?.contains(e.target) || btn?.contains(e.target)))) close(false);
|
|
}
|
|
</script>
|
|
|
|
<svelte:window onclick={outside} />
|
|
|
|
<div class="ia-menu-wrap">
|
|
<button bind:this={btn} type="button" class="ia-icon-btn" aria-label="More for {hostname}" aria-haspopup="menu"
|
|
aria-expanded={open} aria-controls={id}
|
|
onclick={() => (open ? close() : show())}
|
|
onkeydown={(e) => { if (e.key === "ArrowDown") { e.preventDefault(); show("first"); } else if (e.key === "ArrowUp") { e.preventDefault(); show("last"); } }}>
|
|
<Icon name="dots" />
|
|
</button>
|
|
{#if open}
|
|
<ul bind:this={list} class="ia-menu" role="menu" id={id} aria-label={hostname} tabindex="-1" onkeydown={key}>
|
|
{#each items as it (it.label)}
|
|
{#if it.divider}<li role="separator"><hr /></li>{/if}
|
|
<li role="none">
|
|
<button type="button" role="menuitem" tabindex="-1" class:danger={it.danger} aria-disabled={it.reason ? "true" : undefined} onclick={() => pick(it)}>
|
|
<Icon name={it.icon} />{it.label}
|
|
{#if it.reason}<span class="reason">{it.reason}</span>{/if}
|
|
</button>
|
|
</li>
|
|
{/each}
|
|
</ul>
|
|
{/if}
|
|
</div>
|