Files
TREK/client/src/components/shared/usePopoverList.ts
T
Maurice 3dd63331bc fix(tours): format the dropdown changes and lower the hook ref baseline
The editor dropdown change drops the options ref useAnchoredPosition kept
for its measure callback, so the react-hooks/refs count falls by one and
the baseline follows it. The new and touched files are formatted, and the
new rail test gets its id like the rest of the file.
2026-11-27 20:51:46 +01:00

108 lines
3.8 KiB
TypeScript

import { useEffect, useId, useLayoutEffect, useRef, type KeyboardEvent as ReactKeyboardEvent } from 'react';
import { useAnchoredPopover } from './useAnchoredPopover';
const itemsOf = (list: HTMLElement | null) => Array.from(list?.querySelectorAll('button') ?? []);
/** Room kept between an open list and the window's sides. */
const EDGE = 8;
/**
* How a button that opens a list to pick from behaves, whatever it looks like:
* opening moves the focus onto the current choice; the arrow keys, Home and End
* walk the list; Escape closes it and leaves a dialog under it open; Tab closes
* it and goes on from the button; a click beside it closes it. The caller draws
* the button and the list and hands `currentIndex`, the choice held now.
*/
export function usePopoverList(
currentIndex: number,
{
estimatedHeight = 320,
disabled,
}: {
estimatedHeight?: number;
disabled?: boolean;
} = {}
) {
const triggerRef = useRef<HTMLButtonElement>(null);
const menuRef = useRef<HTMLDivElement>(null);
const startAt = useRef(0);
const listId = useId();
const { open, box, bounded, setOpen } = useAnchoredPopover(triggerRef, {
matchWidth: false,
estimatedHeight,
disabled,
});
const listShown = open && !!box;
const toggle = () => {
if (!open) startAt.current = Math.max(0, currentIndex);
setOpen((value) => !value);
};
useEffect(() => {
if (listShown) itemsOf(menuRef.current)[startAt.current]?.focus({ preventScroll: !bounded });
}, [listShown, bounded]);
// The list opens from its button's start; one wider than the room beside a button near the
// window's side moves back inside, as far as it must, before it is painted.
useLayoutEffect(() => {
const menu = menuRef.current;
if (!listShown || !menu || bounded) return;
menu.style.translate = '';
const rect = menu.getBoundingClientRect();
let shift = Math.min(0, window.innerWidth - EDGE - rect.right);
if (rect.left + shift < EDGE) shift = EDGE - rect.left;
if (shift) menu.style.translate = `${Math.round(shift)}px 0`;
}, [listShown, box, bounded]);
useEffect(() => {
if (!open) return;
const onDown = (e: MouseEvent) => {
const target = e.target as Node;
if (triggerRef.current?.contains(target) || menuRef.current?.contains(target)) return;
setOpen(false);
};
// Captured and stopped, so the key takes back the list and not the dialog under it.
const onKey = (e: KeyboardEvent) => {
if (e.key !== 'Escape') return;
e.stopPropagation();
setOpen(false);
triggerRef.current?.focus();
};
document.addEventListener('mousedown', onDown);
document.addEventListener('keydown', onKey, true);
return () => {
document.removeEventListener('mousedown', onDown);
document.removeEventListener('keydown', onKey, true);
};
}, [open, setOpen]);
/** Closes the list after a choice and hands the focus back to the button. */
const close = () => {
setOpen(false);
triggerRef.current?.focus();
};
const onListKey = (e: ReactKeyboardEvent<HTMLDivElement>) => {
if (e.key === 'Tab') {
// Back on the button before the browser moves on, so Tab carries on from there.
setOpen(false);
triggerRef.current?.focus();
return;
}
const items = itemsOf(menuRef.current);
const at = items.indexOf(document.activeElement as HTMLButtonElement);
const last = items.length - 1;
let next: number;
if (e.key === 'ArrowDown') next = at < 0 || at === last ? 0 : at + 1;
else if (e.key === 'ArrowUp') next = at <= 0 ? last : at - 1;
else if (e.key === 'Home') next = 0;
else if (e.key === 'End') next = last;
else return;
e.preventDefault();
items[next]?.focus();
};
return { open, box, bounded, toggle, close, onListKey, triggerRef, menuRef, listId };
}