reactjsmaterial-uitooltippopper.jsreact-popper

Place MaterialUI Tooltip "on top" of anchor element? (React)


Seems impossible to place <Tooltip> "on top" (stacked/layered above) the triggering anchor element.

It always appears outside the parent, using "placement" to decide where. I believe it's the Flip tool within Popper that manages placement and ensures visibility. I've tried passing Popper option modifiers to disable flip, and adjusting the offset. Some recommendations were to skip preventing overflow, and disable GPU acceleration. I'm down a rabbit-hole of MaterialUI internals to accomplish this. I commented out what seems like unlikely overkill solutions.

Example outside MUI: react-tooltip (includes pointer tracking, beyond this question).

<Tooltip title="My Label" placement="top" PopperProps={{
  popperOptions: {
    modifiers: {
      flip: { enabled: false },
      // computeStyle: {
      //   gpuAcceleration: false
      // },
      // preventOverflow: {
      //   enabled: false,
      //   padding: 0
      // },
      offset: {
        offset: '-20px -20px'
      }
    }
  }
}}></Tooltip>
    <h3>My Text</h3>
</Tooltip>

Solution

  • You need to enable the offset like this:

    <Tooltip title="My Label" placement="top" PopperProps={{
      popperOptions: {
        modifiers: {
          flip: { enabled: false },
          offset: {
            enabled: true,
            offset: '0px -60px'
          }
        }
      }
    }}>
        <h3>My Text</h3>
    </Tooltip>
    

    this will enable you to place the tooltip on top