react-tooltip

repository·master·Indexed 26 days ago

https://github.com/reacttooltip/react-tooltip

A React library used to display informative tooltips when users hover over or interact with specific elements. It supports custom HTML/React content via children, CSS selector-based targeting with the anchorSelect prop, and configurable event triggers such as openOnClick, openEvents, and closeEvents. Version 6.0.8.

Tokens
27.9K
Snippets
66
Records
100
Agent score
86%

What's inside react-tooltip

  1. Make tooltips clickable and accessible

    master

    By default, tooltips disappear when the pointer leaves the anchor element. To allow users to interact with elements inside the tooltip (like buttons or inputs) or to satisfy WCAG 'hoverable' requirements, use the clickable prop on the <Tooltip /> component.

    <a id="clickable">◕‿‿◕</a>
    <Tooltip anchorSelect="#clickable" clickable>
      <button>You can click me!</button>
    </Tooltip>
  2. Migrate Color Styles: type to variant

    master

    The type prop used in V4 for styling has been renamed to variant in V5.

    Available values for variant are:

    • 'dark'
    • 'light'
    • 'success'
    • 'warning'
    • 'error'
    • 'info'

    You can apply the variant via the Tooltip component or directly on the anchor element using the data-tooltip-variant attribute.

    import { Tooltip } from 'react-tooltip';
    
    <a
      data-tooltip-id="my-tooltip"
      data-tooltip-content="Hello world!"
      data-tooltip-variant="success"
    >
      ◕‿‿◕
    </a>
    <Tooltip id="my-tooltip" />
  3. Set the default styling

    master

    For versions v5.13.0 or newer, styles are injected into the page by default and no manual import is required.

    If you are using a version older than v5.13.0, you must manually import the CSS file for the tooltip to appear. It is recommended to do this once in your src/index.js or equivalent entry point.

    import 'react-tooltip/dist/react-tooltip.css'
  4. Customize Tooltip using CSS Classes

    master

    To style the tooltip using CSS classes, pass a string to the className prop.

    Note on Specificity: Because react-tooltip uses default styles, you may need to increase your CSS specificity to override them without using !important. For example, instead of just .example, use .parent-class .example to ensure your rules take precedence.

    If you need to style the tooltip arrow specifically, you can target the .example-arrow class within your custom class.

    import { Tooltip } from 'react-tooltip'
    
    <style>
    .example {
      color: #222;
      background-color: rgb(0, 247, 255);
    }
    </style>
    
    <div className="example-container">
      <a
        data-tooltip-id="my-tooltip-styles"
        data-tooltip-content="Hello world!"
      >
        ◕‿‿◕
      </a>
      <Tooltip id="my-tooltip-styles" className="example" />
    </div>
  5. Migrate from V4 to V5

    master

    When upgrading from V4 to V5, note that the core implementation has changed from class components to a TypeScript-based implementation using floating-ui.

    Key Breaking Changes:

    • Module Name: The exported component is now Tooltip instead of ReactTooltip. To maintain compatibility with existing codebases that use the name ReactTooltip, use: import { Tooltip as ReactTooltip } from 'react-tooltip'.
    • Data Attributes: All data attributes are now prefixed with data-tooltip-.
      • data-for $\rightarrow$ data-tooltip-id
      • data-tip $\rightarrow$ data-tooltip-content
    • Content Handling: The getContent prop has been removed. Use the content prop for dynamic content, or the render prop for complex dynamic rendering.
    • Visual Effects: The default behavior is now equivalent to V4's solid effect. To achieve the V4 effect="float" behavior, use the float prop.
    • Padding: Default padding has changed from 8px 21px to 8px 16px.
  6. Run the React Tooltip Scaling Benchmark

    master

    The React Tooltip Scaling Benchmark is an automated harness used to compare the mount/unmount performance of V5 and V6 across various tooltip counts. You can run a single pass, a full statistical run, or aggregate existing results.

    # Single benchmark pass (5 repeats per count)
    node benchmarks/run-benchmark.mjs
    
    # Full statistical run (100 passes across 5 workers, then aggregate)
    yarn benchmark:scaling:full -r 100 -w 5
    
    # Aggregate the latest N results
    node benchmarks/aggregate-benchmarks.mjs --latest 100
  7. Deploy the website to GitHub Pages

    master

    You can deploy the website using different methods depending on your access:

    Using SSH:

    $ USE_SSH=true yarn deploy

    Using GitHub Username (Non-SSH):

    $ GIT_USER=<Your GitHub username> yarn deploy

    If you are using GitHub Pages for hosting, the yarn deploy command builds the website and pushes it to the gh-pages branch.

  8. Replace HTML string APIs with `children` or `render` in V6

    master

    In V6, the data-tooltip-html attribute and the html prop have been removed. To display rich or multiline content, use one of the following methods:

    1. Use children: Pass JSX directly as children to the Tooltip component. This is the preferred method for static rich content.
    2. Use render: Pass a function to the render prop that returns JSX. This is useful for dynamic content.

    Note: data-tooltip-content and the content prop remain supported for plain text strings.

    // Using children for rich content
    <a data-tooltip-id="my-tooltip">◕‿‿◕</a>
    <Tooltip id="my-tooltip">
      <div>
        <h3>Cool stuff</h3>
        <ul>
          <li>This is cool</li>
        </ul>
      </div>
    </Tooltip>
    
    // Using render for rich content
    <a data-tooltip-id="my-tooltip">◕‿‿◕</a>
    <Tooltip
      id="my-tooltip"
      render={() => (
        <div>
          Hello
          <br />
          <b>rich</b> content
        </div>
      )}
    />
  9. Use tooltips with anchor data attributes

    master

    You can trigger tooltips by adding specific data-tooltip-* attributes to your anchor elements.

    1. Import Tooltip from react-tooltip.
    2. Add data-tooltip-id="<tooltip id>" and data-tooltip-content="<your text>" to your anchor elements.
    3. Create a <Tooltip /> component and ensure its id matches the data-tooltip-id used on the anchors.
    import { Tooltip } from 'react-tooltip'
    
    // 1. Anchor elements with data attributes
    <a
      data-tooltip-id="my-tooltip"
      data-tooltip-content="Hello world!"
      data-tooltip-place="top"
    >
      ◕‿‿◕
    </a>
    
    // 2. The Tooltip element with matching ID
    <Tooltip id="my-tooltip" />