Skip to content

Commit

Permalink
feat: add double-click zoom gesture
Browse files Browse the repository at this point in the history
  • Loading branch information
igordanchenko committed Sep 19, 2024
1 parent 100ff49 commit a990a5b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/components/useSensors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default function useSensors() {
const activePointers = useRef<PointerEvent[]>([]);
const pinchZoomDistance = useRef<number>();

const { zoom, changeZoom, changeOffsets } = useZoom();
const { zoom, maxZoom, changeZoom, changeOffsets } = useZoom();
const { carouselRef } = useZoomInternal();
const { prev, next, close } = useController();

Expand Down Expand Up @@ -222,20 +222,26 @@ export default function useSensors() {
}
};

const onDoubleClick = (event: MouseEvent) => {
changeZoom(zoom < maxZoom ? scaleZoom(zoom, 2, 1) : 1, event);
};

return {
onKeyDown,
onPointerDown,
onPointerMove,
onPointerUp,
onPointerLeave: onPointerUp,
onPointerCancel: onPointerUp,
onDoubleClick,
onWheel,
};
}, [
prev,
next,
close,
zoom,
maxZoom,
changeZoom,
changeOffsets,
carouselRef,
Expand Down
15 changes: 15 additions & 0 deletions test/Lightbox.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,21 @@ describe("Lightbox", () => {
expectToBeZoomedOut();
});

it("supports double-click zoom", async () => {
const user = userEvent.setup();

renderLightbox();

await user.dblClick(getController());
expectToBeZoomedIn();

for (let i = 0; i < 3; i += 1) {
// eslint-disable-next-line no-await-in-loop
await user.dblClick(getController());
}
expectToBeZoomedOut();
});

it("supports zoom on custom slides", () => {
let maxZoom: number | undefined;

Expand Down

0 comments on commit a990a5b

Please sign in to comment.