-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
const scrollSectionList = [ | ||
"추첨 이벤트", | ||
"차량 상세정보", | ||
"기대평", | ||
"선착순 이벤트", | ||
]; | ||
|
||
useEffect(() => { | ||
const idx = isVisibleList.findIndex((value) => value === true); | ||
setCurrentSection(idx); | ||
}, [isVisibleList]); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 상태가 바뀌었을 때 useEffect로 다른 상태를 동기적으로 계산하는 것은 별로 좋은 패턴이 아닙니다. (렌더링이 2번 일어남) |
||
function gotoTop() { | ||
window.scrollTo({ top: 0, behavior: "smooth" }); | ||
} | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ( | ||
|
@@ -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> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
}), | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
zustand의 훅 안의 있는 함수는 상태에서 비롯된 다른 상태(derived state)를 계산할 수 있습니다.
그래서, 이런 게 가능합니다.