-
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.
feat: add useOverflow & useScroll hooks
- Loading branch information
1 parent
c0650bb
commit 9b06383
Showing
3 changed files
with
93 additions
and
0 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
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,36 @@ | ||
import { RefObject, useLayoutEffect, useState } from "react"; | ||
|
||
export function useOverflow<T extends HTMLElement | undefined>(ref:RefObject<T | undefined>, callback?:(hasOverflowVertical: boolean, hasOverflowHorizontal: boolean) => void):UseIsOverflowProps { | ||
const [overflowHorizontal, setOverflowHorizontal] = useState(false); | ||
const [overflowVertical, setOverflowVertical] = useState(false); | ||
|
||
|
||
useLayoutEffect(() => { | ||
const { current } = ref; | ||
|
||
const trigger = () => { | ||
const hasOverflowVertical = current && current.scrollHeight > current.clientHeight; | ||
const hasOverflowHorizontal = current && current.scrollWidth > current.clientWidth; | ||
|
||
setOverflowVertical(hasOverflowVertical ?? false); | ||
setOverflowHorizontal(hasOverflowHorizontal ?? false); | ||
|
||
if (!callback) return; | ||
|
||
callback(hasOverflowVertical ?? false, hasOverflowHorizontal ?? false); | ||
} | ||
|
||
if (!current) return; | ||
|
||
trigger(); | ||
}, [ref, callback]) | ||
|
||
return { overflowHorizontal, overflowVertical }; | ||
} | ||
|
||
export interface UseIsOverflowProps { | ||
overflowHorizontal: boolean; | ||
overflowVertical: boolean; | ||
} | ||
|
||
export default useOverflow; |
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,55 @@ | ||
import { RefObject, useLayoutEffect, useState } from "react"; | ||
|
||
export function useScroll<T extends HTMLElement | undefined>(ref: RefObject<T | undefined>): UseScrollProps { | ||
|
||
const [horizontalScrollPosition, setHorizontalScrollPosition] = useState(ref?.current?.scrollLeft ?? 0); | ||
const [verticalScrollPosition, setVerticalScrollPosition] = useState(ref?.current?.scrollTop ?? 0); | ||
|
||
const scrollHorizontal = (increment: number) => { | ||
const { current } = ref; | ||
|
||
if (!current) return; | ||
|
||
console.log(current.scrollLeft); | ||
|
||
current.scrollLeft += increment; | ||
|
||
console.log(current.scrollLeft); | ||
} | ||
|
||
const scrollVertical = (increment: number) => { | ||
const { current } = ref; | ||
|
||
if (!current) return; | ||
|
||
console.log(current.scrollTop); | ||
|
||
current.scrollTop += increment; | ||
|
||
console.log(current.scrollTop); | ||
} | ||
|
||
useLayoutEffect(() => { | ||
const { current } = ref; | ||
|
||
const trigger = () => { | ||
setHorizontalScrollPosition(current?.scrollLeft ?? 0); | ||
setVerticalScrollPosition(current?.scrollTop ?? 0); | ||
} | ||
|
||
if (!current) return; | ||
|
||
trigger(); | ||
}, [ref]) | ||
|
||
return {horizontalScrollPosition, verticalScrollPosition, scrollHorizontal, scrollVertical}; | ||
} | ||
|
||
export default useScroll; | ||
|
||
export interface UseScrollProps { | ||
scrollHorizontal: (increment: number) => void; | ||
scrollVertical: (increment: number) => void; | ||
horizontalScrollPosition: number; | ||
verticalScrollPosition: number; | ||
} |