Skip to content

Commit 5cc5c15

Browse files
Merge pull request #7307 from Katotodan/feature/fix-slider-on-mobile
Fix responsive behavior of SoSpecial carousel items across screen sizes
2 parents 27fc315 + d37f384 commit 5cc5c15

File tree

2 files changed

+56
-81
lines changed

2 files changed

+56
-81
lines changed

src/sections/Home/So-Special-Section/index.js

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
import React from "react";
2+
import React, {useLayoutEffect, useState, useRef} from "react";
33
import Slider from "react-slick";
44
import SoSpecialWrapper from "./so-special-style";
55

@@ -9,6 +9,9 @@ import Image from "../../../components/image";
99
// import { useStyledDarkMode } from "../../../theme/app/useStyledDarkMode";
1010

1111
const SoSpecial = () => {
12+
const [isClient, setIsClient] = useState(false);
13+
const [slidesToShowState, setSlidesToShowState] = useState(null);
14+
const sliderRef = useRef(null);
1215
const data = useStaticQuery(
1316
graphql`query newsList {
1417
allMdx(
@@ -53,56 +56,55 @@ const SoSpecial = () => {
5356
}`
5457
);
5558
const settings = {
56-
dots: false,
59+
dots: slidesToShowState <= 1,
5760
infinite: false,
5861
speed: 500,
59-
slidesToShow: 2.5,
6062
swipeToSlide: true,
61-
62-
responsive: [
63-
{
64-
breakpoint: 1200,
65-
settings: {
66-
slidesToShow: 2.2,
67-
68-
}
69-
},
70-
{
71-
breakpoint: 1024,
72-
settings: {
73-
slidesToShow: 2,
74-
75-
}
76-
},
77-
{
78-
breakpoint: 800,
79-
settings: {
80-
slidesToShow: 1.5,
81-
slidesToScroll: 1,
82-
initialSlide: 1
83-
}
84-
},
85-
{
86-
breakpoint: 600,
87-
settings: {
88-
dots: true,
89-
arrows: false,
90-
slidesToShow: 1,
91-
slidesToScroll: 1,
92-
initialSlide: 1
93-
}
94-
},
95-
{
96-
breakpoint: 400,
97-
settings: {
98-
dots: true,
99-
arrows: false,
100-
slidesToShow: 1,
101-
slidesToScroll: 1
102-
}
103-
}
104-
]
63+
slidesToScroll: 1,
64+
arrows: slidesToShowState !== 1,
65+
slidesToShow: slidesToShowState ?? 2.5,
10566
};
67+
68+
const computeSlides = () => {
69+
const w = typeof window !== "undefined" ? (window.innerWidth || document.documentElement.clientWidth) : 1200;
70+
if (w <= 600) return 1;
71+
if (w <= 800) return 1.5;
72+
if (w <= 1024) return 2;
73+
if (w <= 1200) return 2.2;
74+
return 3;
75+
};
76+
77+
useLayoutEffect(() => {
78+
79+
setIsClient(true);
80+
setSlidesToShowState(computeSlides());
81+
82+
let resizeTimeout = null;
83+
const onResizeDebounced = () => {
84+
clearTimeout(resizeTimeout);
85+
resizeTimeout = setTimeout(() => {
86+
const slides = computeSlides();
87+
setSlidesToShowState((prev) => {
88+
if (prev !== slides) return slides;
89+
return prev;
90+
});
91+
if (sliderRef.current && sliderRef.current.innerSlider && typeof sliderRef.current.innerSlider.onWindowResized === "function") {
92+
sliderRef.current.innerSlider.onWindowResized();
93+
}
94+
}, 100);
95+
};
96+
97+
window.addEventListener("resize", onResizeDebounced);
98+
window.addEventListener("load", onResizeDebounced);
99+
100+
return () => {
101+
window.removeEventListener("resize", onResizeDebounced);
102+
window.removeEventListener("load", onResizeDebounced);
103+
clearTimeout(resizeTimeout);
104+
};
105+
}, []);
106+
107+
if (!isClient || slidesToShowState === null) return null;
106108

107109
// const { isDark } = useStyledDarkMode();
108110

@@ -113,7 +115,7 @@ const SoSpecial = () => {
113115
<h1>We're making a splash</h1>
114116
</div>
115117
<div className="special_carousel">
116-
<Slider {...settings}>
118+
<Slider {...settings} key={`review-slider-${slidesToShowState}`} ref={sliderRef}>
117119
{
118120
data.allMdx.nodes.map(({ id, frontmatter, fields }) => (
119121
<Button className="special-cont_btn" $url={fields.slug} key={id}>

src/sections/Home/So-Special-Section/so-special-style.js

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const SoSpecialWrapper = styled.div`
1212
.slick-list .slick-track{
1313
width:5000px;
1414
}
15-
@media only screen and (max-width:450px){
15+
@media only screen and (max-width:600px){
1616
padding:1rem;
1717
}
1818
.special_carousel{
@@ -82,7 +82,7 @@ const SoSpecialWrapper = styled.div`
8282
opacity: 1;
8383
color: ${props => props.theme.secondaryColor};
8484
}
85-
}
85+
8686
.so-special-head{
8787
padding:2rem;
8888
text-align:center;
@@ -104,17 +104,19 @@ const SoSpecialWrapper = styled.div`
104104
transition: 0.8s cubic-bezier(0.2, 0.8, 0.2, 1);
105105
}
106106
#special-cont_img{
107-
height:70%;
107+
height:17rem;
108108
width:100%;
109109
overflow:hidden;
110110
.gatsby-image-wrapper, .old-gatsby-image-wrapper{
111111
height:100%;
112112
}
113113
img{
114-
height: inherit;
114+
height: 17rem;
115115
display: block;
116116
text-align: center;
117117
margin: auto;
118+
object-fit: fill !important;
119+
image-rendering: auto;
118120
}
119121
}
120122
.slick-dots{
@@ -162,21 +164,8 @@ const SoSpecialWrapper = styled.div`
162164
width:75%;
163165
}
164166
}
165-
@media screen and (min-width: 1199px){
166-
.special-cont_img{
167-
height:20rem;
168-
width:100%;
169-
}
170-
}
171-
@media screen and (max-width: 1199px){
172-
.special-cont_img{
173-
height:20rem;
174-
width:100%;
175-
}
176-
}
177167
@media screen and (max-width: 1024px){
178168
.special_carousel{
179-
margin-bottom: 3rem;
180169
width:80%;
181170
}
182171
.special-cont_img{
@@ -190,13 +179,8 @@ const SoSpecialWrapper = styled.div`
190179
191180
@media screen and (max-width: 850px){
192181
.special_carousel{
193-
margin-bottom: 3rem;
194182
width:100%;
195183
}
196-
.special-cont_img{
197-
width: 22rem;
198-
max-height: 17rem;
199-
}
200184
.so-special-head{
201185
padding:1rem;
202186
text-align:center;
@@ -207,19 +191,11 @@ const SoSpecialWrapper = styled.div`
207191
}
208192
}
209193
@media screen and (max-width: 671px){
210-
.special-cont_img{
211-
width: 100%;
212-
max-height: 20rem;
213-
}
214194
.special-cont_content{
215195
padding:0rem;
216196
}
217197
}
218198
@media screen and (max-width: 500px){
219-
#special-cont_img{
220-
width: 100%;
221-
height: 65%;
222-
}
223199
.so-special-head{
224200
padding: 0.5rem 0;
225201
}
@@ -229,9 +205,6 @@ const SoSpecialWrapper = styled.div`
229205
height:25rem;
230206
margin:0;
231207
}
232-
#special-cont_img{
233-
height: 60%;
234-
}
235208
#special-cont_content{
236209
.special-cont_para
237210
{

0 commit comments

Comments
 (0)