Skip to content

useParentElement

Category
Export Size
497 B
Last Changed
3 months ago

Get parent element of the given element

Demo

Parent element tag: DIV

Usage

When no argument is passed, it will return the parent element of the current component.

js
import { useParentElement } from '@vueuse/core'

const parentEl = useParentElement()

onMounted(() => {
  console.log(parentEl.value)
})

It can also accept a ref as the first argument.

ts
import { useParentElement } from '@vueuse/core'
import { shallowRef } from 'vue'

// Don't forget to bind the ref to the element
const tooltip = shallowRef<HTMLElement | undefined>()

const tooltipWrapper = useParentElement(tooltip)

onMounted(() => {
  console.log(tooltipWrapper.value)
})
js
import { useParentElement } from '@vueuse/core'
import { shallowRef } from 'vue'
// Don't forget to bind the ref to the element
const tooltip = shallowRef()
const tooltipWrapper = useParentElement(tooltip)
onMounted(() => {
  console.log(tooltipWrapper.value)
})

Type Declarations

typescript
export declare function useParentElement(
  element?: MaybeRefOrGetter<HTMLElement | SVGElement | null | undefined>,
): Readonly<ShallowRef<HTMLElement | SVGElement | null | undefined>>

Source

SourceDemoDocs

Contributors

Anthony Fu
IlyaL
Anthony Fu
青椒肉丝
JD Solanki

Changelog

v12.8.0 on
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
v12.3.0 on
59f75 - feat(toValue): deprecate toValue from @vueuse/shared in favor of Vue's native
v12.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v10.0.0-beta.4 on
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter
0a72b - feat(toValue): rename resolveUnref to toValue
v10.0.0-beta.2 on
e8168 - feat: new function (#2855)

Released under the MIT License.

2 days of talks + 1 day of live coding.
50+ speakers. All major frameworks!
Join for Free ➜