-
Notifications
You must be signed in to change notification settings - Fork 0
[URH-12] useWindowSize 신규 #4
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c8b18f6
✨ feat: 기본적인 동작을 하는 useWindowSize 구현
foresec f808427
✨ feat: 딜레이 유무선택이 가능한 옵션 추가
foresec 7285d44
🩹 chore: 관련 interface 추가
foresec 75bac8c
💬 comment: hook을 설명하는 주석 추가
foresec b240659
🔨 refactor: if문 함수 분리 및 타입 수정
foresec a7c8f34
💬 comment: @example 삭제 왜 jsdocs 수정
foresec 3d87c34
✨ feat: 객체형태 대신 매개변수 형태로 params 변경
foresec d4c9838
✨ feat: 초기 렌더링 시 window 객체 유무 판별을 위한 isClient 추가
foresec 9876957
🔨 refactor: isDelay 변수사용 및 의존성 배열에서 변수 삭제
foresec daa0175
🩹 chore: updateWindowSize 함수 위치 변경
foresec 1667f2d
💬 comment: 함수 params 변경에 따른 내용 수정
foresec 5ea4d66
🩹 chore: 폴더 구조 변경으로 hook폴더 안으로 useWindowSize이동
foresec 2aae0ca
Merge branch 'master' into feature/use-window-size
foresec 965d2ad
🔨 refactor: delayTime 조건에 맞춰 type 적용
foresec 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
interface WindowSize { | ||
width: number; | ||
height: number; | ||
} | ||
|
||
/** | ||
* useWindowSize : 브라우저 창의 너비와 높이를 제공하는 훅 | ||
* @param {boolean} [isDelay=true] 딜레이 여부 판단. Default=true | ||
* @param {number} [delayTime=200] 지연 시간(밀리초). isDelay가 true일 때 사용됨.Default=200 | ||
* @returns {{ width: number, height: number }} 창의 너비와 높이를 담은 객체 | ||
* @description | ||
* 브라우저 창의 사이즈가 변경될 때마다 해당 창의 너비와 높이를 업데이트합니다. | ||
* delay유무와 delayTime을 설정할 수 있습니다. | ||
* UI의 세밀한 조작 등 동적인 변화에 유용합니다. | ||
*/ | ||
|
||
const useWindowSize = ( | ||
isDelay: boolean = true, | ||
delayTime: number = 200 | ||
): WindowSize => { | ||
const [windowSize, setWindowSize] = useState<WindowSize>({ | ||
width: window.innerWidth, | ||
height: window.innerHeight, | ||
bicochan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
|
||
useEffect(() => { | ||
let timeoutId: NodeJS.Timeout | undefined; | ||
const handleWindowResize = () => { | ||
if (timeoutId) clearTimeout(timeoutId); | ||
|
||
const updateWindowSize = () => { | ||
setWindowSize({ | ||
width: window.innerWidth, | ||
height: window.innerHeight, | ||
}); | ||
}; | ||
|
||
if (isDelay) { | ||
bicochan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
timeoutId = setTimeout(updateWindowSize, delayTime); | ||
} else { | ||
updateWindowSize(); | ||
} | ||
}; | ||
|
||
window.addEventListener('resize', handleWindowResize); | ||
return () => { | ||
window.removeEventListener('resize', handleWindowResize); | ||
if (timeoutId) clearTimeout(timeoutId); | ||
}; | ||
}, [isDelay, delayTime]); | ||
bicochan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return windowSize; | ||
}; | ||
|
||
export default useWindowSize; |
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.
Uh oh!
There was an error while loading. Please reload this page.