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

feat: scrollable container custom hook #230

Merged
merged 1 commit into from
Mar 16, 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
34 changes: 34 additions & 0 deletions src/@components/@common/hooks/useScrollableContainer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { useRef, useState } from "react";

export default function useScrollableContainer() {
const containerRef = useRef<HTMLElement | null>(null);
const [isDragging, setIsDragging] = useState(false);
const [startX, setStartX] = useState(0);

function handleMouseDown(event: React.MouseEvent<HTMLElement, MouseEvent>) {
setIsDragging(true);
setStartX(event.pageX);
}

function handleMouseMove(event: React.MouseEvent<HTMLElement, MouseEvent>) {
const container = containerRef.current;
if (!isDragging || !container) return;

container.scrollLeft += startX - event.pageX;
setStartX(event.pageX);
}

function handleMouseUp() {
setIsDragging(false);
}

return {
scrollableContainerProps: {
ref: containerRef,
onMouseDown: handleMouseDown,
onMouseMove: handleMouseMove,
onMouseUp: handleMouseUp,
onMouseLeave: handleMouseUp,
joohaem marked this conversation as resolved.
Show resolved Hide resolved
},
};
}
4 changes: 3 additions & 1 deletion src/@components/MainPage/BestPiickle/index.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { headingTitles } from "../../../util/main/headingTitles";
import HeadingTitleContainer from "../../@common/HeadingTitleContainer";
import useScrollableContainer from "../../@common/hooks/useScrollableContainer";
import Loading from "../../@common/Loading";
import { useBestPiickle } from "../hooks/useBestPiickle";
import BestPiickleCard from "./BestPiickleCard";
import St from "./style";

export default function BestPiickle() {
const { bestPiickle } = useBestPiickle();
const { scrollableContainerProps } = useScrollableContainer();

return (
<St.Root>
<HeadingTitleContainer headingTitles={headingTitles[0]} />

{bestPiickle ? (
<St.SliderWrapper>
<St.SliderWrapper {...scrollableContainerProps}>
{bestPiickle &&
bestPiickle.data.slice(0, 5).map((bestPiickle, idx) => {
return <BestPiickleCard key={bestPiickle._id} bestPiickle={bestPiickle} idx={idx} />;
Expand Down
4 changes: 3 additions & 1 deletion src/@components/MainPage/Medley/index.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import useScrollableContainer from "../../@common/hooks/useScrollableContainer";
import Loading from "../../@common/Loading";
import { useMedleyLists } from "../hooks/useMedleyLists";
import MedleyCard from "./MedleyCard";
import St from "./style";

export default function Medley() {
const { randomMedleyLists, isLoading } = useMedleyLists();
const { scrollableContainerProps } = useScrollableContainer();

if (isLoading) return <Loading backgroundColor="white" />;

return (
<St.Container>
<St.Title>👍 여러분을 위해 피클이 직접 뽑은 주제들!</St.Title>
<St.Medley>
<St.Medley {...scrollableContainerProps}>
{randomMedleyLists &&
randomMedleyLists.map((medleyCard) => <MedleyCard key={medleyCard._id} medleyCard={medleyCard} />)}
</St.Medley>
Expand Down