Skip to content

Commit

Permalink
Do not store node's ownerDocument since it can change for imported nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
edoardocavazza committed Jul 18, 2024
1 parent 5b7d15d commit 38921f4
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/purple-bees-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@chialab/loock': minor
---

Do not store node's ownerDocument since it can change for imported nodes.
5 changes: 2 additions & 3 deletions src/focusEnterBehavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ export interface FocusEnterOptions extends FocusManagerOptions {
* @returns The behavior controller.
*/
export function focusEnterBehavior(node: HTMLElement, options: FocusEnterOptions = {}) {
const document = node.ownerDocument;
const { onEnter, onExit } = options;
let focused = false;
let connected = false;

const onFocusIn = () => {
const activeElement = document.activeElement;
const activeElement = node.ownerDocument.activeElement;
if (focused || !activeElement) {
return;
}
Expand Down Expand Up @@ -57,7 +56,7 @@ export function focusEnterBehavior(node: HTMLElement, options: FocusEnterOptions
}
connected = true;
focused = false;
if (document.activeElement && node.contains(document.activeElement)) {
if (node.ownerDocument.activeElement && node.contains(node.ownerDocument.activeElement)) {
onFocusIn();
}
node.addEventListener('focusin', onFocusIn);
Expand Down
5 changes: 2 additions & 3 deletions src/focusFirstChildBehavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { restoreAttribute } from './helpers';
* @returns The behavior controller.
*/
export function focusFirstChildBehavior(node: HTMLElement, options: FocusManagerOptions = {}) {
const document = node.ownerDocument;
let activeElement: HTMLElement | null = null;
let connected = false;
let tabIndex: string | null = null;
Expand All @@ -28,7 +27,7 @@ export function focusFirstChildBehavior(node: HTMLElement, options: FocusManager

const onFocus = () => {
const elements = manager.findFocusable();
const target = document.activeElement as HTMLElement;
const target = node.ownerDocument.activeElement as HTMLElement;
if (target === node) {
if (activeElement && node.contains(activeElement)) {
activeElement.focus();
Expand All @@ -53,7 +52,7 @@ export function focusFirstChildBehavior(node: HTMLElement, options: FocusManager
enterBehavior.connect();
connected = true;
activeElement = null;
if (document.activeElement && node.contains(document.activeElement)) {
if (node.ownerDocument.activeElement && node.contains(node.ownerDocument.activeElement)) {
onFocus();
}
node.addEventListener('focus', onFocus, true);
Expand Down
9 changes: 4 additions & 5 deletions src/focusTrapBehavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ export function focusTrapBehavior(node: HTMLElement, options: FocusTrapOptions =
node.setAttribute('tabindex', '0');
}

const document = node.ownerDocument;
trapStart = startHelper || createTrapHelper(document);
trapStart = startHelper || createTrapHelper(node.ownerDocument);
trapStart.addEventListener(
'focus',
(event) => {
Expand All @@ -143,7 +142,7 @@ export function focusTrapBehavior(node: HTMLElement, options: FocusTrapOptions =
true
);

trapEnd = endHelper || createTrapHelper(document);
trapEnd = endHelper || createTrapHelper(node.ownerDocument);
trapEnd.addEventListener(
'focus',
(event) => {
Expand All @@ -169,7 +168,7 @@ export function focusTrapBehavior(node: HTMLElement, options: FocusTrapOptions =
root = node.shadowRoot;
} else {
root = node.attachShadow({ mode: 'open' });
root.append(document.createElement('slot'));
root.append(node.ownerDocument.createElement('slot'));
}
}
if (root.firstChild !== trapStart) {
Expand All @@ -189,7 +188,7 @@ export function focusTrapBehavior(node: HTMLElement, options: FocusTrapOptions =
if (trapEnd) {
trapEnd.tabIndex = 0;
}
restoreFocusNode = document.activeElement as HTMLElement;
restoreFocusNode = node.ownerDocument.activeElement as HTMLElement;
if (node.contains(restoreFocusNode) && restoreFocusNode !== node) {
restoreFocusNode = node;
} else if (focusContainer) {
Expand Down
5 changes: 3 additions & 2 deletions src/keyboardNavigationBehavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export interface KeyboardNavigationOptions extends FocusManagerOptions {
* @returns The behavior controller.
*/
export function keyboardNavigationBehavior(node: HTMLElement, options: KeyboardNavigationOptions) {
const document = node.ownerDocument;
let connected = false;

const manager = focusManager(node, options);
Expand All @@ -38,7 +37,9 @@ export function keyboardNavigationBehavior(node: HTMLElement, options: KeyboardN
return;
}

const current = node.contains(document.activeElement) ? (document.activeElement as HTMLElement) : null;
const current = node.contains(node.ownerDocument.activeElement)
? (node.ownerDocument.activeElement as HTMLElement)
: null;
if (!current) {
return;
}
Expand Down

0 comments on commit 38921f4

Please sign in to comment.