carbon-react
Version:
A library of reusable React components for easily building user interfaces.
2 lines (1 loc) • 841 B
JavaScript
import{useRef as t,useLayoutEffect as e}from"react";import{autoUpdate as n,computePosition as i}from"@floating-ui/dom";const o=({isOpen:o,reference:a,floating:r,strategy:l="absolute",placement:s,middleware:c,animationFrame:p})=>{const m=t(null);e((()=>{let t;const e=a.current,f=r.current;if(e&&f&&o){const{left:o,top:a,position:r,width:u,height:g}=f.style;m.current={left:o,top:a,position:r,width:u,height:g},Object.assign(f.style,{position:l,top:0,left:0});const d=()=>{i(e,f,{strategy:l,middleware:c,placement:s}).then((({x:t,y:e,placement:n})=>{Object.assign(f.style,{left:`${t}px`,top:`${e}px`,position:l}),f.setAttribute("data-floating-placement",n)}))};t=n(e,f,d,{animationFrame:p})}return()=>{t&&f&&(t(),t=null,Object.assign(f.style,m.current),f.removeAttribute("data-floating-placement"))}}),[o,a,r,l,c,s,p])};export{o as default};