-
Notifications
You must be signed in to change notification settings - Fork 1
feat: Implement carousel component with mock data #107
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7d44b3c
feat: implement carousel component and mock data
BIA3IA b863417
refactor: simplify section and div elements in carousel component
BIA3IA 623b9bf
feat: enhance keyboard navigation for vertical carousel orientation
BIA3IA 4645ad4
chore: remove for merge
toto04 8c87169
Merge remote-tracking branch 'origin/main' into bianca/carousel
toto04 2b3a451
Merge branch 'main' into bianca/carousel
toto04 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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,49 @@ | ||
| "use client" | ||
|
|
||
| import { CardCaption } from "@/components/card-caption" | ||
| import { Carousel, CarouselContent, CarouselDots, CarouselItem } from "@/components/ui/carousel" | ||
|
|
||
| const mockCards = [ | ||
| { | ||
| title: "WeBeepSync", | ||
| caption: | ||
| "WeBeep Sync è una semplice app, user-friendly e senza compromessi che serve per tenere sincronizzati tutti i tuoi file di WeBeep.", | ||
| }, | ||
| { | ||
| title: "PolimiSchedule", | ||
| caption: | ||
| "Genera un file iCalendar (.ics) a partire dal formato testuale dell’Orario delle lezioni. Possibilità di importare su Google Calendar.", | ||
| }, | ||
| { | ||
| title: "WiFiLinux", | ||
| caption: "Scarica ed esegui lo script Python per attivare la connessione permanente al WiFi Polimi.", | ||
| }, | ||
| { | ||
| title: "The TOL Project", | ||
| caption: "Un simulatore gratuito del test di ammissione per le aspiranti matricole di Ingegneria del PoliMi.", | ||
| }, | ||
| ] as const | ||
|
|
||
| // TODO: delete this when merging | ||
| export function CarouselMock() { | ||
| return ( | ||
| <section className="mx-auto flex min-h-screen w-full max-w-4xl flex-col items-center justify-center gap-16 px-7.5"> | ||
| <h1 className="typo-headline-medium sm:typo-display-large bg-linear-to-r from-text-primary via-blue-secondary to-blue-primary bg-clip-text text-transparent"> | ||
| PoliNetwork | ||
| </h1> | ||
|
|
||
| <Carousel className="w-full"> | ||
| <CarouselContent> | ||
| {mockCards.map((card) => ( | ||
| <CarouselItem key={card.title}> | ||
| <div className="flex justify-center"> | ||
| <CardCaption {...card} /> | ||
| </div> | ||
| </CarouselItem> | ||
| ))} | ||
| </CarouselContent> | ||
| <CarouselDots className="mt-8" /> | ||
| </Carousel> | ||
| </section> | ||
| ) | ||
| } |
This file contains hidden or 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,194 @@ | ||
| "use client" | ||
|
|
||
| import useEmblaCarousel, { type UseEmblaCarouselType } from "embla-carousel-react" | ||
| import * as React from "react" | ||
| import { Button } from "@/components/ui/button" | ||
| import { cn } from "@/lib/utils" | ||
|
|
||
| type CarouselApi = UseEmblaCarouselType[1] | ||
| type UseCarouselParameters = Parameters<typeof useEmblaCarousel> | ||
| type CarouselOptions = UseCarouselParameters[0] | ||
| type CarouselPlugin = UseCarouselParameters[1] | ||
|
|
||
| type CarouselProps = { | ||
| opts?: CarouselOptions | ||
| plugins?: CarouselPlugin | ||
| orientation?: "horizontal" | "vertical" | ||
| setApi?: (api: CarouselApi) => void | ||
| } | ||
|
|
||
| type CarouselContextProps = { | ||
| carouselRef: ReturnType<typeof useEmblaCarousel>[0] | ||
| api: ReturnType<typeof useEmblaCarousel>[1] | ||
| scrollPrev: () => void | ||
| scrollNext: () => void | ||
| canScrollPrev: boolean | ||
| canScrollNext: boolean | ||
| selectedIndex: number | ||
| scrollSnaps: number[] | ||
| } & CarouselProps | ||
|
|
||
| const CarouselContext = React.createContext<CarouselContextProps | null>(null) | ||
|
|
||
| function useCarousel() { | ||
| const context = React.useContext(CarouselContext) | ||
|
|
||
| if (!context) { | ||
| throw new Error("useCarousel must be used within a <Carousel />") | ||
| } | ||
|
|
||
| return context | ||
| } | ||
|
|
||
| function Carousel({ | ||
| orientation = "horizontal", | ||
| opts, | ||
| setApi, | ||
| plugins, | ||
| className, | ||
| children, | ||
| ...props | ||
| }: React.ComponentProps<"div"> & CarouselProps) { | ||
| const [carouselRef, api] = useEmblaCarousel( | ||
| { | ||
| ...opts, | ||
| axis: orientation === "horizontal" ? "x" : "y", | ||
| }, | ||
| plugins | ||
| ) | ||
| const [canScrollPrev, setCanScrollPrev] = React.useState(false) | ||
| const [canScrollNext, setCanScrollNext] = React.useState(false) | ||
| const [selectedIndex, setSelectedIndex] = React.useState(0) | ||
| const [scrollSnaps, setScrollSnaps] = React.useState<number[]>([]) | ||
|
|
||
| const onSelect = React.useCallback((api: CarouselApi) => { | ||
| if (!api) return | ||
| setCanScrollPrev(api.canScrollPrev()) | ||
| setCanScrollNext(api.canScrollNext()) | ||
| setSelectedIndex(api.selectedScrollSnap()) | ||
| setScrollSnaps(api.scrollSnapList()) | ||
| }, []) | ||
|
|
||
| const scrollPrev = React.useCallback(() => { | ||
| api?.scrollPrev() | ||
| }, [api]) | ||
|
|
||
| const scrollNext = React.useCallback(() => { | ||
| api?.scrollNext() | ||
| }, [api]) | ||
|
|
||
| const handleKeyDown = React.useCallback( | ||
| (event: React.KeyboardEvent<HTMLDivElement>) => { | ||
| if (orientation === "vertical") { | ||
| if (event.key === "ArrowUp") { | ||
| event.preventDefault() | ||
| scrollPrev() | ||
| } else if (event.key === "ArrowDown") { | ||
| event.preventDefault() | ||
| scrollNext() | ||
| } | ||
| } else if (event.key === "ArrowLeft") { | ||
| event.preventDefault() | ||
| scrollPrev() | ||
| } else if (event.key === "ArrowRight") { | ||
| event.preventDefault() | ||
| scrollNext() | ||
| } | ||
| }, | ||
| [orientation, scrollPrev, scrollNext] | ||
| ) | ||
|
|
||
| React.useEffect(() => { | ||
| if (!api || !setApi) return | ||
| setApi(api) | ||
| }, [api, setApi]) | ||
|
|
||
| React.useEffect(() => { | ||
| if (!api) return | ||
| onSelect(api) | ||
| api.on("reInit", onSelect) | ||
| api.on("select", onSelect) | ||
|
|
||
| return () => { | ||
| api.off("reInit", onSelect) | ||
| api.off("select", onSelect) | ||
| } | ||
| }, [api, onSelect]) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| return ( | ||
| <CarouselContext.Provider | ||
| value={{ | ||
| carouselRef, | ||
| api: api, | ||
| opts, | ||
| orientation: orientation || (opts?.axis === "y" ? "vertical" : "horizontal"), | ||
| scrollPrev, | ||
| scrollNext, | ||
| canScrollPrev, | ||
| canScrollNext, | ||
| selectedIndex, | ||
| scrollSnaps, | ||
| }} | ||
| > | ||
| <section onKeyDownCapture={handleKeyDown} className={cn("relative", className)} data-slot="carousel" {...props}> | ||
| {children} | ||
| </section> | ||
| </CarouselContext.Provider> | ||
| ) | ||
| } | ||
|
|
||
| function CarouselContent({ className, ...props }: React.ComponentProps<"div">) { | ||
| const { carouselRef, orientation } = useCarousel() | ||
|
|
||
| return ( | ||
| <div ref={carouselRef} className="overflow-hidden" data-slot="carousel-content"> | ||
| <div className={cn("flex", orientation === "horizontal" ? "-ml-4" : "-mt-4 flex-col", className)} {...props} /> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| function CarouselItem({ className, ...props }: React.ComponentProps<"fieldset">) { | ||
| const { orientation } = useCarousel() | ||
|
|
||
| return ( | ||
| <fieldset | ||
| aria-roledescription="slide" | ||
| data-slot="carousel-item" | ||
| className={cn("min-w-0 shrink-0 grow-0 basis-full", orientation === "horizontal" ? "pl-4" : "pt-4", className)} | ||
| {...props} | ||
| /> | ||
| ) | ||
| } | ||
|
|
||
| function CarouselDots({ className, ...props }: React.ComponentProps<"div">) { | ||
| const { api, scrollSnaps, selectedIndex } = useCarousel() | ||
|
|
||
| if (scrollSnaps.length <= 1) return null | ||
|
|
||
| return ( | ||
| <div data-slot="carousel-dots" className={cn("flex items-center justify-center gap-3", className)} {...props}> | ||
| {scrollSnaps.map((scrollSnap, index) => ( | ||
| <Button | ||
| key={scrollSnap} | ||
| type="button" | ||
| aria-label={`Go to slide ${index + 1}`} | ||
| aria-current={selectedIndex === index} | ||
| onClick={() => api?.scrollTo(index)} | ||
| className={cn( | ||
| "flex size-5 appearance-none items-center justify-center rounded-full bg-transparent p-0 outline-none transition-colors" | ||
| )} | ||
| > | ||
| <span | ||
| className={cn( | ||
| "block size-3 rounded-full transition-colors", | ||
| selectedIndex === index ? "bg-white/95" : "bg-white/20" | ||
| )} | ||
| /> | ||
| <span className="sr-only">{`Slide ${index + 1}`}</span> | ||
| </Button> | ||
| ))} | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| export { type CarouselApi, Carousel, CarouselContent, CarouselDots, CarouselItem } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.