-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(*): add hooks as a replacement for HOCs
- Loading branch information
1 parent
96c3d22
commit 9853706
Showing
8 changed files
with
253 additions
and
312 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
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,80 @@ | ||
import { useContext, useEffect, useState } from 'react'; | ||
|
||
import { ViewportContext } from './ViewportProvider'; | ||
import { | ||
createInitScrollState, | ||
createInitDimensionsState, | ||
} from './ViewportCollector'; | ||
import { IViewport, IScroll, IDimensions } from './types'; | ||
import { warnNoContextAvailable } from './utils'; | ||
|
||
interface IFullOptions { | ||
recalculateLayoutBeforeUpdate?: (props: IViewport) => any; | ||
disableScrollUpdates?: boolean; | ||
disableDimensionsUpdates?: boolean; | ||
deferUpdateUntilIdle?: boolean; | ||
} | ||
|
||
interface IOptions { | ||
deferUpdateUntilIdle?: boolean; | ||
recalculateLayoutBeforeUpdate?: (props: IViewport) => any; | ||
} | ||
|
||
type HandleViewportChangeType = (viewport: IViewport) => void; | ||
|
||
const useViewportEffect = ( | ||
handleViewportChange: HandleViewportChangeType, | ||
options: IFullOptions, | ||
) => { | ||
const { | ||
addViewportChangeListener, | ||
removeViewportChangeListener, | ||
hasRootProviderAsParent, | ||
} = useContext(ViewportContext); | ||
|
||
if (!hasRootProviderAsParent) { | ||
warnNoContextAvailable('useViewport'); | ||
return; | ||
} | ||
|
||
useEffect( | ||
() => { | ||
addViewportChangeListener(handleViewportChange, { | ||
notifyScroll: () => !options.disableScrollUpdates, | ||
notifyDimensions: () => !options.disableDimensionsUpdates, | ||
notifyOnlyWhenIdle: () => Boolean(options.deferUpdateUntilIdle), | ||
recalculateLayoutBeforeUpdate: options.recalculateLayoutBeforeUpdate, | ||
}); | ||
return () => removeViewportChangeListener(handleViewportChange); | ||
}, | ||
[addViewportChangeListener, removeViewportChangeListener], | ||
); | ||
}; | ||
|
||
export const useScroll = (options: IOptions = {}): IScroll => { | ||
const { scroll } = useViewport({ | ||
disableDimensionsUpdates: true, | ||
...options, | ||
}); | ||
|
||
return scroll; | ||
}; | ||
|
||
export const useDimensions = (options: IOptions = {}): IDimensions => { | ||
const { dimensions } = useViewport({ | ||
disableScrollUpdates: true, | ||
...options, | ||
}); | ||
|
||
return dimensions; | ||
}; | ||
|
||
export const useViewport = (options: IFullOptions = {}): IViewport => { | ||
const [state, setViewport] = useState({ | ||
scroll: createInitScrollState(), | ||
viewport: createInitDimensionsState(), | ||
}); | ||
useViewportEffect(setViewport, options); | ||
|
||
return state; | ||
}; |
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
Oops, something went wrong.