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

Support <tippy> as a child #293

Merged
merged 2 commits into from
Dec 25, 2023

Conversation

kkuegler
Copy link
Contributor

This PR add the "tippy component as a child" behavior from the 4.x version back.

It is slightly different from the old functionality, because this now requires the to='parent' prop to be set on the Tippy compoent.
This should make it easier to understand, where this tooltip belongs and also simplifies the implementation.

I like this "Tippy as a child" style, because in my mind a tooltip is just a small aspect of a component. "Hiding" it as a child seems nicer to me than having Tippy be the parent of the tooltipped element.
This also behaves like :tag="null", i.e. not adding new DOM nodes under or around the tooltipped element. This helps with adding a tooltip without breaking existing styles.

There is some more technical details in the commit messages.

This change allows the following new ways of using Tippy:

<button>
  Click me!
  <tippy to="parent">
    I'm the tooltip
  </tippy>
</button>

or

<button>
  Click me!
  <tippy to="parent" content="I'm the tooltip"/>    
</button>

or

<button>
  Click me!
  <tippy to="parent">
    <template #content>
      I'm the tooltip
    </tooltip>
  </tippy>
</button>

The was used twice and we need this code only once, simlifying
the actual rendering returns.
Having <Tippy> as a child of an element to tooltip was a nice use-case
in 4.x. See KABBOUCHI#123.
We re-add this in a similar manner.

Using to="parent" (as a fixed string), one can now use either the main
<Tippy> slot ("default") or its specific "content" slot to specify
the tooltip content.
The tooltip will be triggered on the parent element without
adding a specifc Tippy node to the DOM.

This should provides a non-interfering way to specify tooltips.

Implementation:
For the to="parent" case, we need to find the parent DOM element, but in
the "worst" case <Tippy> won't render any DOM nodes at all, which could
help us to find the parent.
Instead, we initially render a hidden <span> (and the tooltip content).
Using the span as a template ref ('findParentHelper'), we can get the
parent and assign that to the actual 'elem' ref. This will trigger a
re-render, where we can drop the helper <span> again.
@nuxt-studio
Copy link

nuxt-studio bot commented Jul 31, 2023

Live Preview ready!

Name Edit Preview Latest Commit
vue-tippy Edit on Studio ↗︎ View Live Preview 44b67e4

@kkuegler kkuegler changed the title Support tippy to parent Support <tippy> as a child Jul 31, 2023
@mikoca
Copy link

mikoca commented Nov 30, 2023

Nice I like it.

@KABBOUCHI KABBOUCHI merged commit 5b8f85c into KABBOUCHI:main Dec 25, 2023
4 checks passed
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.

3 participants