-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Matias Pompilio <[email protected]> Co-authored-by: Ulises Jeremias <[email protected]>
- Loading branch information
1 parent
8b1d5d7
commit fa42325
Showing
4 changed files
with
103 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import React, { | ||
forwardRef, | ||
PropsWithChildren, | ||
ReactNode, | ||
Ref, | ||
useRef, | ||
} from "react"; | ||
import { ComponentMeta } from "@storybook/react"; | ||
import { useOverflow } from "@nanlabs/react-hooks"; | ||
|
||
interface BoxProps { | ||
background?: string; | ||
children?: ReactNode; | ||
} | ||
|
||
const Box = forwardRef((props: BoxProps, ref: Ref<HTMLDivElement>) => { | ||
const style = { | ||
display: "flex", | ||
justifyContent: "center", | ||
alignItems: "center", | ||
overflow: "scroll", | ||
background: "#108ee9", | ||
textDirection: "center", | ||
}; | ||
|
||
return ( | ||
<div ref={ref} style={style}> | ||
{props.children} | ||
</div> | ||
); | ||
}); | ||
|
||
Box.displayName = "Box"; | ||
|
||
export const Example: React.FC<PropsWithChildren> = () => { | ||
const ref = useRef<HTMLDivElement | null>(null); | ||
const { overflowActive } = useOverflow({ | ||
elementRef: ref, | ||
}); | ||
|
||
return ( | ||
<Box ref={ref}> | ||
<div | ||
style={{ | ||
minHeight: "800px", | ||
minWidth: "600px", | ||
display: "flex", | ||
alignItems: "center", | ||
justifyContent: "center", | ||
}} | ||
> | ||
{overflowActive ? "Element in Overflow" : "No overflow"} | ||
</div> | ||
</Box> | ||
); | ||
}; | ||
|
||
export default { | ||
title: "React Hooks/useOverflow", | ||
component: Example, | ||
} as ComponentMeta<typeof Example>; |
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 @@ | ||
export { default as useOverflow } from "./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,40 @@ | ||
import { useCallback, useEffect, useState } from "react"; | ||
|
||
export type UseOverflowParams = { | ||
elementRef: React.RefObject<HTMLElement>; | ||
}; | ||
|
||
const checkOverflow = (element: HTMLElement) => { | ||
if (element) | ||
return ( | ||
element.offsetHeight < element.scrollHeight || | ||
element.offsetWidth < element.scrollWidth | ||
); | ||
return false; | ||
}; | ||
|
||
const useOverflow = ({ elementRef }: UseOverflowParams) => { | ||
const [overflowActive, setOverflowActive] = useState<boolean>(false); | ||
|
||
const onResize = useCallback(() => { | ||
if (!elementRef.current) return; | ||
if (checkOverflow(elementRef.current)) { | ||
setOverflowActive(true); | ||
return; | ||
} | ||
setOverflowActive(false); | ||
}, [elementRef]); | ||
|
||
useEffect(() => { | ||
onResize(); | ||
window.addEventListener("resize", onResize); | ||
|
||
return () => window.removeEventListener("resize", onResize); | ||
}, [onResize]); | ||
|
||
return { | ||
overflowActive, | ||
}; | ||
}; | ||
|
||
export default useOverflow; |