Socket
Socket
Sign inDemoInstall

@floating-ui/utils

Package Overview
Dependencies
Maintainers
2
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@floating-ui/utils

Utilities for Floating UI


Version published
Weekly downloads
8.7M
decreased by-18.94%
Maintainers
2
Weekly downloads
 
Created

What is @floating-ui/utils?

The @floating-ui/utils package provides utility functions for positioning floating elements (like tooltips, popovers, dropdowns, etc.) in relation to reference elements in the DOM. It is part of the Floating UI library, which offers a low-level toolkit for creating floating elements.

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

computePosition

This feature calculates the position of a floating element relative to a reference element and applies the calculated position to the floating element.

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

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

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

arrow

The arrow utility positions an arrow element within a floating element so that it is properly aligned with the reference element.

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

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

computePosition(referenceElement, floatingElement, {
  middleware: [arrow({element: arrowElement})],
}).then(data => {
  Object.assign(floatingElement.style, {
    left: `${data.x}px`,
    top: `${data.y}px`,
  });
  Object.assign(arrowElement.style, data.middlewareData.arrow);
});

autoPlacement

The autoPlacement utility automatically chooses the best placement for a floating element based on available space in the viewport.

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

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

computePosition(referenceElement, floatingElement, {
  middleware: [autoPlacement()],
}).then(data => {
  Object.assign(floatingElement.style, {
    left: `${data.x}px`,
    top: `${data.y}px`,
  });
});

flip

The flip utility adjusts the placement of the floating element to prevent it from overflowing the viewport.

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

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

computePosition(referenceElement, floatingElement, {
  middleware: [flip()],
}).then(data => {
  Object.assign(floatingElement.style, {
    left: `${data.x}px`,
    top: `${data.y}px`,
  });
});

offset

The offset utility adds space between the reference element and the floating element, defined by the offset value.

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

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

computePosition(referenceElement, floatingElement, {
  middleware: [offset(10)],
}).then(data => {
  Object.assign(floatingElement.style, {
    left: `${data.x}px`,
    top: `${data.y}px`,
  });
});

Other packages similar to @floating-ui/utils

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