Skip to content

Commit 375cd83

Browse files
authoredNov 7, 2024··
fix: add useScrollendEvent option (#875)
1 parent 3202bd0 commit 375cd83

File tree

2 files changed

+37
-22
lines changed

2 files changed

+37
-22
lines changed
 

‎docs/api/virtualizer.md

+10
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,16 @@ This option allows you to specify the duration to wait after the last scroll eve
240240

241241
The implementation of this option is driven by the need for a reliable mechanism to handle scrolling behavior across different browsers. Until all browsers uniformly support the scrollEnd event.
242242

243+
### `useScrollendEvent`
244+
245+
```tsx
246+
useScrollendEvent: boolean
247+
```
248+
249+
This option allows you to switch to use debounced fallback to reset the isScrolling instance property after `isScrollingResetDelay` milliseconds. The default value is `true`.
250+
251+
The implementation of this option is driven by the need for a reliable mechanism to handle scrolling behavior across different browsers. Until all browsers uniformly support the scrollEnd event.
252+
243253
### `isRtl`
244254

245255
```tsx

‎packages/virtual-core/src/index.ts

+27-22
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,11 @@ export const observeWindowRect = (
130130
const supportsScrollend =
131131
typeof window == 'undefined' ? true : 'onscrollend' in window
132132

133+
type ObserveOffsetCallBack = (offset: number, isScrolling: boolean) => void
134+
133135
export const observeElementOffset = <T extends Element>(
134136
instance: Virtualizer<T, any>,
135-
cb: (offset: number, isScrolling: boolean) => void,
137+
cb: ObserveOffsetCallBack,
136138
) => {
137139
const element = instance.scrollElement
138140
if (!element) {
@@ -144,15 +146,16 @@ export const observeElementOffset = <T extends Element>(
144146
}
145147

146148
let offset = 0
147-
const fallback = supportsScrollend
148-
? () => undefined
149-
: debounce(
150-
targetWindow,
151-
() => {
152-
cb(offset, false)
153-
},
154-
instance.options.isScrollingResetDelay,
155-
)
149+
const fallback =
150+
instance.options.useScrollendEvent && supportsScrollend
151+
? () => undefined
152+
: debounce(
153+
targetWindow,
154+
() => {
155+
cb(offset, false)
156+
},
157+
instance.options.isScrollingResetDelay,
158+
)
156159

157160
const createHandler = (isScrolling: boolean) => () => {
158161
const { horizontal, isRtl } = instance.options
@@ -177,7 +180,7 @@ export const observeElementOffset = <T extends Element>(
177180

178181
export const observeWindowOffset = (
179182
instance: Virtualizer<Window, any>,
180-
cb: (offset: number, isScrolling: boolean) => void,
183+
cb: ObserveOffsetCallBack,
181184
) => {
182185
const element = instance.scrollElement
183186
if (!element) {
@@ -189,15 +192,16 @@ export const observeWindowOffset = (
189192
}
190193

191194
let offset = 0
192-
const fallback = supportsScrollend
193-
? () => undefined
194-
: debounce(
195-
targetWindow,
196-
() => {
197-
cb(offset, false)
198-
},
199-
instance.options.isScrollingResetDelay,
200-
)
195+
const fallback =
196+
instance.options.useScrollendEvent && supportsScrollend
197+
? () => undefined
198+
: debounce(
199+
targetWindow,
200+
() => {
201+
cb(offset, false)
202+
},
203+
instance.options.isScrollingResetDelay,
204+
)
201205

202206
const createHandler = (isScrolling: boolean) => () => {
203207
offset = element[instance.options.horizontal ? 'scrollX' : 'scrollY']
@@ -291,9 +295,8 @@ export interface VirtualizerOptions<
291295
) => void | (() => void)
292296
observeElementOffset: (
293297
instance: Virtualizer<TScrollElement, TItemElement>,
294-
cb: (offset: number, isScrolling: boolean) => void,
298+
cb: ObserveOffsetCallBack,
295299
) => void | (() => void)
296-
297300
// Optional
298301
debug?: boolean
299302
initialRect?: Rect
@@ -321,6 +324,7 @@ export interface VirtualizerOptions<
321324
initialMeasurementsCache?: Array<VirtualItem>
322325
lanes?: number
323326
isScrollingResetDelay?: number
327+
useScrollendEvent?: boolean
324328
enabled?: boolean
325329
isRtl?: boolean
326330
}
@@ -412,6 +416,7 @@ export class Virtualizer<
412416
isScrollingResetDelay: 150,
413417
enabled: true,
414418
isRtl: false,
419+
useScrollendEvent: true,
415420
...opts,
416421
}
417422
}

0 commit comments

Comments
 (0)
Please sign in to comment.