DocsHover & overlays
Get started

Search Subtly documentation

Search by a task, helper name or framework.

Hover & overlays

floating()

Keep popovers inside the screen.

Try the example

Open the panel, change its preferred placement, then resize the browser or move the anchor close to an edge. The panel flips, shifts and shrinks to stay visible.

Panel closed

Actual npm package · local demonstration

Add it to your project

  1. Copy the HTML. Place the markup inside the component where this behavior belongs. The preview’s visual classes are website styles; use your own design system.
  2. Connect the behavior. Copy the JavaScript tab and call its exported mount(element) after your component’s elements exist. Replace simulated delays and data operations with your own application code.
  3. Clean up. Call the returned cleanup when the component unmounts. It aborts the example’s listeners and releases the helper’s timers, observers or state.

API and lifecycle

Install @floating-ui/dom and import from subtly/floating. Call floating(trigger, panel, options) after the panel is mounted and visible. It positions the panel, flips or shifts near edges, and limits its available size.

Interface
placement, gap=8, padding=8, onPlacement; returns cleanup
Lifecycle
The return value is cleanup for auto-update listeners. Call it when closing or unmounting; create a fresh instance when reopening. onPlacement receives the final placement after collision handling.

Accessibility and safety

Implementation notes

Optional dependency required. Positioning does not implement focus, dismissal or ARIA.

Positioning alone does not supply ARIA, focus management, keyboard navigation or dismissal. Those remain responsibilities of the overlay component.

What to check

Open near every viewport edge, resize and scroll, then verify the panel stays reachable. Test focus entry, Escape, outside dismissal and small screens.