Loading...
Please waitLoading...
Please waitPrevent zooming on touch devices with usePreventZoom, providing global and element-specific zoom control with advanced features.
pnpm dlx shadcn@latest add https://usekit.kiron.dev/k/use-prevent-zoom
"use client"
import { usePreventZoom } from "@/hooks/use-prevent-zoom"
const App = () => {
const { disableGlobal, enableGlobal, isGlobalDisabled } = usePreventZoom()
return (
<div>
<p>Zoom is globally {isGlobalDisabled ? "disabled" : "enabled"}</p>
<button onClick={disableGlobal}>Disable Zoom</button>
<button onClick={enableGlobal}>Enable Zoom</button>
</div>
)
}
"use client"
import { usePreventZoom } from "@/hooks/use-prevent-zoom"
const App = () => {
const { disableGlobal, enableGlobal, isGlobalDisabled, toggleGlobal } =
usePreventZoom({ global: true })
return (
<div>
<p>Zoom is globally {isGlobalDisabled ? "disabled" : "enabled"}</p>
<button onClick={toggleGlobal}>
{isGlobalDisabled ? "Enable" : "Disable"} Zoom
</button>
</div>
)
}
"use client"
import { useRef } from "react"
import { usePreventZoom } from "@/hooks/use-prevent-zoom"
const App = () => {
const { disableForElement, enableForElement } = usePreventZoom()
const sectionRef = useRef<HTMLDivElement>(null)
const handleDisableSection = () => {
if (sectionRef.current) {
disableForElement(sectionRef.current)
}
}
const handleEnableSection = () => {
if (sectionRef.current) {
enableForElement(sectionRef.current)
}
}
return (
<div>
<div ref={sectionRef} className="section">
<p>This section can have zoom disabled specifically</p>
<button onClick={handleDisableSection}>Disable Zoom for Section</button>
<button onClick={handleEnableSection}>Enable Zoom for Section</button>
</div>
</div>
)
}
"use client"
import { usePreventZoom } from "@/hooks/use-prevent-zoom"
const App = () => {
const zoomControl = usePreventZoom({
scroll: true, // Disable zoom via Ctrl+scroll
keyboard: true, // Disable zoom via Ctrl+/- keys
pinch: true, // Disable pinch-to-zoom on touch devices
global: false, // Don't start with global disable
})
return (
<div>
<h1>Custom Zoom Control</h1>
<button onClick={zoomControl.disableGlobal}>Disable All Zoom</button>
<button onClick={zoomControl.enableGlobal}>Enable All Zoom</button>
</div>
)
}
usePreventZoom(options?: Options): Return
Parameters:
Name | Type | Default | Description |
---|---|---|---|
scroll | boolean | true | Disable zoom via mouse wheel + Ctrl key |
keyboard | boolean | true | Disable zoom via keyboard shortcuts (Ctrl +, Ctrl -, etc.) |
pinch | boolean | true | Disable zoom via pinch gesture on touch devices |
global | boolean | false | Start with global zoom disabled when hook initializes |
Return Value:
Name | Type | Description |
---|---|---|
disableGlobal | () => void | Function to disable zoom globally across the entire website |
enableGlobal | () => void | Function to enable zoom globally across the entire website |
isGlobalDisabled | boolean | Current state indicating if zoom is disabled globally |
disableForElement | (element: HTMLElement) => () => void | Function to disable zoom for a specific element. Returns cleanup function. |
enableForElement | (element: HTMLElement) => void | Function to enable zoom for a specific element |
toggleGlobal | () => void | Function to toggle the global zoom disabled state |
interface Options {
scroll?: boolean
keyboard?: boolean
pinch?: boolean
global?: boolean
}
interface Return {
disableGlobal: () => void
enableGlobal: () => void
isGlobalDisabled: boolean
disableForElement: (element: HTMLElement) => () => void
enableForElement: (element: HTMLElement) => void
toggleGlobal: () => void
}
import { useEffect, useRef } from "react"
import { usePreventZoom } from "@/hooks/use-prevent-zoom"
const Modal = () => {
const modalRef = useRef<HTMLDivElement>(null)
const { disableForElement } = usePreventZoom()
useEffect(() => {
if (modalRef.current) {
const cleanup = disableForElement(modalRef.current)
return cleanup // Cleanup when modal unmounts
}
}, [disableForElement])
return (
<div ref={modalRef} className="modal">
<h2>Modal Content (Zoom Disabled)</h2>
<p>Zoom is disabled within this modal</p>
</div>
)
}
import { useRef, useState } from "react"
import { usePreventZoom } from "@/hooks/use-prevent-zoom"
const ImageGallery = () => {
const galleryRef = useRef<HTMLDivElement>(null)
const [zoomDisabled, setZoomDisabled] = useState(false)
const { disableForElement, enableForElement } = usePreventZoom()
const toggleZoomForGallery = () => {
if (galleryRef.current) {
if (zoomDisabled) {
enableForElement(galleryRef.current)
} else {
disableForElement(galleryRef.current)
}
setZoomDisabled(!zoomDisabled)
}
}
return (
<div>
<button onClick={toggleZoomForGallery}>
{zoomDisabled ? "Enable" : "Disable"} Zoom for Gallery
</button>
<div ref={galleryRef} className="image-gallery">
<img src="/image1.jpg" alt="Image 1" />
<img src="/image2.jpg" alt="Image 2" />
<img src="/image3.jpg" alt="Image 3" />
</div>
</div>
)
}
usePreventZoom
function with options object and return objectOptions
and Return
interfacesZoom is disabled globally, but you can enable it for specific sections. Hover over enabled sections to zoom normally.
Try zooming here when protection is enabled. Notice how zoom is prevented only within this specific area.