How to enable Zoom on custom slides? #218
Replies: 5 comments 29 replies
|
Are you using a custom slide type or the default image slides with a custom render function? Also, providing a working example (or, at the very least, a code snippet) does wonders in troubleshooting. |
|
Hello @igordanchenko! I have similar question to this unfinished thread. I'm using default image slides with a custom render function. I get slides from server, they can be updated from server. I can implement your solution, but in my case I have to use state for keeping width/height only for current slide and add this info to slides array received from server. Because otherwise I'll have to merge slides array from server with previous slides expanded with sizes. This solution leads to additional rerenders which is probably not a big deal. Anyway I had an idea - to make setImageDimensions from ZoomWrapper a Context and execute this function inside every ImageSlide including custom ones. If that's possible any image slides would be supported without explicitly providing width/height. Did I miss some problem with this approach? |
|
Hey @igordanchenko First for all it's a grate library you have made👍🏻 I have a problem with Zoom on my custom slide, the zoom in and zoom out buttons is disabled. I have creade a custom slide, because i need to add a overlay with som information on top off the image. Images array look like this Code const OrderLineImageRow = ({
images,
customerName,
orderNumber,
unitNumber,
onChange,
editable = true,
}) => {
const [open, setOpen] = useState(false);
const [showIndex, setShowIndex] = useState(0);
const handleOpen = ({ i }) => {
setOpen(true);
setShowIndex(i);
};
const Overlay = ({ slide }) => (
<div>
<p>
Unit: {unitNumber} - {moment(slide.takenAt).format('DD/MM YYYY')} kl.{' '}
{moment(slide.takenAt).format('HH:mm:ss')}
</p>
<p>Adresse: {slide.location}</p>
{orderNumber && (
<p>
Ordrenr.: {orderNumber} Kunde: {customerName}
</p>
)}
</div>
);
const ImageWithOverlay = ({ slide, offset, rect, zoom, maxZoom }) => {
console.log({ zoom });
console.log({ maxZoom });
return (
<LightboxImageContainer>
{slide.location && <Overlay slide={slide} />}
<img
src={`https://res.cloudinary.com/dxwfoqglb/image/upload/${slide.publicId}.${slide.format}`}
alt=""
style={{
maxWidth: `min(${slide.width}px, 100%)`,
maxHeight: `min(${slide.height}px, 100%)`,
}}
/>
</LightboxImageContainer>
);
};
return (
<>
<OrderLineImageGallery>
{images.map((image, i) => {
if (image.visible === false && editable === false) return null;
return (
<OrderLineImageGalleryColumn key={image._id}>
<ImageContainer>
{image.location && <Overlay slide={image} />}
<img
src={`https://res.cloudinary.com/dxwfoqglb/image/upload/c_scale,w_380/${image.publicId}.${image.format}`}
onClick={() => handleOpen({ i })}
/>
</ImageContainer>
{editable && onChange && (
<input
type="checkbox"
checked={image.visible === false ? image.visible : true}
onChange={(e) => onChange(image._id, e.currentTarget.checked)}
/>
)}
</OrderLineImageGalleryColumn>
);
})}
<Lightbox
open={open}
close={() => setOpen(false)}
index={showIndex}
plugins={[Zoom]}
render={{
slide: ({ slide, offset, rect, zoom, maxZoom }) => (
<ImageWithOverlay {...{ slide, offset, rect, zoom, maxZoom }} />
),
}}
slides={images}
/>
</OrderLineImageGallery>
</>
);
};What am I missing? |
|
i seem to have the same problem as @ma10as where my zoom buttons are deactivated. following is sample code from my application. Help would be really appreciated, the solution here offered does not seem to work for me with custom render function and slides property changes suggested and used. im using Vite, Typescript, React, Mantine Ui (Image component) and the latest version of yet-another-react-lightbox lib (3.21.4) ....
import Lightbox, { Slide, SlideImage } from "yet-another-react-lightbox";
import {
Download,
Share,
Fullscreen,
Zoom,
Thumbnails,
Captions,
Counter,
} from "yet-another-react-lightbox/plugins";
import "yet-another-react-lightbox/styles.css";
import "yet-another-react-lightbox/plugins/thumbnails.css";
import "yet-another-react-lightbox/plugins/counter.css";
import "yet-another-react-lightbox/plugins/captions.css";
...
....
export type SlideDataType = SlideImage & {
type: "image" | string;
src: string;
thumbnail: string;
audioSrc?: string;
comment: string | CommentsType | undefined;
comments: CommentsType[] | undefined;
media: MediaCardType;
onAddComment: () => void;
onRotateMedia: () => void;
showCloseButton: boolean;
share: { url: string };
downloadUrl: string;
title: string;
description: string;
vignette: false;
};
...
const renderSlide = (slide: Slide): ReactNode => {
if (slide.type === "audio") {
return (
<div style={styles.slideAudioWrapper}>
<div style={styles.slideAudio}>
{audioPlayer(hostURL, slide.audioSrc!)}
</div>
</div>
);
} else if (slide.type === "other" || slide.type === "media") {
return (
<Image
src={slide.src}
style={{
width: slide.width,
height: slide.height,
maxWidth: "100%",
maxHeight: "100%",
flex: 1,
}}
/>
)
);
}
...
return (
<div>
<Lightbox
slides={slides.map((slide) => ({
...slide,
src: slide.src,
thumbnail: slide.thumbnail,
type: slide.type,
}))}
open={isOpen}
index={currentIndex}
counter={counterStyles}
close={() => dispatch(closeLightbox())}
render={{
thumbnail: ({ slide }) => renderThumbnail(slide),
slide: ({ slide, offset, rect, zoom, maxZoom }) => renderSlide(slide),
}}
plugins={plugins}
zoom={{
scrollToZoom: true,
maxZoomPixelRatio: 2,
zoomInMultiplier: 1.5,
}}
thumbnails={{
showToggle: true,
position: "bottom",
}}
captions={{
showToggle: true,
descriptionTextAlign: "center",
descriptionMaxLines: 2,
}}
on={{ view: ({ index }) => handleSlideChange(index) }}
/>
</div>
);
};i did not post all the code, but all necessary parts that should be of use. |
|
Starting with v3.31.0, the Zoom plugin supports custom slide types out of the box. You can now enable zoom on custom slides using two new zoom props:
<Lightbox
slides={slides}
plugins={[Zoom]}
zoom={{
supports: ["custom-slide"],
maxZoom: 4,
}}
render={{
slide: ({ slide, zoom, maxZoom }) => {
if (slide.type === "custom-slide") {
return <MyCustomSlide slide={slide} zoom={zoom} maxZoom={maxZoom} />;
}
},
}}
/>The Zoom plugin wraps your custom content with a zoom container that handles all zoom gestures (pinch, double-click/tap, scroll, keyboard), CSS transformations, and pan offset clamping automatically. Custom slides don't need See the https://yet-another-react-lightbox.com/plugins/zoom#CustomSlideTypes for details. |




Uh oh!
There was an error while loading. Please reload this page.
We have custom image and video slides but we want to integrate the zoom effect only on the custom image slides.
Is there a way to do this?
All reactions