Skip to content

Tooltip provider for easily sharing tooltip amongst multiple elements #845

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

Merged
merged 25 commits into from
Dec 12, 2022

Conversation

gabrieljablonski
Copy link
Member

@gabrieljablonski gabrieljablonski commented Dec 8, 2022

Closes #837
Closes #847

Using just anchorId without the provider should still work fine.
See App.tsx for usage example.

The only major feature missing is using a prop such as data-tooltip-id on the anchor element and filtering the event listener and observer subscriptions by the tooltip id.
The provider also has to keep the anchorRefs and activeAnchor separate by tooltip id.

Instead of using data-tooltip-id, it would also be possible to change the context data signature to something such as

interface TooltipContextData {
  getAnchorRefs: (tooltipId?: string) => Set<React.RefObject<HTMLElement>>
  attach: (...ref: React.RefObject<HTMLElement>[], tooltipId?: string) => void
  detach: (...ref: React.RefObject<HTMLElement>[], tooltipId?: string) => void
  getActiveAnchor: (tooltipId?: string) => React.RefObject<HTMLElement>
  setActiveAnchor: React.Dispatch<React.SetStateAction<React.RefObject<HTMLElement>>>
}

tooltipId is used to keep track of which tooltip is being accessed.

Without a change like one of these two, the provider only works with one <Tooltip /> element.

If needed, multiple providers (one <Tooltip /> inside each) can be separately used as a workaround.

@gabrieljablonski gabrieljablonski marked this pull request as ready for review December 10, 2022 04:47
@gabrieljablonski gabrieljablonski changed the title [DRAFT] Tooltip provider for easily sharing tooltip amongst multiple elements Tooltip provider for easily sharing tooltip amongst multiple elements Dec 10, 2022
Copy link
Member

@danielbarion danielbarion left a comment

Choose a reason for hiding this comment

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

The looks nice to me, but only 2 things:

I believe we should use explicit words, like event instead of e.

And can you create docs inside docs folder related to the new possibility of use of the Tooltip, please?

Comment on lines +29 to +45
return (
<span
ref={anchorRef}
data-tooltip-place={place}
data-tooltip-content={content}
data-tooltip-html={html}
data-tooltip-variant={variant}
data-tooltip-offset={offset}
data-tooltip-wrapper={wrapper}
data-tooltip-events={events}
data-tooltip-position-strategy={positionStrategy}
data-tooltip-delay-show={delayShow}
data-tooltip-delay-hide={delayHide}
>
{children}
</span>
)
Copy link
Member Author

Choose a reason for hiding this comment

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

After some testing, I've realized cloneElement() doesn't work super well when passing functional components as children to the wrapper.

Using an actual DOM element (span seems ok) didn't seem to cause any issues.

Copy link
Member

@danielbarion danielbarion left a comment

Choose a reason for hiding this comment

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

looks good to me!

@danielbarion danielbarion merged commit 5d912bb into master Dec 12, 2022
@danielbarion danielbarion deleted the tooltip-provider branch December 12, 2022 19:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

One tooltip, many anchors [FEAT REQ] Easier way to attach tooltips to dynamically generated elements
2 participants