Skip to content

feat: clone marquee slides for infinite effect #21

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions src/SliderProvider/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface ISliderContext extends Omit<SliderProviderProps, 'children'> {
isPaused?: boolean
setIsPaused: (is: boolean) => void // eslint-disable-line no-unused-vars
isDragging: boolean
marquee: boolean
}

export const SliderContext = createContext<ISliderContext>({} as ISliderContext);
Expand Down
1 change: 1 addition & 0 deletions src/SliderProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ const SliderProvider: React.FC<SliderProviderProps> = (props) => {
pauseOnHover,
alignLastSlide,
isDragging,
marquee: Boolean(marquee),
Copy link
Member

Choose a reason for hiding this comment

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

marquee is already a boolean and so it doesn't need to be cast here.

id
};

Expand Down
30 changes: 30 additions & 0 deletions src/SliderTrack/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ const SliderTrack: React.FC<SliderTrackProps> = (props) => {
sliderTrackRef,
setScrollRatio,
slideWidth,
slidesToShow,
scrollable,
scrollSnap,
setIsPaused,
pauseOnHover,
alignLastSlide,
isDragging,
marquee,
autoPlay,
id: idFromContext,
} = sliderContext;
Expand Down Expand Up @@ -59,6 +61,34 @@ const SliderTrack: React.FC<SliderTrackProps> = (props) => {
getScrollRatio,
]);

// NOTE: adds infinite scroll when marquee is enabled
useEffect(() => {
const track = sliderTrackRef.current;
if (marquee && track) {
const clones = track?.querySelectorAll('.marquee-clone');
clones?.forEach(clone => clone.remove());

const firstSlide = track.firstElementChild;
if (firstSlide) {
let selectedSlide = firstSlide

for (let i = 0; i < slidesToShow; i++) {
if (i !== 0) {
selectedSlide = selectedSlide.nextSibling as HTMLElement;
}

const clone = selectedSlide.cloneNode(true) as HTMLElement;
clone.style.width = `${100 / slidesToShow}%`;
clone.classList.add('marquee-clone');

if (clone) {
track.appendChild(clone);
}
}
}
}
}, [sliderTrackRef, marquee, scrollSnap, slidesToShow]);
Copy link
Member

Choose a reason for hiding this comment

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

sliderTrackRef and scrollSnap are unnecessary dependencies.


// NOTE: handle updates to the track's current scroll, which could originate from either the user or the program
useEffect(() => {
const track = sliderTrackRef.current;
Expand Down