Skip to content

Commit

Permalink
feat: 오늘의 할 일 버튼 조건부 활성화 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
ghdtjgus76 committed Aug 20, 2024
1 parent 02bacce commit a4665fe
Showing 1 changed file with 31 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,28 @@ import { css } from "@styled-system/css";
import { Flex } from "@styled-system/jsx";
import useHorizontalScroll from "hooks/useHorizontalScroll";
import Image from "next/image";
import { type PropsWithChildren } from "react";
import { type PropsWithChildren, useEffect, useState } from "react";

const DailyTaskCarousel = ({ children }: PropsWithChildren) => {
const [showRightButton, setShowRightButton] = useState(false);

const itemWidth = 386;

const { containerRef, handleScroll } = useHorizontalScroll();

useEffect(() => {
if (containerRef.current) {
const containerWidth = containerRef.current.offsetWidth;
const totalChildrenWidth = Array.from(containerRef.current.children)
.map((child) => (child as HTMLElement).offsetWidth)
.reduce((acc, width) => acc + width, 0);

if (totalChildrenWidth > containerWidth) {
setShowRightButton(true);
}
}
}, [containerRef]);

const handleClickScrollRightButton = () => {
if (containerRef.current) {
const currentScrollPosition = containerRef.current.scrollLeft;
Expand All @@ -30,19 +45,21 @@ const DailyTaskCarousel = ({ children }: PropsWithChildren) => {
>
{children}
</Flex>
<button
aria-label="scroll-right-button"
className={scrollRightButtonStyle}
tabIndex={0}
onClick={handleClickScrollRightButton}
>
<Image
alt="scroll-right-button"
height={52}
src="/images/arrow-button.svg"
width={52}
/>
</button>
{showRightButton && (
<button
aria-label="scroll-right-button"
className={scrollRightButtonStyle}
tabIndex={0}
onClick={handleClickScrollRightButton}
>
<Image
alt="scroll-right-button"
height={52}
src="/images/arrow-button.svg"
width={52}
/>
</button>
)}
</>
);
};
Expand Down

0 comments on commit a4665fe

Please sign in to comment.