floating-ui
A JavaScript library to position floating elements and create interactions for them.
Top Related Projects
A JavaScript library to position floating elements and create interactions for them.
Tooltip, popover, dropdown, and menu library
Shopify’s design system to help us work together to build a great experience for all of our merchants.
Bootstrap components built with React
Material UI: Comprehensive React component library that implements Google's Material Design. Free forever.
Accessibility engine for automated Web UI testing
Quick Overview
Floating UI is a low-level toolkit for creating floating elements like tooltips, popovers, dropdowns, and more. It provides a set of primitives for positioning floating elements next to reference elements, with features like automatic placement, overflow detection, and shift/flip behaviors.
Pros
- Highly customizable and flexible positioning logic
- Framework-agnostic, can be used with any UI library or vanilla JavaScript
- Excellent performance due to its lightweight nature
- Comprehensive documentation and examples
Cons
- Requires more setup and configuration compared to higher-level libraries
- May have a steeper learning curve for beginners
- Limited built-in styling options, requiring additional CSS work
Code Examples
- Basic positioning:
import {computePosition} from '@floating-ui/dom';
const button = document.querySelector('#button');
const tooltip = document.querySelector('#tooltip');
computePosition(button, tooltip).then(({x, y}) => {
Object.assign(tooltip.style, {
left: `${x}px`,
top: `${y}px`,
});
});
- Using middleware for advanced positioning:
import {computePosition, flip, shift, offset} from '@floating-ui/dom';
computePosition(button, tooltip, {
placement: 'top',
middleware: [offset(6), flip(), shift({padding: 5})],
}).then(({x, y}) => {
Object.assign(tooltip.style, {
left: `${x}px`,
top: `${y}px`,
});
});
- React integration with hooks:
import {useFloating, offset, flip, shift} from '@floating-ui/react';
function App() {
const {x, y, strategy, refs} = useFloating({
placement: 'top',
middleware: [offset(10), flip(), shift()],
});
return (
<>
<button ref={refs.setReference}>Button</button>
<div
ref={refs.setFloating}
style={{
position: strategy,
top: y ?? 0,
left: x ?? 0,
}}
>
Tooltip
</div>
</>
);
}
Getting Started
-
Install Floating UI:
npm install @floating-ui/dom
-
Import and use in your project:
import {computePosition} from '@floating-ui/dom'; const button = document.querySelector('#button'); const tooltip = document.querySelector('#tooltip'); function updatePosition() { computePosition(button, tooltip).then(({x, y}) => { Object.assign(tooltip.style, { left: `${x}px`, top: `${y}px`, }); }); } updatePosition();
-
For more advanced usage, refer to the official documentation for middleware options and framework-specific integrations.
Competitor Comparisons
A JavaScript library to position floating elements and create interactions for them.
Pros of floating-ui
- More comprehensive and feature-rich positioning library
- Supports a wider range of use cases and scenarios
- Actively maintained with regular updates and improvements
Cons of floating-ui
- Potentially more complex to set up and use for simple positioning tasks
- Larger bundle size due to additional features and functionality
- May have a steeper learning curve for beginners
Code Comparison
floating-ui:
import {computePosition, flip, shift, offset} from '@floating-ui/dom';
computePosition(referenceEl, floatingEl, {
placement: 'top',
middleware: [offset(6), flip(), shift({padding: 5})],
}).then(({x, y}) => {
Object.assign(floatingEl.style, {
left: `${x}px`,
top: `${y}px`,
});
});
floating-ui:
// This repository is identical to floating-ui/floating-ui
// No separate code comparison is possible
As the repositories are identical, there is no meaningful code comparison to be made. The floating-ui/floating-ui repository appears to be a mirror or duplicate of the main floating-ui project.
Tooltip, popover, dropdown, and menu library
Pros of Tippy.js
- Easier to set up and use out of the box
- More comprehensive documentation and examples
- Includes built-in animations and themes
Cons of Tippy.js
- Less flexible for complex positioning scenarios
- Larger bundle size due to included features
- Less frequent updates and maintenance
Code Comparison
Tippy.js:
tippy('#myElement', {
content: 'Hello, world!',
placement: 'top',
animation: 'scale'
});
Floating UI:
import {computePosition, flip, shift, offset} from '@floating-ui/dom';
computePosition(button, tooltip, {
placement: 'top',
middleware: [offset(6), flip(), shift({padding: 5})]
}).then(({x, y}) => {
Object.assign(tooltip.style, {
left: `${x}px`,
top: `${y}px`,
});
});
Tippy.js provides a simpler API for basic use cases, while Floating UI offers more granular control over positioning logic. Tippy.js includes built-in animations and themes, making it easier to create visually appealing tooltips quickly. However, Floating UI's modular approach allows for more flexibility in complex scenarios and potentially smaller bundle sizes when only specific features are needed.
Shopify’s design system to help us work together to build a great experience for all of our merchants.
Pros of Polaris
- Comprehensive design system with pre-built components
- Tailored for e-commerce and Shopify-specific use cases
- Includes accessibility features and guidelines
Cons of Polaris
- Less flexible for non-Shopify projects
- Steeper learning curve due to its opinionated nature
Code Comparison
Polaris (React component):
import {Button} from '@shopify/polaris';
<Button onClick={handleClick}>Add product</Button>
Floating UI (Positioning):
import {computePosition} from '@floating-ui/dom';
computePosition(referenceEl, floatingEl).then(({x, y}) => {
Object.assign(floatingEl.style, {left: `${x}px`, top: `${y}px`});
});
Key Differences
- Polaris is a full-fledged design system, while Floating UI focuses on positioning algorithms
- Floating UI is more lightweight and can be integrated into various projects
- Polaris provides a consistent look and feel for Shopify-related applications
- Floating UI offers more control over positioning of UI elements
Use Cases
- Choose Polaris for Shopify-centric projects or when a complete design system is needed
- Opt for Floating UI when precise control over element positioning is required across different frameworks
Bootstrap components built with React
Pros of React Bootstrap
- Comprehensive set of pre-built React components based on Bootstrap
- Extensive documentation and community support
- Easy integration with existing Bootstrap-based projects
Cons of React Bootstrap
- Larger bundle size due to full Bootstrap framework inclusion
- Less flexibility for custom styling compared to Floating UI
- May require additional setup for responsive designs
Code Comparison
React Bootstrap:
import { Button, Modal } from 'react-bootstrap';
function Example() {
return (
<Modal show={true}>
<Modal.Header closeButton>
<Modal.Title>Modal heading</Modal.Title>
</Modal.Header>
<Modal.Body>Modal content</Modal.Body>
</Modal>
);
}
Floating UI:
import { useFloating, offset, flip, shift } from '@floating-ui/react';
function Example() {
const {x, y, strategy, refs} = useFloating({
placement: 'bottom',
middleware: [offset(10), flip(), shift()]
});
return (
<div ref={refs.setFloating} style={{position: strategy, top: y, left: x}}>
Floating content
</div>
);
}
The code examples highlight the different approaches: React Bootstrap provides ready-to-use components, while Floating UI offers more granular control over positioning and behavior of floating elements.
Material UI: Comprehensive React component library that implements Google's Material Design. Free forever.
Pros of Material-UI
- Comprehensive UI component library with a wide range of pre-built components
- Follows Google's Material Design guidelines, ensuring a consistent and modern look
- Extensive documentation and community support
Cons of Material-UI
- Larger bundle size due to the extensive component library
- Steeper learning curve for customization and theming
- May require more effort to create unique designs that deviate from Material Design
Code Comparison
Material-UI:
import { Button, TextField } from '@mui/material';
function MyComponent() {
return (
<>
<TextField label="Name" variant="outlined" />
<Button variant="contained">Submit</Button>
</>
);
}
Floating UI:
import { useFloating, offset } from '@floating-ui/react';
function MyComponent() {
const { x, y, reference, floating } = useFloating({
placement: 'bottom',
middleware: [offset(10)]
});
return (
<>
<button ref={reference}>Hover me</button>
<div ref={floating} style={{ top: y ?? 0, left: x ?? 0 }}>
Tooltip content
</div>
</>
);
}
Summary
Material-UI is a comprehensive UI library following Material Design principles, while Floating UI focuses on positioning floating elements. Material-UI offers a wide range of components but may be overkill for simpler projects. Floating UI provides more flexibility for custom positioning but requires more manual implementation of UI components.
Accessibility engine for automated Web UI testing
Pros of axe-core
- Focused on accessibility testing and compliance
- Comprehensive set of accessibility rules and best practices
- Integrates well with various testing frameworks and CI/CD pipelines
Cons of axe-core
- Limited to accessibility testing, not a general-purpose UI library
- May require additional setup and configuration for complex applications
- Learning curve for interpreting and addressing accessibility issues
Code Comparison
axe-core:
axe.run().then(results => {
if (results.violations.length) {
throw new Error('Accessibility issues found');
}
});
Floating UI:
import {computePosition} from '@floating-ui/dom';
computePosition(referenceEl, floatingEl).then(({x, y}) => {
Object.assign(floatingEl.style, {
left: `${x}px`,
top: `${y}px`,
});
});
Summary
axe-core is a specialized tool for accessibility testing, offering comprehensive rules and integration capabilities. It's ideal for projects prioritizing accessibility compliance but may require additional effort to implement and interpret results.
Floating UI, on the other hand, is a positioning library for creating floating elements like tooltips and popovers. It focuses on precise positioning and doesn't directly address accessibility concerns.
Choose axe-core for robust accessibility testing, and Floating UI for creating well-positioned UI elements. For a complete solution, consider using both in conjunction to ensure both proper positioning and accessibility compliance.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
[!NOTE] Popper is now Floating UI! For Popper v2, visit its dedicated branch and its documentation. For help on migrating, check out the Migration Guide.
Floating UI is a small library that helps you create "floating" elements such as tooltips, popovers, dropdowns, and more.
It offers two main features:
- Anchor positioning: Anchor a floating element (such as a tooltip) to another element (such as a button) while simultaneously ensuring it stays in view as best as possible by avoiding collisions. This feature is available for all platforms.
- User interactions for React: Hooks and components for composing interactions to create accessible floating UI components.
README Contributors
|
![]() |
You can support Floating UI in a variety of ways on Open Collective.
Why
Floating elements are absolutely positioned, typically anchored to another UI element. Ensuring a floating element remains anchored next to another element can be challenging, especially in unique layout contexts like scrolling containers.
Absolute positioning can also cause problems when the floating element is too close to the edge of the viewport and becomes obscured, also known as a collision. When a collision occurs, the position must be adjusted to ensure the floating element remains visible.
Further, floating elements are often interactive, which can raise complex accessibility issues when designing user interactions.
Floating UI offers a set of low-level features to help you navigate these challenges and build accessible floating UI components.
Install
To install Floating UI, you can use a package manager like npm or a CDN. There are different versions available for different platforms.
Vanilla
Use on the web with vanilla JavaScript.
npm install @floating-ui/dom
You can either start by reading the tutorial, which teaches you how to use the library by building a basic tooltip, or you can jump right into the API documentation.
React
Use with React DOM or React Native.
React DOM
# Positioning + interactions
npm install @floating-ui/react
# Positioning only (smaller size)
npm install @floating-ui/react-dom
React Native
npm install @floating-ui/react-native
Vue
Use with Vue.
npm install @floating-ui/vue
Canvas or other platforms
If you're targeting a platform other than the vanilla DOM (web), React, or React Native, you can create your own Platform. This allows you to support things like Canvas/WebGL, or other platforms that can run JavaScript.
npm install @floating-ui/core
Contributing
This project is a monorepo written in TypeScript using pnpm workspaces. The website is using Next.js SSG and Tailwind CSS for styling.
- Fork and clone the repo
- Install dependencies in root directory with
pnpm install
- Build initial package dist files with
pnpm run build
Testing grounds
DOM
pnpm run --filter @floating-ui/dom dev
in the root will launch the
@floating-ui/dom
development visual tests at http://localhost:1234
. The
playground uses React to write each test route, bundled by Vite.
Each route has screenshots taken of the page by Playwright to ensure all the functionalities work as expected; this is an easy, reliable and high-level way of testing the positioning logic.
Below the main container are UI controls to turn on certain state and options. Every single combination of state is tested visually via the snapshots to cover as much as possible.
React
pnpm run --filter @floating-ui/react dev
in the root will launch the
@floating-ui/react
development tests at http://localhost:1234
.
Credits
The floating shapes in the banner image are made by the amazing artists @artstar3d, @killnicole and @liiiiiiii on Figma â check out their work!
License
MIT
Top Related Projects
A JavaScript library to position floating elements and create interactions for them.
Tooltip, popover, dropdown, and menu library
Shopify’s design system to help us work together to build a great experience for all of our merchants.
Bootstrap components built with React
Material UI: Comprehensive React component library that implements Google's Material Design. Free forever.
Accessibility engine for automated Web UI testing
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot