-
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.
feature: carousel v2 component (#402)
* feat: add carousel v2 component * chore: update tailwind shadow and style * feat: add carousel v2 to home page * feat: carousel v2 transition animation
- Loading branch information
1 parent
f9e4097
commit 1d26dcc
Showing
7 changed files
with
258 additions
and
22 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,47 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import Carousel from "./"; | ||
|
||
const Card = (props: any) => ( | ||
<div className="text-white mx-4 px-12 py-8 border border-white rounded-md text-8xl font-mono"> | ||
{props.name} | ||
</div> | ||
); | ||
|
||
const meta: Meta<typeof Carousel> = { | ||
title: "Data Display/CarouselV2", | ||
component: Carousel, | ||
tags: ["autodocs"], | ||
decorators: [ | ||
(Story, ctx) => { | ||
return ( | ||
<div className="flex justify-center"> | ||
<Story {...ctx} /> | ||
</div> | ||
); | ||
}, | ||
], | ||
args: { | ||
uniqueKey: "name", | ||
items: [{ name: "TEST 1" }, { name: "TEST 2" }, { name: "TEST 3" }], | ||
Component: Card, | ||
}, | ||
argTypes: { | ||
Component: { | ||
options: ["Card"], | ||
defaultValue: Card, | ||
mapping: { | ||
Card, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof Carousel>; | ||
|
||
export const Playground: Story = { | ||
render: (args) => <Carousel {...args} />, | ||
}; | ||
|
||
Playground.args = {}; |
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,93 @@ | ||
import { CSSProperties, FC, useEffect, useRef, useState } from "react"; | ||
import Icon from "../../Icon/v2/Icon"; | ||
|
||
interface CarouselProps< | ||
Item extends Record<string, unknown>, | ||
Key extends keyof Item = keyof Item, | ||
> { | ||
items: (Item & Record<Key, string>)[]; | ||
uniqueKey: Key; | ||
Component: FC<Item>; | ||
} | ||
|
||
export default function Carousel<Item extends Record<string, unknown>>({ | ||
items, | ||
uniqueKey, | ||
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={item[uniqueKey]} | ||
className="shrink-0 w-[var(--max-width)]" | ||
style={ | ||
{ | ||
"--max-width": `${maxWidth}px`, | ||
} as CSSProperties | ||
} | ||
> | ||
<Component {...item} /> | ||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
<button | ||
type="button" | ||
className={buttonClassName} | ||
onClick={handleChangePage("next")} | ||
> | ||
<Icon name="navArrowRight" className={buttonIconClassName} /> | ||
</button> | ||
</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,5 @@ | ||
import Carousel from "./Carousel"; | ||
|
||
export * from "./Carousel"; | ||
|
||
export default Carousel; |
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