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: calculates slide width on first pass #15

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions src/SliderProvider/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, {
useEffect,
useId,
useMemo,
useReducer,
useRef,
useState,
Expand Down Expand Up @@ -82,7 +83,6 @@ const SliderProvider: React.FC<SliderProviderProps> = (props) => {
const scrollable = scrollableFromProps === undefined ? useFreeScroll : scrollableFromProps;

const [scrollRatio, setScrollRatio] = useState(0);
const [slideWidth, setSlideWidth] = useState<string | undefined>();
const [isPaused, setIsPaused] = useState(false);
const [isFullyScrolled, setIsFullyScrolled] = useState(false);
const sliderTrackRef = useRef<HTMLDivElement>(null);
Expand All @@ -94,6 +94,10 @@ const SliderProvider: React.FC<SliderProviderProps> = (props) => {
slides: [],
});

const memoizedSlideWidth = useMemo(() => {
return `${(slidesToShow > 1 ? 1 / slidesToShow : slidesToShow) * 100}%`
}, [slidesToShow]);

Copy link

@adiba adiba Apr 20, 2023

Choose a reason for hiding this comment

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

No need to memoize. We can just write const slideWidth = `...`;

Copy link
Member

Choose a reason for hiding this comment

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

I think we may want to keep the slider widths uninitialized, see my comment here: #17 (comment)

Copy link
Contributor Author

@JarrodMFlesch JarrodMFlesch Apr 25, 2023

Choose a reason for hiding this comment

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

I don't see the benefit of waiting a render cycle to provide known information via context?

useDragScroll({
ref: sliderTrackRef,
scrollYAxis: false,
Expand Down Expand Up @@ -133,13 +137,6 @@ const SliderProvider: React.FC<SliderProviderProps> = (props) => {
smoothscroll.polyfill(); // enables scrollTo.behavior: 'smooth' on Safari
}, []);

useEffect(() => {
const newSlideWidth = `${(slidesToShow > 1 ? 1 / slidesToShow : slidesToShow) * 100}%`;
setSlideWidth(newSlideWidth);
}, [
slidesToShow,
]);

// let user control pause, if they need to
useEffect(() => {
if (typeof pause !== 'undefined') setIsPaused(pause);
Expand Down Expand Up @@ -206,7 +203,7 @@ const SliderProvider: React.FC<SliderProviderProps> = (props) => {
});
},
autoPlay,
slideWidth,
slideWidth: memoizedSlideWidth,
slidesToShow,
slideOnSelect,
scrollable,
Expand Down