Files
outline/app/utils/isKeyboardWidget.ts
Tom Moor 8fb8f4d3d4 feat: Alt+Up/Down moves to the previous/next document in the sidebar (#14034)
* feat: Alt+Up/Down moves to the previous/next document in the sidebar

* feat: Sidebar keyboard navigation works across collections and root rows
2026-10-11 15:33:19 -04:00

51 lines
1.6 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import isTextInput from "./isTextInput";
// ARIA roles for widgets that implement their own keyboard interaction, such as
// arrow key navigation, typeahead, or dismissal. Focus usually sits on a
// descendant of the widget, so ancestors are checked as well.
// https://www.w3.org/WAI/ARIA/apg/patterns/
const widgetRoles = [
"menu",
"menubar",
"listbox",
"combobox",
"tree",
"treegrid",
"grid",
"dialog",
"alertdialog",
"radiogroup",
"slider",
"spinbutton",
];
const widgetSelector = widgetRoles.map((role) => `[role="${role}"]`).join(",");
/**
* Checks whether the given event target is, or is contained by, a control that
* handles its own keyboard input – a text input, or a widget such as a menu,
* listbox, or dialog. Global keyboard shortcuts should not fire while such a
* control holds focus, otherwise they compete with it for the same keys.
*
* @param target the event target to check.
* @returns true if the target handles its own keyboard input.
*/
export function isKeyboardWidget(target: EventTarget | null): boolean {
return (
target instanceof Element &&
(isTextInput(target) || isWithinKeyboardWidget(target))
);
}
/**
* Checks whether the given event target is, or is contained by, a widget that
* handles its own keyboard interaction, such as a menu, listbox, or dialog.
* Unlike isKeyboardWidget, plain text inputs outside of a widget are ignored.
*
* @param target the event target to check.
* @returns true if the target is within a keyboard widget.
*/
export function isWithinKeyboardWidget(target: EventTarget | null): boolean {
return target instanceof Element && !!target.closest(widgetSelector);
}