
Security News
ECMAScript 2025 Finalized with Iterator Helpers, Set Methods, RegExp.escape, and More
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
@floating-ui/react-dom
Advanced tools
The @floating-ui/react-dom package is a library for creating floating elements that can be positioned around a target element in a React application. It provides a robust API for positioning tooltips, popovers, dropdowns, and other floating elements, with extensive control over placement, flipping, and shifting based on available space in the viewport.
Positioning Tooltips
This code demonstrates how to create a simple tooltip that appears above a button when clicked. It uses the useFloating hook from @floating-ui/react-dom to manage the tooltip's position and behavior, including automatic adjustment for viewport boundaries.
import { useFloating, offset, flip, shift, arrow } from '@floating-ui/react-dom';
import { useState, useRef } from 'react';
function Tooltip() {
const [open, setOpen] = useState(false);
const arrowRef = useRef(null);
const {x, y, reference, floating, strategy} = useFloating({
placement: 'top',
middleware: [offset(5), flip(), shift({padding: 5}), arrow({element: arrowRef})]
});
return (
<>
<button ref={reference} onClick={() => setOpen(!open)}>
Hover me
</button>
{open && (
<div ref={floating} style={{position: strategy, top: y ?? '', left: x ?? ''}}>
Tooltip content
<div ref={arrowRef} />
</div>
)}
</>
);
}
Creating Popovers
This example shows how to create a popover that appears to the right of a button when clicked. The useFloating hook is used to handle dynamic positioning and flipping to ensure the popover remains visible within the viewport.
import { useFloating, offset, flip, shift } from '@floating-ui/react-dom';
import { useState, useRef } from 'react';
function Popover() {
const [open, setOpen] = useState(false);
const {x, y, reference, floating, strategy} = useFloating({
placement: 'right-start',
middleware: [offset(10), flip(), shift({padding: 8})]
});
return (
<>
<button ref={reference} onClick={() => setOpen(!open)}>
Click me
</button>
{open && (
<div ref={floating} style={{position: strategy, top: y ?? '', left: x ?? ''}}>
Popover content
</div>
)}
</>
);
}
Popper.js is a popular library for managing poppers in web applications. It provides similar functionalities to @floating-ui/react-dom, such as dynamic positioning and flipping of poppers based on the viewport. However, @floating-ui/react-dom is specifically tailored for React and offers a more React-friendly API with hooks.
Tippy.js is another library focused on creating tooltips and popovers. It builds on top of Popper.js and adds an abstraction layer that includes default styling and behavior. Tippy.js is easier to use for simple tooltips and popovers but offers less low-level control compared to @floating-ui/react-dom, which is more configurable and suited for complex positioning scenarios in React applications.
This is the library to use Floating UI with React DOM.
FAQs
Floating UI for React DOM
The npm package @floating-ui/react-dom receives a total of 14,470,698 weekly downloads. As such, @floating-ui/react-dom popularity was classified as popular.
We found that @floating-ui/react-dom demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.
Research
North Korean threat actors linked to the Contagious Interview campaign return with 35 new malicious npm packages using a stealthy multi-stage malware loader.