Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix partner slider animation in home page #5966

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
92 changes: 62 additions & 30 deletions src/sections/Home/Partners-home/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import React, { useState, useEffect } from "react";
import { Container, Row } from "../../../reusecore/Layout";
import SectionTitle from "../../../reusecore/SectionTitle";
import PartnerItemWrapper from "./partnerSection.style";
Expand All @@ -8,27 +8,57 @@ import Slider from "react-slick";
import "slick-carousel/slick/slick.css";
import "slick-carousel/slick/slick-theme.css";

const settings = {
initialSlide: 1,
lazyLoad: true,
arrows: false,
dots: false,
infinite: true,
speed: 500,
centerMode: true,
variableWidth: true,
autoplay: true,
autoplaySpeed: 1500,
className: "partner-slider",
responsive: [
{
breakpoint: 1400,
settings: "unslick"
}
]
};
const PartnerImage = ({ partner }) => (
<div className={partner.innerDivStyle}>
<img
className="partner-image"
id={partner.name}
src={partner.imageLink}
alt={partner.name}
width={partner.imageWidth}
height={partner.imageHeight}
/>
</div>
);

const Projects = () => {
const [imagesLoaded, setImagesLoaded] = useState(false);

useEffect(() => {
const imagePromises = partners.map(partner => {
return new Promise((resolve, reject) => {
const img = new Image();
img.onload = resolve;
img.onerror = reject;
img.src = partner.imageLink;
});
});

Promise.all(imagePromises)
.then(() => setImagesLoaded(true))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why we using promise here? Will any one image anytime fail to load then all images will fail.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Promises seem to be ironically named. 😆

.catch(error => console.error("Error preloading images:", error));
}, []);

const settings = {
initialSlide: 1,
lazyLoad: false,
arrows: false,
dots: false,
infinite: true,
speed: 500,
centerMode: true,
variableWidth: true,
autoplay: true,
autoplaySpeed: 1500,
className: "partner-slider",
responsive: [
{
breakpoint: 1400,
settings: "unslick"
}
]
};

return (
<PartnerItemWrapper>
<Container>
Expand All @@ -42,17 +72,19 @@ const Projects = () => {
</SectionTitle>
</Row>
</Container>
<Slider {...settings}>
{partners.map((partner, index) => (
<Link className="partner-card" to={partner.imageRoute} key={index}>
<div className={partner.innerDivStyle}>
<img className="partner-image" id={partner.name} loading="lazy" src={partner.imageLink} alt={partner.name} width={partner.imageWidth} height={partner.imageHeight} />
</div>
</Link>
))}
</Slider>
{imagesLoaded ? (
<Slider {...settings}>
{partners.map((partner, index) => (
<Link className="partner-card" to={partner.imageRoute} key={index}>
<PartnerImage partner={partner} />
</Link>
))}
</Slider>
) : (
<div style={{ textAlign: 'center', padding: '20px' }}>Loading partners...</div>
)}
</PartnerItemWrapper>
);
};

export default Projects;
export default Projects;
Loading