Skip to content

Commit

Permalink
feat: Add ability to replace useRect with custom observer (#75)
Browse files Browse the repository at this point in the history
* feat: Add ability to replace useRect with custom observer

* fix: naming based on review
  • Loading branch information
o-alexandrov authored Oct 22, 2020
1 parent 4299a9b commit 5be380f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ const {
overscan,
horizontal,
scrollToFn,
useObserver,
})
```

Expand Down Expand Up @@ -266,6 +267,11 @@ const {
- Optional
- This function, if passed, is responsible for implementing the scrollTo log for the parentRef which is used when methods like `scrolllToOffset` and `scrollToIndex` are called.
- Eg. You can use this function implement smooth scrolling by using the supplied offset and the `defaultScrollToFn` as seen in the sandbox's **Smooth Scroll** example.
- `useObserver: Function(parentRef) => ({ width: number; height: number })`
- Optional
- This hook, if passed, is responsible for getting `parentRef`'s dimensions
- Eg. You can use this hook to replace [@reach/observe-rect](https://github.com/reach/observe-rect) that `react-virtual` uses by default with [ResizeObserver API](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver)
- Caution! Depending on your bundling target, you might need to add [resize-observer-polyfill](https://www.npmjs.com/package/resize-observer-polyfill)
- `paddingStart: Integer`
- Defaults to `0`
- The amount of padding in pixels to add to the start of the virtual list
Expand Down
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ export function useVirtual({
parentRef,
horizontal,
scrollToFn,
useObserver,
}) {
const sizeKey = horizontal ? 'width' : 'height'
const scrollKey = horizontal ? 'scrollLeft' : 'scrollTop'
const latestRef = React.useRef({})
const useMeasureParent = useObserver || useRect

const { [sizeKey]: outerSize } = useRect(parentRef) || {
const { [sizeKey]: outerSize } = useMeasureParent(parentRef) || {
[sizeKey]: 0,
}

Expand Down

0 comments on commit 5be380f

Please sign in to comment.