Skip to content

Commit

Permalink
fix(useElementSize): get value in onMounted hook (#3366)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
chaii3 and antfu committed Nov 9, 2023
1 parent 71b4653 commit 165c88a
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/core/useElementSize/index.ts
@@ -1,3 +1,4 @@
import { tryOnMounted } from '@vueuse/shared'
import { computed, ref, watch } from 'vue-demi'
import type { MaybeComputedElementRef } from '../unrefElement'
import type { UseResizeObserverOptions } from '../useResizeObserver'
Expand Down Expand Up @@ -58,6 +59,14 @@ export function useElementSize(
options,
)

tryOnMounted(() => {
const ele = unrefElement(target)
if (ele) {
width.value = 'offsetWidth' in ele ? ele.offsetWidth : initialSize.width
height.value = 'offsetHeight' in ele ? ele.offsetHeight : initialSize.height
}
})

const stop2 = watch(
() => unrefElement(target),
(ele) => {
Expand Down

0 comments on commit 165c88a

Please sign in to comment.