@@ -9,6 +9,8 @@ import { tcls } from '@/lib/tailwind';
99
1010import styles from './ZoomImage.module.css' ;
1111
12+ const PADDING = 32 ; // Padding around the image in the modal, in pixels
13+
1214/**
1315 * Replacement for an <img> tag that allows zooming.
1416 * The implementation uses the experimental View Transition API in Chrome for a smooth transition.
@@ -28,14 +30,9 @@ export function ZoomImage(
2830
2931 // Only allow zooming when image will not actually be larger and on mobile
3032 React . useEffect ( ( ) => {
31- if ( isTouchDevice ( ) ) {
32- return ;
33- }
34-
3533 const imageWidth = typeof width === 'number' ? width : 0 ;
3634 let viewWidth = 0 ;
3735
38- const mediaQueryList = window . matchMedia ( '(min-width: 768px)' ) ;
3936 const resizeObserver =
4037 typeof ResizeObserver !== 'undefined'
4138 ? new ResizeObserver ( ( entries ) => {
@@ -52,8 +49,9 @@ export function ZoomImage(
5249 : null ;
5350
5451 const onChange = ( ) => {
55- if ( ! mediaQueryList . matches ) {
56- // Don't allow zooming on mobile
52+ const viewInModalWidth = window . innerWidth - PADDING * 2 ;
53+ if ( viewWidth >= viewInModalWidth ) {
54+ // If the image will be smaller or same size as it is in the modal, disable zooming
5755 setZoomable ( false ) ;
5856 } else if ( resizeObserver && imageWidth && viewWidth && imageWidth <= viewWidth ) {
5957 // Image can't be zoomed if it's already rendered as it's largest size
@@ -63,10 +61,6 @@ export function ZoomImage(
6361 }
6462 } ;
6563
66- if ( 'addEventListener' in mediaQueryList ) {
67- mediaQueryList . addEventListener ( 'change' , onChange ) ;
68- }
69-
7064 if ( imgRef . current ) {
7165 resizeObserver ?. observe ( imgRef . current ) ;
7266 }
@@ -78,9 +72,6 @@ export function ZoomImage(
7872
7973 return ( ) => {
8074 resizeObserver ?. disconnect ( ) ;
81- if ( 'removeEventListener' in mediaQueryList ) {
82- mediaQueryList . removeEventListener ( 'change' , onChange ) ;
83- }
8475 } ;
8576 } , [ imgRef , width ] ) ;
8677
@@ -290,12 +281,3 @@ function startViewTransition(callback: () => void, onEnd?: () => void) {
290281 onEnd ?.( ) ;
291282 }
292283}
293-
294- function isTouchDevice ( ) : boolean {
295- return (
296- 'ontouchstart' in window ||
297- navigator . maxTouchPoints > 0 ||
298- // @ts -ignore
299- navigator . msMaxTouchPoints > 0
300- ) ;
301- }
0 commit comments