Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,9 @@ interface IHandleElement {
onGrab(cb: (event: [mousePosition: PointCoordinates, elementRect: IGenericRect]) => void): OffCallbackHandler;
}

const isLeftClick = (event: PointerEvent) => event.button === 0;
const isMousePointer = (event: PointerEvent) => event.pointerType === 'mouse';

class HandleDomElement
extends Emitter<{
grab: [PointCoordinates, IGenericRect];
Expand All @@ -350,7 +353,7 @@ class HandleDomElement
public setElement(element: HTMLElement) {
const onGrab = (event: PointerEvent) => {
const element = event.currentTarget as HTMLElement;
if (!element) {
if (!element || (isMousePointer(event) && !isLeftClick(event))) {
return;
}
event.preventDefault();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ type Story = StoryObj<typeof meta>;

export const Default: Story = {};

// TODO: test other pointer types (pen, touch, etc)
// TODO: use `userEvent` for all tests
const moveHelper = async (handle: HTMLElement, offset: { x: number; y: number }) => {
const handleRect = handle.getBoundingClientRect();
Expand Down Expand Up @@ -253,6 +254,36 @@ export const DraggingBehavior: Story = {
expect(finalRect.y).toBeCloseTo(initialRect.y, 0);
});
});

await step('should not allow dragging with right click', async () => {
const draggable = await canvas.findByTestId('draggable-box');
const handle = await canvas.findByTestId('drag-handle');

const initialRect = draggable.getBoundingClientRect();

// for some reason `fireEvent` and `userEvent` do not change the `button` property of the event
// so we need to create a new event manually
await fireEvent(
handle,
new PointerEvent('pointerdown', {
button: 2,
pointerType: 'mouse',
}),
);

await fireEvent.pointerMove(document.documentElement, {
clientX: 50,
clientY: 50,
});

await fireEvent.pointerUp(document.documentElement);

await waitFor(() => {
const finalRect = draggable.getBoundingClientRect();
expect(finalRect.x).toBeCloseTo(initialRect.x, 0);
expect(finalRect.y).toBeCloseTo(initialRect.y, 0);
});
});
},
};

Expand Down
Loading