Skip to content

Commit cd84439

Browse files
authored
Fallback to check for type of HTMLElement using duck typing (#484)
1 parent 32aa9ff commit cd84439

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

packages/react-resizable-panels/src/utils/dom/getPanelGroupElement.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1+
import { isHTMLElement } from "./isHTMLElement";
2+
13
export function getPanelGroupElement(
24
id: string,
35
rootElement: ParentNode | HTMLElement = document
46
): HTMLElement | null {
5-
//If the root element is the PanelGroup
6-
if (
7-
rootElement instanceof HTMLElement &&
8-
(rootElement as HTMLElement)?.dataset?.panelGroupId == id
9-
) {
7+
// If the root element is the PanelGroup
8+
if (isHTMLElement(rootElement) && rootElement.dataset.panelGroupId == id) {
109
return rootElement as HTMLElement;
1110
}
1211

13-
//Else query children
12+
// Else query children
1413
const element = rootElement.querySelector(
1514
`[data-panel-group][data-panel-group-id="${id}"]`
1615
);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export function isHTMLElement(target: unknown): target is HTMLElement {
2+
if (target instanceof HTMLElement) {
3+
return true;
4+
}
5+
6+
// Fallback to duck typing to handle edge case of portals within a popup window
7+
return (
8+
typeof target === "object" &&
9+
target !== null &&
10+
"tagName" in target &&
11+
"getAttribute" in target
12+
);
13+
}

0 commit comments

Comments
 (0)