Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Carousel 추가 #82

Merged
merged 4 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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