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

Add tooltip component #2843

Merged
merged 38 commits into from
May 10, 2021
Merged

Add tooltip component #2843

merged 38 commits into from
May 10, 2021

Conversation

davwheat
Copy link
Member

@davwheat davwheat commented May 8, 2021

Prevents breaking changes as a result of #2697. Progresses flarum/issue-archive#179.

Changes proposed in this pull request:
This is still WIP. Created as a draft now to allow for any comments to be made while the component is still being made.

This adds a new Tooltip component to core. This should be used by extensions instead of $.tooltip to aid in the shift to other tooltip libraries (hopefully CSS tooltips!) in the future.

I've tried to include the main tooltip options that are used by extensions, such as delay and html, but these won't be easy or possible to keep if we shift to CSS tooltips, so they've been marked as deprecated and only exist to allow extensions to more easily shift to this component.

Reviewers should focus on:
Will the deprecation of html and delay cause an issue with #2697 as they aren't available in that? Do we consider removing deprecated features within a stable release ok, bearing in mind there is a fairly bold warning in the docblock, or should we just not implement them as part of the component in the first place.

I think implementing them as deprecated features will help ext devs migrate to this component faster and more easily now, bearing in mind the large number of other changes they need to perform.

Should we also add a deprecation warning to the tooltip helper when it's called from outside the Tooltip component to make this more obvious to devs?

Screenshot

Confirmed

  • Frontend changes: tested on a local Flarum installation.
  • Backend changes: tests are green (run composer test).

Required changes:

  • Related documentation PR: (Remove if irrelevant)
  • Related core extension PRs: (Remove if irrelevant)

@davwheat davwheat added this to the 1.0 milestone May 8, 2021
@davwheat davwheat self-assigned this May 8, 2021
@davwheat davwheat marked this pull request as draft May 8, 2021 23:27
js/src/common/components/Tooltip.tsx Outdated Show resolved Hide resolved
@askvortsov1 askvortsov1 removed this from the 1.0 milestone May 9, 2021
@askvortsov1
Copy link
Sponsor Member

No need for both the issue and the PR on the milestone

@davwheat davwheat marked this pull request as ready for review May 9, 2021 01:57
@davwheat
Copy link
Member Author

davwheat commented May 9, 2021

I'd love to hear opinions on the deprecation warning added in 356df4d.

I don't know how I feel about the way this is implemented, but it was the best I could think of. Maybe my brain at a time other than 3am might be able to do a better job!

It only serves to inform extension devs that their current tooltip method is deprecated and that they should migrate to the new version.

@davwheat
Copy link
Member Author

davwheat commented May 9, 2021

I've forgotten to implement tooltipVisible 🙈

I'll do that tomorrow.

Copy link
Sponsor Member

@askvortsov1 askvortsov1 left a comment

Choose a reason for hiding this comment

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

The more I look at this, the less I like that the tooltip is created as an additional HTML node. Adding a tooltip is a modification to an existing element, not the creation of a new element.

Would pseudoelement conflicts arise when a JS tooltip is used? Conceptually a CSS-only tooltip is cool, but the more I see workarounds needed to implement it, the less I'm convinced that CSS is ready.

js/src/common/index.js Show resolved Hide resolved
Copy link
Member

@SychO9 SychO9 left a comment

Choose a reason for hiding this comment

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

I'm bad at typescript so I'd rather not review this, but I do have a comment

js/src/common/components/Tooltip.tsx Outdated Show resolved Hide resolved
Instead of using a container to house the tooltip, we'll now modify the
first direct child of the Tooltip component.

The Tooltip component will ensure that:
- children are passed to it
- only one child is present
- that child is an actual HTML Element and not a text node, or similar
- that child is currently present in the DOM

Only after all of the above are satisfied, will the tooltip be created
on that element. We store a reference to the DOM node that the tooltip
should be created on, then use this to perform tooltip actions via
jQuery. If this element gets changes (e.g. the tooltip content is
updated to another element) then the tooltip will be recreated.

If any of the first 3 requirements are not satisfied, an error will
be thrown to alert the developer to their misuse of this component.

To make this work, we do need to overwrite the title attribute of
the element with the tooltip, but this is the only solution other than
specifying `title` as an option when making the tooltip, but this is
not accessible by screenreaders unless they simulate a hover on the
element.
@davwheat
Copy link
Member Author

davwheat commented May 10, 2021

Ok, I've changed the component to apply the tooltip directly to the first child of the component.

Along comes a lot of error detection measures to ensure devs find out if they're using this component incorrectly.

For more info, see the message on commit 22ebd0b

@davwheat
Copy link
Member Author

This has made the bundle a lot bigger, but a chunk of this can be shaved off if you don't think that manually showing/hiding tooltips is important. I personally don't at all, as tooltips shouldn't be used for that.

@davwheat davwheat requested a review from askvortsov1 May 10, 2021 00:54
Copy link
Sponsor Member

@askvortsov1 askvortsov1 left a comment

Choose a reason for hiding this comment

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

Overall, I like this very much! Left some relatively minor style comments.

js/src/common/components/Tooltip.tsx Show resolved Hide resolved
js/src/common/components/Tooltip.tsx Show resolved Hide resolved
private updateVisibility() {
if (this.childDomNode === null) return;

if (this.attrs.tooltipVisible === true) {
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

These very type-specific checks are confusing to me: isn't tooltipVisible required to be boolean?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's bool or undefined. We need the strong checks to ensure we don't attempt to hide or show the tooltip when it's undefined.

We don't need the true one, but I thought matching them looked nicer and less like a mistake.

js/src/common/components/Tooltip.tsx Outdated Show resolved Hide resolved
js/src/common/components/Tooltip.tsx Outdated Show resolved Hide resolved
js/src/common/components/Tooltip.tsx Show resolved Hide resolved
@davwheat davwheat requested a review from askvortsov1 May 10, 2021 16:37
@askvortsov1
Copy link
Sponsor Member

Other than these, code works locally.

@davwheat davwheat merged commit f9e8424 into master May 10, 2021
@davwheat davwheat deleted the dw/add-tooltip-component branch May 10, 2021 20:06
@SychO9 SychO9 added this to the 1.0 milestone May 10, 2021
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.

4 participants