-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: 서비스에서 사용되는 훅 정리 및 리팩토링 (#ATR-597)
- Loading branch information
Showing
13 changed files
with
122 additions
and
4 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module.exports = { | ||
parserOptions: { | ||
project: ['./tsconfig.json'], | ||
tsconfigRootDir: __dirname, | ||
}, | ||
rules: { | ||
'import/no-unresolved': [2, { caseSensitive: false }], | ||
}, | ||
settings: { | ||
'import/resolver': { | ||
typescript: {}, | ||
}, | ||
}, | ||
} |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export * from './use-outside-click' | ||
export * from './use-debounce' | ||
export * from './use-window-event' | ||
export * from './use-scroll-observer' | ||
export * from './use-scroll-progress' |
1 change: 1 addition & 0 deletions
1
packages/design-system/packages/hooks/src/use-debounce/index.ts
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as useDebounce } from './useDebounce' |
15 changes: 15 additions & 0 deletions
15
packages/design-system/packages/hooks/src/use-debounce/useDebounce.ts
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React from 'react' | ||
|
||
export default function useDebounce<T>(value: T, delay: number) { | ||
const [debouncedValue, setDebouncedValue] = React.useState<T>(value) | ||
|
||
React.useEffect(() => { | ||
const timeoutId = setTimeout(() => { | ||
setDebouncedValue(value) | ||
}, delay) | ||
|
||
return () => clearTimeout(timeoutId) | ||
}, [value, delay]) | ||
|
||
return debouncedValue | ||
} |
1 change: 1 addition & 0 deletions
1
packages/design-system/packages/hooks/src/use-outside-click/index.ts
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as useOutsideClick } from './useOutsideClick' |
21 changes: 21 additions & 0 deletions
21
packages/design-system/packages/hooks/src/use-outside-click/useOutsideClick.ts
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import React from 'react' | ||
|
||
export default function useOutsideClick(callback: () => void) { | ||
const targetAreaRef = React.useRef<HTMLElement | null>(null) | ||
|
||
React.useEffect(() => { | ||
const clickHandler = (e: MouseEvent) => { | ||
if (!targetAreaRef.current) return | ||
if (targetAreaRef.current.contains(e.target as Node | null)) { | ||
return | ||
} | ||
callback() | ||
} | ||
document.addEventListener('click', clickHandler) | ||
return () => { | ||
document.removeEventListener('click', clickHandler) | ||
} | ||
}, [callback]) | ||
|
||
return targetAreaRef | ||
} |
1 change: 1 addition & 0 deletions
1
packages/design-system/packages/hooks/src/use-scroll-observer/index.ts
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as useScrollObserver } from './useScrollObserver' |
31 changes: 31 additions & 0 deletions
31
packages/design-system/packages/hooks/src/use-scroll-observer/useScrollObserver.ts
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import React from 'react' | ||
|
||
export default function useScrollObserver( | ||
callback: () => void, | ||
options?: IntersectionObserverInit, | ||
) { | ||
const targetRef = React.useRef<HTMLElement | null>(null) | ||
const observerRef = React.useRef<IntersectionObserver | null>(null) | ||
const defaultOptions = { root: null, rootMargin: '0px', threshold: 1.0 } | ||
|
||
React.useEffect(() => { | ||
if (!observerRef.current) { | ||
observerRef.current = new IntersectionObserver(([target]) => { | ||
if (target.isIntersecting) { | ||
callback() | ||
} | ||
}, options ?? defaultOptions) | ||
} | ||
|
||
if (targetRef.current) { | ||
observerRef.current.observe(targetRef.current) | ||
} | ||
|
||
return () => { | ||
observerRef.current?.disconnect() | ||
observerRef.current = null | ||
} | ||
}, [targetRef, callback]) | ||
|
||
return targetRef | ||
} |
1 change: 1 addition & 0 deletions
1
packages/design-system/packages/hooks/src/use-scroll-progress/index.ts
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as useScrollProgress } from './useScrollProgress' |
16 changes: 16 additions & 0 deletions
16
packages/design-system/packages/hooks/src/use-scroll-progress/useScrollProgress.ts
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import React from 'react' | ||
import { useWindowEvent } from '../use-window-event' | ||
|
||
export default function useScrollProgress() { | ||
const [scrollProgress, setScrollProgress] = React.useState(0) | ||
const handleScroll = () => { | ||
const currentScroll = window.scrollY | ||
const totalHeight = document.body.scrollHeight - window.innerHeight | ||
const progress = Math.round((currentScroll / totalHeight) * 100) | ||
setScrollProgress(progress) | ||
} | ||
|
||
useWindowEvent('scroll', handleScroll) | ||
|
||
return { scrollProgress } | ||
} |
1 change: 1 addition & 0 deletions
1
packages/design-system/packages/hooks/src/use-window-event/index.ts
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as useWindowEvent } from './useWindowEvent' |
14 changes: 14 additions & 0 deletions
14
packages/design-system/packages/hooks/src/use-window-event/useWindowEvent.ts
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import React from 'react' | ||
|
||
export default function useWindowEvent<K extends keyof DocumentEventMap>( | ||
type: K, | ||
listener: (this: Window, e: DocumentEventMap[K]) => void, | ||
option?: boolean | AddEventListenerOptions, | ||
) { | ||
React.useEffect(() => { | ||
window.addEventListener(type as any, listener, option) | ||
return () => { | ||
window.removeEventListener(type as any, listener, option) | ||
} | ||
}, [type, listener, option]) | ||
} |
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