-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SOEOPSFY24-206 | add slider component
- Loading branch information
1 parent
ca1b925
commit bbbb296
Showing
18 changed files
with
269 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import React, { HTMLAttributes } from "react"; | ||
import { Slideshow } from "@/components/Slideshow/Slideshow"; | ||
|
||
type Props = HTMLAttributes<HTMLElement> & { | ||
children: React.ReactNode; | ||
}; | ||
|
||
export const Carousel = ({ children, ...props }: Props) => { | ||
const slides = React.Children.toArray(children); | ||
|
||
return ( | ||
<article {...props} aria-labelledby="carousel-label"> | ||
<div className="relative left-1/2 mb-32 mt-0 w-screen -translate-x-1/2"> | ||
<Slideshow className="mx-auto w-[calc(100%-50px)] xl:w-[calc(100%-150px)]"> | ||
{slides.map((slide, slideIndex) => { | ||
return ( | ||
<CarouselSlide | ||
key={slideIndex} // You can use slideIndex or slide.id if available | ||
slide={slide} | ||
slideNumber={slideIndex + 1} | ||
totalSlides={slides.length} | ||
/> | ||
); | ||
})} | ||
</Slideshow> | ||
</div> | ||
</article> | ||
); | ||
}; | ||
|
||
const CarouselSlide = ({ | ||
slide, | ||
slideNumber, | ||
totalSlides, | ||
}: { | ||
slide: React.ReactNode; | ||
slideNumber: number; | ||
totalSlides: number; | ||
}) => { | ||
const labelId = `slide-${slideNumber}`; | ||
|
||
return ( | ||
<div | ||
role="group" | ||
aria-roledescription="slide" | ||
aria-labelledby={labelId} | ||
aria-label={`${slideNumber} of ${totalSlides}`} | ||
> | ||
{slide} | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
"use client"; | ||
|
||
import { HTMLAttributes, JSX, useEffect, useRef } from "react"; | ||
import Slider, { CustomArrowProps, Settings } from "react-slick"; | ||
import { | ||
ArrowLongRightIcon, | ||
ArrowLongLeftIcon, | ||
} from "@heroicons/react/16/solid"; | ||
import { cnb } from "cnbuilder"; | ||
|
||
const NextArrow = ({ className, onClick }: CustomArrowProps) => { | ||
const slickDisabled = className?.includes("slick-disabled"); | ||
return ( | ||
<button | ||
className={cnb( | ||
"hocus:outline-3 absolute right-5 top-1/2 z-50 flex h-20 w-20 items-center justify-center rounded-full border-2 border-white bg-digital-red hocus:bg-digital-red hocus:outline hocus:outline-digital-red", | ||
{ | ||
"bg-black-40 hocus:bg-black-40 hocus:outline-0": slickDisabled, | ||
}, | ||
)} | ||
onClick={onClick} | ||
aria-label="Next" | ||
disabled={slickDisabled} | ||
> | ||
<ArrowLongRightIcon width={40} className="text-white" /> | ||
</button> | ||
); | ||
}; | ||
|
||
const PrevArrow = ({ className, onClick }: CustomArrowProps) => { | ||
const slickDisabled = className?.includes("slick-disabled"); | ||
return ( | ||
<button | ||
className={cnb( | ||
"hocus:outline-3 absolute left-5 top-1/2 z-50 flex h-20 w-20 items-center justify-center rounded-full border-2 border-white bg-digital-red hocus:bg-digital-red hocus:outline hocus:outline-digital-red", | ||
{ | ||
"bg-black-40 hocus:bg-black-40 hocus:outline-0": slickDisabled, | ||
}, | ||
)} | ||
onClick={onClick} | ||
aria-label="Previous" | ||
disabled={slickDisabled} | ||
> | ||
<ArrowLongLeftIcon width={40} className="text-white" /> | ||
</button> | ||
); | ||
}; | ||
|
||
type SlideshowProps = HTMLAttributes<HTMLDivElement> & { | ||
children: JSX.Element | JSX.Element[]; | ||
slideshowProps?: Omit<Settings, "children">; | ||
}; | ||
|
||
// Slide padding styles are added in the tailwind index.css file. | ||
export const Slideshow = ({ | ||
children, | ||
slideshowProps, | ||
...props | ||
}: SlideshowProps) => { | ||
const slideShowRef = useRef<HTMLDivElement>(null); | ||
|
||
const adjustSlideLinks = () => { | ||
// Set tabindex attributes based on if the slides are visible or not. | ||
const hiddenLinks = slideShowRef.current?.querySelectorAll( | ||
".slick-slide:not(.slick-active) a", | ||
); | ||
if (hiddenLinks) { | ||
[...hiddenLinks].map((link) => link.setAttribute("tabindex", "-1")); | ||
} | ||
|
||
const visibleLinks = slideShowRef.current?.querySelectorAll( | ||
".slick-slide.slick-active a", | ||
); | ||
if (visibleLinks) { | ||
[...visibleLinks].map((link) => link.removeAttribute("tabindex")); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
adjustSlideLinks(); | ||
}, []); | ||
|
||
const settings: Settings = { | ||
afterChange: () => adjustSlideLinks(), | ||
autoplay: false, | ||
centerMode: false, | ||
className: | ||
"[&_.slick-track]:flex [&_.slick-track] [&_.slick-slider]:relative [&_.slick-slide>div]:h-full [&_.slick-slide>div>div]:h-full", | ||
dots: false, | ||
infinite: false, | ||
initialSlide: 0, | ||
nextArrow: <NextArrow />, | ||
prevArrow: <PrevArrow />, | ||
slidesToScroll: 1, | ||
slidesToShow: 3, | ||
speed: 500, | ||
responsive: [ | ||
{ | ||
breakpoint: 1200, | ||
settings: { | ||
slidesToShow: 2, | ||
slidesToScroll: 1, | ||
centerMode: false, | ||
}, | ||
}, | ||
{ | ||
breakpoint: 768, | ||
settings: { | ||
slidesToShow: 1, | ||
slidesToScroll: 1, | ||
centerMode: false, | ||
}, | ||
}, | ||
], | ||
...slideshowProps, | ||
}; | ||
return ( | ||
<section | ||
ref={slideShowRef} | ||
{...props} | ||
aria-roledescription="carousel" | ||
className={cnb("relative", props.className)} | ||
> | ||
<Slider {...settings}>{children}</Slider> | ||
</section> | ||
); | ||
}; |
Large diffs are not rendered by default.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
out/_next/static/css/8ffb7d69ec5bfd11.css → out/_next/static/css/6e166f209665cd3e.css
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.