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

Make Badge implement HasTooltip #10

Open
ngonzalezpazFC opened this issue Oct 3, 2023 · 2 comments
Open

Make Badge implement HasTooltip #10

ngonzalezpazFC opened this issue Oct 3, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@ngonzalezpazFC
Copy link

ngonzalezpazFC commented Oct 3, 2023

It would be useful if each Badge could display a tooltip when hovering over it.

badge

As a workaround I made it work with this code:

                    Span span = new Span(i);
                    Tooltip.forComponent(span).withText(getTranslation(
                            "snaplets.usermanager.viewsview.hardcodedroles.tooltip"))
                            .withPosition(Tooltip.TooltipPosition.TOP_START);

                    Badge b = new Badge(span);
                    b.addThemeName(BadgeVariant.CONTRAST.getVariantName());
                    b.getStyle().setCursor("pointer");
@ngonzalezpazFC ngonzalezpazFC added the enhancement New feature or request label Oct 3, 2023
@paodb paodb added this to Inbox (needs triage) in Flowing Code Addons (legacy) via automation Oct 3, 2023
@paodb paodb moved this from Inbox (needs triage) to To Do in Flowing Code Addons (legacy) Oct 3, 2023
@paodb paodb self-assigned this Oct 3, 2023
@paodb
Copy link
Member

paodb commented Oct 23, 2023

I've been investigating this and according to the HasTooltip interface documentation I don't think adding the implementation od the interface is the right way to go here. It says "Components that implement this interface get a element added inside thecomponent's light DOM and are expected to handle it appropriately on theclient-side. Use this interface ONLY if you are implementing a new component that also has a Web Component counterpart with a custom tooltip support. Otherwise, use Tooltip.forComponent(Component) instead". This is not the case of the Badge class, as it is only a extension of Span class thus it has no web-component counterpart, meaning that it has no support for the HasTooltip interface.
Taking that into account, using Tooltip.forComponent(Component) is okay in this scenario. In the shared example, there's no need to define a Span as the Badge class is already a Span, so, it should be like this:

      Badge b = new Badge();
      b.addThemeName(BadgeVariant.CONTRAST.getVariantName());
      b.getStyle().setCursor("pointer");
      Tooltip.forComponent(badge)
          .withText(getTranslation("snaplets.usermanager.viewsview.hardcodedroles.tooltip"))
          .withPosition(Tooltip.TooltipPosition.TOP_START);

If the implementation of the HasTooltip interface it's still required, then a refactor of the Badge class should be needed in order to have a web-component part that could give support to vaadin-tooltip.

@paodb paodb moved this from To Do to Inbox (needs triage) in Flowing Code Addons (legacy) Oct 30, 2023
@javier-godoy
Copy link
Member

The following works (24.2.2):

public class SpanWithTooltip extends Span implements HasTooltip {

    private static final String TOOLTIP_DATA_KEY = "my_tooltip";
   
    @Override
    public Tooltip setTooltipText(String text) {
      var tooltip = getForComponent(this);
      if (tooltip == null) {
        tooltip = Tooltip.forComponent(this);
        setForComponent(this, tooltip);
      }
      tooltip.setText(text);
      return tooltip;
    }

    @Override
    public Tooltip getTooltip() {
      var tooltip = getForComponent(this);
      if (tooltip == null) {
        tooltip = setTooltipText(null);
      }
      return tooltip;
    }

    private static Tooltip getForComponent(Component component) {
      return (Tooltip) ComponentUtil.getData(component, TOOLTIP_DATA_KEY);
    }

    private static void setForComponent(Component component, Tooltip tooltip) {
      ComponentUtil.setData(component, TOOLTIP_DATA_KEY, tooltip);
    }

  }

After vaadin/flow-components#5676 is merged the following should also work:

public class SpanWithTooltip extends Span implements HasTooltip {

    @Override
    public Tooltip setTooltipText(String text) {
      var tooltip = Tooltip.forComponent(this);
      tooltip.setText(text);
      return tooltip;
    }

    @Override
    public Tooltip getTooltip() {
      return Tooltip.forComponent(this);
    }
	
}

NOTE WELL I'm not suggesting that we must implement it in this manner. Rather, it's a response to the argument that, "if the implementation of the HasTooltip interface is still necessary, then a refactoring of the Badge class would be required to include a web-component part that could provide support for vaadin-tooltip."

@javier-godoy javier-godoy moved this from Inbox (needs triage) to Under consideration in Flowing Code Addons (legacy) Nov 27, 2023
@paodb paodb removed their assignment Jan 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Under consideration
Flowing Code Addons (legacy)
  
Under consideration
Development

No branches or pull requests

3 participants