Skip to content

Commit 53abedf

Browse files
doyuliilyaliao9romise
authored
feat(useIntersectionObserver): make rootMargin reactive (#4934)
Co-authored-by: IlyaL <[email protected]> Co-authored-by: Vida Xie <[email protected]>
1 parent a43eadb commit 53abedf

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

packages/core/useIntersectionObserver/demo.vue

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
<script setup lang="ts">
22
import { useIntersectionObserver } from '@vueuse/core'
3-
import { shallowRef, useTemplateRef } from 'vue'
3+
import { computed, shallowRef, useTemplateRef } from 'vue'
44
55
const root = useTemplateRef('root')
66
const target = useTemplateRef('target')
77
const isVisible = shallowRef(false)
88
9+
const rootMarginTop = shallowRef(0)
10+
const rootMarginRight = shallowRef(0)
11+
const rootMarginBottom = shallowRef(0)
12+
const rootMarginLeft = shallowRef(0)
13+
const rootMargin = computed(() => `${rootMarginTop.value || 0}px ${rootMarginRight.value || 0}px ${rootMarginBottom.value || 0}px ${rootMarginLeft.value || 0}px`)
14+
915
const { isActive, pause, resume } = useIntersectionObserver(
1016
target,
1117
([entry]) => {
1218
isVisible.value = entry?.isIntersecting || false
1319
},
14-
{ root },
20+
{ root, rootMargin },
1521
)
1622
</script>
1723

@@ -25,6 +31,34 @@ const { isActive, pause, resume } = useIntersectionObserver(
2531
<span>Enable</span>
2632
</label>
2733
</div>
34+
<note class="mb-2">
35+
{{ `RootMargin: ${rootMargin}` }}
36+
</note>
37+
<div
38+
grid="~ cols-4 "
39+
gap-2
40+
>
41+
<input
42+
v-model="rootMarginTop"
43+
type="text"
44+
placeholder="Top"
45+
>
46+
<input
47+
v-model="rootMarginRight"
48+
type="text"
49+
placeholder="Right"
50+
>
51+
<input
52+
v-model="rootMarginBottom"
53+
type="text"
54+
placeholder="Bottom"
55+
>
56+
<input
57+
v-model="rootMarginLeft"
58+
type="text"
59+
placeholder="Left"
60+
>
61+
</div>
2862
<div ref="root" class="root">
2963
<p class="notice">
3064
Scroll me down!

packages/core/useIntersectionObserver/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export interface UseIntersectionObserverOptions extends ConfigurableWindow {
2424
/**
2525
* A string which specifies a set of offsets to add to the root's bounding_box when calculating intersections.
2626
*/
27-
rootMargin?: string
27+
rootMargin?: MaybeRefOrGetter<string>
2828

2929
/**
3030
* Either a single number or an array of numbers between 0.0 and 1.
@@ -53,7 +53,7 @@ export function useIntersectionObserver(
5353
): UseIntersectionObserverReturn {
5454
const {
5555
root,
56-
rootMargin = '0px',
56+
rootMargin,
5757
threshold = 0,
5858
window = defaultWindow,
5959
immediate = true,
@@ -70,8 +70,8 @@ export function useIntersectionObserver(
7070

7171
const stopWatch = isSupported.value
7272
? watch(
73-
() => [targets.value, unrefElement(root as MaybeComputedElementRef), isActive.value] as const,
74-
([targets, root]) => {
73+
() => [targets.value, unrefElement(root as MaybeComputedElementRef), toValue(rootMargin), isActive.value] as const,
74+
([targets, root, rootMargin]) => {
7575
cleanup()
7676
if (!isActive.value)
7777
return

0 commit comments

Comments
 (0)