Initial commit of Descrybe v2 without local scratch artifacts.
Drop one-shot tmp/axe scripts and agent i18n scratch so the Gitea tree is deployable.
This commit is contained in:
commit
8580c996c3
1285 files changed
+325780
No files matched your search
@@ -0,0 +1,103 @@
|
||||
/** Focusable controls for dialog / drawer traps (critical a11y). */
|
||||
const FOCUSABLE_SELECTOR = [
|
||||
"a[href]",
|
||||
"button:not([disabled])",
|
||||
"input:not([disabled]):not([type='hidden'])",
|
||||
"select:not([disabled])",
|
||||
"textarea:not([disabled])",
|
||||
"[tabindex]:not([tabindex='-1'])"
|
||||
].join(",");
|
||||
|
||||
export function getFocusable(container: HTMLElement): HTMLElement[] {
|
||||
return Array.from(container.querySelectorAll<HTMLElement>(FOCUSABLE_SELECTOR)).filter(
|
||||
(el) =>
|
||||
!el.hasAttribute("disabled") &&
|
||||
el.getAttribute("aria-hidden") !== "true" &&
|
||||
el.tabIndex !== -1 &&
|
||||
!el.closest("[inert]")
|
||||
);
|
||||
}
|
||||
|
||||
export type FocusTrapOptions = {
|
||||
initialFocus?: HTMLElement | null;
|
||||
restoreFocus?: boolean;
|
||||
/** Extra nodes in the Tab cycle (e.g. tutorial spotlight target). */
|
||||
extraFocusables?: () => Array<HTMLElement | null | undefined>;
|
||||
};
|
||||
|
||||
export type FocusTrapHandle = {
|
||||
deactivate: () => void;
|
||||
};
|
||||
|
||||
function collectCycle(container: HTMLElement, options?: FocusTrapOptions): HTMLElement[] {
|
||||
const seen = new Set<HTMLElement>();
|
||||
const out: HTMLElement[] = [];
|
||||
for (const el of getFocusable(container)) {
|
||||
if (seen.has(el)) continue;
|
||||
seen.add(el);
|
||||
out.push(el);
|
||||
}
|
||||
for (const el of options?.extraFocusables?.() ?? []) {
|
||||
if (!el || seen.has(el) || !el.isConnected) continue;
|
||||
seen.add(el);
|
||||
out.push(el);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Trap Tab/Shift+Tab inside `container` (+ optional extras), restore focus on deactivate.
|
||||
*/
|
||||
export function activateFocusTrap(
|
||||
container: HTMLElement,
|
||||
options?: FocusTrapOptions
|
||||
): FocusTrapHandle {
|
||||
const previouslyFocused =
|
||||
document.activeElement instanceof HTMLElement ? document.activeElement : null;
|
||||
|
||||
const focusInitial = () => {
|
||||
const preferred = options?.initialFocus;
|
||||
if (preferred && preferred.isConnected) {
|
||||
preferred.focus();
|
||||
return;
|
||||
}
|
||||
const items = collectCycle(container, options);
|
||||
(items[0] ?? container).focus();
|
||||
};
|
||||
|
||||
requestAnimationFrame(focusInitial);
|
||||
|
||||
function onKeydown(event: KeyboardEvent) {
|
||||
if (event.key !== "Tab") return;
|
||||
const items = collectCycle(container, options);
|
||||
if (items.length === 0) {
|
||||
event.preventDefault();
|
||||
container.focus();
|
||||
return;
|
||||
}
|
||||
const first = items[0];
|
||||
const last = items[items.length - 1];
|
||||
const active = document.activeElement;
|
||||
const inCycle = active instanceof HTMLElement && items.includes(active);
|
||||
if (event.shiftKey) {
|
||||
if (!inCycle || active === first) {
|
||||
event.preventDefault();
|
||||
last.focus();
|
||||
}
|
||||
} else if (!inCycle || active === last) {
|
||||
event.preventDefault();
|
||||
first.focus();
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener("keydown", onKeydown, true);
|
||||
|
||||
return {
|
||||
deactivate() {
|
||||
document.removeEventListener("keydown", onKeydown, true);
|
||||
if (options?.restoreFocus !== false && previouslyFocused?.isConnected) {
|
||||
previouslyFocused.focus();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
/** WAI-ARIA menu item roles used by shared DropdownMenuItem. */
|
||||
export const MENU_ITEM_SELECTOR =
|
||||
'[role="menuitem"], [role="menuitemradio"], [role="menuitemcheckbox"]';
|
||||
|
||||
export type MenuKeyAction =
|
||||
| { type: "close" }
|
||||
| { type: "focus"; index: number }
|
||||
| { type: "none" };
|
||||
|
||||
/** Enabled menu items inside a menu root (skips aria/data-disabled). */
|
||||
export function getMenuItems(container: ParentNode): HTMLElement[] {
|
||||
return Array.from(container.querySelectorAll<HTMLElement>(MENU_ITEM_SELECTOR)).filter(
|
||||
(el) => el.getAttribute("aria-disabled") !== "true" && el.dataset.disabled === undefined
|
||||
);
|
||||
}
|
||||
|
||||
/** Index to focus when a menu opens (first enabled item, or -1). */
|
||||
export function openMenuFocusIndex(itemCount: number): number {
|
||||
return itemCount > 0 ? 0 : -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pure keyboard → action map for an open menu (Arrow/Home/End/Escape).
|
||||
* `currentIndex` may be -1 when nothing is focused yet.
|
||||
*/
|
||||
export function menuKeyAction(key: string, currentIndex: number, itemCount: number): MenuKeyAction {
|
||||
if (key === "Escape") return { type: "close" };
|
||||
if (itemCount <= 0) return { type: "none" };
|
||||
|
||||
const clamped = currentIndex < 0 || currentIndex >= itemCount ? -1 : currentIndex;
|
||||
|
||||
switch (key) {
|
||||
case "ArrowDown":
|
||||
return {
|
||||
type: "focus",
|
||||
index: clamped < 0 ? 0 : (clamped + 1) % itemCount
|
||||
};
|
||||
case "ArrowUp":
|
||||
return {
|
||||
type: "focus",
|
||||
index: clamped < 0 ? itemCount - 1 : (clamped - 1 + itemCount) % itemCount
|
||||
};
|
||||
case "Home":
|
||||
return { type: "focus", index: 0 };
|
||||
case "End":
|
||||
return { type: "focus", index: itemCount - 1 };
|
||||
default:
|
||||
return { type: "none" };
|
||||
}
|
||||
}
|
||||
|
||||
/** Keys that open a closed menu from the trigger (APG menu button). */
|
||||
export function isMenuOpenKey(key: string): boolean {
|
||||
return key === "ArrowDown" || key === "ArrowUp";
|
||||
}
|
||||
|
||||
/**
|
||||
* Case-insensitive substring filter over option labels (combobox / typeahead helpers).
|
||||
* Empty query returns all items (same reference order).
|
||||
*/
|
||||
export function filterOptionsByQuery<T>(
|
||||
items: readonly T[],
|
||||
query: string,
|
||||
getLabel: (item: T) => string
|
||||
): T[] {
|
||||
const q = query.trim().toLowerCase();
|
||||
if (!q) return [...items];
|
||||
return items.filter((item) => getLabel(item).toLowerCase().includes(q));
|
||||
}
|
||||
Reference in new issue
Block a user