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

Added: Disable pinch zoom on iOS #1284

Merged
merged 2 commits into from
Sep 24, 2024
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
2 changes: 2 additions & 0 deletions frontend/src/components/App/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import Profile from "../Profile/Profile";
import Reload from "../Reload/Reload";
import StoreProfile from "../StoreProfile/StoreProfile";
import useDisableRightClickOnTouchDevices from "../../hooks/useDisableRightClickOnTouchDevices";
import useDisableIOSPinchZoomOnTouchDevices from "@/hooks/useDisableIOSPinchZoomOnTouchDevices";
import { InternalRedirect } from "../InternalRedirect/InternalRedirect";
import Helmet from "@/components/Helmet/Helmet";

Expand All @@ -31,6 +32,7 @@ const App = () => {
const queryParams = window.location.search;

useDisableRightClickOnTouchDevices();
useDisableIOSPinchZoomOnTouchDevices();

useEffect(() => {
const urlParams = new URLSearchParams(queryParams);
Expand Down
36 changes: 36 additions & 0 deletions frontend/src/hooks/useDisableIOSPinchZoomOnTouchDevices.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { vi, describe, test, expect, afterEach } from "vitest";
import { renderHook } from "@testing-library/react-hooks";
import { waitFor } from "@testing-library/react";
import useDisableIOSPinchZoomOnTouchDevices from "./useDisableIOSPinchZoomOnTouchDevices";

describe("useDisableIOSPinchZoomOnTouchDevices", () => {
afterEach(() => {
vi.unstubAllGlobals();
});

test("should prevent gesturestart on touch devices", async () => {
vi.stubGlobal("ontouchstart", true);

const mockEvent = new Event('gesturestart');
mockEvent.preventDefault = vi.fn();

const spy = vi.spyOn(mockEvent, 'preventDefault').mockImplementation(() => { });

renderHook(() => useDisableIOSPinchZoomOnTouchDevices());

await waitFor(() => document.dispatchEvent(mockEvent));
await waitFor(() => expect(spy).toHaveBeenCalledTimes(1));
});

test("should not prevent gesturestart on non-touch devices", () => {
const mockEvent = new Event('gesturestart');
mockEvent.preventDefault = vi.fn();

const spy = vi.spyOn(mockEvent, 'preventDefault').mockImplementation(() => { });

renderHook(() => useDisableIOSPinchZoomOnTouchDevices());

document.dispatchEvent(mockEvent);
expect(spy).not.toHaveBeenCalled();
});
});
20 changes: 20 additions & 0 deletions frontend/src/hooks/useDisableIOSPinchZoomOnTouchDevices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useEffect } from 'react';

const useDisableIOSPinchZoomOnTouchDevices = () => useEffect(() => {
const isTouchDevice = () => !!('ontouchstart' in window || !!(navigator.maxTouchPoints));

const handlePinchZoom = (event: Event) => {
if (isTouchDevice()) {
event.preventDefault();
}
};

document.addEventListener('gesturestart', handlePinchZoom);

return () => {
document.removeEventListener('gesturestart', handlePinchZoom);
};
}, []);


export default useDisableIOSPinchZoomOnTouchDevices;
3 changes: 3 additions & 0 deletions frontend/src/scss/layout.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ body.root {
background-position: center center;
background-repeat: no-repeat;
min-height: 100vh;

/* disable pinch zoom on iOS */
touch-action: manipulation;
}

#root {
Expand Down
Loading