Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(useParentElement): new function #2855

Merged
merged 5 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export * from './useOffsetPagination'
export * from './useOnline'
export * from './usePageLeave'
export * from './useParallax'
export * from './useParentElement'
export * from './usePerformanceObserver'
export * from './usePermission'
export * from './usePointer'
Expand Down
9 changes: 9 additions & 0 deletions packages/core/useParentElement/Demo.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script lang="ts" setup>
import { useParentElement } from '@vueuse/core'

const parentElement = useParentElement()
</script>

<template>
<p>Parent element tag: {{ parentElement ? parentElement.tagName : 'Finding...' }}</p>
</template>
36 changes: 36 additions & 0 deletions packages/core/useParentElement/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
category: Elements
---

# useParentElement

Get parent element of the given element

## 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'

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

const tooltipWrapper = useParentElement(tooltip)

onMounted(() => {
console.log(tooltipWrapper.value)
})
```
47 changes: 47 additions & 0 deletions packages/core/useParentElement/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { defineComponent, h, isVue2, nextTick, ref } from 'vue-demi'
import { mount } from '../../.test'
import { useParentElement } from '.'

describe.skipIf(isVue2)('useParentElement', () => {
it('should be defined', () => {
expect(useParentElement).toBeDefined()
})

it('should work without ref', async () => {
let parentElement: any

const Child = defineComponent({
setup() {
parentElement = useParentElement()
return () => h('div', {})
},
})

mount({
setup() {
return () => h('parent', h(Child))
},
})

await nextTick()

expect(parentElement.value!.tagName).to.equal('PARENT')
})

it('should accept ref', async () => {
const liEl = ref()
const parentElement = useParentElement(liEl)

mount(defineComponent({
setup() {
return () => h('ul', {}, [
h('li', { ref: liEl }),
])
},
}))

await nextTick()

expect(parentElement.value!.tagName).to.equal('UL')
})
})
23 changes: 23 additions & 0 deletions packages/core/useParentElement/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { MaybeComputedRef } from '@vueuse/shared'
import { resolveUnref, tryOnMounted } from '@vueuse/shared'
import type { Ref } from 'vue-demi'
import { shallowRef, watch } from 'vue-demi'
import { unrefElement } from '../unrefElement'
import { useCurrentElement } from '../useCurrentElement'

export function useParentElement(
element: MaybeComputedRef<HTMLElement | SVGElement | null | undefined> = useCurrentElement<HTMLElement | SVGAElement>(),
): Readonly<Ref<HTMLElement | SVGElement | null | undefined>> {
const parentElement = shallowRef<HTMLElement | SVGElement | null | undefined>()

const update = () => {
const el = unrefElement(element)
if (el)
parentElement.value = el.parentElement
}

tryOnMounted(update)
watch(() => resolveUnref(element), update)

return parentElement
}