Skip to content

Commit

Permalink
lazy load react pinch
Browse files Browse the repository at this point in the history
  • Loading branch information
phartenfeller committed Feb 4, 2024
1 parent a6bd21c commit 46e92a8
Showing 1 changed file with 31 additions and 19 deletions.
50 changes: 31 additions & 19 deletions src/components/blog/BlogImagePopup.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import React, { Fragment } from 'react';
import React, { Fragment, Suspense } from 'react';
import { Dialog, Transition } from '@headlessui/react';
import { TransformWrapper, TransformComponent } from 'react-zoom-pan-pinch';
import useImagePreview from '../../state/useImagePreview';

const TransformWrapper = React.lazy(() =>
import('react-zoom-pan-pinch').then((module) => ({
default: module.TransformWrapper,
}))
);
const TransformComponent = React.lazy(() =>
import('react-zoom-pan-pinch').then((module) => ({
default: module.TransformComponent,
}))
);

export default function BlogImagePopup() {
const { isOpen, close, clear, imgSrc, alt, width, height } =
useImagePreview();
Expand Down Expand Up @@ -73,23 +83,25 @@ export default function BlogImagePopup() {
</button>
</div>
<div className="m-2 cursor-move bg-zinc-100 shadow-inner">
<TransformWrapper wheel={{ step: 12 }}>
<TransformComponent>
<img
width={width}
height={height}
src={imgSrc}
className="object-contain"
alt={alt}
style={{
maxWidth: '85vw',
maxHeight: '85vh',
width,
height,
}}
/>
</TransformComponent>
</TransformWrapper>
<Suspense fallback={<div>Loading...</div>}>
<TransformWrapper wheel={{ step: 12 }}>
<TransformComponent>
<img
width={width}
height={height}
src={imgSrc}
className="object-contain"
alt={alt}
style={{
maxWidth: '85vw',
maxHeight: '85vh',
width,
height,
}}
/>
</TransformComponent>
</TransformWrapper>
</Suspense>
</div>
</div>
</Transition.Child>
Expand Down

0 comments on commit 46e92a8

Please sign in to comment.