Skip to content

Commit 13f361f

Browse files
kricsleo9romise
andauthored
feat(useElementVisibility): add initialValue option (#5159)
Co-authored-by: Vida Xie <[email protected]>
1 parent e120472 commit 13f361f

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

packages/core/useElementVisibility/index.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ describe('useElementVisibility', () => {
2525
expect(visible.value).toBeFalsy()
2626
})
2727

28+
it('should allow set initial value', () => {
29+
const visible = useElementVisibility(el, { initialValue: true })
30+
expect(visible.value).toBeTruthy()
31+
})
32+
2833
describe('when internally using useIntersectionObserver', async () => {
2934
beforeAll(() => {
3035
vi.resetAllMocks()

packages/core/useElementVisibility/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ import { defaultWindow } from '../_configurable'
88
import { useIntersectionObserver } from '../useIntersectionObserver'
99

1010
export interface UseElementVisibilityOptions extends ConfigurableWindow, Pick<UseIntersectionObserverOptions, 'threshold'> {
11+
/**
12+
* Initial value.
13+
*
14+
* @default false
15+
*/
16+
initialValue?: boolean
1117
/**
1218
* @see https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/rootMargin
1319
*/
@@ -39,8 +45,9 @@ export function useElementVisibility(
3945
threshold = 0,
4046
rootMargin,
4147
once = false,
48+
initialValue = false,
4249
} = options
43-
const elementIsVisible = shallowRef(false)
50+
const elementIsVisible = shallowRef(initialValue)
4451

4552
const { stop } = useIntersectionObserver(
4653
element,

0 commit comments

Comments
 (0)