-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: carousel 추가 * docs: 캐러셀 문서 추가 * refactor: 필요없는 type, 속성 제거
- Loading branch information
Showing
6 changed files
with
121 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
|
||
import Carousel from './Carousel'; | ||
|
||
const meta: Meta<typeof Carousel> = { | ||
title: 'Carousel', | ||
component: Carousel, | ||
}; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof Carousel>; | ||
|
||
export const Default: Story = { | ||
args: { | ||
carouselList: [ | ||
{ | ||
id: 0, | ||
children: <div>1</div>, | ||
}, | ||
{ | ||
id: 1, | ||
children: <div>2</div>, | ||
}, | ||
{ | ||
id: 2, | ||
children: <div>3</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,65 @@ | ||
import type { ReactNode } from 'react'; | ||
import { useEffect, useState } from 'react'; | ||
import styled from 'styled-components'; | ||
|
||
export interface CarouselProps { | ||
carouselList: { | ||
id: number; | ||
children: ReactNode; | ||
}[]; | ||
} | ||
|
||
const Carousel = ({ carouselList }: CarouselProps) => { | ||
const extendedCarouselList = [...carouselList, carouselList[0]]; | ||
const [currentIndex, setCurrentIndex] = useState(0); | ||
|
||
const CAROUSEL_WIDTH = window.innerWidth; | ||
|
||
const showNextSlide = () => { | ||
setCurrentIndex((prev) => (prev === carouselList.length ? 0 : prev + 1)); | ||
}; | ||
|
||
useEffect(() => { | ||
const timer = setInterval(showNextSlide, 2000); | ||
|
||
return () => clearInterval(timer); | ||
}, [currentIndex]); | ||
|
||
return ( | ||
<CarouselContainer> | ||
<CarouselWrapper | ||
style={{ | ||
transform: 'translateX(-' + currentIndex * CAROUSEL_WIDTH + 'px)', | ||
transition: currentIndex === length - 1 ? '' : 'all 0.5s ease-in-out', | ||
}} | ||
> | ||
{extendedCarouselList.map(({ id, children }, index) => ( | ||
<CarouselItem | ||
key={index === extendedCarouselList.length - 1 ? `${id}-last` : id} | ||
style={{ width: `${CAROUSEL_WIDTH}px` }} | ||
> | ||
{children} | ||
</CarouselItem> | ||
))} | ||
</CarouselWrapper> | ||
</CarouselContainer> | ||
); | ||
}; | ||
|
||
export default Carousel; | ||
|
||
const CarouselContainer = styled.div` | ||
display: flex; | ||
width: 100%; | ||
border: 1px solid ${({ theme }) => theme.colors.gray2}; | ||
border-radius: 10px; | ||
overflow: hidden; | ||
`; | ||
|
||
const CarouselWrapper = styled.ul` | ||
display: flex; | ||
`; | ||
|
||
const CarouselItem = styled.li` | ||
height: fit-content; | ||
`; |
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 type { CarouselProps } 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