vuux
Version:
Vue3 Nuxt3 Nuxt4 组件库
2 lines (1 loc) • 2.77 kB
JavaScript
;Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("vue"),V=(A,m,p,z)=>{const r=l.ref(!1),o=l.reactive({position:"fixed",top:"0px",left:"0px",visibility:"hidden"}),B=l.ref("placement-top"),j=l.computed(()=>{const t=[B.value];return A.shadow&&t.push("is-shadow"),t.join(" ")}),d=8,v=12,f=(t,n,e)=>Math.max(n,Math.min(e,t)),q=(t,n,e,C,y,k)=>{const a=Math.max(0,t),i=Math.max(0,n),h=Math.min(y,t+e),O=Math.min(k,n+C),s=Math.max(0,h-a),E=Math.max(0,O-i);return s*E};let g=null,w=null,b=null,x=null,M=!1;const F=()=>{r.value=!1,z("close")},G=()=>{r.value=!1,z("ok")},I=async()=>{if(!m.value||!p.value)return;const t=m.value,n=p.value;o.visibility="hidden",o.top="0px",o.left="0px",await l.nextTick();const e=t.getBoundingClientRect(),C=window.innerWidth,y=window.innerHeight,k=n.getBoundingClientRect(),a=k.width,i=k.height,h=14,O=[{name:"placement-top",left:e.left+(e.width-a)/2,top:e.top-i-h},{name:"placement-bottom",left:e.left+(e.width-a)/2,top:e.bottom+h},{name:"placement-left",left:e.left-a-h,top:e.top+(e.height-i)/2},{name:"placement-right",left:e.right+h,top:e.top+(e.height-i)/2}];let s=O[0],E=-1;for(const L of O){const W=q(L.left,L.top,a,i,C,y),$=L.name==="placement-top"?1e-4:0;W+$>E&&(E=W+$,s=L)}const S=f(s.left,d,Math.max(d,C-a-d)),R=f(s.top,d,Math.max(d,y-i-d)),H=e.left+e.width/2,N=e.top+e.height/2;let u=0;s.name==="placement-top"||s.name==="placement-bottom"?(u=H-S,u=f(u,v,Math.max(v,a-v))):(u=N-R,u=f(u,v,Math.max(v,i-v))),o.left=`${Math.round(S)}px`,o.top=`${Math.round(R)}px`,o["--arrow-position"]=`${Math.round(u)}px`,B.value=s.name,o.visibility="visible"},c=()=>{r.value&&(M||(M=!0,x=requestAnimationFrame(()=>{I().catch(()=>{}),M=!1})))},U=()=>{!m.value||!p.value||(g=new ResizeObserver(()=>c()),w=new ResizeObserver(()=>c()),g.observe(m.value),w.observe(p.value),b=new IntersectionObserver(t=>{for(const n of t)n.intersectionRatio<1&&c()},{threshold:[0,.25,.5,.75,1]}),b.observe(p.value),window.addEventListener("scroll",c,{passive:!0,capture:!0}),window.addEventListener("resize",c,{passive:!0}))},P=()=>{g?.disconnect(),g=null,w?.disconnect(),w=null,b?.disconnect(),b=null,window.removeEventListener("scroll",c,!0),window.removeEventListener("resize",c),x&&(cancelAnimationFrame(x),x=null),M=!1},_=()=>{A.disabled||(r.value=!0)},T=t=>{const n=p.value,e=m.value;!n||!e||n.contains(t.target)||e.contains(t.target)||(r.value=!1)};return l.watch(()=>r.value,async t=>{t?(await l.nextTick(),await I(),U(),document.addEventListener("click",T)):(o.visibility="hidden",P(),document.removeEventListener("click",T))}),l.onBeforeUnmount(()=>{P(),document.removeEventListener("click",T)}),{isOpen:r,popupStyle:o,placementClass:j,handleTriggerClick:_,emitCancel:F,emitConfirm:G}};exports.useConfirmPopup=V;