Skip to content

Commit 3fd7071

Browse files
Attach "pointerup" and "pointercancel" listeners to ownerDocument body (#475)
This PR changes which element the handlePointerUp event handler is added to, it now adds the event handler to the body element of each owner docment where a panelResizeHandle is rendered. This accounts for use cases where the panelResizeHandle is rendered in a document that is in a child window. This PR also adds a test for checking which document body the event handler is added to, which demonstrates a usecase where PanelResizeHandles are rendered to a separate document. addresses #468 --------- Co-authored-by: Brian Vaughn <[email protected]>
1 parent 84df208 commit 3fd7071

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

packages/react-resizable-panels/src/PanelResizeHandle.test.tsx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ describe("PanelResizeHandle", () => {
3131
beforeEach(() => {
3232
// @ts-expect-error
3333
global.IS_REACT_ACT_ENVIRONMENT = true;
34+
3435
container = document.createElement("div");
3536
document.body.appendChild(container);
3637

@@ -259,6 +260,42 @@ describe("PanelResizeHandle", () => {
259260
});
260261
});
261262

263+
describe("portals and child windows", () => {
264+
test("should add the pointerup event to the separate document NOT the main document", () => {
265+
act(() => {
266+
root.unmount();
267+
});
268+
269+
const separateWindowDocument =
270+
document.implementation.createHTMLDocument();
271+
272+
container = separateWindowDocument.createElement("div");
273+
274+
separateWindowDocument.body.appendChild(container);
275+
276+
vi.spyOn(separateWindowDocument.body, "addEventListener");
277+
vi.spyOn(document.body, "addEventListener");
278+
279+
expectedWarnings = [];
280+
281+
root = createRoot(container);
282+
283+
const { leftElement } = setupMockedGroup();
284+
285+
act(() => {
286+
dispatchPointerEvent("pointerdown", leftElement);
287+
dispatchPointerEvent("pointerup", leftElement);
288+
});
289+
290+
expect(separateWindowDocument.body.addEventListener).toHaveBeenCalledWith(
291+
"pointerup",
292+
expect.anything(),
293+
expect.anything()
294+
);
295+
expect(document.body.addEventListener).not.toHaveBeenCalled();
296+
});
297+
});
298+
262299
describe("data attributes", () => {
263300
test("should initialize with the correct props based attributes", () => {
264301
const { leftElement, rightElement } = setupMockedGroup();

packages/react-resizable-panels/src/PanelResizeHandleRegistry.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,12 @@ function updateListeners() {
328328
});
329329
}
330330

331-
window.addEventListener("pointerup", handlePointerUp, options);
332-
window.addEventListener("pointercancel", handlePointerUp, options);
331+
ownerDocumentCounts.forEach((_, ownerDocument) => {
332+
const { body } = ownerDocument;
333+
334+
body.addEventListener("pointerup", handlePointerUp, options);
335+
body.addEventListener("pointercancel", handlePointerUp, options);
336+
});
333337
} else {
334338
ownerDocumentCounts.forEach((count, ownerDocument) => {
335339
const { body } = ownerDocument;

0 commit comments

Comments
 (0)