Skip to content

Commit

Permalink
�Carousel 추가 (#82)
Browse files Browse the repository at this point in the history
* feat: carousel 추가

* docs: 캐러셀 문서 추가

* refactor: 필요없는 type, 속성 제거
  • Loading branch information
hae-on committed Nov 6, 2023
1 parent ca333e5 commit b8abbad
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 3 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,23 @@ const Parent = () => {

<br />

## Carousel

캐러셀 컴포넌트입니다.

### Example

```jsx
const carouselList = [0, 1, 2].map((index) => ({
id: index,
children: <div>{index}</div>,
}));

<Carousel carouselList={carouselList} />;
```

<br />

## **Checkbox**

체크박스 컴포넌트입니다.
Expand Down
30 changes: 30 additions & 0 deletions src/Carousel/Carousel.stories.tsx
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>,
},
],
},
};
65 changes: 65 additions & 0 deletions src/Carousel/Carousel.tsx
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;
`;
5 changes: 5 additions & 0 deletions src/Carousel/index.ts
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;
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ export { default as Badge } from './Badge';
export { default as BottomSheet } from './BottomSheet';
export * from './BottomSheet';
export { default as Button } from './Button';
export { default as Carousel } from './Carousel';
export { default as Checkbox } from './Checkbox';
export { default as Divider } from './Divider';
export { default as Heading } from './Heading';
export { default as Input } from './Input';
export { default as Link } from './Link';
export { default as Skeleton } from './Skeleton';
export { default as Spacing } from './Spacing';
export { default as Text } from './Text';
export { default as Textarea } from './Textarea';
export { default as Input } from './Input';
export { default as Skeleton } from './Skeleton';
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ComponentPropsWithRef, ComponentPropsWithoutRef, ElementType } from 'react';
import type { ComponentPropsWithRef, ComponentPropsWithoutRef, ElementType, ReactNode } from 'react';

export type Sizes = 'xs' | 'sm' | 'md' | 'lg' | 'xl';

Expand Down

0 comments on commit b8abbad

Please sign in to comment.