retro-react
Version:
A React component library for building retro-style websites
2 lines (1 loc) • 1.96 kB
JavaScript
;var e=require("../../_virtual/_tslib.js"),t=require("react"),r=require("../../node_modules/@theme-ui/core/jsx-runtime/dist/theme-ui-core-jsx-runtime.esm.js"),i=require("../../utils/classNames.js"),n=require("../../constants/commonClassNames.js"),s=require("../portal/Portal.js"),o=require("./Tooltip.styled.js");const l=(e,t,r)=>{if(!e.current||!t.current)return;const i=e.current.getBoundingClientRect(),n=t.current.getBoundingClientRect(),{innerWidth:s,innerHeight:o}=window;let l=0,a=0;switch(r){case"top":l=i.top-n.height-10,a=i.left+(i.width-n.width)/2;break;case"right":l=i.top+(i.height-n.height)/2,a=i.right+10;break;case"bottom":l=i.bottom+10,a=i.left+(i.width-n.width)/2;break;case"left":l=i.top+(i.height-n.height)/2,a=i.left-n.width-10}l=Math.min(o-n.height-10,Math.max(10,l)),a=Math.min(s-n.width-10,Math.max(10,a)),t.current.style.top=`${l}px`,t.current.style.left=`${a}px`},a=a=>{var{label:u,variant:c="default",position:d="top",children:h,delay:m=500,sx:p}=a,f=e.__rest(a,["label","variant","position","children","delay","sx"]);const v=t.useRef(null),w=t.useRef(null),[g,j]=t.useState(!1),x=t.useRef(null);t.useEffect((()=>{g&&requestAnimationFrame((()=>{l(v,w,d)}))}),[g,d]),t.useEffect((()=>{const e=()=>{g&&l(v,w,d)},t=()=>{g&&l(v,w,d)};return window.addEventListener("scroll",e,!0),window.addEventListener("resize",t),()=>{window.removeEventListener("scroll",e,!0),window.removeEventListener("resize",t),x.current&&clearTimeout(x.current)}}),[g,d]);return r.jsxs(o.TooltipWrapper,Object.assign({onMouseEnter:()=>{x.current&&clearTimeout(x.current),x.current=setTimeout((()=>{j(!0)}),m)},onMouseLeave:()=>{x.current&&clearTimeout(x.current),j(!1)}},f,{children:[t.cloneElement(h,{ref:v}),r.jsx(s.Portal,{children:r.jsx(o.TooltipContent,Object.assign({ref:w,$visible:g,$variant:c,$position:d,className:i.classNames("tooltip-root",n.default),sx:p},{children:u}),void 0)},void 0)]}),void 0)};a.displayName="Tooltip",exports.Tooltip=a;