React-tooltip: Control position of tooltip's tip

Created on 1 Dec 2015  路  9Comments  路  Source: wwayne/react-tooltip

Is there a way to control the tooltip's tip (the little arrow below the content bubble) more specifically?
Example, say I want a tooltip's tip to be at the bottom left side of the tooltip as opposed to being always in the center depending on whether it is set to be top/bottom/left/right.

Feature

Most helpful comment

this works for me :)

.tooltip { &::after { left: 82% !important; } }

All 9 comments

no, have't supported this feature at the moment

Yeah, a way to define the position of the tip would be great. We can do it now with some hacky global CSS, but it's not pretty.

@Techwraith Could you share your hacky global CSS?

@Techwraith I'd also be interested in your hacky css

I set 'left' property of '__react_component_tooltip.place-bottom' class and it helped in my case. Also I used 'data-offset' property.

.__react_component_tooltip.place-bottom:after { left: 90%; }

+1

+1

After much playing around came up with this function to dynamically position the tip to account for the edges of the page:

export default ({ left, top }, event, triggerElement, tooltipElement) => {
  // left position of the element you hovered over + half it's width
  const arrowLeft = triggerElement.getBoundingClientRect().left + triggerElement.offsetWidth / 2;
  // the triggering element's bottom edge
  const arrowTop = triggerElement.getBoundingClientRect().top + triggerElement.offsetHeight + 4;
  const sheet = document.createElement('style');

  // after and before are black border triangle and white triangle
  sheet.innerHTML = `
    .__react_component_tooltip.place-bottom::after {
      position: fixed;
      top: ${arrowTop}px;
      left: ${arrowLeft}px;
    }
    .__react_component_tooltip.place-bottom::before {
      position: fixed;
      top: ${arrowTop - 1}px;
      left: ${arrowLeft}px;
    }
  `;
  document.body.appendChild(sheet);

  // https://github.com/wwayne/react-tooltip/issues/476
  return {
    top,
    left: Math.min(left, window.innerWidth - tooltipElement.offsetWidth),
  };
};

Usage:

import overridePosition from './overridePosition';

 <ReactTooltip
     overridePosition={overridePosition}
>

Screenshot 2020-07-09 16 14 09

this works for me :)

.tooltip { &::after { left: 82% !important; } }

Was this page helpful?
0 / 5 - 0 ratings