mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-04-28 02:23:53 +02:00
Merge pull request #1021 from mvanhorn/osc/951-functional-state-carousel
refactor: use functional state updates in hero carousel callbacks
This commit is contained in:
commit
cae4262e35
1 changed files with 16 additions and 11 deletions
|
|
@ -157,21 +157,26 @@ function HeroCarousel() {
|
||||||
const [isGifExpanded, setIsGifExpanded] = useState(false);
|
const [isGifExpanded, setIsGifExpanded] = useState(false);
|
||||||
const directionRef = useRef<"forward" | "backward">("forward");
|
const directionRef = useRef<"forward" | "backward">("forward");
|
||||||
|
|
||||||
const goTo = useCallback(
|
const goTo = useCallback((newIndex: number) => {
|
||||||
(newIndex: number) => {
|
setActiveIndex((prev) => {
|
||||||
directionRef.current = newIndex >= activeIndex ? "forward" : "backward";
|
directionRef.current = newIndex >= prev ? "forward" : "backward";
|
||||||
setActiveIndex(newIndex);
|
return newIndex;
|
||||||
},
|
});
|
||||||
[activeIndex]
|
}, []);
|
||||||
);
|
|
||||||
|
|
||||||
const goToPrev = useCallback(() => {
|
const goToPrev = useCallback(() => {
|
||||||
goTo(activeIndex <= 0 ? carouselItems.length - 1 : activeIndex - 1);
|
setActiveIndex((prev) => {
|
||||||
}, [activeIndex, goTo]);
|
directionRef.current = "backward";
|
||||||
|
return prev <= 0 ? carouselItems.length - 1 : prev - 1;
|
||||||
|
});
|
||||||
|
}, []);
|
||||||
|
|
||||||
const goToNext = useCallback(() => {
|
const goToNext = useCallback(() => {
|
||||||
goTo(activeIndex >= carouselItems.length - 1 ? 0 : activeIndex + 1);
|
setActiveIndex((prev) => {
|
||||||
}, [activeIndex, goTo]);
|
directionRef.current = "forward";
|
||||||
|
return prev >= carouselItems.length - 1 ? 0 : prev + 1;
|
||||||
|
});
|
||||||
|
}, []);
|
||||||
|
|
||||||
const item = carouselItems[activeIndex];
|
const item = carouselItems[activeIndex];
|
||||||
const isForward = directionRef.current === "forward";
|
const isForward = directionRef.current === "forward";
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue