ui icon indicating copy to clipboard operation
ui copied to clipboard

Toggle wrapped inside Tooltip causes background to always be transparent

Open xyba1337 opened this issue 1 year ago • 2 comments

Essentially whenever the Toggle is wrapped inside a Tooltip, it sets the background of the Toggle to transparent even though the pressed state is active.

Active pressed state without tooltip: 9dcJd2S

Active pressed state when wrapped inside tooltip: Screenshot_70

<TooltipProvider>
    {ToggleButtons.map((val, idx) => {
      return (
        <Tooltip delayDuration={50}>
          <TooltipTrigger asChild>
            <Toggle
              size="sm"
              pressed={editor.isActive(val.isActive)}
              onPressedChange={val.onPressedChange}
              className="toolboxRounded"
            >
              { val.icon }
            </Toggle>
          </TooltipTrigger>
          <TooltipContent>
            <span>{ val.tooltip }</span>
          </TooltipContent>
        </Tooltip>
      );
    })}
</TooltipProvider>

Is this intended and is there possibly any workaround for this?

xyba1337 avatar Nov 21 '23 11:11 xyba1337

One workaround would be to remove asChild, not ideal though. Will create 2 elements when tabbing through the page.

OliverGrack avatar Dec 03 '23 16:12 OliverGrack

You can add a conditional background style based on the pressed state.

Example using tailwind

<Tooltip delayDuration={50}>
  <TooltipTrigger asChild>
    <Toggle
      size="sm"
      pressed={editor.isActive(val.isActive)}
      onPressedChange={val.onPressedChange}
      className={cn("toolboxRounded", editor.isActive(val.isActive) ? "bg-border" : "")}
    >
      {val.icon}
    </Toggle>
  </TooltipTrigger>
  <TooltipContent>
    <span>{val.tooltip}</span>
  </TooltipContent>
</Tooltip>

Carpye avatar Jan 02 '24 05:01 Carpye

This issue has been automatically closed because it received no activity for a while. If you think it was closed by accident, please leave a comment. Thank you.

shadcn avatar Feb 12 '24 23:02 shadcn

I just ran into this issue. Using asChild causes the value of data-state to be overridden, as both components use the same prop. This is the cause of the styles not being updated, as the TW classes depend on data-state.

I wonder if this could be worked around by simply renaming the props to something like data-tooltip-state and data-toggle-state?

effectivetom avatar Feb 21 '24 03:02 effectivetom

I ran into the same issue. Thanks @effectivetom for the explanation.

Another workaround is to wrap the Toggle inside a div, so that the Tooltip sets the data-state on that div instead of overriding the data-state on the Toggle.

<Tooltip>
  <TooltipTrigger asChild>
    <div>
      <Toggle>
        ...
      </Toggle>
    </div>
  </TooltipTrigger>
  <TooltipContent>
     ...
  </TooltipContent>
</Tooltip>

antsorin avatar Mar 06 '24 10:03 antsorin

Yeah that works too, although it compromises on semantics and accessibility.

I just realised this is actually a Radix concern, and found these issues which provide some more context:

https://github.com/radix-ui/primitives/discussions/560 https://github.com/radix-ui/primitives/issues/602

This one should probably remain closed.

effectivetom avatar Mar 06 '24 20:03 effectivetom