Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit fcadfa5

Browse files
committed
Add PiP move threshold
1 parent 918f151 commit fcadfa5

File tree

2 files changed

+47
-23
lines changed

2 files changed

+47
-23
lines changed

src/components/structures/PictureInPictureDragger.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ export default class PictureInPictureDragger extends React.Component<IProps> {
7070
() => this.animationCallback(),
7171
() => requestAnimationFrame(() => this.scheduledUpdate.trigger()),
7272
);
73+
private startingPositionX = 0;
74+
private startingPositionY = 0;
7375

7476
private _moving = false;
7577
public get moving(): boolean {
@@ -192,11 +194,22 @@ export default class PictureInPictureDragger extends React.Component<IProps> {
192194
event.stopPropagation();
193195

194196
this.mouseHeld = true;
197+
this.startingPositionX = event.clientX;
198+
this.startingPositionY = event.clientY;
195199
};
196200

197201
private onMoving = (event: MouseEvent): void => {
198202
if (!this.mouseHeld) return;
199203

204+
if (
205+
Math.abs(this.startingPositionX - event.clientX) < 5 &&
206+
Math.abs(this.startingPositionY - event.clientY) < 5
207+
) {
208+
// User needs to move the widget by at least five pixels.
209+
// Improves click detection when using a touchpad or with nervous hands.
210+
return;
211+
}
212+
200213
event.preventDefault();
201214
event.stopPropagation();
202215

test/components/structures/PictureInPictureDragger-test.tsx

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -82,28 +82,39 @@ describe("PictureInPictureDragger", () => {
8282
});
8383
});
8484

85-
it("doesn't leak drag events to children as clicks", async () => {
86-
const clickSpy = jest.fn();
87-
render(
88-
<PictureInPictureDragger draggable={true}>
89-
{[
90-
({ onStartMoving }) => (
91-
<div onMouseDown={onStartMoving} onClick={clickSpy}>
92-
Hello
93-
</div>
94-
),
95-
]}
96-
</PictureInPictureDragger>,
97-
);
98-
const target = screen.getByText("Hello");
99-
100-
// A click without a drag motion should go through
101-
await userEvent.pointer([{ keys: "[MouseLeft>]", target }, { keys: "[/MouseLeft]" }]);
102-
expect(clickSpy).toHaveBeenCalled();
103-
104-
// A drag motion should not trigger a click
105-
clickSpy.mockClear();
106-
await userEvent.pointer([{ keys: "[MouseLeft>]", target }, { coords: { x: 60, y: 60 } }, "[/MouseLeft]"]);
107-
expect(clickSpy).not.toHaveBeenCalled();
85+
describe("when rendering the dragger", () => {
86+
let clickSpy;
87+
let target;
88+
89+
beforeEach(() => {
90+
clickSpy = jest.fn();
91+
render(
92+
<PictureInPictureDragger draggable={true}>
93+
{[
94+
({ onStartMoving }) => (
95+
<div onMouseDown={onStartMoving} onClick={clickSpy}>
96+
Hello
97+
</div>
98+
),
99+
]}
100+
</PictureInPictureDragger>,
101+
);
102+
target = screen.getByText("Hello");
103+
});
104+
105+
it("and clicking without a drag motion, it should pass the click to children", async () => {
106+
await userEvent.pointer([{ keys: "[MouseLeft>]", target }, { keys: "[/MouseLeft]" }]);
107+
expect(clickSpy).toHaveBeenCalled();
108+
});
109+
110+
it("and clicking with a drag motion above the threshold of 5px, it should not pass the click to children", async () => {
111+
await userEvent.pointer([{ keys: "[MouseLeft>]", target }, { coords: { x: 60, y: 2 } }, "[/MouseLeft]"]);
112+
expect(clickSpy).not.toHaveBeenCalled();
113+
});
114+
115+
it("and clickign with a drag motion below the threshold of 5px, it should pass the click to the children", async () => {
116+
await userEvent.pointer([{ keys: "[MouseLeft>]", target }, { coords: { x: 4, y: 4 } }, "[/MouseLeft]"]);
117+
expect(clickSpy).toHaveBeenCalled();
118+
});
108119
});
109120
});

0 commit comments

Comments
 (0)