Skip to Content
ComponentsFloating

Floating layer

The primitives that power Tooltip, Menu, Popover, Select dropdowns and the pickers. Use them to build your own anchored floating UI.

useFloating

Positions a floating element (rendered in a portal) against an anchor: fixed positioning, flips to the opposite side when there isn’t room, and clamps to the viewport. Updates on scroll / resize / size changes.

import { useFloating, FloatingPortal } from '@gg-software/ui'; function Dropdown() { const [open, setOpen] = useState(false); const { anchorRef, floatingRef, floatingStyle, side } = useFloating< HTMLButtonElement, HTMLDivElement >({ open, placement: 'bottom-start', offset: 4, matchWidth: true }); return ( <> <button ref={anchorRef} onClick={() => setOpen((o) => !o)}> Toggle </button> {open && ( <FloatingPortal> <div ref={floatingRef} style={floatingStyle}> Floating content (resolved side: {side}) </div> </FloatingPortal> )} </> ); }

Options

OptionTypeDefaultDescription
openboolean— (required)position is only computed while open
placementFloatingPlacement'bottom'preferred side/alignment
offsetnumber4gap between anchor and floating element
matchWidthbooleanfalsemake the floating element match the anchor’s width (dropdowns)
paddingnumber8min distance to keep from the viewport edge

Result

FieldTypeDescription
anchorRefRefObject<A>attach to the anchor element
floatingRefRefObject<F>attach to the floating element
floatingStyleCSSPropertiesapply to the floating element (fixed position)
sideFloatingSideresolved side after any flip (useful for arrows)
update() => voidrecompute the position manually

Types

type FloatingSide = 'top' | 'bottom' | 'left' | 'right'; type FloatingAlign = 'start' | 'center' | 'end'; type FloatingPlacement = | FloatingSide | 'top-start' | 'top-end' | 'bottom-start' | 'bottom-end';

FloatingPortal

Renders children into document.body via a React portal (and renders nothing during SSR). No props besides children.

<FloatingPortal> <div ref={floatingRef} style={floatingStyle}>…</div> </FloatingPortal>