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

Conversation

jd-solanki
Copy link
Contributor

Description

This function allows getting the parent element of the current element or passed ref.

Additional context

Porting useParentElement from Anu to VueUse as discussed here: #2814


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@jd-solanki jd-solanki changed the title feat(useParent): new function feat(useParentElement): new function Mar 9, 2023
@vaakian
Copy link
Contributor

vaakian commented Mar 10, 2023

BTW, I am wondering if it's possible to aware that a Element was moved to another ParentElement?

<div id="container1">
  <div id="target"></div>
</div>
<div id="container2">
  <!-- move #target here -->
</div>

<script>
    const container2 = document.querySelector('#container2')
    const target = document.querySelector('#target')
    // move #target to #container2
    container2.appendChild(target)
</script>

I think maybe MutationObserver would work.

Comment on lines 15 to 17
return {
parentElement,
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return {
parentElement,
}
return parentElement

Do you foresee any other things to return in the future?

import { getCurrentInstance, ref } from 'vue-demi'
import { unrefElement } from '../unrefElement'

export const useParentElement = (element?: MaybeComputedRef<HTMLElement | SVGElement | null | undefined>) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use function on top-level

antfu
antfu previously approved these changes Mar 28, 2023
@antfu antfu enabled auto-merge (squash) March 28, 2023 09:33
@antfu antfu merged commit e81685a into vueuse:main Mar 28, 2023
4 checks passed
@jd-solanki
Copy link
Contributor Author

Sorry team, I was busy lately and couldn't update the PR. Thanks, Anthony for the updates 🙏🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants