-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #417 from Game-as-a-Service/feature/carousel-v2
feature: carousel v2
- Loading branch information
Showing
14 changed files
with
489 additions
and
131 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
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
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,90 +1,23 @@ | ||
import { CSSProperties, FC, Key, useEffect, useRef, useState } from "react"; | ||
import Icon from "@/components/shared/Icon"; | ||
import CarouselButton from "./CarouselButton"; | ||
import CarouselPagination from "./CarouselPagination"; | ||
import CarouselMain from "./CarouselMain"; | ||
import { CarouselProvider } from "./CarouselContext"; | ||
import { CarouselProps, TItem } from "./Carousel.type"; | ||
|
||
interface CarouselProps<Item extends Record<string, unknown>> { | ||
items: Item[]; | ||
renderKey: (item: Item) => Key; | ||
Component: FC<Item>; | ||
} | ||
|
||
export default function Carousel<Item extends Record<string, unknown>>({ | ||
export default function Carousel<Item extends TItem>({ | ||
items, | ||
renderKey, | ||
Component, | ||
}: Readonly<CarouselProps<Item>>) { | ||
const [showIndex, setShowIndex] = useState(0); | ||
const [maxWidth, setMaxWidth] = useState(0); | ||
const carouselRef = useRef<HTMLDivElement>(null); | ||
|
||
const handleChangePage = (action: "prev" | "next") => () => { | ||
const maxIndex = items.length - 1; | ||
|
||
switch (action) { | ||
case "prev": | ||
setShowIndex((preIndex) => { | ||
const newIndex = preIndex - 1; | ||
return newIndex > -1 ? newIndex : maxIndex; | ||
}); | ||
break; | ||
case "next": | ||
setShowIndex((preIndex) => { | ||
const newIndex = preIndex + 1; | ||
return newIndex <= maxIndex ? newIndex : 0; | ||
}); | ||
break; | ||
default: | ||
} | ||
}; | ||
|
||
const buttonClassName = | ||
"p-2.5 shrink-0 bg-white/4 shadow-default rounded-2xl"; | ||
const buttonIconClassName = "stroke-white w-6 h-6 pointer-events-none"; | ||
|
||
useEffect(() => { | ||
setMaxWidth(carouselRef.current?.clientWidth || 0); | ||
}, []); | ||
|
||
return ( | ||
<div className="flex items-center"> | ||
<button | ||
type="button" | ||
className={buttonClassName} | ||
onClick={handleChangePage("prev")} | ||
> | ||
<Icon name="NavArrowLeft" className={buttonIconClassName} /> | ||
</button> | ||
<div ref={carouselRef} className="overflow-hidden w-full"> | ||
<ul | ||
className="flex transition-transform translate-x-[var(--translate-x)]" | ||
style={ | ||
{ | ||
"--translate-x": `${showIndex * maxWidth * -1}px`, | ||
} as CSSProperties | ||
} | ||
> | ||
{Array.isArray(items) && | ||
items.map((item) => ( | ||
<li | ||
key={renderKey(item)} | ||
className="shrink-0 w-[var(--max-width)]" | ||
style={ | ||
{ | ||
"--max-width": `${maxWidth}px`, | ||
} as CSSProperties | ||
} | ||
> | ||
<Component {...item} /> | ||
</li> | ||
))} | ||
</ul> | ||
<CarouselProvider items={items} renderKey={renderKey} Component={Component}> | ||
<div className="flex items-center"> | ||
<CarouselButton type="previous" /> | ||
<CarouselMain> | ||
<CarouselPagination /> | ||
</CarouselMain> | ||
<CarouselButton type="next" /> | ||
</div> | ||
<button | ||
type="button" | ||
className={buttonClassName} | ||
onClick={handleChangePage("next")} | ||
> | ||
<Icon name="NavArrowRight" className={buttonIconClassName} /> | ||
</button> | ||
</div> | ||
</CarouselProvider> | ||
); | ||
} |
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,51 @@ | ||
import { Dispatch, FC, Key, PropsWithChildren } from "react"; | ||
import { IconName } from "@/components/shared/Icon"; | ||
|
||
export type TItem = Record<string, unknown> & { id: Key }; | ||
|
||
export type CarouselItemProps<Item extends TItem> = Item & { | ||
showIndex: number; | ||
index: number; | ||
}; | ||
|
||
export interface ICarouselContext<Item extends TItem = TItem> { | ||
items: Item[]; | ||
showIndex: number; | ||
renderKey: (item: Item) => Key; | ||
Component: FC<CarouselItemProps<Item>>; | ||
} | ||
|
||
export interface CarouselProps<Item extends TItem = TItem> { | ||
items: ICarouselContext<Item>["items"]; | ||
Component: ICarouselContext<Item>["Component"]; | ||
renderKey?: ICarouselContext<Item>["renderKey"]; | ||
} | ||
|
||
export const enum CarouselActionType { | ||
Previous = "previous", | ||
Next = "next", | ||
SetPage = "setPage", | ||
UpdateItems = "updateItems", | ||
} | ||
|
||
export type TCarouselAction<Item extends TItem> = | ||
| { type: CarouselActionType.Previous } | ||
| { type: CarouselActionType.Next } | ||
| { type: CarouselActionType.SetPage; payload: { page: number } } | ||
| { type: CarouselActionType.UpdateItems; payload: { items: Item[] } }; | ||
|
||
export type TCarouselDispatchContext<Item extends TItem = TItem> = Dispatch< | ||
TCarouselAction<Item> | ||
>; | ||
|
||
export interface CarouselProviderProps<Item extends TItem> | ||
extends PropsWithChildren { | ||
items: ICarouselContext<Item>["items"]; | ||
Component: ICarouselContext<Item>["Component"]; | ||
renderKey?: ICarouselContext<Item>["renderKey"]; | ||
} | ||
|
||
export interface CarouselButtonConfig { | ||
iconName: IconName; | ||
actionType: CarouselActionType.Previous | CarouselActionType.Next; | ||
} |
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,42 @@ | ||
import Icon from "@/components/shared/Icon"; | ||
import { useCarouselDispatch } from "./CarouselContext"; | ||
import { CarouselActionType, CarouselButtonConfig } from "./Carousel.type"; | ||
|
||
export const enum CarouselButtonType { | ||
Previous = "previous", | ||
Next = "next", | ||
} | ||
|
||
interface CarouselButtonProps { | ||
type: CarouselButtonType | `${CarouselButtonType}`; | ||
} | ||
|
||
const configs: Record<CarouselButtonType, CarouselButtonConfig> = { | ||
[CarouselButtonType.Previous]: { | ||
iconName: "NavArrowLeft", | ||
actionType: CarouselActionType.Previous, | ||
}, | ||
[CarouselButtonType.Next]: { | ||
iconName: "NavArrowRight", | ||
actionType: CarouselActionType.Next, | ||
}, | ||
}; | ||
|
||
export default function CarouselButton({ | ||
type, | ||
}: Readonly<CarouselButtonProps>) { | ||
const dispatch = useCarouselDispatch(); | ||
const config = configs[type]; | ||
return ( | ||
<button | ||
type="button" | ||
className="p-2.5 shrink-0 bg-white/4 shadow-default rounded-2xl" | ||
onClick={() => dispatch({ type: config.actionType })} | ||
> | ||
<Icon | ||
name={config.iconName} | ||
className="stroke-white w-6 h-6 pointer-events-none" | ||
/> | ||
</button> | ||
); | ||
} |
Oops, something went wrong.