Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226<script lang="ts"> import type { Snippet } from 'svelte'; import { dismissal, fitted, floating } from './popover';
// float puts the pane in the top layer, out of reach of a window's clipping. // row styles are global, a list drawn without the pane gets them from `.menu-rows` let { open = $bindable(false), host, label, as = 'div', role = 'menu', up = false, start = false, float = false, wide = false, onclick, children, }: { open?: boolean; host: HTMLElement | undefined; label?: string; as?: 'div' | 'ul'; role?: string; up?: boolean; start?: boolean; float?: boolean; /** for a pane that holds its own content, like a search, rather than rows */ wide?: boolean; onclick?: (event: MouseEvent) => void; children: Snippet; } = $props(); let pane = $state<HTMLElement>();
// only a keyboard close hands focus back, a click would leave a ring on the trigger let keyed = false; $effect(() => { if (!open) return; keyed = false; const key = () => (keyed = true); const press = () => (keyed = false); addEventListener('keydown', key, true); addEventListener('pointerdown', press, true); return () => { removeEventListener('keydown', key, true); removeEventListener('pointerdown', press, true); if (!keyed) return; queueMicrotask(() => { const now = document.activeElement; if (now && now !== document.body && now.isConnected) return; const trigger = host?.matches('[aria-haspopup]') ? host : host?.querySelector<HTMLElement>('[aria-haspopup]'); trigger?.focus({ preventScroll: true }); }); }; }); $effect(() => open ? dismissal( (target) => !!(host?.contains(target) || pane?.contains(target)), () => (open = false), ) : undefined, );
const ROWS = ':is([role^="menuitem"], .item, li > button):not(:disabled)';
// focus the pane, not a row, which would show a ring after a click function focusPane(node: HTMLElement) { queueMicrotask(() => { if (!node.contains(document.activeElement)) node.focus({ preventScroll: true }); }); }
function keys(event: KeyboardEvent) { if (!['ArrowDown', 'ArrowUp', 'Home', 'End'].includes(event.key)) return; const rows = [...(pane?.querySelectorAll<HTMLElement>(ROWS) ?? [])]; if (!rows.length) return; event.preventDefault(); event.stopPropagation(); const at = rows.indexOf(document.activeElement as HTMLElement); const checked = rows.findIndex( (row) => row.getAttribute('aria-checked') === 'true', ); const down = event.key === 'ArrowDown'; let next = (at + (down ? 1 : -1) + rows.length) % rows.length; if (event.key === 'Home') next = 0; else if (event.key === 'End') next = rows.length - 1; else if (at < 0) next = checked >= 0 ? checked : down ? 0 : rows.length - 1; rows[next]?.focus(); }</script>
{#if open} <svelte:element this={as} bind:this={pane} class="menu-pane menu-rows nodrag nowheel holds-wheel" class:up class:start class:float class:wide {role} tabindex="-1" aria-label={label} popover={float ? 'manual' : undefined} {@attach float ? floating(host) : fitted(up)} {@attach focusPane} onkeydown={keys} {onclick} > {@render children()} </svelte:element>{/if}
<style> .menu-pane { position: absolute; top: calc(100% + 6px); right: var(--menu-edge, 0); z-index: 30; display: grid; gap: 1px; width: max-content; min-width: 160px; max-width: min(340px, 80vw); max-height: 260px; margin: 0; padding: 4px; overflow-y: auto; overscroll-behavior: contain; color: var(--text); font-size: calc(var(--type-md) * var(--ui-scale)); list-style: none; background: var(--surface); border: 0; border-radius: 12px; box-shadow: var(--shadow-raised); cursor: auto; pointer-events: auto; } .menu-pane.start { right: auto; left: var(--menu-edge, 0); } .menu-pane.up { top: auto; bottom: calc(100% + 6px); } .menu-pane.wide { width: min(400px, calc(100vw - 24px)); max-width: none; max-height: none; padding: 0; overflow: visible; } .menu-pane.float { position: fixed; inset: auto; max-height: none; } .menu-pane:focus { outline: none; } :global(.menu-rows :is(.item, li > button)) { display: flex; align-items: center; gap: 10px; width: 100%; padding: 7px 10px; color: var(--text); font: inherit; font-size: calc(var(--type-md) * var(--ui-scale)); text-align: left; text-decoration: none; background: none; border: 0; border-radius: 8px; cursor: pointer; } :global( .menu-rows :is(.item, li > button):is(:hover, :focus-visible):not(:disabled) ) { background: var(--hover); outline: none; } :global(.menu-rows :is(.item, li > button)[aria-checked='true']) { color: var(--accent); background: color-mix(in oklab, var(--accent) 15%, var(--surface)); } :global(.menu-rows :is(.item, li > button):disabled) { opacity: 0.5; cursor: default; } :global(.menu-rows :is(.item, li > button) svg) { flex: none; color: var(--muted); } :global(.menu-rows .item kbd) { margin-left: auto; } :global(.menu-rows .note) { margin-left: auto; color: var(--muted); font-size: calc(var(--type-sm) * var(--ui-scale)); font-variant-numeric: tabular-nums; white-space: nowrap; } @media (max-width: 760px) { .menu-pane.float { inset: auto 12px 12px; width: auto; max-width: none; padding: 12px; border-radius: 18px; z-index: 50; } :global(.menu-pane.float :is(.item, li > button)) { min-height: 48px; font-size: var(--type-xl); } }</style>