-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[components] Refactor scroll components
- Loading branch information
1 parent
ac07157
commit e21e1cb
Showing
6 changed files
with
50 additions
and
31 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,6 @@ | ||
import {useContext} from 'react' | ||
import {ScrollContext} from './scrollContext' | ||
|
||
export function useScroll() { | ||
return useContext(ScrollContext) | ||
} |
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 |
---|---|---|
@@ -1 +1,4 @@ | ||
export * from './hooks' | ||
export * from './ScrollContainer' | ||
export * from './scrollMonitor' | ||
export * from './types' |
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,4 @@ | ||
import {createContext} from 'react' | ||
import {ScrollContextValue} from './types' | ||
|
||
export const ScrollContext = createContext<ScrollContextValue>({}) |
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,28 @@ | ||
import React, {useCallback} from 'react' | ||
import {useScroll} from './hooks' | ||
import {ScrollContext} from './scrollContext' | ||
import {ScrollEventHandler} from './types' | ||
|
||
interface ScrollMonitorProps { | ||
onScroll: ScrollEventHandler | ||
children?: React.ReactNode | ||
} | ||
|
||
export function ScrollMonitor({onScroll, children}: ScrollMonitorProps) { | ||
const parentContext = useScroll() | ||
|
||
const handleScroll = useCallback( | ||
(event: Event) => { | ||
onScroll(event) | ||
|
||
if (parentContext.onScroll) { | ||
parentContext.onScroll(event) | ||
} | ||
}, | ||
[parentContext, onScroll] | ||
) | ||
|
||
return ( | ||
<ScrollContext.Provider value={{onScroll: handleScroll}}>{children}</ScrollContext.Provider> | ||
) | ||
} |
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 type ScrollEventHandler = (event: Event) => void | ||
|
||
export interface ScrollContextValue { | ||
onScroll?: ScrollEventHandler | ||
} |