Socket
Socket
Sign inDemoInstall

@floating-ui/dom

Package Overview
Dependencies
2
Maintainers
2
Versions
67
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @floating-ui/dom

Floating UI for the web


Version published
Weekly downloads
11M
decreased by-0.51%
Maintainers
2
Install size
417 kB
Created
Weekly downloads
 

Package description

What is @floating-ui/dom?

The @floating-ui/dom package is a library for creating floating elements that can be positioned next to a reference element on the DOM, such as tooltips, popovers, dropdowns, and more. It provides low-level primitives for positioning and updating the placement of these elements in relation to their reference elements and the viewport.

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

Positioning

This feature allows you to compute the position of a floating element relative to a reference element. The computePosition function returns a promise that resolves with the coordinates where the floating element should be placed.

import {computePosition} from '@floating-ui/dom';

const referenceElement = document.querySelector('#reference');
const floatingElement = document.querySelector('#floating');

computePosition(referenceElement, floatingElement).then(({x, y}) => {
  Object.assign(floatingElement.style, {
    left: `${x}px`,
    top: `${y}px`,
    position: 'absolute',
  });
});

Auto-Placement

Auto-placement automatically adjusts the placement of the floating element to ensure it stays in view within the viewport or a specified boundary. It can flip or shift the element as needed.

import {autoPlacement} from '@floating-ui/dom';

const referenceElement = document.querySelector('#reference');
const floatingElement = document.querySelector('#floating');

autoPlacement(referenceElement, floatingElement, {
  placement: 'bottom',
  middleware: [{name: 'flip', options: {padding: 8}}]
}).then(placement => {
  // Apply the placement to the floating element
});

Middleware

Middleware allows for advanced positioning behavior by adding additional logic during the position computation. Examples include 'shift' to keep the floating element within view and 'flip' to flip the element to the opposite side of the reference element if it would overflow the viewport.

import {computePosition, shift, flip} from '@floating-ui/dom';

const referenceElement = document.querySelector('#reference');
const floatingElement = document.querySelector('#floating');

computePosition(referenceElement, floatingElement, {
  middleware: [shift(), flip()]
}).then(({x, y, middlewareData}) => {
  // Use the x, y coordinates and middleware data to position the floating element
});

Other packages similar to @floating-ui/dom

Readme

Source

@floating-ui/dom

This is the library to use Floating UI on the web, wrapping @floating-ui/core with DOM interface logic.

Keywords

FAQs

Last updated on 12 Feb 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