Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: still resizes after releasing mouse outside window (#340) #373

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/react-resizable-panels/src/PanelResizeHandle.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,18 @@ describe("PanelResizeHandle", () => {
verifyAttribute(leftElement, "data-resize-handle-active", null);
verifyAttribute(rightElement, "data-resize-handle-active", null);
});

it("should stop dragging when the mouse goes beyond the browser size", () => {
const { leftElement, rightElement } = setupMockedGroup();
verifyAttribute(leftElement, "data-resize-handle-active", null);
verifyAttribute(rightElement, "data-resize-handle-active", null);

act(() => {
dispatchPointerEvent("pointermove", leftElement, { clientX: -1 });
});
verifyAttribute(leftElement, "data-resize-handle-state", "inactive");
verifyAttribute(rightElement, "data-resize-handle-state", "inactive");
});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test seems to give a false positive. (I can remove the change you made entirely and it will still pass.)

Copy link
Owner

@bvaughn bvaughn Jul 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW I also think this fix may make the most sense in the resize handle registry (where other Window event listeners are) but I haven't thought about it much. I'll give it a quick try this afternoon.

Copy link
Owner

@bvaughn bvaughn Jul 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason the test gives a false positive is that the handle is not down when the move event is fired.

That being said, since the test isn't simulating "mouseout" events– I don't think the approach would work anyway. This might be better structured as an e2e test.

I will take a pass at fixing the test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the code review.
I will attempt to fix the test so that it works properly. Thank you. 😀

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I forgot to leave an update. As I was looking at this the other day, I realize that I already had code in place to handle this (in the resize handler registry) and it mostly seems to work, but it doesn't work in the case of Code Sandbox (probably something specific to iframes seemingly not receiving "pointerleave" events). Might be worth focusing on that use case.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interestingly, having just added an iframe– this library seems to work fine. So I'm not sure what it is about Code Sandbox that's different. 🤔

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New route was added here: 29cae93

Example test route here

Can be configured via URL args just like the other e2e route but has a wrapper <iframe> which should in theory allow us to repro the behavior reported in #340 but hasn't yet for some reason.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! I think I just realized what's going on by accidentally pushing a reference to localhost in my commit. I think the issue is when the iframe is in a different domain than the parent frame. Lemme poke at that a bit more.

Copy link
Owner

@bvaughn bvaughn Jul 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I'm able to trigger a similar behavior by using sandbox="allow-scripts". I'll move forward from there and try to add a failing e2e test.

Edit 0586271

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check out #374; I think I found a fix.

});

describe("a11y", () => {
Expand Down
22 changes: 22 additions & 0 deletions packages/react-resizable-panels/src/PanelResizeHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,28 @@
stopDragging,
]);

useEffect(() => {
const handleMouseOut = (event: MouseEvent) => {
if (
event.clientX < 0 ||
event.clientY < 0 ||
event.clientX > window.innerWidth ||
event.clientY > window.innerHeight
) {
setState("hover");
stopDragging();
const { onDragging } = callbacksRef.current;
if (onDragging) {
onDragging(false);
}
}
};
window.addEventListener('mouseout', handleMouseOut);
return () => {
window.removeEventListener('mouseout', handleMouseOut);
};
}, []);

Check warning on line 216 in packages/react-resizable-panels/src/PanelResizeHandle.ts

View workflow job for this annotation

GitHub Actions / tests-e2e

React Hook useEffect has a missing dependency: 'stopDragging'. Either include it or remove the dependency array

useWindowSplitterResizeHandlerBehavior({
disabled,
handleId: resizeHandleId,
Expand Down
11 changes: 8 additions & 3 deletions packages/react-resizable-panels/src/utils/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ import { assert } from "./assert";

const util = require("util");

export function dispatchPointerEvent(type: string, target: HTMLElement) {
interface IDispatchPointerEventOption {
clientX?: number;
clientY?: number;
}
export function dispatchPointerEvent(type: string, target: HTMLElement, options: IDispatchPointerEventOption = {}) {
const rect = target.getBoundingClientRect();

const clientX = rect.left + rect.width / 2;
const clientY = rect.top + rect.height / 2;
const { clientX: initClientX, clientY: initClientY } = options;
const clientX = initClientX || rect.left + rect.width / 2;
const clientY = initClientY || rect.top + rect.height / 2;

const event = new MouseEvent(type, {
bubbles: true,
Expand Down
Loading