Skip to content

Commit 5be380f

Browse files
authored
feat: Add ability to replace useRect with custom observer (#75)
* feat: Add ability to replace useRect with custom observer * fix: naming based on review
1 parent 4299a9b commit 5be380f

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ const {
237237
overscan,
238238
horizontal,
239239
scrollToFn,
240+
useObserver,
240241
})
241242
```
242243

@@ -266,6 +267,11 @@ const {
266267
- Optional
267268
- 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.
268269
- 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.
270+
- `useObserver: Function(parentRef) => ({ width: number; height: number })`
271+
- Optional
272+
- This hook, if passed, is responsible for getting `parentRef`'s dimensions
273+
- 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)
274+
- Caution! Depending on your bundling target, you might need to add [resize-observer-polyfill](https://www.npmjs.com/package/resize-observer-polyfill)
269275
- `paddingStart: Integer`
270276
- Defaults to `0`
271277
- The amount of padding in pixels to add to the start of the virtual list

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ export function useVirtual({
1414
parentRef,
1515
horizontal,
1616
scrollToFn,
17+
useObserver,
1718
}) {
1819
const sizeKey = horizontal ? 'width' : 'height'
1920
const scrollKey = horizontal ? 'scrollLeft' : 'scrollTop'
2021
const latestRef = React.useRef({})
22+
const useMeasureParent = useObserver || useRect
2123

22-
const { [sizeKey]: outerSize } = useRect(parentRef) || {
24+
const { [sizeKey]: outerSize } = useMeasureParent(parentRef) || {
2325
[sizeKey]: 0,
2426
}
2527

0 commit comments

Comments
 (0)