Skip to content

Commit

Permalink
Merge pull request #69 from softeerbootcamp4th/feature/64-scroll
Browse files Browse the repository at this point in the history
[feat] 스크롤 따라 헤더바 상태 변경되는 것 구현
  • Loading branch information
lybell-art authored Aug 6, 2024
2 parents 625a855 + a145d93 commit da93efc
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 22 deletions.
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
14 changes: 8 additions & 6 deletions src/header/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { useSectionStore } from "../scroll/store";
export default function Header() {
const ITEM_WIDTH = 96; // w-24
const ITEM_GAP = 32; // gap-8
const currentSection = useSectionStore((state) => state.currentSection);
const currentSection = useSectionStore((state) => {
return state.isVisibleList.findIndex((value) => value === true);
});
const scrollSectionList = [
"추첨 이벤트",
"차량 상세정보",
Expand All @@ -30,10 +32,10 @@ export default function Header() {
}

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

const position = Math.floor(
ITEM_WIDTH / 4 + currentSection * (ITEM_WIDTH + ITEM_GAP),
ITEM_WIDTH / 4 + (currentSection - 1) * (ITEM_WIDTH + ITEM_GAP),
);
return {
"--pos": position,
Expand All @@ -53,16 +55,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

0 comments on commit da93efc

Please sign in to comment.