How To Make My React-multi-carousel Use A Light-box Feature For My Reactjs App
I have images coming in from a rest api, into my application. Those images then get populated into a react-multi-carousel. What I would like to do is to make those images clickable
Solution 1:
You can do the lightbox by hand by issuing an onClick
for each image and enlarging the image, adding captions, full screen etc.
It is a lot easier if you can make use of existing libraries to implement lightbox feature for your carousal such as react-images.
Here is the working copy of your code with light box feature implemented(using react-multi-carousel and react-images).
https://codesandbox.io/s/react-multi-corousal-issue-72s9o?file=/src/Slider.js
Here is the code snippet.
importReact, { Component } from"react";
// import "../slider/slider.css";import"./slider.css";
importCarouselfrom"react-multi-carousel";
import"react-multi-carousel/lib/styles.css";
importLightBox, { Modal, ModalGateway } from"react-images";
const responsive = {
superLargeDesktop: {
breakpoint: { max: 4000, min: 3000 },
items: 1
},
desktop: {
breakpoint: { max: 3000, min: 1024 },
items: 1
},
tablet: {
breakpoint: { max: 1024, min: 464 },
items: 1
},
mobile: {
breakpoint: { max: 464, min: 0 },
items: 1
}
};
classSliderextendsComponent {
_isMounted = false;
state = {
awsApiData: [],
loading: false,
selectedIndex: 0,
lightboxIsOpen: false
};
componentDidMount() {
this._isMounted = true;
console.log("app mounted");
this.setState({ loading: true });
/*global fetch */// fetch("https://onelbip0e6.execute-api.eu-west-2.amazonaws.com/xxxxx")fetch("https://picsum.photos/v2/list?page=1&limit=10")
.then(data => data.json())
.then(data =>// this.setState({ awsApiData: data[0], loading: false }, () =>this.setState(
{
awsApiData: data.map(item => ({ source: item.download_url })),
loading: false
},
() =>console.log(data)
)
);
}
componentWillUnmount() {
this._isMounted = false;
}
toggleLightbox = selectedIndex => {
this.setState(state => ({
lightboxIsOpen: !state.lightboxIsOpen,
selectedIndex
}));
};
render() {
return (
<div>
{this.state.loading ? (
<divclassName="text-center">Loading...</div>
) : (
<><CarouseladditionalTransfrom={0}showDots={false}arrows={true}autoPlaySpeed={3000}autoPlay={true}centerMode={false}className="slider"containerClass="container-with-dots"dotListClass="dots"draggablefocusOnSelect={false}infiniteitemClass="carousel-top"keyBoardControlminimumTouchDrag={80}renderButtonGroupOutside={false}renderDotsOutsideresponsive={responsive}
>
{Object.values(this.state.awsApiData).map((post, indx) => {
return (
<divclassName="mt-5"key={indx}onClick={() => this.toggleLightbox(indx)}
>
<imgclassName="media-img card-img-top card-img-hero"src={post.source}alt="Alt text"
/></div>
);
})}
</Carousel><ModalGateway>
{this.state.lightboxIsOpen ? (
<ModalonClose={this.toggleLightbox}><LightBoxcomponents={{FooterCaption: () =><div>footer caption</div>
}}
currentIndex={this.state.selectedIndex}
// formatters={{ getAltText }}
frameProps={{ autoSize: "height" }}
views={this.state.awsApiData}
/>
</Modal>
) : null}
</ModalGateway></>
)}
</div>
);
}
}
exportdefaultSlider;
Post a Comment for "How To Make My React-multi-carousel Use A Light-box Feature For My Reactjs App"