floating()
Keep popovers inside the screen. Flip/shift/size floating panels.
Try it
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.
Save your work or invite someone to the project.
Panel closed
<div id="your-component">
<div class="floating-demo"><div class="floating-stage"><button class="floating-trigger" aria-haspopup="dialog" aria-expanded="false" aria-controls="floating-example-panel">Open anchored panel</button><section class="floating-panel" id="floating-example-panel" role="dialog" aria-label="Project actions" hidden><div class="floating-panel-heading"><strong>Project actions</strong><button class="floating-close" type="button" aria-label="Close panel">×</button></div><p>Save your work or invite someone to the project.</p><button type="button">Save changes</button><button type="button">Invite collaborator</button></section></div><label class="floating-placement">Preferred placement <select><option value="bottom-start">Bottom start</option><option value="top-start">Top start</option><option value="right-start">Right</option><option value="left-start">Left</option></select></label><p class="demo-status" aria-live="polite">Panel closed</p></div>
</div>import { floating } from 'subtly/floating';
const el = document.querySelector('#your-component');
const listeners = new AbortController();
const { signal } = listeners;
const status = text => {
el.querySelector('.demo-status').textContent = text;
};
// Local simulation only. Replace with your real data operation.
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
function setup() {
const trigger = el.querySelector('.floating-trigger');
const panel = el.querySelector('.floating-panel');
const placement = el.querySelector('select');
const floatingReady=Promise.resolve({floating});
let stop, closed = true;
function close() {
closed = true;
stop?.(); stop = undefined;
panel.hidden = true;
trigger.setAttribute('aria-expanded', 'false');
trigger.focus(); status('Panel closed');
}
function open() {
closed = false;
panel.hidden = false;
trigger.setAttribute('aria-expanded', 'true');
floatingReady.then(({ floating }) => {
if (closed || signal.aborted) return;
stop?.();
stop = floating(trigger, panel, {
placement: placement.value, gap: 8, padding: 12,
onPlacement: value => status('Panel open · placed ' + value)
});
}).catch(() => status('The positioning module could not load.'));
}
trigger.addEventListener('click', () => panel.hidden ? open() : close(), { signal });
el.querySelector('.floating-close').addEventListener('click', close, { signal });
placement.addEventListener('change', () => { if (!panel.hidden) open(); }, { signal });
el.addEventListener('keydown', event => {
if (event.key === 'Escape' && !panel.hidden) close();
}, { signal });
return () => { closed = true; stop?.(); };
}
const stop = setup();
// Call when this component is removed.
function cleanup() {
listeners.abort();
stop?.();
}Add it to your project
- Copy the HTML. Use the HTML tab above and place the markup inside a dedicated component root. Layout classes belong to this documentation website; use your own styles.
- Connect the behavior. Copy the JavaScript tab, update
#your-componentand initialize it after the elements exist. Replace simulated operations with your own data. - Clean up on unmount. Call the example’s
cleanup()when the component is removed. It aborts application listeners and releases the utility’s timers or observers.
The preview and JavaScript tab use the same utility wiring. status() writes plain text into the example’s status element. wait() is a simulated local delay. For examples with data-ix attributes, start the declarative runtime once at application level, as shown in the code. Use your framework guide to place setup and cleanup correctly.
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.
View documented module source
/** Collision-aware positioning using Floating UI's DOM package (not React).
* npm install @floating-ui/dom
* Panel must be mounted, visible and positioned fixed before calling.
* Returns cleanup. Reopen by creating a new instance. */
import {autoUpdate,computePosition,offset,flip,shift,size} from '@floating-ui/dom';
export function floating(trigger,panel,{placement='bottom-start',gap=8,padding=8,onPlacement}={}) {
let disposed=false, version=0;
const stop=autoUpdate(trigger,panel,async()=>{
const id=++version;
const result=await computePosition(trigger,panel,{strategy:'fixed',placement,middleware:[offset(gap),flip({padding}),shift({padding}),size({padding,apply({availableHeight,availableWidth}){if(!disposed&&id===version)Object.assign(panel.style,{maxHeight:`${Math.max(0,availableHeight)}px`,maxWidth:`${Math.max(0,availableWidth)}px`});}})]});
if(disposed||id!==version)return;
Object.assign(panel.style,{position:'fixed',left:`${result.x}px`,top:`${result.y}px`});
onPlacement?.(result.placement);
});
return ()=>{disposed=true;stop();};
}