Skip to content

Commit

Permalink
refactor: Carousel 컴포넌트 이벤트 버블링 방지 코드 추가 (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
dladncks1217 authored Jan 7, 2024
1 parent 603ca2d commit 9192b39
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
11 changes: 9 additions & 2 deletions src/components/GeneralCarousel/Dots.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { css } from '@emotion/react';
import type { MouseEvent } from 'react';

import { Theme } from '@styles/Theme';

Expand All @@ -20,15 +21,21 @@ const Dots = ({ imageLength, activeNumber, moveImage }: DotsProps) => {
type="button"
key={crypto.randomUUID()}
css={dotStyle(true)}
onClick={() => moveImage(index)}
onClick={(e: MouseEvent<HTMLButtonElement>) => {
e.stopPropagation();
moveImage(index);
}}
/>
);
return (
<button
type="button"
key={crypto.randomUUID()}
css={dotStyle(false)}
onClick={() => moveImage(index)}
onClick={(e: MouseEvent<HTMLButtonElement>) => {
e.stopPropagation();
moveImage(index);
}}
/>
);
})}
Expand Down
4 changes: 3 additions & 1 deletion src/components/GeneralCarousel/GeneralCarousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface useGeneralCarouselProps {
width: number;
height: number;
items: React.FC<React.SVGProps<SVGSVGElement>>[] | string[];
showNavigationOnHover?: boolean;
showArrows?: boolean;
showDots?: boolean;
}
Expand All @@ -26,6 +27,7 @@ const GeneralCarousel = ({
width,
height,
items,
showNavigationOnHover = true,
showArrows = true,
showDots = true,
}: useGeneralCarouselProps) => {
Expand All @@ -38,7 +40,7 @@ const GeneralCarousel = ({
<Dots imageLength={items.length} activeNumber={viewIndex} moveImage={handleMoveImage} />
)}
{showArrows && items.length !== 1 && (
<div css={getButtonContainerStyling(true)}>
<div css={getButtonContainerStyling(showNavigationOnHover)}>
<button type="button" css={leftButtonStyling} onClick={handleClickLeft}>
<LeftIcon />
</button>
Expand Down
7 changes: 5 additions & 2 deletions src/hooks/useGeneralCarousel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useRef, useState } from 'react';
import type { MouseEvent } from 'react';
import { flushSync } from 'react-dom';

const useGeneralCarousel = (items: React.FC<React.SVGProps<SVGSVGElement>>[] | string[]) => {
Expand All @@ -20,7 +21,8 @@ const useGeneralCarousel = (items: React.FC<React.SVGProps<SVGSVGElement>>[] | s
}
};

const handleClickLeft = () => {
const handleClickLeft = (e: MouseEvent<HTMLButtonElement>) => {
e.stopPropagation();
if (itemRef.current) {
flushSync(() => {
if (viewIndex === 0) setViewIndex(0);
Expand All @@ -35,7 +37,8 @@ const useGeneralCarousel = (items: React.FC<React.SVGProps<SVGSVGElement>>[] | s
}
};

const handleClickRight = () => {
const handleClickRight = (e: MouseEvent<HTMLButtonElement>) => {
e.stopPropagation();
if (itemRef.current) {
flushSync(() => {
if (viewIndex === items.length - 1) setViewIndex(viewIndex);
Expand Down

0 comments on commit 9192b39

Please sign in to comment.