Build packages / CI (push) Blocked by required conditions
Build packages / Version (push) Waiting to run
CI / Web (format, types, tests, bundles) (push) Blocked by required conditions
CI / ShellCheck (push) Blocked by required conditions
CI / ucode static checks (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 24.10) (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 25.12) (push) Blocked by required conditions
- The personal page loads faster and costs the router less: its script and styles come compressed, stay in the browser until the next update, and the icon comes with the page instead of a request of its own - The page uses the system's own font (SF Pro on Apple devices, Segoe UI on Windows, Roboto on Android) instead of downloading one - The top of the page shows the server's name alone, so it fits on a phone - Text can be selected only where it is there to be copied (addresses, file names, the configuration), with the name field's own selection: a soft highlight with handles that glides after the pointer - Sheets scroll without a scrollbar - Red buttons (Remove) light up under the pointer, as the others do - Demo: "Demo" and GitHub are one button; on a phone it floats at the bottom of the screen, and switching scenarios no longer makes the edge fades jump
91 lines
3.1 KiB
TypeScript
91 lines
3.1 KiB
TypeScript
// A small dropdown under its button; closes on pick, outside click, Escape,
|
|
// scrolling or navigation. A second click on the same button closes it.
|
|
|
|
import { E } from '../e';
|
|
import { onNavigate } from './nav';
|
|
import { run } from './toast';
|
|
|
|
export interface MenuItem {
|
|
label: string;
|
|
icon?: Node;
|
|
danger?: boolean;
|
|
warn?: boolean;
|
|
run: () => unknown;
|
|
}
|
|
|
|
type OpenMenu = HTMLElement & { dlAnchor?: HTMLElement; dlClose?: () => void };
|
|
|
|
export function dropdown(anchor: HTMLElement, items: (MenuItem | null)[]) {
|
|
const prev = document.querySelector('.dl-dd') as OpenMenu | null;
|
|
if (prev) {
|
|
if (prev.dlClose) prev.dlClose();
|
|
else prev.remove();
|
|
if (prev.dlAnchor === anchor) return;
|
|
}
|
|
const menu: OpenMenu = E(
|
|
'div',
|
|
{ class: 'dl-dd dl-root', role: 'menu' },
|
|
items.map((it) =>
|
|
it
|
|
? E(
|
|
'button',
|
|
{
|
|
type: 'button',
|
|
role: 'menuitem',
|
|
class: it.danger ? 'danger' : it.warn ? 'warn' : '',
|
|
click: () => {
|
|
close();
|
|
run(it.run);
|
|
},
|
|
},
|
|
[it.icon ?? '', E('span', {}, it.label)],
|
|
)
|
|
: E('hr'),
|
|
),
|
|
);
|
|
document.body.appendChild(menu);
|
|
const r = anchor.getBoundingClientRect();
|
|
const w = menu.offsetWidth,
|
|
h = menu.offsetHeight;
|
|
// Open upwards when the menu would run off the bottom of the screen.
|
|
menu.style.top = `${r.bottom + 6 + h > window.innerHeight - 8 && r.top - 6 - h > 8 ? r.top - 6 - h : r.bottom + 6}px`;
|
|
menu.style.left = `${Math.max(8, Math.min(r.right - w, window.innerWidth - w - 8))}px`;
|
|
const onDoc = (ev: Event) => {
|
|
if (!menu.contains(ev.target as Node) && !anchor.contains(ev.target as Node)) close();
|
|
};
|
|
const onKey = (ev: KeyboardEvent) => {
|
|
if (ev.key === 'Escape' || ev.key === 'Tab') return close();
|
|
// Arrow keys walk the items (wrapping), Home/End jump; a menu that has the
|
|
// keyboard focus must not scroll the page under it.
|
|
const items = [...menu.querySelectorAll<HTMLButtonElement>('button:not([disabled])')];
|
|
if (!items.length || !['ArrowDown', 'ArrowUp', 'Home', 'End'].includes(ev.key)) return;
|
|
ev.preventDefault();
|
|
const i = items.indexOf(document.activeElement as HTMLButtonElement);
|
|
const next =
|
|
ev.key === 'Home'
|
|
? 0
|
|
: ev.key === 'End'
|
|
? items.length - 1
|
|
: ev.key === 'ArrowDown'
|
|
? (i + 1) % items.length
|
|
: (i - 1 + items.length) % items.length;
|
|
items[next].focus({ preventScroll: true });
|
|
};
|
|
const offNav = onNavigate(close);
|
|
menu.dlAnchor = anchor;
|
|
menu.dlClose = close;
|
|
function close() {
|
|
menu.remove();
|
|
offNav();
|
|
document.removeEventListener('mousedown', onDoc, true);
|
|
document.removeEventListener('keydown', onKey, true);
|
|
window.removeEventListener('scroll', close, true);
|
|
}
|
|
setTimeout(() => {
|
|
document.addEventListener('mousedown', onDoc, true);
|
|
document.addEventListener('keydown', onKey, true);
|
|
window.addEventListener('scroll', close, true);
|
|
});
|
|
(menu.querySelector('button') as HTMLElement | null)?.focus({ preventScroll: true });
|
|
}
|