Skip to content
On this page

unrefElement

Category
Export Size
783 B
Last Changed
6 months ago

Unref for dom element.

Usage

html
<template>
  <div ref="div"/>
  <HelloWorld ref="hello"/>
</template>

<script setup>
import { ref } from 'vue'
import { unrefElement } from '@vueuse/core'

const div = ref() // will be bind to the <div> element
const hello = ref() // will be bind to the HelloWorld Component

onMounted(() => {
  console.log(unrefElement(div)) // the <div> element
  console.log(unrefElement(hello)) // the root element of the HelloWorld Component
})
</script>
<template>
  <div ref="div"/>
  <HelloWorld ref="hello"/>
</template>

<script setup>
import { ref } from 'vue'
import { unrefElement } from '@vueuse/core'

const div = ref() // will be bind to the <div> element
const hello = ref() // will be bind to the HelloWorld Component

onMounted(() => {
  console.log(unrefElement(div)) // the <div> element
  console.log(unrefElement(hello)) // the root element of the HelloWorld Component
})
</script>

Type Declarations

typescript
export declare type VueInstance = ComponentPublicInstance
export declare type MaybeElementRef<T extends MaybeElement = MaybeElement> =
  MaybeRef<T>
export declare type MaybeComputedElementRef<
  T extends MaybeElement = MaybeElement
> = MaybeComputedRef<T>
export declare type MaybeElement =
  | HTMLElement
  | SVGElement
  | VueInstance
  | undefined
  | null
export declare type UnRefElementReturn<T extends MaybeElement = MaybeElement> =
  T extends VueInstance ? Exclude<MaybeElement, VueInstance> : T | undefined
/**
 * Get the dom element of a ref of element or Vue component instance
 *
 * @param elRef
 */
export declare function unrefElement<T extends MaybeElement>(
  elRef: MaybeComputedElementRef<T>
): UnRefElementReturn<T>
export declare type VueInstance = ComponentPublicInstance
export declare type MaybeElementRef<T extends MaybeElement = MaybeElement> =
  MaybeRef<T>
export declare type MaybeComputedElementRef<
  T extends MaybeElement = MaybeElement
> = MaybeComputedRef<T>
export declare type MaybeElement =
  | HTMLElement
  | SVGElement
  | VueInstance
  | undefined
  | null
export declare type UnRefElementReturn<T extends MaybeElement = MaybeElement> =
  T extends VueInstance ? Exclude<MaybeElement, VueInstance> : T | undefined
/**
 * Get the dom element of a ref of element or Vue component instance
 *
 * @param elRef
 */
export declare function unrefElement<T extends MaybeElement>(
  elRef: MaybeComputedElementRef<T>
): UnRefElementReturn<T>

Source

SourceDocs

Contributors

Anthony Fu
Julian Meinking
Jelf

Changelog

v8.9.1 on 7/8/2022
a9ccc - feat(all): use MaybeComputedRef (#1768)
v8.7.4 on 6/18/2022
3d846 - fix: revert #1657
v8.7.0 on 6/16/2022
050da - fix: don't return the Vue instance when $el is null/undefined (#1657)
v8.1.0 on 3/16/2022
3e194 - fix: improve types (#1409)

Released under the MIT License.