-
-
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.
* Add use-scroll hook * changes from feedback --------- Co-authored-by: Matias Pompilio <[email protected]>
- Loading branch information
1 parent
be2d849
commit 8b1d5d7
Showing
4 changed files
with
106 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,71 @@ | ||
import React, { | ||
forwardRef, | ||
PropsWithChildren, | ||
ReactNode, | ||
Ref, | ||
useRef, | ||
} from "react"; | ||
import { ComponentMeta } from "@storybook/react"; | ||
import { useScroll } 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", | ||
background: props.background, | ||
minWidth: "200px", | ||
minHeight: "250px", | ||
}; | ||
|
||
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 { scroll } = useScroll({ | ||
elementRef: ref, | ||
}); | ||
|
||
return ( | ||
<div | ||
style={{ | ||
display: "flex", | ||
justifyContent: "center", | ||
position: "relative", | ||
}} | ||
> | ||
<button onClick={() => scroll("left", 200)}>Left</button> | ||
<div | ||
style={{ | ||
overflow: "hidden", | ||
display: "flex", | ||
flexDirection: "row", | ||
width: "200px", | ||
}} | ||
ref={ref} | ||
> | ||
<Box background="red">Item 1</Box> | ||
<Box background="blue">Item 2</Box> | ||
<Box background="green">Item 3</Box> | ||
</div> | ||
<button onClick={() => scroll("right", 200)}>Right</button> | ||
</div> | ||
); | ||
}; | ||
|
||
export default { | ||
title: "React Hooks/useScroll", | ||
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 useScroll } from "./useScroll"; |
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,33 @@ | ||
import { useCallback } from "react"; | ||
|
||
export type UseScrollParams = { | ||
elementRef: React.RefObject<HTMLElement>; | ||
}; | ||
|
||
export type ScrollDirections = "top" | "bot" | "left" | "right"; | ||
|
||
const useScroll = ({ elementRef }: UseScrollParams) => { | ||
const scroll = useCallback( | ||
(direction: ScrollDirections, spaceToScroll: number) => { | ||
if (!elementRef.current) return; | ||
|
||
const topMovement = direction === "top" ? spaceToScroll : 0; | ||
const botMovement = direction === "bot" ? -spaceToScroll : 0; | ||
const leftMovement = direction === "left" ? -spaceToScroll : 0; | ||
const rightMovement = direction === "right" ? spaceToScroll : 0; | ||
|
||
elementRef.current.scrollBy({ | ||
top: topMovement || botMovement, | ||
left: leftMovement || rightMovement, | ||
behavior: "smooth", | ||
}); | ||
}, | ||
[elementRef] | ||
); | ||
|
||
return { | ||
scroll, | ||
}; | ||
}; | ||
|
||
export default useScroll; |