Skip to content

Commit

Permalink
fix(useElementVisiblity): can configurable threshold (#3715)
Browse files Browse the repository at this point in the history
Co-authored-by: Doctor Wu <44631608+Doctor-wu@users.noreply.github.com>
  • Loading branch information
huiliangShen and Doctor-wu committed Feb 20, 2024
1 parent 9a47a72 commit ce9bbec
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
6 changes: 6 additions & 0 deletions packages/core/useElementVisibility/index.test.ts
Expand Up @@ -19,6 +19,12 @@ describe('useElementVisibility', () => {
expect(visible.value).toBeFalsy()
})

it('should work when threshold is undefined', () => {
// @ts-expect-error set threshold null
const visible = useElementVisibility(el, { threshold: null })
expect(visible.value).toBeFalsy()
})

describe('when internally using useIntersectionObserver', async () => {
beforeAll(() => {
vi.resetAllMocks()
Expand Down
7 changes: 4 additions & 3 deletions packages/core/useElementVisibility/index.ts
@@ -1,11 +1,12 @@
import type { MaybeRefOrGetter } from '@vueuse/shared'
import { ref } from 'vue-demi'
import type { MaybeComputedElementRef } from '../unrefElement'
import type { UseIntersectionObserverOptions } from '../useIntersectionObserver'
import { useIntersectionObserver } from '../useIntersectionObserver'
import type { ConfigurableWindow } from '../_configurable'
import { defaultWindow } from '../_configurable'

export interface UseElementVisibilityOptions extends ConfigurableWindow {
export interface UseElementVisibilityOptions extends ConfigurableWindow, Pick<UseIntersectionObserverOptions, 'threshold'> {
scrollTarget?: MaybeRefOrGetter<HTMLElement | undefined | null>
}

Expand All @@ -18,7 +19,7 @@ export function useElementVisibility(
element: MaybeComputedElementRef,
options: UseElementVisibilityOptions = {},
) {
const { window = defaultWindow, scrollTarget } = options
const { window = defaultWindow, scrollTarget, threshold = 0 } = options
const elementIsVisible = ref(false)

useIntersectionObserver(
Expand All @@ -39,7 +40,7 @@ export function useElementVisibility(
{
root: scrollTarget,
window,
threshold: 0,
threshold,
},
)

Expand Down

0 comments on commit ce9bbec

Please sign in to comment.