Files
t3code/apps/web/src/lib/editableFocus.ts
T
2026-10-07 17:11:05 -07:00

22 lines
832 B
TypeScript

const EDITABLE_SELECTOR = [
"input",
"textarea",
"select",
'[contenteditable=""]',
'[contenteditable="true"]',
'[contenteditable="plaintext-only"]',
'[role="textbox"]',
].join(",");
/**
* Whether a text-editing element owns the keyboard. Shortcuts that share
* their chord with native editing (mod+z) must yield when this is true.
* Focus inside an open shadow root counts: a key event's target is retargeted
* to the shadow host, so the host's focused descendant is what is typed into.
*/
export function isEditableFocused(target: EventTarget | null = document.activeElement): boolean {
let element = target instanceof Element ? target : null;
while (element?.shadowRoot?.activeElement) element = element.shadowRoot.activeElement;
return element !== null && element.closest(EDITABLE_SELECTOR) !== null;
}