Skip to content

Commit

Permalink
feat: impl banner component
Browse files Browse the repository at this point in the history
  • Loading branch information
malloc3141 committed Nov 17, 2024
1 parent 6cb0184 commit 70a5938
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
Binary file added packages/web/public/temp/1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added packages/web/public/temp/2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added packages/web/public/temp/3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
83 changes: 83 additions & 0 deletions packages/web/src/common/components/Banner/Banner.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import React, { useState, useEffect } from "react";
import styled from "styled-components";
import Image from "next/image";

type BannerProps = {
images?: string[];
onClick?: (idx: number) => void;
};

const BannerWrapper = styled.div`
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
padding: 0px;
gap: 0px;
position: relative;
border-radius: 4px;
`;

const Arrow = styled.button`
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
padding: 5px;
gap: 0px;
margin: 0 -40px;
width: 18px;
height: 18px;
background: rgba(94, 94, 94, 0.5);
color: white;
border-radius: 4px;
z-index: 1;
`;

const Banner: React.FC<BannerProps> = ({
images = ["temp/1.jpg", "temp/2.jpg", "temp/3.jpg"],
onClick = _idx => {},
}) => {
const [currentIndex, setCurrentIndex] = useState<number>(0);

const handlePrev = () => {
const index = currentIndex === 0 ? images.length - 1 : currentIndex - 1;
setCurrentIndex(index);
};

const handleNext = () => {
const index = currentIndex === images.length - 1 ? 0 : currentIndex + 1;
setCurrentIndex(index);
};

useEffect(() => {
const interval = setInterval(() => {
handleNext();
}, 5000); // 5초마다 이미지 변경

return () => clearInterval(interval);
}, [currentIndex]);

return (
<BannerWrapper>
<Arrow onClick={handlePrev}>{"<"}</Arrow>

<div>
<Image
src={`/${images[currentIndex]}`}
alt={`배너 이미지 ${currentIndex + 1}`}
width={840}
height={336}
onClick={() => onClick(currentIndex)}
/>
</div>

<Arrow onClick={handleNext}>{">"}</Arrow>
</BannerWrapper>
);
};
export default Banner;

0 comments on commit 70a5938

Please sign in to comment.