Socket
Socket
Sign inDemoInstall

@floating-ui/react

Package Overview
Dependencies
10
Maintainers
2
Versions
45
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

@floating-ui/react

Floating UI for React


Version published
Maintainers
2
Weekly downloads
1,533,894
decreased by-7.88%

Weekly downloads

Package description

What is @floating-ui/react?

The @floating-ui/react package is a library for creating floating elements that can be positioned next to a given reference element. It is commonly used for tooltips, popovers, dropdowns, and similar components. The library provides a set of hooks and utilities to manage the positioning and behavior of these floating elements in a React application.

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

Positioning Tooltips

This code demonstrates how to use @floating-ui/react to position a tooltip above a reference button, with an offset and the ability to flip based on available space.

import {useFloating, offset, flip} from '@floating-ui/react';

function Tooltip() {
  const {x, y, reference, floating, strategy} = useFloating({
    placement: 'top',
    middleware: [offset(10), flip()]
  });

  return (
    <>
      <button ref={reference}>Reference Element</button>
      <div ref={floating} style={{
        position: strategy,
        top: y ?? '',
        left: x ?? ''
      }}>Floating Content</div>
    </>
  );
}

Creating Popovers

This example shows how to create a popover component positioned to the right of a reference element, including shifting to stay within the viewport and an arrow pointing to the reference.

import {useFloating, shift, arrow} from '@floating-ui/react';

function Popover({referenceElement}) {
  const {x, y, floating, strategy, middlewareData} = useFloating({
    placement: 'right-start',
    middleware: [shift(), arrow({element: arrowElement})]
  });

  return (
    <div ref={floating} style={{
      position: strategy,
      top: y ?? '',
      left: x ?? ''
    }}>
      Popover Content
      <div ref={arrowElement} style={{
        position: 'absolute',
        [middlewareData.arrow.x != null ? 'left' : 'top']: middlewareData.arrow.x ?? middlewareData.arrow.y
      }} />
    </div>
  );
}

Other packages similar to @floating-ui/react

Readme

Source

@floating-ui/react

This is the library to use Floating UI with React.

Keywords

FAQs

Last updated on 26 Mar 2024

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc