Skip to content

Commit dd98a7a

Browse files
MatkolitMateusz Kołodziej
andauthored
feat(useScroll): add missing measure documentation (#4727)
Co-authored-by: Mateusz Kołodziej <mateusz@net47.pl>
1 parent 589a319 commit dd98a7a

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

packages/core/useScroll/demo.vue

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<script setup lang="ts">
22
import { useScroll } from '@vueuse/core'
3-
import { computed, shallowRef, toRefs, useTemplateRef } from 'vue'
3+
import { computed, nextTick, shallowRef, toRefs, useTemplateRef } from 'vue'
44
55
const el = useTemplateRef<HTMLElement>('el')
66
const smooth = shallowRef(false)
77
const behavior = computed(() => smooth.value ? 'smooth' : 'auto')
8-
const { x, y, isScrolling, arrivedState, directions } = useScroll(el, { behavior })
8+
const { x, y, isScrolling, arrivedState, directions, measure } = useScroll(el, { behavior })
99
const { left, right, top, bottom } = toRefs(arrivedState)
1010
const { left: toLeft, right: toRight, top: toTop, bottom: toBottom } = toRefs(directions)
1111
@@ -27,12 +27,20 @@ const displayY = computed({
2727
y.value = Number.parseFloat(val)
2828
},
2929
})
30+
31+
const height = shallowRef<'h-[500px]' | 'h-[200px]'>('h-[500px]')
32+
function updateScrollPosition() {
33+
height.value = height.value === 'h-[500px]' ? 'h-[200px]' : 'h-[500px]'
34+
nextTick(() => {
35+
measure()
36+
})
37+
}
3038
</script>
3139

3240
<template>
3341
<div class="flex">
3442
<div ref="el" class="w-300px h-300px m-auto overflow-scroll bg-gray-500/5 rounded">
35-
<div class="w-500px h-400px relative">
43+
<div class="w-500px relative" :class="height">
3644
<div position="absolute left-0 top-0" bg="gray-500/5" p="x-2 y-1">
3745
TopLeft
3846
</div>
@@ -64,7 +72,13 @@ const displayY = computed({
6472
<input v-model="displayY" type="number" min="0" max="100" step="10" class="w-full !min-w-0">
6573
</div>
6674
</div>
67-
<label for="smooth-scrolling-option" text="right" opacity="75">Smooth scrolling</label>
75+
<div class="col-span-full flex items-center justify-between">
76+
Measure
77+
<button @click="updateScrollPosition">
78+
Toggle height
79+
</button>
80+
</div>
81+
<label for="smooth-scrolling-option" class="whitespace-nowrap" text="right" opacity="75">Smooth scrolling</label>
6882
<span><input id="smooth-scrolling-option" v-model="smooth" type="checkbox"></span>
6983
<span text="right" opacity="75">isScrolling</span>
7084
<BooleanDisplay :value="isScrolling" />

packages/core/useScroll/index.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,40 @@ const behavior = computed(() => smooth.value ? 'smooth' : 'auto')
7171
const { x, y } = useScroll(el, { behavior })
7272
```
7373

74+
### Recalculate scroll state
75+
76+
You can call the `measure()` method to manually update the scroll position and `arrivedState` at any time.
77+
78+
This is useful, for example, after dynamic content changes or when you want to recalculate the scroll state outside of scroll events.
79+
80+
```ts
81+
import { useScroll } from '@vueuse/core'
82+
import { nextTick, onMounted, useTemplateRef, watch } from 'vue'
83+
84+
const el = useTemplateRef<HTMLElement>('el')
85+
const reactiveValue = shallowRef(false)
86+
87+
const { measure } = useScroll(el)
88+
89+
// In a watcher
90+
watch(reactiveValue, () => {
91+
measure()
92+
})
93+
94+
// Or inside any function
95+
function updateScrollState() {
96+
// ...some logic
97+
nextTick(() => {
98+
measure()
99+
})
100+
}
101+
```
102+
103+
> [!NOTE]
104+
> it's recommended to call `measure()` inside `nextTick()`, to ensure the DOM is updated first.
105+
> The scroll state is initialized automatically `onMount`.
106+
> You only need to call `measure()` manually if you want to recalculate the state after some dynamic changes.
107+
74108
## Directive Usage
75109

76110
```vue

0 commit comments

Comments
 (0)