
Product
Rubygems Ecosystem Support Now Generally Available
Socket's Rubygems ecosystem support is moving from beta to GA, featuring enhanced security scanning to detect supply chain threats beyond traditional CVEs in your Ruby dependencies.
@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
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 0 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.
Product
Socket's Rubygems ecosystem support is moving from beta to GA, featuring enhanced security scanning to detect supply chain threats beyond traditional CVEs in your Ruby dependencies.
Research
The Socket Research Team investigates a malicious npm package that appears to be an Advcash integration but triggers a reverse shell during payment success, targeting servers handling transactions.
Security Fundamentals
The Socket Threat Research Team uncovers how threat actors weaponize shell techniques across npm, PyPI, and Go ecosystems to maintain persistence and exfiltrate data.