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] 스크롤 따라 헤더바 상태 변경되는 것 구현 #69

Merged
merged 2 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/comment/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useRef } from "react";
import useSectionInitialize from "../scroll/useSectionInitialize";

function CommentSection() {
const SECTION_IDX = 2;
const SECTION_IDX = 3;
const sectionRef = useRef(null);
useSectionInitialize(SECTION_IDX, sectionRef);

Expand Down
2 changes: 1 addition & 1 deletion src/detailInformation/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useRef } from "react";
import useSectionInitialize from "../scroll/useSectionInitialize.js";

function DetailInformation() {
const SECTION_IDX = 1;
const SECTION_IDX = 2;
const sectionRef = useRef(null);
useSectionInitialize(SECTION_IDX, sectionRef);

Expand Down
31 changes: 19 additions & 12 deletions src/header/index.jsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import style from "./index.module.css";
import scrollTo from "../scroll/scrollTo";
import { useSectionStore } from "../scroll/store";
import { useEffect, useState } from "react";

export default function Header() {
const ITEM_WIDTH = 96; // w-24
const ITEM_GAP = 32; // gap-8
const currentSection = useSectionStore((state) => state.currentSection);
const isVisibleList = useSectionStore((state) => state.isVisibleList);
const [currentSection, setCurrentSection] = useState(0);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

zustand의 훅 안의 있는 함수는 상태에서 비롯된 다른 상태(derived state)를 계산할 수 있습니다.

그래서, 이런 게 가능합니다.

const currentSection = useSectionStore( (state)=>{
  return state.isVisibleList.findIndex((value) => value);
} );

const scrollSectionList = [
"추첨 이벤트",
"차량 상세정보",
"기대평",
"선착순 이벤트",
];

useEffect(() => {
const idx = isVisibleList.findIndex((value) => value === true);
setCurrentSection(idx);
}, [isVisibleList]);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

상태가 바뀌었을 때 useEffect로 다른 상태를 동기적으로 계산하는 것은 별로 좋은 패턴이 아닙니다. (렌더링이 2번 일어남)

function gotoTop() {
window.scrollTo({ top: 0, behavior: "smooth" });
}
Expand All @@ -30,14 +37,14 @@ export default function Header() {
}

function scrollDynamicStyle() {
if (currentSection < 0) return;

const position = Math.floor(
ITEM_WIDTH / 4 + currentSection * (ITEM_WIDTH + ITEM_GAP),
);
return {
"--pos": position,
};
if (currentSection > 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

왜 eager return을 빼고 도로 if문으로 돌아오셨나요?

const position = Math.floor(
ITEM_WIDTH / 4 + (currentSection - 1) * (ITEM_WIDTH + ITEM_GAP),
);
return {
"--pos": position,
};
}
}

return (
Expand All @@ -53,16 +60,16 @@ export default function Header() {
{scrollSectionList.map((scrollSection, index) => (
<div
key={index}
onClick={() => onClickScrollSection(index)}
className={`flex justify-center items-center w-24 cursor-pointer ${currentSection === index ? "text-black" : "text-neutral-300"}`}
onClick={() => onClickScrollSection(index + 1)}
className={`flex justify-center items-center w-24 cursor-pointer ${currentSection - 1 === index ? "text-black" : "text-neutral-300"}`}
>
{scrollSection}
</div>
))}

<div
style={scrollDynamicStyle()}
className={`w-[50px] h-[3px] bg-black transition ease-in-out duration-200 absolute bottom-0 left-0 ${currentSection < 0 ? "hidden" : style.moveBar}`}
className={`w-[50px] h-[3px] bg-black transition ease-in-out duration-200 absolute bottom-0 left-0 ${currentSection > 0 ? style.moveBar : "hidden"}`}
/>
</div>

Expand Down
2 changes: 1 addition & 1 deletion src/interactions/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useRef } from "react";
import useSectionInitialize from "../scroll/useSectionInitialize";

export default function InteractionPage() {
const SECTION_IDX = 0;
const SECTION_IDX = 1;
const sectionRef = useRef(null);
useSectionInitialize(SECTION_IDX, sectionRef);

Expand Down
11 changes: 8 additions & 3 deletions src/scroll/store.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { create } from "zustand";

export const useSectionStore = create((set) => ({
sectionList: [null, null, null, null],
currentSection: -1,
setCurrentSection: (newSection) => set({ currentSection: newSection }),
sectionList: [null, null, null, null, null],
isVisibleList: [false, false, false, false, false],
uploadSection: (index, section) =>
set((state) => {
const updatedList = [...state.sectionList];
updatedList[index] = section;
return { sectionList: updatedList };
}),
setIsVisibleList: (index, value) =>
set((state) => {
const updatedList = [...state.isVisibleList];
updatedList[index] = value;
return { isVisibleList: updatedList };
}),
}));
13 changes: 4 additions & 9 deletions src/scroll/useSectionInitialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import { useSectionStore } from "./store";

export default function useSectionInitialize(SECTION_IDX, sectionRef) {
const uploadSection = useSectionStore((state) => state.uploadSection);
const setCurrentSection = useSectionStore((state) => state.setCurrentSection);
const currentSection = useSectionStore((state) => state.currentSection);

const setIsVisibleList = useSectionStore((state) => state.setIsVisibleList);
useEffect(() => {
const sectionDOM = sectionRef.current;
if (sectionDOM) {
Expand All @@ -15,12 +13,10 @@ export default function useSectionInitialize(SECTION_IDX, sectionRef) {
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
// 미구현
}
setIsVisibleList(SECTION_IDX, entry.isIntersecting);
});
},
{ threshold: 0.05 },
{ threshold: 0.01 },
);

if (sectionDOM) {
Expand All @@ -35,7 +31,6 @@ export default function useSectionInitialize(SECTION_IDX, sectionRef) {
SECTION_IDX,
sectionRef,
uploadSection,
setCurrentSection,
currentSection,
setIsVisibleList,
]);
}
2 changes: 1 addition & 1 deletion src/simpleInformation/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ContentSection from "./contentSection";
import useSectionInitialize from "../scroll/useSectionInitialize";

export default function SimpleInformation() {
const SECTION_IDX = -1;
const SECTION_IDX = 0;
const sectionRef = useRef(null);
const contentList = JSONData.content;
useSectionInitialize(SECTION_IDX, sectionRef);
Expand Down