-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
271 additions
and
204 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 |
---|---|---|
@@ -1,37 +1,34 @@ | ||
.saltCarousel { | ||
position: relative; | ||
overflow: hidden; | ||
display: grid; | ||
grid-template-columns: min-content auto min-content; | ||
grid-template-areas: "prev-button slider next-button" "dots dots dots"; | ||
} | ||
/*.saltCarousel {*/ | ||
/* position: relative;*/ | ||
/* overflow: hidden;*/ | ||
/* display: grid;*/ | ||
/* grid-template-columns: min-content auto min-content;*/ | ||
/* grid-template-areas: "prev-button slider next-button" "dots dots dots";*/ | ||
/*}*/ | ||
|
||
.saltCarousel.saltCarousel-compact { | ||
grid-template-areas: "slider slider slider" "prev-button dots next-button"; | ||
} | ||
/*.saltCarousel.saltCarousel-compact {*/ | ||
/* grid-template-areas: "slider slider slider" "prev-button dots next-button";*/ | ||
/*}*/ | ||
|
||
.saltCarousel-scroll { | ||
display: flex; | ||
scroll-snap-type: x mandatory; | ||
overflow-x: scroll; | ||
scroll-behavior: smooth; | ||
} | ||
/*.saltCarousel-scroll {*/ | ||
|
||
.saltCarousel-prev-button { | ||
grid-area: prev-button; | ||
height: 100%; | ||
} | ||
/*}*/ | ||
|
||
.saltCarousel-next-button { | ||
grid-area: next-button; | ||
height: 100%; | ||
} | ||
/*.saltCarousel-prev-button {*/ | ||
/* grid-area: prev-button;*/ | ||
/* height: 100%;*/ | ||
/*}*/ | ||
|
||
.saltCarousel-slider { | ||
grid-area: slider; | ||
} | ||
/*.saltCarousel-next-button {*/ | ||
/* grid-area: next-button;*/ | ||
/* height: 100%;*/ | ||
/*}*/ | ||
|
||
.saltCarousel-dots { | ||
grid-area: dots; | ||
justify-self: center; | ||
} | ||
/*.saltCarousel-slider {*/ | ||
/* grid-area: slider;*/ | ||
/*}*/ | ||
|
||
/*.saltCarousel-dots {*/ | ||
/* grid-area: dots;*/ | ||
/* justify-self: center;*/ | ||
/*}*/ |
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
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 |
---|---|---|
@@ -1,28 +1,60 @@ | ||
import { createContext } from "@salt-ds/core"; | ||
import { type SyntheticEvent, useContext } from "react"; | ||
import { | ||
type ReactNode, | ||
type SyntheticEvent, | ||
useContext, | ||
useState, | ||
} from "react"; | ||
|
||
export interface CarouselContextValue { | ||
activeSlide: number; | ||
|
||
nextSlide: (event: SyntheticEvent) => void; | ||
prevSlide: (event: SyntheticEvent) => void; | ||
goToSlide: (index: number) => void; | ||
slides: string[]; | ||
registerSlide: (slideId: string) => void; | ||
} | ||
|
||
export const CarouselContext = createContext<CarouselContextValue>( | ||
export const CarouselContext = createContext<CarouselContextValue | null>( | ||
"CarouselContext", | ||
{ | ||
activeSlide: 0, | ||
nextSlide: () => undefined, | ||
prevSlide: () => undefined, | ||
goToSlide: () => undefined, | ||
slides: [], | ||
registerSlide: () => undefined, | ||
}, | ||
null, | ||
); | ||
|
||
export function useCarousel() { | ||
return useContext(CarouselContext); | ||
const context = useContext(CarouselContext); | ||
if (!context) { | ||
throw new Error("useCarousel must be used within carousel provider"); | ||
} | ||
return context; | ||
} | ||
|
||
export function CarouselProvider({ children }: { children: ReactNode }) { | ||
// TODO: check active slide to initialy set the carousel prop | ||
const [activeSlide, setActiveSlide] = useState(0); | ||
const [slides, setSlides] = useState<string[]>([]); | ||
|
||
const registerSlide = (slideId: string) => { | ||
setSlides((prev) => [...prev, slideId]); | ||
}; | ||
const scrollToSlide = (index: number) => { | ||
setActiveSlide(index); | ||
}; | ||
const nextSlide = () => scrollToSlide(activeSlide + 1); | ||
const prevSlide = () => scrollToSlide(activeSlide - 1); | ||
const goToSlide = (index: number) => scrollToSlide(index); | ||
|
||
return ( | ||
<CarouselContext.Provider | ||
value={{ | ||
activeSlide, | ||
nextSlide, | ||
prevSlide, | ||
goToSlide, | ||
slides, | ||
registerSlide, | ||
}} | ||
> | ||
{children} | ||
</CarouselContext.Provider> | ||
); | ||
} |
Empty file.
Oops, something went wrong.