Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@floating-ui/react-dom

Package Overview
Dependencies
Maintainers
0
Versions
45
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@floating-ui/react-dom

Floating UI for React DOM

  • 2.1.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
9.3M
decreased by-2.17%
Maintainers
0
Weekly downloads
 
Created

What is @floating-ui/react-dom?

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.

What are @floating-ui/react-dom's main functionalities?

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>
      )}
    </>
  );
}

Other packages similar to @floating-ui/react-dom

Keywords

FAQs

Package last updated on 15 Sep 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc