fix: home page carousel causing scroll on item change and fix other performance issues
This commit is contained in:
5 files changed
+120
-94
No files matched your search
@@ -11,6 +11,7 @@
|
||||
&-sidebar {
|
||||
overflow-y: auto;
|
||||
height: 100%;
|
||||
position: relative;
|
||||
@include glass-effect($bg-color: rgba(10, 10, 14, 0.4));
|
||||
border-radius: 24px;
|
||||
grid-area: sidebar;
|
||||
@@ -170,4 +171,8 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.carousel-ticker-wrapper {
|
||||
scroll-margin-block: 24px;
|
||||
}
|
||||
|
||||
@@ -26,18 +26,42 @@ const Carousel = ({
|
||||
const [isPaused, setIsPaused] = useState(false);
|
||||
const sidebarRef = React.useRef<HTMLDivElement>(null);
|
||||
const tickerRefs = React.useRef<(HTMLDivElement | null)[]>([]);
|
||||
const currentSlideRef = React.useRef(currentSlide);
|
||||
|
||||
const [setDirection] = useCarouselStore((state) => [state.setDirection]);
|
||||
|
||||
useEffect(() => {
|
||||
currentSlideRef.current = currentSlide;
|
||||
}, [currentSlide]);
|
||||
|
||||
useEffect(() => {
|
||||
onChange(currentSlide);
|
||||
|
||||
// Auto-scroll sidebar to keep active item in view
|
||||
if (tickerRefs.current[currentSlide]) {
|
||||
tickerRefs.current[currentSlide]?.scrollIntoView({
|
||||
behavior: "smooth",
|
||||
block: "nearest",
|
||||
});
|
||||
const activeTicker = tickerRefs.current[currentSlide];
|
||||
const sidebar = sidebarRef.current;
|
||||
|
||||
if (activeTicker && sidebar) {
|
||||
const sidebarTop = sidebar.scrollTop;
|
||||
const sidebarBottom = sidebarTop + sidebar.clientHeight;
|
||||
const activeTickerTop = activeTicker.offsetTop;
|
||||
const activeTickerBottom =
|
||||
activeTicker.offsetTop + activeTicker.offsetHeight;
|
||||
|
||||
// If element is below the visible area
|
||||
if (activeTickerBottom > sidebarBottom) {
|
||||
sidebar.scrollTo({
|
||||
top: activeTickerBottom - sidebar.clientHeight + 24, // 24px padding
|
||||
behavior: "smooth",
|
||||
});
|
||||
}
|
||||
// If element is above the visible area
|
||||
else if (activeTickerTop < sidebarTop) {
|
||||
sidebar.scrollTo({
|
||||
top: activeTickerTop - 24, // 24px padding
|
||||
behavior: "smooth",
|
||||
});
|
||||
}
|
||||
}
|
||||
}, [currentSlide, content, onChange]);
|
||||
|
||||
@@ -57,11 +81,11 @@ const Carousel = ({
|
||||
|
||||
const handleTickerClick = useCallback(
|
||||
(index: number) => {
|
||||
if (index === currentSlide) return;
|
||||
setDirection(index > currentSlide ? "right" : "left");
|
||||
if (index === currentSlideRef.current) return;
|
||||
setDirection(index > currentSlideRef.current ? "right" : "left");
|
||||
setCurrentSlide(index);
|
||||
},
|
||||
[currentSlide, setDirection],
|
||||
[setDirection],
|
||||
);
|
||||
|
||||
if (!api) return null;
|
||||
@@ -85,7 +109,7 @@ const Carousel = ({
|
||||
ref={(el) => {
|
||||
tickerRefs.current[index] = el;
|
||||
}}
|
||||
style={{ scrollMarginBlock: "24px" }}
|
||||
className="carousel-ticker-wrapper"
|
||||
>
|
||||
<CarouselTickers
|
||||
imageUrl={
|
||||
@@ -105,7 +129,8 @@ const Carousel = ({
|
||||
? `${item.ProductionYear ?? ""} - ${new Date(item.EndDate).getFullYear().toString()}`
|
||||
: (item.ProductionYear?.toString() ?? "")
|
||||
}
|
||||
onClick={() => handleTickerClick(index)}
|
||||
onTickerClick={handleTickerClick}
|
||||
index={index}
|
||||
/>
|
||||
</div>
|
||||
))}
|
||||
|
||||
@@ -5,37 +5,45 @@ type CarouselTickersProps = {
|
||||
imageUrl?: string;
|
||||
itemName: string;
|
||||
itemYear: string;
|
||||
onClick: () => void;
|
||||
onTickerClick: (index: number) => void;
|
||||
index: number;
|
||||
isActive: boolean;
|
||||
};
|
||||
|
||||
const CarouselTickers = ({
|
||||
imageUrl,
|
||||
itemName,
|
||||
itemYear,
|
||||
onClick,
|
||||
isActive,
|
||||
}: CarouselTickersProps) => {
|
||||
return (
|
||||
<div
|
||||
className={`carousel-ticker${isActive ? " active" : ""}`}
|
||||
onClick={onClick}
|
||||
>
|
||||
<img
|
||||
src={imageUrl}
|
||||
alt={itemName}
|
||||
className="carousel-ticker-image"
|
||||
// style={{ width: "5em", height: "100%", objectFit: "cover" }}
|
||||
/>
|
||||
<div>
|
||||
<Typography variant="subtitle1" className="carousel-ticker-title">
|
||||
{itemName}
|
||||
</Typography>
|
||||
<Typography variant="caption" className="carousel-ticker-year">
|
||||
{itemYear}
|
||||
</Typography>
|
||||
const CarouselTickers = React.memo(
|
||||
({
|
||||
imageUrl,
|
||||
itemName,
|
||||
itemYear,
|
||||
onTickerClick,
|
||||
index,
|
||||
isActive,
|
||||
}: CarouselTickersProps) => {
|
||||
const handleClick = React.useCallback(() => {
|
||||
onTickerClick(index);
|
||||
}, [index, onTickerClick]);
|
||||
|
||||
return (
|
||||
<div
|
||||
className={`carousel-ticker${isActive ? " active" : ""}`}
|
||||
onClick={handleClick}
|
||||
>
|
||||
<img
|
||||
src={imageUrl}
|
||||
alt={itemName}
|
||||
className="carousel-ticker-image"
|
||||
// style={{ width: "5em", height: "100%", objectFit: "cover" }}
|
||||
/>
|
||||
<div>
|
||||
<Typography variant="subtitle1" className="carousel-ticker-title">
|
||||
{itemName}
|
||||
</Typography>
|
||||
<Typography variant="caption" className="carousel-ticker-year">
|
||||
{itemYear}
|
||||
</Typography>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
);
|
||||
},
|
||||
);
|
||||
export default CarouselTickers;
|
||||
Reference in new issue
Block a user