Skip to content

Commit fab863a

Browse files
steklopodantfubrendomaciel
authored
fix(useScroll): add onError hook and avoid throws by default, fix #3580 (#3605)
Co-authored-by: Anthony Fu <[email protected]> Co-authored-by: Brendo Maciel <[email protected]>
1 parent c44fea4 commit fab863a

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

packages/core/useScroll/index.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { computed, reactive, ref } from 'vue-demi'
22
import type { MaybeRefOrGetter } from '@vueuse/shared'
33
import { noop, toValue, tryOnMounted, useDebounceFn, useThrottleFn } from '@vueuse/shared'
44
import { useEventListener } from '../useEventListener'
5+
import { unrefElement } from '../unrefElement'
56
import type { ConfigurableWindow } from '../_configurable'
67
import { defaultWindow } from '../_configurable'
78

@@ -58,6 +59,13 @@ export interface UseScrollOptions extends ConfigurableWindow {
5859
* @default 'auto'
5960
*/
6061
behavior?: MaybeRefOrGetter<ScrollBehavior>
62+
63+
/**
64+
* On error callback
65+
*
66+
* Default log error to `console.error`
67+
*/
68+
onError?: (error: unknown) => void
6169
}
6270

6371
/**
@@ -97,6 +105,7 @@ export function useScroll(
97105
},
98106
behavior = 'auto',
99107
window = defaultWindow,
108+
onError = (e) => { console.error(e) },
100109
} = options
101110

102111
const internalX = ref(0)
@@ -169,19 +178,19 @@ export function useScroll(
169178
if (!window)
170179
return
171180

172-
const el = (
173-
(target as Window).document
174-
? (target as Window).document.documentElement
175-
: (target as Document).documentElement ?? target
176-
) as HTMLElement
181+
const el: Element = (
182+
(target as Window)?.document?.documentElement
183+
|| (target as Document)?.documentElement
184+
|| unrefElement(target as HTMLElement | SVGElement)
185+
) as Element
177186

178187
const { display, flexDirection } = getComputedStyle(el)
179188

180189
const scrollLeft = el.scrollLeft
181190
directions.left = scrollLeft < internalX.value
182191
directions.right = scrollLeft > internalX.value
183192

184-
const left = Math.abs(scrollLeft) <= 0 + (offset.left || 0)
193+
const left = Math.abs(scrollLeft) <= (offset.left || 0)
185194
const right = Math.abs(scrollLeft)
186195
+ el.clientWidth >= el.scrollWidth
187196
- (offset.right || 0)
@@ -206,7 +215,7 @@ export function useScroll(
206215

207216
directions.top = scrollTop < internalY.value
208217
directions.bottom = scrollTop > internalY.value
209-
const top = Math.abs(scrollTop) <= 0 + (offset.top || 0)
218+
const top = Math.abs(scrollTop) <= (offset.top || 0)
210219
const bottom = Math.abs(scrollTop)
211220
+ el.clientHeight >= el.scrollHeight
212221
- (offset.bottom || 0)
@@ -251,11 +260,15 @@ export function useScroll(
251260
)
252261

253262
tryOnMounted(() => {
254-
const _element = toValue(element)
255-
if (!_element)
256-
return
257-
258-
setArrivedState(_element)
263+
try {
264+
const _element = toValue(element)
265+
if (!_element)
266+
return
267+
setArrivedState(_element)
268+
}
269+
catch (e) {
270+
onError(e)
271+
}
259272
})
260273

261274
useEventListener(

0 commit comments

Comments
 (0)