React Popper Tooltip
react-popper-tooltip is a React library that provides a flexible hook (`usePopperTooltip`) for creating smart, accessible tooltips, popovers, and overlays. It leverages the robust positioning engine of `react-popper` (which in turn uses `popper.js`) under the hood. Currently at version 4.4.2, the library shows an active release cadence with frequent patch and minor updates addressing bugs and adding features like Shadow DOM support and improved hover mechanics. A key differentiator since its v4 release is the exclusive reliance on a hook-based API, moving away from older render prop patterns for a more modern and flexible developer experience. It provides optional default styling but is designed to be highly customizable with any CSS or CSS-in-JS solution.
Common errors
-
TypeError: (0 , react_popper_tooltip__WEBPACK_IMPORTED_MODULE_1__.TooltipTrigger) is not a function
cause Attempting to use the `TooltipTrigger` render prop component, which was removed in version 4.0.0.fixMigrate your tooltip implementation to use the `usePopperTooltip` hook instead of `TooltipTrigger`. -
Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.
cause The `usePopperTooltip` hook was incorrectly imported as a default export instead of a named export.fixEnsure `usePopperTooltip` is imported as a named export: `import { usePopperTooltip } from 'react-popper-tooltip';`. -
Tooltip appears, but it is unstyled or looks like a plain div without an arrow.
cause The default CSS styles (`dist/styles.css`) have not been imported, or custom styles are missing/incorrectly applied.fixAdd `import 'react-popper-tooltip/dist/styles.css';` to your project's entry point, or apply custom CSS to elements with `className='tooltip-container'` and `className='tooltip-arrow'`.
Warnings
- breaking Version 4.0.0 introduced a significant breaking change by deprecating and removing the `TooltipTrigger` render prop API. The `usePopperTooltip` hook is now the exclusive and recommended way to create tooltips.
- gotcha Versions 4.1.0 and 4.1.2 introduced and attempted to fix issues with a new hover trigger mechanism, which had unintended side effects impacting tooltip interactions. Version 4.2.0 reverted to an older, more stable hover mechanic while addressing previous problems.
- gotcha The primary documentation on the GitHub repository and npmjs.com is for v4.x and the `usePopperTooltip` hook API. If you are using the older v3.x render prop API, you must refer to the v3-specific documentation as the current docs will not be applicable.
- gotcha While `react-popper-tooltip` provides a `dist/styles.css` file for basic styling, it is not automatically applied. Developers must manually import this file or provide their own custom styling for the tooltip container and arrow elements to achieve a visual tooltip.
Install
-
npm install react-popper-tooltip -
yarn add react-popper-tooltip -
pnpm add react-popper-tooltip
Imports
- usePopperTooltip
import usePopperTooltip from 'react-popper-tooltip';
import { usePopperTooltip } from 'react-popper-tooltip'; - styles.css
import 'react-popper-tooltip/dist/styles.css';
- UsePopperTooltipOptions
import type { UsePopperTooltipOptions } from 'react-popper-tooltip';
Quickstart
import * as React from 'react';
import { render } from 'react-dom';
import { usePopperTooltip } from 'react-popper-tooltip';
import 'react-popper-tooltip/dist/styles.css';
function App() {
const {
getArrowProps,
getTooltipProps,
setTooltipRef,
setTriggerRef,
visible,
} = usePopperTooltip();
return (
<div className="App">
<button type="button" ref={setTriggerRef}>
Trigger
</button>
{visible && (
<div
ref={setTooltipRef}
{...getTooltipProps({ className: 'tooltip-container' })}
>
<div {...getArrowProps({ className: 'tooltip-arrow' })} />
Tooltip Content
</div>
)}
</div>
);
}
render(<App />, document.getElementById('root'));