Skip to content

Commit 0fa179e

Browse files
DerZadeantfu
andauthored
feat(useElementBounding): add updateTiming option (#3869)
Co-authored-by: Anthony Fu <[email protected]>
1 parent 905b970 commit 0fa179e

File tree

1 file changed

+19
-1
lines changed
  • packages/core/useElementBounding

1 file changed

+19
-1
lines changed

packages/core/useElementBounding/index.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ export interface UseElementBoundingOptions {
3333
* @default true
3434
*/
3535
immediate?: boolean
36+
37+
/**
38+
* Timing to recalculate the bounding box
39+
*
40+
* Setting to `next-frame` can be useful when using this together with something like {@link useBreakpoints}
41+
* and therefore the layout (which influences the bounding box of the observed element) is not updated on the current tick.
42+
*
43+
* @default 'sync'
44+
*/
45+
updateTiming?: 'sync' | 'next-frame'
3646
}
3747

3848
/**
@@ -50,6 +60,7 @@ export function useElementBounding(
5060
windowResize = true,
5161
windowScroll = true,
5262
immediate = true,
63+
updateTiming = 'sync',
5364
} = options
5465

5566
const height = ref(0)
@@ -61,7 +72,7 @@ export function useElementBounding(
6172
const x = ref(0)
6273
const y = ref(0)
6374

64-
function update() {
75+
function recalculate() {
6576
const el = unrefElement(target)
6677

6778
if (!el) {
@@ -90,6 +101,13 @@ export function useElementBounding(
90101
y.value = rect.y
91102
}
92103

104+
function update() {
105+
if (updateTiming === 'sync')
106+
recalculate()
107+
else if (updateTiming === 'next-frame')
108+
requestAnimationFrame(() => recalculate())
109+
}
110+
93111
useResizeObserver(target, update)
94112
watch(() => unrefElement(target), ele => !ele && update())
95113
// trigger by css or style

0 commit comments

Comments
 (0)