-
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.
Browse files
Browse the repository at this point in the history
Carousel 컴포넌트 Compound 패턴 적용을 통한 재구축
- Loading branch information
Showing
7 changed files
with
191 additions
and
137 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,98 @@ | ||
import LeftIcon from '@assets/svg/left-icon.svg'; | ||
import RightIcon from '@assets/svg/right-icon.svg'; | ||
import { createContext, useMemo } from 'react'; | ||
import type { PropsWithChildren } from 'react'; | ||
|
||
import useCarousel from '@hooks/useCarousel'; | ||
|
||
import Box from '@components/Box/Box'; | ||
|
||
import { | ||
buttonContainerStyling, | ||
containerStyling, | ||
leftButtonStyling, | ||
rightButtonStyling, | ||
sliderWrapperStyling, | ||
} from './Carousel.style'; | ||
import CarouselItem from './CarouselItem'; | ||
import Dots from './Dots'; | ||
|
||
export interface CarouselProps extends PropsWithChildren { | ||
width: number; | ||
height: number; | ||
length: number; | ||
showNavigationOnHover?: boolean; | ||
showArrows?: boolean; | ||
showDots?: boolean; | ||
children?: JSX.Element | JSX.Element[]; | ||
} | ||
|
||
export const CarouselContext = createContext<{ | ||
viewIndex: number; | ||
width: number; | ||
height: number; | ||
itemRef: React.MutableRefObject<HTMLDivElement | null>; | ||
} | null>(null); | ||
|
||
const Carousel = ({ | ||
width, | ||
height, | ||
length, | ||
showNavigationOnHover = true, | ||
showArrows = true, | ||
showDots = true, | ||
children, | ||
}: CarouselProps) => { | ||
const { viewIndex, itemRef, carouselBoxRef, handleMoveImage, handleClickLeft, handleClickRight } = | ||
useCarousel(length); | ||
|
||
const context = useMemo( | ||
() => ({ | ||
width, | ||
height, | ||
viewIndex, | ||
itemRef, | ||
carouselBoxRef, | ||
handleMoveImage, | ||
handleClickLeft, | ||
handleClickRight, | ||
}), | ||
[ | ||
width, | ||
height, | ||
viewIndex, | ||
itemRef, | ||
carouselBoxRef, | ||
handleMoveImage, | ||
handleClickLeft, | ||
handleClickRight, | ||
] | ||
); | ||
|
||
return ( | ||
<CarouselContext.Provider value={context}> | ||
<div css={containerStyling(width, height)} ref={carouselBoxRef}> | ||
{showArrows && length !== 1 && ( | ||
<div css={buttonContainerStyling(showNavigationOnHover)}> | ||
<button type="button" css={leftButtonStyling} onClick={handleClickLeft}> | ||
<LeftIcon /> | ||
</button> | ||
<button type="button" css={rightButtonStyling} onClick={handleClickRight}> | ||
<RightIcon /> | ||
</button> | ||
</div> | ||
)} | ||
|
||
{showDots && ( | ||
<Dots imageLength={length} activeNumber={viewIndex} moveImage={handleMoveImage} /> | ||
)} | ||
|
||
<Box css={sliderWrapperStyling(width, height)}>{children}</Box> | ||
</div> | ||
</CarouselContext.Provider> | ||
); | ||
}; | ||
|
||
Carousel.Item = CarouselItem; | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { useContext, useEffect, useRef } from 'react'; | ||
import type { PropsWithChildren } from 'react'; | ||
|
||
import { CarouselContext } from '@components/GeneralCarousel/Carousel'; | ||
import { carouselItemStyling } from '@components/GeneralCarousel/Carousel.style'; | ||
|
||
export interface CarouselItemProps extends PropsWithChildren { | ||
index: number; | ||
} | ||
|
||
const CarouselItem = ({ index, children }: CarouselItemProps) => { | ||
const ref = useRef<HTMLDivElement | null>(null); | ||
const context = useContext(CarouselContext); | ||
|
||
if (!context) throw Error('Carousel.Item is only available within Carousel.'); | ||
|
||
const { width, height, viewIndex, itemRef } = context; | ||
|
||
useEffect(() => { | ||
if (ref.current) { | ||
if (index === viewIndex) itemRef.current = ref.current; | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [viewIndex]); | ||
|
||
return ( | ||
<div ref={ref} css={carouselItemStyling(width, height)}> | ||
{children} | ||
</div> | ||
); | ||
}; | ||
|
||
export default CarouselItem; |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.