Skip to content
On this page

useCurrentElement

Category
Export Size
813 B
Last Changed
6 months ago

Get the DOM element of current component as a ref.

Demo

Open your console.log to see the element

Usage

ts
import { useCurrentElement } from '@vueuse/core'

const el = useCurrentElement() // ComputedRef<Element>
import { useCurrentElement } from '@vueuse/core'

const el = useCurrentElement() // ComputedRef<Element>

Caveats

This functions uses $el under the hood.

Value of the ref will be undefined until the component is mounted.

  • For components with a single root element, it will point to that element.
  • For components with text root, it will point to the text node.
  • 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.

It's recommend to only use this function for components with a single root element.

Type Declarations

typescript
export declare function useCurrentElement<
  T extends Element = Element
>(): ComputedRefWithControl<T>
export declare function useCurrentElement<
  T extends Element = Element
>(): ComputedRefWithControl<T>

Source

SourceDemoDocs

Contributors

vaakian X
Anthony Fu

Changelog

v9.1.1 on 8/23/2022
c534e - fix(useActiveElement): use computedWithControl instead of counter ref (#2093)
v8.4.0 on 5/3/2022
e24db - feat: new function

Released under the MIT License.