React Tooltip Component

5.30.1 · active · verified Sun Apr 19

react-tooltip is a comprehensive and highly customizable React component for displaying tooltips. Currently at version 5.30.1, it boasts an active development cycle with frequent patch and minor releases that introduce new features, resolve bugs, and enhance performance. Built on `floating-ui`, it offers significant performance improvements over its predecessors and is written entirely in TypeScript, providing robust type definitions out-of-the-box. Key differentiators include extensive customization options via props and data attributes, support for various event triggers (e.g., hover, click), and built-in accessibility features like automatic `aria-describedby` generation. It requires React and ReactDOM versions 16.14.0 or higher as peer dependencies, ensuring compatibility with modern React applications.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic hover and click triggered tooltips, along with programmatic control.

import React, { useRef, useState } from 'react';
import { Tooltip, TooltipRef } from 'react-tooltip';

function MyComponent() {
  const tooltipRef = useRef<TooltipRef>(null);
  const [isTooltipOpen, setIsTooltipOpen] = useState(false);

  const showProgrammaticTooltip = () => {
    if (tooltipRef.current) {
      tooltipRef.current.showTooltip();
      setIsTooltipOpen(true);
    }
  };

  const hideProgrammaticTooltip = () => {
    if (tooltipRef.current) {
      tooltipRef.current.hideTooltip();
      setIsTooltipOpen(false);
    }
  };

  return (
    <div>
      <a
        data-tooltip-id="my-hover-tooltip"
        data-tooltip-content="Hello, hover world!"
      >
        Hover over me
      </a>
      <Tooltip id="my-hover-tooltip" />

      <br /><br />

      <button
        data-tooltip-id="my-click-tooltip"
        data-tooltip-content="Click me for content!"
      >
        Click for Tooltip
      </button>
      <Tooltip id="my-click-tooltip" place="right" events={['click']} />

      <br /><br />

      <button onClick={showProgrammaticTooltip} disabled={isTooltipOpen}>
        Show Programmatic Tooltip
      </button>
      <button onClick={hideProgrammaticTooltip} disabled={!isTooltipOpen}>
        Hide Programmatic Tooltip
      </button>
      <span
        id="programmatic-anchor"
        style={{ marginLeft: '10px', padding: '5px', border: '1px solid gray' }}
      >
        Anchor
      </span>
      <Tooltip
        id="programmatic-tooltip"
        content="I'm controlled by buttons!"
        isOpen={isTooltipOpen}
        setIsOpen={setIsTooltipOpen}
        anchorSelect="#programmatic-anchor"
        ref={tooltipRef}
        place="bottom"
      />
    </div>
  );
}

export default MyComponent;

view raw JSON →