Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useCurrentElement.md
1---2category: Component3---45# useCurrentElement67Get the DOM element of current component as a ref.89## Usage1011```ts12import { useCurrentElement } from '@vueuse/core'1314const el = useCurrentElement() // ComputedRef<Element>15```1617Or pass a specific vue component1819```vue20<script setup lang="ts">21import { useCurrentElement, VueInstance } from '@vueuse/core'22import { shallowRef } from 'vue'2324const componentRef = shallowRef<VueInstance>(null as unknown as VueInstance)2526const el = useCurrentElement(componentRef) // ComputedRef<Element>27</script>2829<template>30<div>31<OtherVueComponent ref="componentRef" />32<p>Hello world</p>33</div>34</template>35```3637## Caveats3839This functions uses [`$el` under the hood](https://vuejs.org/api/component-instance.html#el).4041Value of the ref will be `undefined` until the component is mounted.4243- For components with a single root element, it will point to that element.44- For components with text root, it will point to the text node.45- For components with multiple root nodes, it will be the placeholder DOM node that Vue uses to keep track of the component's position in the DOM.4647It's recommend to only use this function for components with **a single root element**.4849## Type Declarations5051```ts52export declare function useCurrentElement<53T extends MaybeElement = MaybeElement,54R extends VueInstance = VueInstance,55E extends MaybeElement = MaybeElement extends T56? IsAny<R["$el"]> extends false57? R["$el"]58: T59: T,60>(rootComponent?: MaybeElementRef<R>): ComputedRefWithControl<E>61```62