Skip to content
Source
Sign inSign up

editableFocus.ts

16 lines611 Bytes

1const EDITABLE_SELECTOR = [
2 "input",
3 "textarea",
4 "select",
5 '[contenteditable=""]',
6 '[contenteditable="true"]',
7 '[contenteditable="plaintext-only"]',
8 '[role="textbox"]',
9].join(",");
10​
11/** Includes focused descendants of open shadow roots, whose key events target the host. */
12export function isEditableFocused(target: EventTarget | null = document.activeElement): boolean {
13 let element = target instanceof Element ? target : null;
14 while (element?.shadowRoot?.activeElement) element = element.shadowRoot.activeElement;
15 return element !== null && element.closest(EDITABLE_SELECTOR) !== null;