@vuu-ui/vuu-popups 0.8.17 → 0.8.18-debug

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/esm/index.js CHANGED
@@ -1,2 +1,2033 @@
1
- import{Scrim as Nt}from"@salt-ds/lab";import Dt from"classnames";import{useCallback as St,useRef as Bt}from"react";import{useThemeAttributes as xt}from"@vuu-ui/vuu-shell";import{useLayoutEffect as Re,useRef as vt,useState as Et}from"react";import{createPortal as wt}from"react-dom";function Tt(e){return typeof e=="function"?e():e}var Rt="vuu-portal-root",B=({children:e,container:o=document.body,id:t=Rt,onRender:n,open:r=!0,themeAttributes:s})=>{var g;let[i,d]=Et(!1),l=vt(null),a=(g=Tt(o))!=null?g:document.body,[u,c,p]=xt(s);return Re(()=>{let v=document.getElementById(t);v?l.current=v:(l.current=document.createElement("div"),l.current.id=t);let M=l.current;a.contains(M)||a.appendChild(M),M.classList.add(u,c),M.dataset.mode=p,d(!0)},[t,a,u,c,p]),Re(()=>{requestAnimationFrame(()=>{n==null||n()})},[n]),r&&i&&l.current&&e?wt(e,l.current):null};import{Button as Lt,Text as Ht}from"@salt-ds/core";import It from"classnames";import{jsx as Le,jsxs as At}from"react/jsx-runtime";var kt="vuuDialogHeader",He=({hideCloseButton:e=!1,title:o,onClose:t,...n})=>At("div",{...n,className:It(kt,"vuuToolbarProxy"),children:[Le(Ht,{className:"dialogHeader",children:o}),!e&&Le(Lt,{onClick:t,"data-align":"end","data-icon":"close",variant:"secondary"},"close")]});import{jsx as U,jsxs as Ot}from"react/jsx-runtime";var ie="vuuDialog",Ie=({children:e,className:o,isOpen:t=!1,onClose:n,title:r,hideCloseButton:s=!1,...i})=>{let d=Bt(null),l=St(()=>{n==null||n()},[n]);return t?U(B,{children:U(Nt,{className:`${ie}-scrim`,open:t,autoFocusRef:d,children:Ot("div",{...i,className:Dt(ie,o),ref:d,children:[U(He,{hideCloseButton:s,onClose:l,title:r}),U("div",{className:`${ie}-body`,children:e})]})})}):null};import{useCallback as $t,useState as Kt}from"react";import{jsx as Ft}from"react/jsx-runtime";var nr=()=>{let[e,o]=Kt(),t=$t(()=>{o(void 0)},[]);return{dialog:e?Ft(Ie,{className:"vuDialog",isOpen:!0,onClose:t,style:{maxHeight:500},title:e.title,hideCloseButton:e.hideCloseButton,children:e.content}):null,setDialogState:o}};import{useCallback as ze,useRef as me}from"react";import We,{useLayoutEffect as to,useMemo as oo,useRef as no}from"react";import qe from"classnames";import{useId as ro}from"@vuu-ui/vuu-layout";import{useCallback as X,useMemo as zt,useRef as ue,useState as Yt}from"react";var ke=e=>e.closest("[data-root='true']")!==null,Ae=(e,o)=>{var t;return e.ariaHasPopup==="true"&&((t=e.dataset)==null?void 0:t.idx)===`${o}`||e.querySelector(`:scope > [data-index='${o}'][aria-haspopup='true']`)!==null};function Wt(e,...o){let t=new Set(e);for(let n of o)for(let r of n)t.add(r);return t}var qt="Enter";var Gt="Delete",Vt=new Set([qt,Gt]),Ut=new Set(["Tab"]),Xt=new Set(["ArrowRight","ArrowLeft"]),Ne=new Set(["Home","End","ArrowDown","ArrowUp"]),De=new Set(["Home","End","ArrowRight","ArrowLeft"]),Jt=new Set(["F1","F2","F3","F4","F5","F6","F7","F8","F9","F10","F11","F12"]),cr=Wt(Vt,De,Ne,Xt,Jt,Ut);var Se=({key:e},o="vertical")=>(o==="vertical"?Ne:De).has(e);import{isValidNumber as ae}from"@vuu-ui/vuu-utils";var Be=({autoHighlightFirstItem:e=!1,count:o,defaultHighlightedIdx:t,highlightedIndex:n,onActivate:r,onHighlight:s,onCloseMenu:i,onOpenMenu:d})=>{var E;if(ae(n)&&ae(t))throw Error("useKeyboardNavigation do not pass values for both highlightedIndex and defaultHighlightedIdx");let l=ae(n),a=ue((E=t!=null?t:n)!=null?E:e?0:-1),[,u]=Yt(null),c=X(b=>{a.current=b,s==null||s(b),u({})},[s]),p=X(b=>{b!==a.current&&(l||c(b))},[l,c]),g=ue(!0),v=ue(!1),M=b=>v.current=b,P=l?n:a.current,C=X(b=>{let h=Qt(o,b.key,a.current);h!==a.current&&p(h)},[o,p]),w=X(b=>{if(Se(b))b.preventDefault(),b.stopPropagation(),g.current=!0,C(b);else if((b.key==="ArrowRight"||b.key==="Enter")&&Ae(b.target,P)){let m=b.target.querySelector(`:scope > [data-index='${P}']`);m&&(d==null||d(m,!0))}else b.key==="ArrowLeft"&&!ke(b.target)?i(P):b.key==="Enter"?(b.preventDefault(),b.stopPropagation(),r&&r(P)):b.key==="Tab"&&i(-1)},[P,C,r,i,d]),R=zt(()=>({onFocus:()=>{P===-1&&c(0)},onKeyDown:w,onMouseDownCapture:()=>{g.current=!1,M(!0)},onMouseMove:()=>{g.current&&(g.current=!1)},onMouseLeave:()=>{g.current=!0,M(!1),p(-1)}}),[w,P,c,p]);return{focusVisible:g.current?P:-1,controlledHighlighting:l,highlightedIndex:P,setHighlightedIndex:p,listProps:R,setIgnoreFocus:M}};function Qt(e,o,t){return o==="ArrowUp"?t>0?t-1:t:t===null?0:t===e-1?t:t+1}import Oe,{useCallback as Zt,useMemo as jt}from"react";var le=e=>e.type===J||!!e.props["data-group"],_t=e=>{if(Array.isArray(e)&&ce(e[0]))return e[0]},eo=(e,o,t,n=!1)=>{let{props:{children:r}}=e;return{childWithId:Oe.cloneElement(e,{hasSeparator:n,id:`${o}`,key:o,children:t?_t(r):r}),grandChildren:t?r:void 0}},$e=(e,o)=>{let t=Zt(()=>{let s=(i,d=o,l={},a={})=>{let u=l[d]=[],c=0,p=!1;return Oe.Children.forEach(i,g=>{if(!ce(g))if(g.type===Ke)p=!0;else{let v=le(g),M=`${d}-${c}`,{props:{action:P,options:C}}=g,{childWithId:w,grandChildren:R}=eo(g,M,v,p);u.push(w),R?s(R,M,l,a):a[M]={action:P,options:C},c+=1,p=!1}}),[l,a]};return s(e)},[o,e]),[n,r]=jt(()=>t(),[t]);return[n,r]};import{Fragment as ao,jsx as K}from"react/jsx-runtime";var Fe="vuuMenuList",Ke=()=>K("li",{className:"vuuMenuItem-divider"}),J=()=>null,z=({children:e,idx:o,options:t,...n})=>K("div",{...n,children:e}),Ge=({children:e})=>K(ao,{children:e});Ge.displayName="MenuItemLabel";z.Label=Ge;var so=e=>We.isValidElement(e)&&typeof e.type!="string"&&"displayName"in e.type?e.type.displayName:void 0,ce=e=>so(e)==="MenuItemLabel",io=e=>e.props["data-icon"],pe=({activatedByKeyboard:e,childMenuShowing:o,children:t,className:n,defaultHighlightedIdx:r,highlightedIdx:s,id:i,isRoot:d,listItemProps:l,onHighlightMenuItem:a,onActivate:u,onCloseMenu:c,openMenu:p,...g})=>{let v=ro(i),M=no(null),P=oo(()=>new Map,[]),C=f=>{var x;let y=(x=M.current)==null?void 0:x.querySelector(`:scope > [data-index='${f}']`);y!=null&&y.id&&(u==null||u(y.id))},{focusVisible:w,highlightedIndex:R,listProps:E}=Be({count:We.Children.count(t),defaultHighlightedIdx:r,highlightedIndex:s,onActivate:C,onHighlight:a,onOpenMenu:p,onCloseMenu:c}),b=o==null?w:-1;to(()=>{var f;o===void 0&&e&&((f=M.current)==null||f.focus())},[e,o]);let h=()=>R===void 0||R===-1?void 0:P.get(R);function m(){let f={...l,role:"menuitem"},y=(I,T,k)=>T?[K("span",{className:"vuuIconContainer","data-icon":k},"icon")].concat(I):I;function x(I,T,k,D){var Te;let{children:S,className:bt,"data-icon":Ee,id:V,hasSeparator:yt,label:re,...Ct}=T.props,se=le(T),we=se&&o===V,Pt=we?`${v}-${V}`:void 0;I.push(K(z,{...Ct,...f,...uo(V,k,(Te=T.key)!=null?Te:V,R,b,bt,yt),"aria-controls":Pt,"aria-haspopup":se||void 0,"aria-expanded":we||void 0,children:y(se&&re!=null?re:S,D,Ee)}))}let L=[];if(t.length>0){let I=t.some(io);t.forEach((T,k)=>{x(L,T,k,I)})}return L}return K("div",{...g,...E,"aria-activedescendant":h(),className:qe(Fe,n,{[`${Fe}-childMenuShowing`]:o!==void 0}),"data-root":d||void 0,id:v,ref:M,role:"menu",children:m()})},uo=(e,o,t,n,r,s,i)=>({id:`menuitem-${e}`,key:t!=null?t:o,"data-index":o,"data-highlighted":o===n||void 0,className:qe("vuuMenuItem",s,{"vuuMenuItem-separator":i,focusVisible:r===o})});pe.displayName="MenuList";import{useCallback as N,useMemo as lo,useRef as Y,useState as co}from"react";var de=e=>e==null?void 0:e.closest("[data-index],[aria-posinset]");var Ue=(e,o,t)=>e.map((n,r)=>r===e.length-1?{...n,[t]:n[t]-o}:n),po=(e,o)=>Ue(e,o,"left"),mo=(e,o)=>Ue(e,o,"top"),fo=(e,o)=>{let[t,n]=o.slice(-2),r=document.getElementById(`${e}-${n.id}`);if(r===null)throw Error(`useCascade.flipSides element with id ${n.id} not found`);let{width:s}=r.getBoundingClientRect();return o.map(i=>i===n?{...i,left:t.left-(s-2)}:i)},go=(e,o)=>{let[{left:t,top:n}]=o.slice(-1),{offsetWidth:r,offsetTop:s}=e;return{left:t+r,top:s+n}},Xe=(e,o)=>{let t=e.lastIndexOf("-");return e.startsWith("menuitem")?t>-1?e.slice(9,t):o:t>-1?e.slice(0,t):o},Mo=e=>e.slice(9),Ve=({ariaExpanded:e,ariaHasPopup:o,id:t},n)=>{if(t.startsWith("menuitem"))return{hostMenuId:Xe(t,n),targetMenuId:Mo(t),menuItemId:t,isGroup:o==="true",isOpen:e==="true"};throw Error(`getMenuItemDetails #${t} is not a menuitem`)},Je=({id:e,onActivate:o,onMouseEnterItem:t,position:{x:n,y:r}})=>{let[,s]=co({}),i=Y([{id:e,left:n,top:r}]),d=N(h=>i.current.findIndex(m=>m.id===h)!==-1,[]),l=N(h=>{let m=p.current[h];if(m===void 0)throw Error(`getOpenMenuState no entry for menu ${h}`);return m},[]),a=N(h=>{i.current=h,s({})},[]),u=Y(),c=Y(),p=Y({[e]:"no-popup"}),g=N((h=e,m,f=null)=>{if(h===e&&f===null)a([{id:e,left:n,top:r}]);else{p.current[h]="popup-open";let y=document.getElementById(f);if(y!==null){let{left:x,top:L}=go(y,i.current);a(i.current.concat({id:m,left:x,top:L}))}else throw Error(`openMenu no menuItem ${f}`)}},[e,n,r,a]),v=N(h=>{if(h===e)a([]);else{let m=i.current.slice(),f=m.pop();p.current[f.id]="no-popup";let y=m.at(-1);y&&(p.current[y.id]="no-popup"),a(m)}},[e,a]),M=N(h=>{let m=i.current.slice(),f=h.slice(9),{id:y}=m.at(-1);for(;m.length>1&&!f.startsWith(y);){let x=Xe(y,e);m.pop(),p.current[y]="no-popup",p.current[x]="no-popup",{id:y}=m[m.length-1]}m.length<i.current.length&&a(m)},[e,a]),P=N(()=>{u.current&&(clearTimeout(u.current),u.current=void 0)},[]),C=N((h,m,f,y=300)=>{P(),u.current=window.setTimeout(()=>{M(f),p.current[h]="popup-open",p.current[m]="no-popup",g(h,m,f)},y)},[P,M,g]),w=N((h,m,f)=>{p.current[m]="pending-close",c.current=window.setTimeout(()=>{M(f)},400)},[M]),R=N(()=>{let{current:h}=i,m=h.at(-1),f=m?document.getElementById(m.id):void 0;if(f){let{right:y,bottom:x}=f.getBoundingClientRect(),{clientHeight:L,clientWidth:I}=document.body;if(y>I){let T=h.length>1?fo(e,h):po(h,y-I);a(T)}else if(x>L){let T=mo(h,x-L);a(T)}typeof f.tabIndex=="number"&&f.focus()}},[e,a]),E=N((h,m=!1)=>{let{hostMenuId:f,targetMenuId:y,menuItemId:x,isGroup:L,isOpen:I}=Ve(h,e),{current:{[f]:T}}=p,k=m?0:void 0;if(T==="no-popup"&&L)p.current[f]="popup-pending",C(f,y,x,k);else if(T==="popup-pending"&&!L)p.current[f]="no-popup",clearTimeout(u.current),u.current=void 0;else if(T==="popup-pending"&&L)clearTimeout(u.current),C(f,y,x,k);else if(T==="popup-open")if(d(y)){let D=l(y);switch(M(x),D){case"pending-close":clearTimeout(c.current),c.current=void 0,p.current[y]="no-popup",P();break;default:}}else{let[D,S]=i.current.slice(-2);D.id===f&&p.current[S.id]!=="pending-close"?(w(f,S.id,x),L&&!I&&C(f,y,x,k)):D.id===f&&L&&x!==S.id&&p.current[S.id]==="pending-close"||L?C(f,y,x,k):p.current[S.id]!=="pending-close"&&M(x)}T==="pending-close"&&(P(),clearTimeout(c.current),c.current=void 0,p.current[f]="popup-open")},[P,M,l,d,e,w,C]),b=lo(()=>({onMouseEnter:h=>{let m=de(h.target);E(m),t(h,m.id)},onClick:h=>{let m=de(h.target),{isGroup:f,menuItemId:y}=Ve(m,e);f?E(m):o(y)}}),[o,t,e,E]);return{closeMenu:v,handleRender:R,listItemProps:b,openMenu:E,openMenus:i.current}};import{useId as ho}from"@vuu-ui/vuu-layout";import{Fragment as yo,jsx as Qe}from"react/jsx-runtime";import{createElement as Ye}from"react";var bo=()=>{},fe=({PortalProps:e,activatedByKeyboard:o,children:t,className:n,id:r,onClose:s=()=>{},position:i={x:0,y:0},style:d,...l})=>{let a=me(s);a.current=s;let u=ho(r),c=me(bo),[p,g]=$e(t,u),v=me(o),M=ze(()=>{v.current=!1},[]),P=ze(x=>{let L=x.slice(9),{action:I,options:T}=g[L];c.current(u),s({type:"menu-action",menuId:I,options:T})},[g,u,s]),{closeMenu:C,listItemProps:w,openMenu:R,openMenus:E,handleRender:b}=Je({id:`${u}`,onActivate:P,onMouseEnterItem:M,position:i});c.current=C;let h=()=>{v.current=!0,C()},m=()=>{},f=E.length-1,y=x=>{if(!(x>=f)){let{id:L}=E[x+1];return L}};return Qe(yo,{children:E.map(({id:x,left:L,top:I},T,k)=>{let D=y(T);return Ye(B,{...e,key:T,onRender:b},Qe(Ze,{anchorElement:{current:document.body},placement:"absolute",position:{left:L,top:I},children:Ye(pe,{...l,activatedByKeyboard:v.current,childMenuShowing:D,className:n,id:x,isRoot:T===0,key:T,listItemProps:w,onActivate:P,onHighlightMenuItem:m,onCloseMenu:h,openMenu:R,style:d,tabIndex:T===k.length-1?0:void 0},p[x])}))})})};fe.displayName="ContextMenu";import{createContext as Co,useCallback as Po,useMemo as xo}from"react";import{jsx as ge}from"react/jsx-runtime";var Q=Co(null),vo=({children:e,context:o,menuActionHandler:t,menuBuilder:n})=>{let r=xo(()=>o!=null&&o.menuBuilders&&n?o.menuBuilders.concat(n):n?[n]:(o==null?void 0:o.menuBuilders)||[],[o,n]),s=Po(i=>{var d;if(t!=null&&t(i)||(d=o==null?void 0:o.menuActionHandler)!=null&&d.call(o,i))return!0},[o,t]);return ge(Q.Provider,{value:{menuActionHandler:s,menuBuilders:r},children:e})},ns=({children:e,label:o,menuActionHandler:t,menuBuilder:n})=>ge(Q.Consumer,{children:r=>ge(vo,{context:r,label:o,menuActionHandler:t,menuBuilder:n,children:e})});import{isGroupMenuItemDescriptor as Fo}from"@vuu-ui/vuu-utils";import{cloneElement as Wo,useCallback as Me,useContext as qo,useMemo as Go}from"react";import Ro from"classnames";import Lo,{createElement as et}from"react";import q from"react-dom";import*as je from"react-dom";import Eo from"classnames";var wo=1,To=({className:e,dataMode:o,x:t=0,y:n=0,win:r=window})=>{let s=r.document.createElement("div");return s.className=Eo(`vuuPopup ${wo++}`,e),s.style.cssText=`left:${t}px; top:${n}px;`,o&&(s.dataset.mode=o),r.document.body.appendChild(s),s},us=e=>To(e),_e=(e,o,t,n,r)=>{o.style.cssText=`left:${t}px; top:${n}px;position: absolute;`,je.render(e,o,r)};var O=!1,A=[],tt=e=>(e==null?void 0:e.type)==="menu-action",ot=e=>(e==null?void 0:e.type)==="click-away";function j(e){if(e.key==="Esc"){if(A.length)rt();else if(O){let o=document.body.querySelector(".vuuDialog");o&&q.unmountComponentAtNode(o)}}}function nt(e){if(A.length){let o=document.body.querySelectorAll(".vuuPopup,#vuu-portal-root");for(let t=0;t<o.length;t++)if(o[t].contains(e.target))return;rt({mouseEvt:e,type:"click-away"})}}function rt(e){if(A.length===1)H.hidePopup(e,"anon","all");else if(A.length){let o=document.body.querySelectorAll(".vuuPopup");for(let t=0;t<o.length;t++)q.unmountComponentAtNode(o[t]);st("*")}}function Ho(){O===!1&&(O=!0,window.addEventListener("keydown",j,!0))}function Io(){O&&(O=!1,window.removeEventListener("keydown",j,!0))}function ko(e){A.indexOf(e)===-1&&(A.push(e),O===!1&&(window.addEventListener("keydown",j,!0),window.addEventListener("click",nt,!0)))}function st(e){if(A.length){if(e==="*")A.length=0;else{let o=A.indexOf(e);o!==-1&&A.splice(o,1)}A.length===0&&O===!1&&(window.removeEventListener("keydown",j,!0),window.removeEventListener("click",nt,!0))}}var Ao=({children:e,position:o,style:t})=>{let n=Ro("hwPopup","hwPopupContainer",o);return et("div",{className:n,style:t},e)},No=1,H=class{static showPopup({group:o="all",name:t="anon",left:n=0,position:r="",right:s="auto",top:i=0,width:d="auto",component:l}){if(!l)throw Error("PopupService showPopup, no component supplied");typeof l.props.onClose=="function"?H.onClose=l.props.onClose:H.onClose=void 0,ko(t),document.addEventListener("keydown",H.escapeKeyListener,!0);let a=document.body.querySelector(".vuuPopup."+o);a===null&&(a=document.createElement("div"),a.className="vuuPopup "+o,document.body.appendChild(a));let u={width:d};_e(et(Ao,{key:No++,position:r,style:u},l),a,n,i,()=>{H.keepWithinThePage(a,s)})}static escapeKeyListener(o){o.key==="Escape"&&H.hidePopup({type:"escape",event:o})}static hidePopup(o,t="anon",n="all"){var r;if(A.indexOf(t)!==-1){st(t);let s=document.body.querySelector(`.vuuPopup.${n}`);s&&q.unmountComponentAtNode(s)}document.removeEventListener("keydown",H.escapeKeyListener,!0),(r=H==null?void 0:H.onClose)==null||r.call(H,o)}static keepWithinThePage(o,t="auto"){let n=o.querySelector(".vuuPopupContainer > *");if(n){let{top:r,left:s,width:i,height:d,right:l}=n.getBoundingClientRect(),a=window.innerWidth,c=window.innerHeight-(r+d);c<0&&(n.style.top=Math.round(r)+c+"px");let p=a-(s+i);if(p<0&&(n.style.left=Math.round(s)+p+"px"),typeof t=="number"&&t!==l){let g=t-l;n.style.left=s+g+"px"}}}},Z=class{static showDialog(o){let t=".vuuDialog",n=o.props.onClose;Ho(),q.render(Lo.cloneElement(o,{container:t,onClose:()=>{Z.closeDialog(),n&&n()}}),document.body.querySelector(t))}static closeDialog(){Io();let o=document.body.querySelector(".vuuDialog");o&&q.unmountComponentAtNode(o)}};import $o from"classnames";import{useCallback as Do,useLayoutEffect as So,useRef as Bo,useState as Oo}from"react";var it=(e,o,t,n,r,s)=>{let{bottom:i,height:d,left:l,right:a,top:u,width:c}=e.getBoundingClientRect();switch(o){case"below":return{left:l+t,top:i+n};case"right":return{left:a+t,top:u+n};case"below-center":return{left:l+c/2+t,top:i+n};case"below-right":return{left:l,minWidth:r,top:i+n};case"below-full-width":return{left:l+t,minWidth:r,top:i+n,width:c};case"center":return s?{left:c/2-s.width/2+t,top:d/2-s.height/2+n,visibility:"visible"}:{left:c/2+t,top:d/2+n,visibility:"hidden"};default:throw Error("Popup getPositionRelativeToAnchor only supported placement values are below and right")}},_=({anchorElement:e,minWidth:o,offsetLeft:t=0,offsetTop:n=0,placement:r,position:s})=>{let i=Bo(null),[d,l]=Oo(s);So(()=>{if(r==="absolute"&&s)l(s);else if(e.current){let u=i.current===null?void 0:i.current.getBoundingClientRect(),c=it(e.current,r,t,n,o,u);l(c)}},[e,o,t,n,r,s]);let a=Do(u=>{if(i.current=u,u&&r==="center"&&e.current){let{height:c,width:p}=u.getBoundingClientRect();l(it(e.current,r,t,n,void 0,{height:c,width:p}))}},[e,t,n,r]);return{position:d,popupRef:r==="center"?a:void 0}};import{jsx as Ko}from"react/jsx-runtime";var Ze=({children:e,className:o,anchorElement:t,minWidth:n,placement:r,position:s})=>{let{popupRef:i,position:d}=_({anchorElement:t,minWidth:n,placement:r,position:s});return d===void 0?null:Ko("div",{className:$o("vuuPortal",o),ref:i,style:d,children:e})};import{useThemeAttributes as Vo}from"@vuu-ui/vuu-shell";import{jsx as he}from"react/jsx-runtime";var ut=(e,o)=>{let t=qo(Q),[n,r,s]=Vo(),i=Go(()=>({themeClass:n,densityClass:r,dataMode:s}),[s,r,n]),d=Me((u,c,p)=>{let g=[];for(let v of u)g=g.concat(v(c,p));return g},[]),l=Me((u,c,{ContextMenuProps:p,contextMenu:g,...v})=>{var P,C;if((P=u.stopPropagation)==null||P.call(u),(C=u.preventDefault)==null||C.call(u),g)return Xo({x:u.clientX,y:u.clientY},g);let M=[];if(e&&M.push(e),t&&Array.isArray(t==null?void 0:t.menuBuilders)&&t.menuBuilders.length>0&&M.push(...t.menuBuilders),M.length>0){let w=d(M,c,v),R=E=>(o==null?void 0:o(E))===!0?!0:t==null?void 0:t.menuActionHandler(E);w.length&&R&&Jo(u,w,R,{PortalProps:{themeAttributes:i},...p})}else console.warn("useContextMenu, no menuBuilders configured. These should be supplied via the ContextMenuProvider(s)")},[d,t,o,e,i]),a=Me(()=>{console.log("hide context menu")},[]);return[l,a]},Uo={},Xo=(e,o)=>{H.showPopup({focus:!0,left:0,top:0,component:Wo(o,{position:e})})},Jo=(e,o,t,{position:n,...r}=Uo)=>{let s=a=>{let u=(c,p)=>Fo(c)?he(J,{label:c.label,children:c.children.map(u)},p):he(z,{action:c.action,className:c.className,"data-icon":c.icon,options:c.options,children:c.label},p);return a.map(u)},i=a=>{var u;tt(a)&&(t(a),H.hidePopup()),(u=r==null?void 0:r.onClose)==null||u.call(r,a)},d=n!=null?n:{x:e.clientX,y:e.clientY},l=he(fe,{...r,onClose:i,position:d,children:s(o)});H.showPopup({left:0,top:0,component:l,focus:!0})};import{useCallback as be,useRef as at,useState as zo}from"react";import Yo from"classnames";import{Button as Qo}from"@salt-ds/core";import{useId as Zo}from"@vuu-ui/vuu-layout";import{jsx as _o}from"react/jsx-runtime";var ye="vuuPopupMenu",jo=e=>{if(e){let{bottom:o,left:t}=e.getBoundingClientRect();return{x:t,y:o+6}}},di=({className:e,label:o,icon:t=o?"chevron-down":"more-vert",id:n,menuActionHandler:r,menuBuilder:s,menuLocation:i="header",menuOptions:d,onMenuClose:l,tabIndex:a=0,...u})=>{let c=at(null),p=at(!1),[g,v]=zo(!1),M=Zo(n),[P]=ut(s,r),C=be(E=>{console.log("menu Open ",{el:E})},[]),w=be(E=>{v(!1),ot(E)?E.mouseEvt.target===c.current&&(p.current=!0):requestAnimationFrame(()=>{var b;l==null||l(E),a!==-1&&((b=c.current)==null||b.focus())})},[l,a]),R=be(E=>{p.current?p.current=!1:(v(!0),P(E,i,{ContextMenuProps:{id:`${M}-menu`,onClose:w,openMenu:C,position:jo(c.current)},...d}))},[w,C,M,i,d,P]);return _o(Qo,{...u,"aria-controls":`${M}-menu-root`,"aria-expanded":g,"aria-haspopup":"menu",className:Yo(ye,e,{[`${ye}-withCaption`]:o!==void 0,[`${ye}-open`]:g}),"data-icon":t,id:M,onClick:R,ref:c,tabIndex:a,variant:"secondary",children:o})};import{useThemeAttributes as en}from"@vuu-ui/vuu-shell";import{Button as lt}from"@salt-ds/core";import tn from"classnames";import{useLayoutEffect as on,useRef as ct}from"react";import{jsx as G,jsxs as pt}from"react/jsx-runtime";var F="vuuPrompt",nn={current:document.body},rn={},Ri=({PopupProps:e=rn,cancelButtonLabel:o="Cancel",confirmButtonLabel:t="Confirm",icon:n,onCancel:r,onConfirm:s,style:i,text:d,title:l,variant:a="info",...u})=>{let{anchorElement:c=nn,offsetLeft:p=0,offsetTop:g=0,placement:v="below"}=e,[M,P,C]=en(),{position:w}=_({anchorElement:c,offsetLeft:p,offsetTop:g,placement:v}),R=ct(null),E=ct(null);return on(()=>{if(R.current&&(R.current.showModal(),E.current&&E.current.focus(),v.endsWith("center"))){let{width:b}=R.current.getBoundingClientRect();R.current.style.marginLeft=`-${b/2}px`}},[v]),G("dialog",{...u,className:tn(F,`${F}-${a}`,M),"data-mode":C,ref:R,style:{...i,...w},children:pt("form",{className:`${F}-form`,children:[G("div",{className:`${F}-header`,"data-icon":n,children:l}),G("div",{className:`${F}-text`,children:d}),pt("div",{className:`${F}-buttonBar`,children:[G(lt,{onClick:r,variant:"secondary",children:o}),G(lt,{onClick:s,ref:E,value:"default",children:t})]})]})})};import{useLayoutEffect as sn,useState as un}from"react";var an=(e,o,t,n)=>{let{bottom:r,height:s,left:i,right:d,top:l,width:a}=e.getBoundingClientRect(),u=i+a/2,c=l+s/2;switch(o){case"above":return{left:u+t,top:l+n};case"below":return{left:u+t,top:r+n};case"right":return{left:d+t,top:c+t};case"left":return{left:i+t,top:c+t};default:throw Error("Tooltip getPositionRelativeToAnchor only supported placement values are left, right, below and right")}},dt=({anchorElement:e,offsetLeft:o=0,offsetTop:t=0,placement:n})=>{let[r,s]=un();return sn(()=>{if(e.current){let i=an(e.current,n,o,t);s(i)}},[e,o,t,n]),r};import ln from"classnames";import{jsx as Pe}from"react/jsx-runtime";var Ce="vuuTooltip",qi=({anchorElement:e,children:o,id:t,onMouseEnter:n,onMouseLeave:r,placement:s,status:i,style:d})=>{let l=dt({anchorElement:e,placement:s});return l===void 0?null:Pe(B,{children:Pe("div",{className:ln(Ce,{[`${Ce}-error`]:i==="error"}),"data-align":s,id:t,style:{...d,...l},children:Pe("span",{className:`${Ce}-content`,onMouseEnter:n,onMouseLeave:r,children:o})})})};import{useCallback as $,useRef as ee,useState as cn}from"react";import{useId as pn}from"@vuu-ui/vuu-layout";var Yi=({id:e,placement:o="right",tooltipContent:t})=>{let n=ee(),r=ee(null),s=ee(),i=ee(),[d,l]=cn(),a=pn(e),u=$(C=>{var w;C.key==="Escape"&&((w=n.current)==null||w.call(n))},[]);n.current=$(()=>{l(void 0),document.removeEventListener("keydown",u)},[u]);let c=$(()=>{window.clearTimeout(i.current)},[]),p=$(()=>{var C;(C=n.current)==null||C.call(n)},[]),g=$(()=>{let{current:C}=r;C&&(l({anchorElement:r,children:t,id:`${a}-tooltip`,onMouseEnter:c,onMouseLeave:p,placement:o}),document.addEventListener("keydown",u)),s.current=void 0},[u,c,p,a,o,t]),v=$(C=>{let w=C.target;w&&(r.current=w,s.current=window.setTimeout(g,800))},[g]),M=$(()=>{r.current&&(s.current?(window.clearTimeout(s.current),s.current=void 0):n.current&&(i.current=window.setTimeout(n.current,200)))},[]);return{anchorProps:{"aria-describedby":`${a}-tooltip`,onMouseEnter:v,onMouseLeave:M},tooltipProps:d}};import dn,{useState as ft,useContext as mn,useCallback as fn,useEffect as gn}from"react";import mt from"classnames";import{getUniqueId as Mn}from"@vuu-ui/vuu-utils";import{jsx as W,jsxs as ve}from"react/jsx-runtime";var hn=60,gt=6e3,xe=1e3,bn=300,Mt=56,oe=300,yn=10,Cn=10,ne=50,te="vuuToastNotifications",Pn=(r=>(r.Info="info",r.Success="success",r.Warning="warning",r.Error="error",r))(Pn||{}),ht=dn.createContext({notify:()=>"have you forgotten to provide a NotificationProvider?"}),uu=e=>{let[o,t]=ft([]),n=fn(r=>{let s={...r,id:Mn()};t(i=>[...i,s]),setTimeout(()=>{t(i=>i.filter(d=>d!==s))},gt+xe*2)},[]);return ve(ht.Provider,{value:{notify:n},children:[W("div",{style:{width:oe+ne+Cn},children:o.map((r,s)=>W(xn,{top:hn+(Mt+yn)*s,notification:r},r.id))}),e.children]})},au=()=>mn(ht),xn=e=>{let{top:o,notification:t,animated:n=!0}=e,[r,s]=ft(-oe-ne);return gn(()=>{setTimeout(()=>s(ne)),n&&setTimeout(()=>s(-oe-ne),gt+xe)},[n]),W(B,{children:ve("div",{className:mt(`${te}-toast`,t.type),style:{height:Mt,right:r,width:oe,top:o,transition:n?`right ${xe}ms, top ${bn}ms `:"none"},children:[W("div",{className:mt(`${te}-toastIcon`,`${t.type}-icon`)}),ve("div",{className:`${te}-toastContent`,children:[W("strong",{className:`${te}-toastHeader`,children:t.header}),W("div",{children:t.body})]})]})})};export{fe as ContextMenu,Q as ContextMenuContext,ns as ContextMenuProvider,Ie as Dialog,He as DialogHeader,Z as DialogService,z as MenuItem,J as MenuItemGroup,pe as MenuList,Pn as NotificationLevel,ht as NotificationsContext,uu as NotificationsProvider,Ze as PopupComponent,di as PopupMenu,H as PopupService,B as Portal,Ri as Prompt,Ke as Separator,xn as ToastNotification,qi as Tooltip,us as createContainer,ce as isMenuItemLabel,ot as reasonIsClickAway,tt as reasonIsMenuAction,_e as renderPortal,_ as useAnchoredPosition,ut as useContextMenu,nr as useDialog,au as useNotifications,Yi as useTooltip};
1
+ // src/dialog/Dialog.tsx
2
+ import cx5 from "clsx";
3
+ import { useThemeAttributes } from "@vuu-ui/vuu-utils";
4
+ import { useCallback as useCallback2, useLayoutEffect as useLayoutEffect2, useRef as useRef2 } from "react";
5
+
6
+ // src/dialog-header/DialogHeader.tsx
7
+ import { Button, Text } from "@salt-ds/core";
8
+ import cx from "clsx";
9
+ import { jsx, jsxs } from "react/jsx-runtime";
10
+ var classBase = "vuuDialogHeader";
11
+ var DialogHeader = ({
12
+ hideCloseButton = false,
13
+ title,
14
+ onClose,
15
+ ...htmlAttributes
16
+ }) => {
17
+ return /* @__PURE__ */ jsxs("div", { ...htmlAttributes, className: cx(classBase, "vuuToolbarProxy"), children: [
18
+ /* @__PURE__ */ jsx(Text, { className: "dialogHeader", children: title }),
19
+ !hideCloseButton && /* @__PURE__ */ jsx(
20
+ Button,
21
+ {
22
+ onClick: onClose,
23
+ "data-align": "end",
24
+ "data-icon": "close",
25
+ variant: "secondary"
26
+ },
27
+ "close"
28
+ )
29
+ ] });
30
+ };
31
+
32
+ // src/popup/popup-service.ts
33
+ import cx3 from "clsx";
34
+ import React, {
35
+ createElement
36
+ } from "react";
37
+ import ReactDOM2 from "react-dom";
38
+
39
+ // src/portal-deprecated/render-portal.tsx
40
+ import * as ReactDOM from "react-dom";
41
+ import cx2 from "clsx";
42
+ var containerId = 1;
43
+ var getPortalContainer = ({
44
+ className,
45
+ dataMode,
46
+ x = 0,
47
+ y = 0,
48
+ win = window
49
+ }) => {
50
+ const el = win.document.createElement("div");
51
+ el.className = cx2(`vuuPopup ${containerId++}`, className);
52
+ el.style.cssText = `left:${x}px; top:${y}px;`;
53
+ if (dataMode) {
54
+ el.dataset.mode = dataMode;
55
+ }
56
+ win.document.body.appendChild(el);
57
+ return el;
58
+ };
59
+ var createContainer = (props) => getPortalContainer(props);
60
+ var renderPortal = (component, container, x, y, onRender) => {
61
+ container.style.cssText = `left:${x}px; top:${y}px;position: absolute;`;
62
+ ReactDOM.render(component, container, onRender);
63
+ };
64
+
65
+ // src/popup/popup-service.ts
66
+ var _dialogOpen = false;
67
+ var _popups = [];
68
+ var reasonIsMenuAction = (reason) => (reason == null ? void 0 : reason.type) === "menu-action";
69
+ var reasonIsClickAway = (reason) => (reason == null ? void 0 : reason.type) === "click-away";
70
+ function specialKeyHandler(e) {
71
+ if (e.key === "Esc") {
72
+ if (_popups.length) {
73
+ closeAllPopups();
74
+ } else if (_dialogOpen) {
75
+ const dialogRoot = document.body.querySelector(".vuuDialog");
76
+ if (dialogRoot) {
77
+ ReactDOM2.unmountComponentAtNode(dialogRoot);
78
+ }
79
+ }
80
+ }
81
+ }
82
+ function outsideClickHandler(e) {
83
+ if (_popups.length) {
84
+ const popupContainers = document.body.querySelectorAll(
85
+ ".vuuPopup,#vuu-portal-root"
86
+ );
87
+ for (let i = 0; i < popupContainers.length; i++) {
88
+ if (popupContainers[i].contains(e.target)) {
89
+ return;
90
+ }
91
+ }
92
+ closeAllPopups({ mouseEvt: e, type: "click-away" });
93
+ }
94
+ }
95
+ function closeAllPopups(reason) {
96
+ if (_popups.length === 1) {
97
+ PopupService.hidePopup(reason, "anon", "all");
98
+ } else if (_popups.length) {
99
+ const popupContainers = document.body.querySelectorAll(".vuuPopup");
100
+ for (let i = 0; i < popupContainers.length; i++) {
101
+ ReactDOM2.unmountComponentAtNode(popupContainers[i]);
102
+ }
103
+ popupClosed("*");
104
+ }
105
+ }
106
+ function dialogOpened() {
107
+ if (_dialogOpen === false) {
108
+ _dialogOpen = true;
109
+ window.addEventListener("keydown", specialKeyHandler, true);
110
+ }
111
+ }
112
+ function dialogClosed() {
113
+ if (_dialogOpen) {
114
+ _dialogOpen = false;
115
+ window.removeEventListener("keydown", specialKeyHandler, true);
116
+ }
117
+ }
118
+ function popupOpened(name) {
119
+ if (_popups.indexOf(name) === -1) {
120
+ _popups.push(name);
121
+ if (_dialogOpen === false) {
122
+ window.addEventListener("keydown", specialKeyHandler, true);
123
+ window.addEventListener("click", outsideClickHandler, true);
124
+ }
125
+ }
126
+ }
127
+ function popupClosed(name) {
128
+ if (_popups.length) {
129
+ if (name === "*") {
130
+ _popups.length = 0;
131
+ } else {
132
+ const pos = _popups.indexOf(name);
133
+ if (pos !== -1) {
134
+ _popups.splice(pos, 1);
135
+ }
136
+ }
137
+ if (_popups.length === 0 && _dialogOpen === false) {
138
+ window.removeEventListener("keydown", specialKeyHandler, true);
139
+ window.removeEventListener("click", outsideClickHandler, true);
140
+ }
141
+ }
142
+ }
143
+ var PopupComponent = ({
144
+ children,
145
+ position,
146
+ style
147
+ }) => {
148
+ const className = cx3("hwPopup", "hwPopupContainer", position);
149
+ return createElement("div", { className, style }, children);
150
+ };
151
+ var incrementingKey = 1;
152
+ var PopupService = class _PopupService {
153
+ static showPopup({
154
+ group = "all",
155
+ name = "anon",
156
+ left = 0,
157
+ position = "",
158
+ right = "auto",
159
+ top = 0,
160
+ width = "auto",
161
+ component
162
+ }) {
163
+ if (!component) {
164
+ throw Error(`PopupService showPopup, no component supplied`);
165
+ }
166
+ if (typeof component.props.onClose === "function") {
167
+ _PopupService.onClose = component.props.onClose;
168
+ } else {
169
+ _PopupService.onClose = void 0;
170
+ }
171
+ popupOpened(name);
172
+ document.addEventListener("keydown", _PopupService.escapeKeyListener, true);
173
+ let el = document.body.querySelector(".vuuPopup." + group);
174
+ if (el === null) {
175
+ el = document.createElement("div");
176
+ el.className = "vuuPopup " + group;
177
+ document.body.appendChild(el);
178
+ }
179
+ const style = { width };
180
+ renderPortal(
181
+ createElement(
182
+ PopupComponent,
183
+ { key: incrementingKey++, position, style },
184
+ component
185
+ ),
186
+ el,
187
+ left,
188
+ top,
189
+ () => {
190
+ _PopupService.keepWithinThePage(el, right);
191
+ }
192
+ );
193
+ }
194
+ static escapeKeyListener(evt) {
195
+ if (evt.key === "Escape") {
196
+ _PopupService.hidePopup({ type: "escape", event: evt });
197
+ }
198
+ }
199
+ static hidePopup(reason, name = "anon", group = "all") {
200
+ var _a;
201
+ if (_popups.indexOf(name) !== -1) {
202
+ popupClosed(name);
203
+ const popupRoot = document.body.querySelector(`.vuuPopup.${group}`);
204
+ if (popupRoot) {
205
+ ReactDOM2.unmountComponentAtNode(popupRoot);
206
+ }
207
+ }
208
+ document.removeEventListener(
209
+ "keydown",
210
+ _PopupService.escapeKeyListener,
211
+ true
212
+ );
213
+ (_a = _PopupService == null ? void 0 : _PopupService.onClose) == null ? void 0 : _a.call(_PopupService, reason);
214
+ }
215
+ static keepWithinThePage(el, right = "auto") {
216
+ const target = el.querySelector(".vuuPopupContainer > *");
217
+ if (target) {
218
+ const {
219
+ top,
220
+ left,
221
+ width,
222
+ height,
223
+ right: currentRight
224
+ } = target.getBoundingClientRect();
225
+ const w = window.innerWidth;
226
+ const h = window.innerHeight;
227
+ const overflowH = h - (top + height);
228
+ if (overflowH < 0) {
229
+ target.style.top = Math.round(top) + overflowH + "px";
230
+ }
231
+ const overflowW = w - (left + width);
232
+ if (overflowW < 0) {
233
+ target.style.left = Math.round(left) + overflowW + "px";
234
+ }
235
+ if (typeof right === "number" && right !== currentRight) {
236
+ const adjustment = right - currentRight;
237
+ target.style.left = left + adjustment + "px";
238
+ }
239
+ }
240
+ }
241
+ };
242
+ var DialogService = class _DialogService {
243
+ static showDialog(dialog) {
244
+ const containerEl = ".vuuDialog";
245
+ const onClose = dialog.props.onClose;
246
+ dialogOpened();
247
+ ReactDOM2.render(
248
+ React.cloneElement(dialog, {
249
+ container: containerEl,
250
+ onClose: () => {
251
+ _DialogService.closeDialog();
252
+ if (onClose) {
253
+ onClose();
254
+ }
255
+ }
256
+ }),
257
+ document.body.querySelector(containerEl)
258
+ );
259
+ }
260
+ static closeDialog() {
261
+ dialogClosed();
262
+ const dialogRoot = document.body.querySelector(".vuuDialog");
263
+ if (dialogRoot) {
264
+ ReactDOM2.unmountComponentAtNode(dialogRoot);
265
+ }
266
+ }
267
+ };
268
+
269
+ // src/popup/Popup.tsx
270
+ import cx4 from "clsx";
271
+
272
+ // src/popup/useAnchoredPosition.ts
273
+ import { useCallback, useLayoutEffect, useRef, useState } from "react";
274
+ var getPositionRelativeToAnchor = (anchorElement, placement, offsetLeft, offsetTop, minWidth, dimensions) => {
275
+ const { bottom, height, left, right, top, width } = anchorElement.getBoundingClientRect();
276
+ switch (placement) {
277
+ case "below":
278
+ return { left: left + offsetLeft, top: bottom + offsetTop };
279
+ case "right":
280
+ return { left: right + offsetLeft, top: top + offsetTop };
281
+ case "below-center":
282
+ return { left: left + width / 2 + offsetLeft, top: bottom + offsetTop };
283
+ case "below-right":
284
+ return { left, minWidth, top: bottom + offsetTop };
285
+ case "below-full-width":
286
+ return {
287
+ left: left + offsetLeft,
288
+ minWidth,
289
+ top: bottom + offsetTop,
290
+ width
291
+ };
292
+ case "center":
293
+ if (dimensions) {
294
+ return {
295
+ left: width / 2 - dimensions.width / 2 + offsetLeft,
296
+ top: height / 2 - dimensions.height / 2 + offsetTop,
297
+ visibility: "visible"
298
+ };
299
+ } else {
300
+ return {
301
+ left: width / 2 + offsetLeft,
302
+ top: height / 2 + offsetTop,
303
+ visibility: "hidden"
304
+ };
305
+ }
306
+ default:
307
+ throw Error(
308
+ "Popup getPositionRelativeToAnchor only supported placement values are below and right"
309
+ );
310
+ }
311
+ };
312
+ var useAnchoredPosition = ({
313
+ anchorElement,
314
+ minWidth,
315
+ offsetLeft = 0,
316
+ offsetTop = 0,
317
+ placement,
318
+ position: positionProp
319
+ }) => {
320
+ const popupRef = useRef(null);
321
+ const [position, setPosition] = useState(positionProp);
322
+ useLayoutEffect(() => {
323
+ if (placement === "absolute" && positionProp) {
324
+ setPosition(positionProp);
325
+ } else if (anchorElement.current && placement !== "auto") {
326
+ const dimensions = popupRef.current === null ? void 0 : popupRef.current.getBoundingClientRect();
327
+ const position2 = getPositionRelativeToAnchor(
328
+ anchorElement.current,
329
+ placement,
330
+ offsetLeft,
331
+ offsetTop,
332
+ minWidth,
333
+ dimensions
334
+ );
335
+ setPosition(position2);
336
+ }
337
+ }, [anchorElement, minWidth, offsetLeft, offsetTop, placement, positionProp]);
338
+ const popupCallbackRef = useCallback(
339
+ (el) => {
340
+ popupRef.current = el;
341
+ if (el && placement === "center" && anchorElement.current) {
342
+ const { height, width } = el.getBoundingClientRect();
343
+ setPosition(
344
+ getPositionRelativeToAnchor(
345
+ anchorElement.current,
346
+ placement,
347
+ offsetLeft,
348
+ offsetTop,
349
+ void 0,
350
+ { height, width }
351
+ )
352
+ );
353
+ }
354
+ },
355
+ [anchorElement, offsetLeft, offsetTop, placement]
356
+ );
357
+ return {
358
+ position,
359
+ popupRef: placement === "center" ? popupCallbackRef : void 0
360
+ };
361
+ };
362
+
363
+ // src/popup/Popup.tsx
364
+ import { jsx as jsx2 } from "react/jsx-runtime";
365
+ var PopupComponent2 = ({
366
+ children,
367
+ className,
368
+ anchorElement,
369
+ minWidth,
370
+ placement,
371
+ position: positionProp
372
+ }) => {
373
+ const { popupRef, position } = useAnchoredPosition({
374
+ anchorElement,
375
+ minWidth,
376
+ placement,
377
+ position: positionProp
378
+ });
379
+ return position === void 0 ? null : /* @__PURE__ */ jsx2("div", { className: cx4(`vuuPortal`, className), ref: popupRef, style: position, children });
380
+ };
381
+
382
+ // src/dialog/Dialog.tsx
383
+ import { jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
384
+ var classBase2 = "vuuDialog";
385
+ var AnchorBody = { current: document.body };
386
+ var EMPTY_PROPS = {};
387
+ var Dialog = ({
388
+ PopupProps = EMPTY_PROPS,
389
+ children,
390
+ className,
391
+ isOpen = false,
392
+ onClose,
393
+ style,
394
+ title,
395
+ hideCloseButton = false,
396
+ ...htmlAttributes
397
+ }) => {
398
+ const {
399
+ anchorElement = AnchorBody,
400
+ offsetLeft = 0,
401
+ offsetTop = 0,
402
+ placement = "auto"
403
+ } = PopupProps;
404
+ const rootRef = useRef2(null);
405
+ const portalRef = useRef2(null);
406
+ const [themeClass, densityClass, dataMode] = useThemeAttributes();
407
+ const { position } = useAnchoredPosition({
408
+ anchorElement,
409
+ offsetLeft,
410
+ offsetTop,
411
+ placement
412
+ });
413
+ const close = useCallback2(() => {
414
+ onClose == null ? void 0 : onClose();
415
+ }, [onClose]);
416
+ useLayoutEffect2(() => {
417
+ if (rootRef.current) {
418
+ if (isOpen) {
419
+ rootRef.current.showModal();
420
+ const { left, top } = rootRef.current.getBoundingClientRect();
421
+ if (portalRef.current) {
422
+ portalRef.current.style.cssText = `left:-${left}px;position:absolute;top:-${top}px;`;
423
+ }
424
+ } else {
425
+ rootRef.current.close();
426
+ }
427
+ if (placement.endsWith("center")) {
428
+ const { width } = rootRef.current.getBoundingClientRect();
429
+ rootRef.current.style.marginLeft = `-${width / 2}px`;
430
+ }
431
+ }
432
+ }, [isOpen, placement]);
433
+ return /* @__PURE__ */ jsxs2(
434
+ "dialog",
435
+ {
436
+ ...htmlAttributes,
437
+ className: cx5(classBase2, themeClass),
438
+ "data-mode": dataMode,
439
+ onClose: close,
440
+ id: "vuu-dialog",
441
+ ref: rootRef,
442
+ style: { ...style, ...position },
443
+ children: [
444
+ /* @__PURE__ */ jsx3(
445
+ DialogHeader,
446
+ {
447
+ hideCloseButton,
448
+ onClose: close,
449
+ title
450
+ }
451
+ ),
452
+ /* @__PURE__ */ jsx3("div", { className: `${classBase2}-body`, children }),
453
+ /* @__PURE__ */ jsx3("div", { id: "vuu-dialog-portal-root", ref: portalRef })
454
+ ]
455
+ }
456
+ );
457
+ };
458
+
459
+ // src/dialog/useDialog.tsx
460
+ import { useCallback as useCallback3, useState as useState2 } from "react";
461
+ import { jsx as jsx4 } from "react/jsx-runtime";
462
+ var useDialog = () => {
463
+ const [dialogState, setDialogState] = useState2();
464
+ const handleClose = useCallback3(() => {
465
+ setDialogState(void 0);
466
+ }, []);
467
+ const dialog = dialogState ? /* @__PURE__ */ jsx4(
468
+ Dialog,
469
+ {
470
+ className: "vuDialog",
471
+ isOpen: true,
472
+ onClose: handleClose,
473
+ style: { maxHeight: 500 },
474
+ title: dialogState.title,
475
+ hideCloseButton: dialogState.hideCloseButton,
476
+ children: dialogState.content
477
+ }
478
+ ) : null;
479
+ return {
480
+ dialog,
481
+ setDialogState
482
+ };
483
+ };
484
+
485
+ // src/menu/ContextMenu.tsx
486
+ import { useId as useId2 } from "@vuu-ui/vuu-utils";
487
+ import { useCallback as useCallback7, useRef as useRef7 } from "react";
488
+
489
+ // src/portal/Portal.tsx
490
+ import { useThemeAttributes as useThemeAttributes2 } from "@vuu-ui/vuu-utils";
491
+ import { useLayoutEffect as useLayoutEffect3, useRef as useRef3, useState as useState3 } from "react";
492
+ import { createPortal } from "react-dom";
493
+ function getContainer(container) {
494
+ return typeof container === "function" ? container() : container;
495
+ }
496
+ var DEFAULT_ID = ["vuu-dialog-portal-root", "vuu-portal-root"];
497
+ var getFirstAvailableElementById = (id) => {
498
+ if (Array.isArray(id)) {
499
+ for (const i of id) {
500
+ const element = document.getElementById(i);
501
+ if (element) {
502
+ return element;
503
+ }
504
+ }
505
+ } else {
506
+ return document.getElementById(id);
507
+ }
508
+ return null;
509
+ };
510
+ var Portal = ({
511
+ children,
512
+ container: containerProp = document.body,
513
+ id = DEFAULT_ID,
514
+ onRender,
515
+ open = true,
516
+ themeAttributes
517
+ }) => {
518
+ var _a;
519
+ const [mounted, setMounted] = useState3(false);
520
+ const portalRef = useRef3(null);
521
+ const container = (_a = getContainer(containerProp)) != null ? _a : document.body;
522
+ const [themeClass, densityClass, dataMode] = useThemeAttributes2(themeAttributes);
523
+ useLayoutEffect3(() => {
524
+ const root = getFirstAvailableElementById(id);
525
+ if (root) {
526
+ portalRef.current = root;
527
+ } else {
528
+ portalRef.current = document.createElement("div");
529
+ portalRef.current.id = typeof id === "string" ? id : id.length > 0 ? id.at(-1) : "vuu-portal-root";
530
+ }
531
+ const el = portalRef.current;
532
+ if (!container.contains(el)) {
533
+ container.appendChild(el);
534
+ }
535
+ el.classList.add(themeClass, densityClass);
536
+ el.dataset.mode = dataMode;
537
+ setMounted(true);
538
+ }, [id, container, themeClass, densityClass, dataMode]);
539
+ useLayoutEffect3(() => {
540
+ requestAnimationFrame(() => {
541
+ onRender == null ? void 0 : onRender();
542
+ });
543
+ }, [onRender]);
544
+ if (open && mounted && portalRef.current && children) {
545
+ return createPortal(children, portalRef.current);
546
+ }
547
+ return null;
548
+ };
549
+
550
+ // src/menu/MenuList.tsx
551
+ import cx6 from "clsx";
552
+ import React3, {
553
+ useLayoutEffect as useLayoutEffect4,
554
+ useMemo as useMemo3,
555
+ useRef as useRef5
556
+ } from "react";
557
+ import { useId } from "@vuu-ui/vuu-utils";
558
+
559
+ // src/menu/use-items-with-ids-next.ts
560
+ import React2, { useCallback as useCallback4, useMemo } from "react";
561
+ var isMenuItemGroup = (child) => child.type === MenuItemGroup || !!child.props["data-group"];
562
+ var getLabelFromChildren = (children) => {
563
+ if (Array.isArray(children) && isMenuItemLabel(children[0])) {
564
+ return children[0];
565
+ }
566
+ };
567
+ var assignId = (child, path, group, hasSeparator = false) => {
568
+ const {
569
+ props: { children }
570
+ } = child;
571
+ return {
572
+ childWithId: React2.cloneElement(child, {
573
+ hasSeparator,
574
+ id: `${path}`,
575
+ key: path,
576
+ children: group ? getLabelFromChildren(children) : children
577
+ }),
578
+ grandChildren: group ? children : void 0
579
+ };
580
+ };
581
+ var useItemsWithIdsNext = (childrenProp, rootId) => {
582
+ const normalizeChildren = useCallback4(() => {
583
+ const collectChildren = (children, path = rootId, menus2 = {}, actions2 = {}) => {
584
+ const list = menus2[path] = [];
585
+ let idx = 0;
586
+ let hasSeparator = false;
587
+ React2.Children.forEach(children, (child) => {
588
+ if (isMenuItemLabel(child)) {
589
+ } else if (child.type === Separator) {
590
+ hasSeparator = true;
591
+ } else {
592
+ const hasChildItems = isMenuItemGroup(child);
593
+ const childPath = `${path}-${idx}`;
594
+ const {
595
+ props: { action, options }
596
+ } = child;
597
+ const { childWithId, grandChildren } = assignId(
598
+ child,
599
+ childPath,
600
+ hasChildItems,
601
+ hasSeparator
602
+ );
603
+ list.push(childWithId);
604
+ if (grandChildren) {
605
+ collectChildren(grandChildren, childPath, menus2, actions2);
606
+ } else {
607
+ actions2[childPath] = { action, options };
608
+ }
609
+ idx += 1;
610
+ hasSeparator = false;
611
+ }
612
+ });
613
+ return [menus2, actions2];
614
+ };
615
+ return collectChildren(childrenProp);
616
+ }, [rootId, childrenProp]);
617
+ const [menus, actions] = useMemo(
618
+ () => normalizeChildren(),
619
+ [normalizeChildren]
620
+ );
621
+ return [menus, actions];
622
+ };
623
+
624
+ // src/menu/use-keyboard-navigation.ts
625
+ import {
626
+ useCallback as useCallback5,
627
+ useMemo as useMemo2,
628
+ useRef as useRef4,
629
+ useState as useState4
630
+ } from "react";
631
+
632
+ // src/menu/utils.ts
633
+ var isRoot = (el) => el.closest(`[data-root='true']`) !== null;
634
+ var hasPopup = (el, idx) => {
635
+ var _a;
636
+ return el.ariaHasPopup === "true" && ((_a = el.dataset) == null ? void 0 : _a.idx) === `${idx}` || el.querySelector(`:scope > [data-index='${idx}'][aria-haspopup='true']`) !== null;
637
+ };
638
+
639
+ // src/menu/key-code.ts
640
+ function union(set1, ...sets) {
641
+ const result = new Set(set1);
642
+ for (const set of sets) {
643
+ for (const element of set) {
644
+ result.add(element);
645
+ }
646
+ }
647
+ return result;
648
+ }
649
+ var Enter = "Enter";
650
+ var Delete = "Delete";
651
+ var actionKeys = /* @__PURE__ */ new Set([Enter, Delete]);
652
+ var focusKeys = /* @__PURE__ */ new Set(["Tab"]);
653
+ var arrowLeftRightKeys = /* @__PURE__ */ new Set(["ArrowRight", "ArrowLeft"]);
654
+ var verticalNavigationKeys = /* @__PURE__ */ new Set(["Home", "End", "ArrowDown", "ArrowUp"]);
655
+ var horizontalNavigationKeys = /* @__PURE__ */ new Set([
656
+ "Home",
657
+ "End",
658
+ "ArrowRight",
659
+ "ArrowLeft"
660
+ ]);
661
+ var functionKeys = /* @__PURE__ */ new Set([
662
+ "F1",
663
+ "F2",
664
+ "F3",
665
+ "F4",
666
+ "F5",
667
+ "F6",
668
+ "F7",
669
+ "F8",
670
+ "F9",
671
+ "F10",
672
+ "F11",
673
+ "F12"
674
+ ]);
675
+ var specialKeys = union(
676
+ actionKeys,
677
+ horizontalNavigationKeys,
678
+ verticalNavigationKeys,
679
+ arrowLeftRightKeys,
680
+ functionKeys,
681
+ focusKeys
682
+ );
683
+ var isNavigationKey = ({ key }, orientation = "vertical") => {
684
+ const navigationKeys = orientation === "vertical" ? verticalNavigationKeys : horizontalNavigationKeys;
685
+ return navigationKeys.has(key);
686
+ };
687
+
688
+ // src/menu/use-keyboard-navigation.ts
689
+ import { isValidNumber } from "@vuu-ui/vuu-utils";
690
+ var useKeyboardNavigation = ({
691
+ autoHighlightFirstItem = false,
692
+ count,
693
+ defaultHighlightedIdx,
694
+ highlightedIndex: highlightedIndexProp,
695
+ onActivate,
696
+ onHighlight,
697
+ // onKeyDown,
698
+ onCloseMenu,
699
+ onOpenMenu
700
+ }) => {
701
+ var _a;
702
+ if (isValidNumber(highlightedIndexProp) && isValidNumber(defaultHighlightedIdx)) {
703
+ throw Error(
704
+ "useKeyboardNavigation do not pass values for both highlightedIndex and defaultHighlightedIdx"
705
+ );
706
+ }
707
+ const controlledHighlighting = isValidNumber(highlightedIndexProp);
708
+ const highlightedIndexRef = useRef4(
709
+ (_a = defaultHighlightedIdx != null ? defaultHighlightedIdx : highlightedIndexProp) != null ? _a : autoHighlightFirstItem ? 0 : -1
710
+ );
711
+ const [, forceRender] = useState4(null);
712
+ const setHighlightedIdx = useCallback5(
713
+ (idx) => {
714
+ highlightedIndexRef.current = idx;
715
+ onHighlight == null ? void 0 : onHighlight(idx);
716
+ forceRender({});
717
+ },
718
+ [onHighlight]
719
+ );
720
+ const setHighlightedIndex = useCallback5(
721
+ (idx) => {
722
+ if (idx !== highlightedIndexRef.current) {
723
+ if (!controlledHighlighting) {
724
+ setHighlightedIdx(idx);
725
+ }
726
+ }
727
+ },
728
+ [controlledHighlighting, setHighlightedIdx]
729
+ );
730
+ const keyBoardNavigation = useRef4(true);
731
+ const ignoreFocus = useRef4(false);
732
+ const setIgnoreFocus = (value) => ignoreFocus.current = value;
733
+ const highlightedIndex = controlledHighlighting ? highlightedIndexProp : highlightedIndexRef.current;
734
+ const navigateChildldItems = useCallback5(
735
+ (e) => {
736
+ const nextIdx = nextItemIdx(count, e.key, highlightedIndexRef.current);
737
+ if (nextIdx !== highlightedIndexRef.current) {
738
+ setHighlightedIndex(nextIdx);
739
+ }
740
+ },
741
+ [count, setHighlightedIndex]
742
+ );
743
+ const handleKeyDown = useCallback5(
744
+ (e) => {
745
+ if (isNavigationKey(e)) {
746
+ e.preventDefault();
747
+ e.stopPropagation();
748
+ keyBoardNavigation.current = true;
749
+ navigateChildldItems(e);
750
+ } else if ((e.key === "ArrowRight" || e.key === "Enter") && hasPopup(e.target, highlightedIndex)) {
751
+ const menuEl = e.target;
752
+ const menuItemEl = menuEl.querySelector(
753
+ `:scope > [data-index='${highlightedIndex}']`
754
+ );
755
+ if (menuItemEl) {
756
+ onOpenMenu == null ? void 0 : onOpenMenu(menuItemEl, true);
757
+ }
758
+ } else if (e.key === "ArrowLeft" && !isRoot(e.target)) {
759
+ onCloseMenu(highlightedIndex);
760
+ } else if (e.key === "Enter") {
761
+ e.preventDefault();
762
+ e.stopPropagation();
763
+ onActivate && onActivate(highlightedIndex);
764
+ } else if (e.key === "Tab") {
765
+ onCloseMenu(-1);
766
+ }
767
+ },
768
+ [
769
+ highlightedIndex,
770
+ navigateChildldItems,
771
+ onActivate,
772
+ onCloseMenu,
773
+ onOpenMenu
774
+ ]
775
+ );
776
+ const listProps = useMemo2(
777
+ () => ({
778
+ onFocus: () => {
779
+ if (highlightedIndex === -1) {
780
+ setHighlightedIdx(0);
781
+ }
782
+ },
783
+ onKeyDown: handleKeyDown,
784
+ onMouseDownCapture: () => {
785
+ keyBoardNavigation.current = false;
786
+ setIgnoreFocus(true);
787
+ },
788
+ // onMouseEnter would seem less expensive but it misses some cases
789
+ onMouseMove: () => {
790
+ if (keyBoardNavigation.current) {
791
+ keyBoardNavigation.current = false;
792
+ }
793
+ },
794
+ onMouseLeave: () => {
795
+ keyBoardNavigation.current = true;
796
+ setIgnoreFocus(false);
797
+ setHighlightedIndex(-1);
798
+ }
799
+ }),
800
+ [handleKeyDown, highlightedIndex, setHighlightedIdx, setHighlightedIndex]
801
+ );
802
+ return {
803
+ focusVisible: keyBoardNavigation.current ? highlightedIndex : -1,
804
+ controlledHighlighting,
805
+ highlightedIndex,
806
+ setHighlightedIndex,
807
+ // keyBoardNavigation,
808
+ listProps,
809
+ setIgnoreFocus
810
+ };
811
+ };
812
+ function nextItemIdx(count, key, idx) {
813
+ if (key === "ArrowUp") {
814
+ if (idx > 0) {
815
+ return idx - 1;
816
+ } else {
817
+ return idx;
818
+ }
819
+ } else {
820
+ if (idx === null) {
821
+ return 0;
822
+ } else if (idx === count - 1) {
823
+ return idx;
824
+ } else {
825
+ return idx + 1;
826
+ }
827
+ }
828
+ }
829
+
830
+ // src/menu/MenuList.tsx
831
+ import { Fragment, jsx as jsx5 } from "react/jsx-runtime";
832
+ var classBase3 = "vuuMenuList";
833
+ var Separator = () => /* @__PURE__ */ jsx5("li", { className: "vuuMenuItem-divider" });
834
+ var MenuItemGroup = () => null;
835
+ var MenuItem = ({
836
+ children,
837
+ idx,
838
+ options,
839
+ ...props
840
+ }) => {
841
+ return /* @__PURE__ */ jsx5("div", { ...props, children });
842
+ };
843
+ var MenuItemLabel = ({ children }) => /* @__PURE__ */ jsx5(Fragment, { children });
844
+ MenuItemLabel.displayName = "MenuItemLabel";
845
+ MenuItem.Label = MenuItemLabel;
846
+ var getDisplayName = (item) => React3.isValidElement(item) && typeof item.type !== "string" && "displayName" in item.type ? item.type.displayName : void 0;
847
+ var isMenuItemLabel = (item) => getDisplayName(item) === "MenuItemLabel";
848
+ var hasIcon = (child) => child.props["data-icon"];
849
+ var MenuList = ({
850
+ activatedByKeyboard,
851
+ childMenuShowing,
852
+ children,
853
+ className,
854
+ defaultHighlightedIdx,
855
+ highlightedIdx: highlightedIdxProp,
856
+ id: idProp,
857
+ isRoot: isRoot2,
858
+ listItemProps,
859
+ onHighlightMenuItem,
860
+ onActivate,
861
+ onCloseMenu,
862
+ openMenu: onOpenMenu,
863
+ ...props
864
+ }) => {
865
+ const id = useId(idProp);
866
+ const root = useRef5(null);
867
+ const mapIdxToId = useMemo3(() => /* @__PURE__ */ new Map(), []);
868
+ const handleActivate = (idx) => {
869
+ var _a;
870
+ const el = (_a = root.current) == null ? void 0 : _a.querySelector(`:scope > [data-index='${idx}']`);
871
+ (el == null ? void 0 : el.id) && (onActivate == null ? void 0 : onActivate(el.id));
872
+ };
873
+ const { focusVisible, highlightedIndex, listProps } = useKeyboardNavigation({
874
+ count: React3.Children.count(children),
875
+ defaultHighlightedIdx,
876
+ highlightedIndex: highlightedIdxProp,
877
+ onActivate: handleActivate,
878
+ onHighlight: onHighlightMenuItem,
879
+ onOpenMenu,
880
+ onCloseMenu
881
+ });
882
+ const appliedFocusVisible = childMenuShowing == void 0 ? focusVisible : -1;
883
+ useLayoutEffect4(() => {
884
+ var _a;
885
+ if (childMenuShowing === void 0 && activatedByKeyboard) {
886
+ (_a = root.current) == null ? void 0 : _a.focus();
887
+ }
888
+ }, [activatedByKeyboard, childMenuShowing]);
889
+ const getActiveDescendant = () => highlightedIndex === void 0 || highlightedIndex === -1 ? void 0 : mapIdxToId.get(highlightedIndex);
890
+ function renderContent() {
891
+ const propsCommonToAllListItems = {
892
+ ...listItemProps,
893
+ role: "menuitem"
894
+ };
895
+ const maybeIcon = (childElement, withIcon, iconName) => withIcon ? [
896
+ /* @__PURE__ */ jsx5(
897
+ "span",
898
+ {
899
+ className: "vuuIconContainer",
900
+ "data-icon": iconName
901
+ },
902
+ "icon"
903
+ )
904
+ ].concat(childElement) : childElement;
905
+ function addClonedChild(list, child, idx, withIcon) {
906
+ var _a;
907
+ const {
908
+ children: children2,
909
+ className: className2,
910
+ "data-icon": iconName,
911
+ id: itemId,
912
+ hasSeparator,
913
+ label,
914
+ ...props2
915
+ } = child.props;
916
+ const hasSubMenu = isMenuItemGroup(child);
917
+ const subMenuShowing = hasSubMenu && childMenuShowing === itemId;
918
+ const ariaControls = subMenuShowing ? `${id}-${itemId}` : void 0;
919
+ list.push(
920
+ /* @__PURE__ */ jsx5(
921
+ MenuItem,
922
+ {
923
+ ...props2,
924
+ ...propsCommonToAllListItems,
925
+ ...getMenuItemProps(
926
+ itemId,
927
+ idx,
928
+ (_a = child.key) != null ? _a : itemId,
929
+ highlightedIndex,
930
+ appliedFocusVisible,
931
+ className2,
932
+ hasSeparator
933
+ ),
934
+ "aria-controls": ariaControls,
935
+ "aria-haspopup": hasSubMenu || void 0,
936
+ "aria-expanded": subMenuShowing || void 0,
937
+ children: hasSubMenu ? maybeIcon(label != null ? label : children2, withIcon, iconName) : maybeIcon(children2, withIcon, iconName)
938
+ }
939
+ )
940
+ );
941
+ }
942
+ const listItems = [];
943
+ if (children.length > 0) {
944
+ const withIcon = children.some(hasIcon);
945
+ children.forEach((child, idx) => {
946
+ addClonedChild(listItems, child, idx, withIcon);
947
+ });
948
+ }
949
+ return listItems;
950
+ }
951
+ return /* @__PURE__ */ jsx5(
952
+ "div",
953
+ {
954
+ ...props,
955
+ ...listProps,
956
+ "aria-activedescendant": getActiveDescendant(),
957
+ className: cx6(classBase3, className, {
958
+ [`${classBase3}-childMenuShowing`]: childMenuShowing !== void 0
959
+ }),
960
+ "data-root": isRoot2 || void 0,
961
+ id,
962
+ ref: root,
963
+ role: "menu",
964
+ children: renderContent()
965
+ }
966
+ );
967
+ };
968
+ var getMenuItemProps = (itemId, idx, key, highlightedIdx, focusVisible, className, hasSeparator) => ({
969
+ id: `menuitem-${itemId}`,
970
+ key: key != null ? key : idx,
971
+ "data-index": idx,
972
+ "data-highlighted": idx === highlightedIdx || void 0,
973
+ className: cx6("vuuMenuItem", className, {
974
+ "vuuMenuItem-separator": hasSeparator,
975
+ focusVisible: focusVisible === idx
976
+ })
977
+ });
978
+ MenuList.displayName = "MenuList";
979
+
980
+ // src/menu/use-cascade.ts
981
+ import {
982
+ useCallback as useCallback6,
983
+ useMemo as useMemo4,
984
+ useRef as useRef6,
985
+ useState as useState5
986
+ } from "react";
987
+
988
+ // src/menu/list-dom-utils.ts
989
+ var closestListItem = (el) => el == null ? void 0 : el.closest("[data-index],[aria-posinset]");
990
+
991
+ // src/menu/use-cascade.ts
992
+ var nudge = (menus, distance, pos) => {
993
+ return menus.map(
994
+ (m, i) => i === menus.length - 1 ? {
995
+ ...m,
996
+ [pos]: m[pos] - distance
997
+ } : m
998
+ );
999
+ };
1000
+ var nudgeLeft = (menus, distance) => nudge(menus, distance, "left");
1001
+ var nudgeUp = (menus, distance) => nudge(menus, distance, "top");
1002
+ var flipSides = (id, menus) => {
1003
+ const [parentMenu, menu] = menus.slice(-2);
1004
+ const el = document.getElementById(`${id}-${menu.id}`);
1005
+ if (el === null) {
1006
+ throw Error(`useCascade.flipSides element with id ${menu.id} not found`);
1007
+ }
1008
+ const { width } = el.getBoundingClientRect();
1009
+ return menus.map(
1010
+ (m) => m === menu ? {
1011
+ ...m,
1012
+ left: parentMenu.left - (width - 2)
1013
+ } : m
1014
+ );
1015
+ };
1016
+ var getPosition = (el, openMenus) => {
1017
+ const [{ left, top: menuTop }] = openMenus.slice(-1);
1018
+ const { offsetWidth: width, offsetTop: top } = el;
1019
+ return { left: left + width, top: top + menuTop };
1020
+ };
1021
+ var getHostMenuId = (id, rootId) => {
1022
+ const pos = id.lastIndexOf("-");
1023
+ if (id.startsWith("menuitem")) {
1024
+ return pos > -1 ? id.slice(9, pos) : rootId;
1025
+ } else {
1026
+ return pos > -1 ? id.slice(0, pos) : rootId;
1027
+ }
1028
+ };
1029
+ var getTargetMenuId = (id) => id.slice(9);
1030
+ var getMenuItemDetails = ({ ariaExpanded, ariaHasPopup, id }, rootId) => {
1031
+ if (id.startsWith("menuitem")) {
1032
+ return {
1033
+ hostMenuId: getHostMenuId(id, rootId),
1034
+ targetMenuId: getTargetMenuId(id),
1035
+ menuItemId: id,
1036
+ isGroup: ariaHasPopup === "true",
1037
+ isOpen: ariaExpanded === "true"
1038
+ };
1039
+ } else {
1040
+ throw Error(`getMenuItemDetails #${id} is not a menuitem`);
1041
+ }
1042
+ };
1043
+ var useCascade = ({
1044
+ id: rootId,
1045
+ onActivate,
1046
+ onMouseEnterItem,
1047
+ position: { x: posX, y: posY }
1048
+ }) => {
1049
+ const [, forceRefresh] = useState5({});
1050
+ const openMenus = useRef6([
1051
+ { id: rootId, left: posX, top: posY }
1052
+ ]);
1053
+ const menuIsOpen = useCallback6(
1054
+ (menuId) => openMenus.current.findIndex((menu) => menu.id === menuId) !== -1,
1055
+ []
1056
+ );
1057
+ const getOpenMenuStatus = useCallback6((menuId) => {
1058
+ const state = menuState.current[menuId];
1059
+ if (state === void 0) {
1060
+ throw Error(`getOpenMenuState no entry for menu ${menuId}`);
1061
+ }
1062
+ return state;
1063
+ }, []);
1064
+ const setOpenMenus = useCallback6((menus) => {
1065
+ openMenus.current = menus;
1066
+ forceRefresh({});
1067
+ }, []);
1068
+ const menuOpenPendingTimeout = useRef6();
1069
+ const menuClosePendingTimeout = useRef6();
1070
+ const menuState = useRef6({ [rootId]: "no-popup" });
1071
+ const openMenu = useCallback6(
1072
+ (hostMenuId = rootId, targetMenuId, itemId = null) => {
1073
+ if (hostMenuId === rootId && itemId === null) {
1074
+ setOpenMenus([{ id: rootId, left: posX, top: posY }]);
1075
+ } else {
1076
+ menuState.current[hostMenuId] = "popup-open";
1077
+ const el = document.getElementById(itemId);
1078
+ if (el !== null) {
1079
+ const { left, top } = getPosition(el, openMenus.current);
1080
+ setOpenMenus(
1081
+ openMenus.current.concat({ id: targetMenuId, left, top })
1082
+ );
1083
+ } else {
1084
+ throw Error(`openMenu no menuItem ${itemId}`);
1085
+ }
1086
+ }
1087
+ },
1088
+ [rootId, posX, posY, setOpenMenus]
1089
+ );
1090
+ const closeMenu = useCallback6(
1091
+ (menuId) => {
1092
+ if (menuId === rootId) {
1093
+ setOpenMenus([]);
1094
+ } else {
1095
+ const menus = openMenus.current.slice();
1096
+ const lastMenu = menus.pop();
1097
+ menuState.current[lastMenu.id] = "no-popup";
1098
+ const parentMenu = menus.at(-1);
1099
+ if (parentMenu) {
1100
+ menuState.current[parentMenu.id] = "no-popup";
1101
+ }
1102
+ setOpenMenus(menus);
1103
+ }
1104
+ },
1105
+ [rootId, setOpenMenus]
1106
+ );
1107
+ const closeMenus = useCallback6(
1108
+ (menuItemId) => {
1109
+ const menus = openMenus.current.slice();
1110
+ const menuItemMenuId = menuItemId.slice(9);
1111
+ let { id: lastMenuId } = menus.at(-1);
1112
+ while (menus.length > 1 && !menuItemMenuId.startsWith(lastMenuId)) {
1113
+ const parentMenuId = getHostMenuId(lastMenuId, rootId);
1114
+ menus.pop();
1115
+ menuState.current[lastMenuId] = "no-popup";
1116
+ menuState.current[parentMenuId] = "no-popup";
1117
+ ({ id: lastMenuId } = menus[menus.length - 1]);
1118
+ }
1119
+ if (menus.length < openMenus.current.length) {
1120
+ setOpenMenus(menus);
1121
+ }
1122
+ },
1123
+ [rootId, setOpenMenus]
1124
+ );
1125
+ const clearAnyScheduledOpenTasks = useCallback6(() => {
1126
+ if (menuOpenPendingTimeout.current) {
1127
+ clearTimeout(menuOpenPendingTimeout.current);
1128
+ menuOpenPendingTimeout.current = void 0;
1129
+ }
1130
+ }, []);
1131
+ const scheduleOpen = useCallback6(
1132
+ (hostMenuId, targetMenuId, menuItemId, delay = 300) => {
1133
+ clearAnyScheduledOpenTasks();
1134
+ menuOpenPendingTimeout.current = window.setTimeout(() => {
1135
+ closeMenus(menuItemId);
1136
+ menuState.current[hostMenuId] = "popup-open";
1137
+ menuState.current[targetMenuId] = "no-popup";
1138
+ openMenu(hostMenuId, targetMenuId, menuItemId);
1139
+ }, delay);
1140
+ },
1141
+ [clearAnyScheduledOpenTasks, closeMenus, openMenu]
1142
+ );
1143
+ const scheduleClose = useCallback6(
1144
+ (hostMenuId, openMenuId, itemId) => {
1145
+ menuState.current[openMenuId] = "pending-close";
1146
+ menuClosePendingTimeout.current = window.setTimeout(() => {
1147
+ closeMenus(itemId);
1148
+ }, 400);
1149
+ },
1150
+ [closeMenus]
1151
+ );
1152
+ const handleRender = useCallback6(() => {
1153
+ const { current: menus } = openMenus;
1154
+ const menu = menus.at(-1);
1155
+ const el = menu ? document.getElementById(menu.id) : void 0;
1156
+ if (el) {
1157
+ const { right, bottom } = el.getBoundingClientRect();
1158
+ const { clientHeight, clientWidth } = document.body;
1159
+ if (right > clientWidth) {
1160
+ const newMenus = menus.length > 1 ? flipSides(rootId, menus) : nudgeLeft(menus, right - clientWidth);
1161
+ setOpenMenus(newMenus);
1162
+ } else if (bottom > clientHeight) {
1163
+ const newMenus = nudgeUp(menus, bottom - clientHeight);
1164
+ setOpenMenus(newMenus);
1165
+ }
1166
+ if (typeof el.tabIndex === "number") {
1167
+ el.focus();
1168
+ }
1169
+ }
1170
+ }, [rootId, setOpenMenus]);
1171
+ const triggerChildMenu = useCallback6(
1172
+ (menuItemEl, immediate = false) => {
1173
+ const { hostMenuId, targetMenuId, menuItemId, isGroup, isOpen } = getMenuItemDetails(menuItemEl, rootId);
1174
+ const {
1175
+ current: { [hostMenuId]: state }
1176
+ } = menuState;
1177
+ const delay = immediate ? 0 : void 0;
1178
+ if (state === "no-popup" && isGroup) {
1179
+ menuState.current[hostMenuId] = "popup-pending";
1180
+ scheduleOpen(hostMenuId, targetMenuId, menuItemId, delay);
1181
+ } else if (state === "popup-pending" && !isGroup) {
1182
+ menuState.current[hostMenuId] = "no-popup";
1183
+ clearTimeout(menuOpenPendingTimeout.current);
1184
+ menuOpenPendingTimeout.current = void 0;
1185
+ } else if (state === "popup-pending" && isGroup) {
1186
+ clearTimeout(menuOpenPendingTimeout.current);
1187
+ scheduleOpen(hostMenuId, targetMenuId, menuItemId, delay);
1188
+ } else if (state === "popup-open") {
1189
+ if (menuIsOpen(targetMenuId)) {
1190
+ const menuStatus = getOpenMenuStatus(targetMenuId);
1191
+ closeMenus(menuItemId);
1192
+ switch (menuStatus) {
1193
+ case "pending-close":
1194
+ clearTimeout(menuClosePendingTimeout.current);
1195
+ menuClosePendingTimeout.current = void 0;
1196
+ menuState.current[targetMenuId] = "no-popup";
1197
+ clearAnyScheduledOpenTasks();
1198
+ break;
1199
+ default:
1200
+ }
1201
+ } else {
1202
+ const [parentOfLastOpenedMenu, lastOpenedMenu] = openMenus.current.slice(-2);
1203
+ if (parentOfLastOpenedMenu.id === hostMenuId && menuState.current[lastOpenedMenu.id] !== "pending-close") {
1204
+ scheduleClose(hostMenuId, lastOpenedMenu.id, menuItemId);
1205
+ if (isGroup && !isOpen) {
1206
+ scheduleOpen(hostMenuId, targetMenuId, menuItemId, delay);
1207
+ }
1208
+ } else if (parentOfLastOpenedMenu.id === hostMenuId && isGroup && menuItemId !== lastOpenedMenu.id && menuState.current[lastOpenedMenu.id] === "pending-close") {
1209
+ scheduleOpen(hostMenuId, targetMenuId, menuItemId, delay);
1210
+ } else if (isGroup) {
1211
+ scheduleOpen(hostMenuId, targetMenuId, menuItemId, delay);
1212
+ } else if (!(menuState.current[lastOpenedMenu.id] === "pending-close")) {
1213
+ closeMenus(menuItemId);
1214
+ }
1215
+ }
1216
+ }
1217
+ if (state === "pending-close") {
1218
+ clearAnyScheduledOpenTasks();
1219
+ clearTimeout(menuClosePendingTimeout.current);
1220
+ menuClosePendingTimeout.current = void 0;
1221
+ menuState.current[hostMenuId] = "popup-open";
1222
+ }
1223
+ },
1224
+ [
1225
+ clearAnyScheduledOpenTasks,
1226
+ closeMenus,
1227
+ getOpenMenuStatus,
1228
+ menuIsOpen,
1229
+ rootId,
1230
+ scheduleClose,
1231
+ scheduleOpen
1232
+ ]
1233
+ );
1234
+ const listItemProps = useMemo4(
1235
+ () => ({
1236
+ onMouseEnter: (evt) => {
1237
+ const menuItemEl = closestListItem(evt.target);
1238
+ triggerChildMenu(menuItemEl);
1239
+ onMouseEnterItem(evt, menuItemEl.id);
1240
+ },
1241
+ onClick: (evt) => {
1242
+ const listItemEl = closestListItem(evt.target);
1243
+ const { isGroup, menuItemId } = getMenuItemDetails(listItemEl, rootId);
1244
+ if (isGroup) {
1245
+ triggerChildMenu(listItemEl);
1246
+ } else {
1247
+ onActivate(menuItemId);
1248
+ }
1249
+ }
1250
+ }),
1251
+ [onActivate, onMouseEnterItem, rootId, triggerChildMenu]
1252
+ );
1253
+ return {
1254
+ closeMenu,
1255
+ handleRender,
1256
+ listItemProps,
1257
+ openMenu: triggerChildMenu,
1258
+ openMenus: openMenus.current
1259
+ };
1260
+ };
1261
+
1262
+ // src/menu/ContextMenu.tsx
1263
+ import { Fragment as Fragment2, jsx as jsx6 } from "react/jsx-runtime";
1264
+ import { createElement as createElement2 } from "react";
1265
+ var noop = () => void 0;
1266
+ var ContextMenu = ({
1267
+ PortalProps: PortalProps2,
1268
+ activatedByKeyboard,
1269
+ children: childrenProp,
1270
+ className,
1271
+ id: idProp,
1272
+ onClose = () => void 0,
1273
+ position = { x: 0, y: 0 },
1274
+ style,
1275
+ ...menuListProps
1276
+ }) => {
1277
+ const closeHandlerRef = useRef7(onClose);
1278
+ closeHandlerRef.current = onClose;
1279
+ const id = useId2(idProp);
1280
+ const closeMenuRef = useRef7(noop);
1281
+ const [menus, actions] = useItemsWithIdsNext(childrenProp, id);
1282
+ const navigatingWithKeyboard = useRef7(activatedByKeyboard);
1283
+ const handleMouseEnterItem = useCallback7(() => {
1284
+ navigatingWithKeyboard.current = false;
1285
+ }, []);
1286
+ const handleActivate = useCallback7(
1287
+ (menuItemId) => {
1288
+ const actionId = menuItemId.slice(9);
1289
+ const { action, options } = actions[actionId];
1290
+ closeMenuRef.current(id);
1291
+ onClose({
1292
+ type: "menu-action",
1293
+ menuId: action,
1294
+ options
1295
+ });
1296
+ },
1297
+ [actions, id, onClose]
1298
+ );
1299
+ const {
1300
+ closeMenu,
1301
+ listItemProps,
1302
+ openMenu: onOpenMenu,
1303
+ openMenus,
1304
+ handleRender
1305
+ } = useCascade({
1306
+ // FIXME
1307
+ id: `${id}`,
1308
+ onActivate: handleActivate,
1309
+ onMouseEnterItem: handleMouseEnterItem,
1310
+ position
1311
+ });
1312
+ closeMenuRef.current = closeMenu;
1313
+ const handleCloseMenu = () => {
1314
+ navigatingWithKeyboard.current = true;
1315
+ closeMenu();
1316
+ };
1317
+ const handleHighlightMenuItem = () => {
1318
+ };
1319
+ const lastMenu = openMenus.length - 1;
1320
+ const getChildMenuId = (i) => {
1321
+ if (i >= lastMenu) {
1322
+ return void 0;
1323
+ } else {
1324
+ const { id: id2 } = openMenus[i + 1];
1325
+ return id2;
1326
+ }
1327
+ };
1328
+ return /* @__PURE__ */ jsx6(Fragment2, { children: openMenus.map(({ id: menuId, left, top }, i, all) => {
1329
+ const childMenuId = getChildMenuId(i);
1330
+ return /* @__PURE__ */ createElement2(Portal, { ...PortalProps2, key: i, onRender: handleRender }, /* @__PURE__ */ jsx6(
1331
+ PopupComponent2,
1332
+ {
1333
+ anchorElement: { current: document.body },
1334
+ placement: "absolute",
1335
+ position: { left, top },
1336
+ children: /* @__PURE__ */ createElement2(
1337
+ MenuList,
1338
+ {
1339
+ ...menuListProps,
1340
+ activatedByKeyboard: navigatingWithKeyboard.current,
1341
+ childMenuShowing: childMenuId,
1342
+ className,
1343
+ id: menuId,
1344
+ isRoot: i === 0,
1345
+ key: i,
1346
+ listItemProps,
1347
+ onActivate: handleActivate,
1348
+ onHighlightMenuItem: handleHighlightMenuItem,
1349
+ onCloseMenu: handleCloseMenu,
1350
+ openMenu: onOpenMenu,
1351
+ style,
1352
+ tabIndex: i === all.length - 1 ? 0 : void 0
1353
+ },
1354
+ menus[menuId]
1355
+ )
1356
+ }
1357
+ ));
1358
+ }) });
1359
+ };
1360
+ ContextMenu.displayName = "ContextMenu";
1361
+
1362
+ // src/menu/context-menu-provider.tsx
1363
+ import { createContext, useCallback as useCallback8, useMemo as useMemo5 } from "react";
1364
+ import { jsx as jsx7 } from "react/jsx-runtime";
1365
+ var ContextMenuContext = createContext(
1366
+ null
1367
+ );
1368
+ var Provider = ({
1369
+ children,
1370
+ context,
1371
+ menuActionHandler,
1372
+ menuBuilder
1373
+ }) => {
1374
+ const menuBuilders = useMemo5(() => {
1375
+ if ((context == null ? void 0 : context.menuBuilders) && menuBuilder) {
1376
+ return context.menuBuilders.concat(menuBuilder);
1377
+ } else if (menuBuilder) {
1378
+ return [menuBuilder];
1379
+ } else {
1380
+ return (context == null ? void 0 : context.menuBuilders) || [];
1381
+ }
1382
+ }, [context, menuBuilder]);
1383
+ const handleMenuAction = useCallback8(
1384
+ (reason) => {
1385
+ var _a;
1386
+ if (menuActionHandler == null ? void 0 : menuActionHandler(reason)) {
1387
+ return true;
1388
+ }
1389
+ if ((_a = context == null ? void 0 : context.menuActionHandler) == null ? void 0 : _a.call(context, reason)) {
1390
+ return true;
1391
+ }
1392
+ },
1393
+ [context, menuActionHandler]
1394
+ );
1395
+ return /* @__PURE__ */ jsx7(
1396
+ ContextMenuContext.Provider,
1397
+ {
1398
+ value: {
1399
+ menuActionHandler: handleMenuAction,
1400
+ menuBuilders
1401
+ },
1402
+ children
1403
+ }
1404
+ );
1405
+ };
1406
+ var ContextMenuProvider = ({
1407
+ children,
1408
+ label,
1409
+ menuActionHandler,
1410
+ menuBuilder
1411
+ }) => {
1412
+ return /* @__PURE__ */ jsx7(ContextMenuContext.Consumer, { children: (parentContext) => /* @__PURE__ */ jsx7(
1413
+ Provider,
1414
+ {
1415
+ context: parentContext,
1416
+ label,
1417
+ menuActionHandler,
1418
+ menuBuilder,
1419
+ children
1420
+ }
1421
+ ) });
1422
+ };
1423
+
1424
+ // src/menu/useContextMenu.tsx
1425
+ import {
1426
+ isGroupMenuItemDescriptor,
1427
+ useThemeAttributes as useThemeAttributes3
1428
+ } from "@vuu-ui/vuu-utils";
1429
+ import { cloneElement, useCallback as useCallback9, useContext, useMemo as useMemo6 } from "react";
1430
+ import { jsx as jsx8 } from "react/jsx-runtime";
1431
+ var useContextMenu = (menuBuilder, menuActionHandler) => {
1432
+ const ctx = useContext(ContextMenuContext);
1433
+ const [themeClass, densityClass, dataMode] = useThemeAttributes3();
1434
+ const themeAttributes = useMemo6(
1435
+ () => ({
1436
+ themeClass,
1437
+ densityClass,
1438
+ dataMode
1439
+ }),
1440
+ [dataMode, densityClass, themeClass]
1441
+ );
1442
+ const buildMenuOptions = useCallback9(
1443
+ (menuBuilders, location, options) => {
1444
+ let results = [];
1445
+ for (const menuBuilder2 of menuBuilders) {
1446
+ results = results.concat(menuBuilder2(location, options));
1447
+ }
1448
+ return results;
1449
+ },
1450
+ []
1451
+ );
1452
+ const handleShowContextMenu = useCallback9(
1453
+ (e, location, { ContextMenuProps: ContextMenuProps2, contextMenu, ...options }) => {
1454
+ var _a, _b;
1455
+ (_a = e.stopPropagation) == null ? void 0 : _a.call(e);
1456
+ (_b = e.preventDefault) == null ? void 0 : _b.call(e);
1457
+ if (contextMenu) {
1458
+ return showContextMenuComponent(
1459
+ {
1460
+ x: e.clientX,
1461
+ y: e.clientY
1462
+ },
1463
+ contextMenu
1464
+ );
1465
+ }
1466
+ const menuBuilders = [];
1467
+ if (menuBuilder) {
1468
+ menuBuilders.push(menuBuilder);
1469
+ }
1470
+ if (ctx && Array.isArray(ctx == null ? void 0 : ctx.menuBuilders) && ctx.menuBuilders.length > 0) {
1471
+ menuBuilders.push(...ctx.menuBuilders);
1472
+ }
1473
+ if (menuBuilders.length > 0) {
1474
+ const menuItemDescriptors = buildMenuOptions(
1475
+ menuBuilders,
1476
+ location,
1477
+ options
1478
+ );
1479
+ const menuHandler = (action) => {
1480
+ if ((menuActionHandler == null ? void 0 : menuActionHandler(action)) === true) {
1481
+ return true;
1482
+ } else {
1483
+ return ctx == null ? void 0 : ctx.menuActionHandler(action);
1484
+ }
1485
+ };
1486
+ if (menuItemDescriptors.length && menuHandler) {
1487
+ showContextMenu(e, menuItemDescriptors, menuHandler, {
1488
+ PortalProps: {
1489
+ themeAttributes
1490
+ },
1491
+ ...ContextMenuProps2
1492
+ });
1493
+ }
1494
+ } else {
1495
+ console.warn(
1496
+ "useContextMenu, no menuBuilders configured. These should be supplied via the ContextMenuProvider(s)"
1497
+ );
1498
+ }
1499
+ },
1500
+ [buildMenuOptions, ctx, menuActionHandler, menuBuilder, themeAttributes]
1501
+ );
1502
+ const hideContextMenu = useCallback9(() => {
1503
+ console.log("hide context menu");
1504
+ }, []);
1505
+ return [handleShowContextMenu, hideContextMenu];
1506
+ };
1507
+ var NO_OPTIONS = {};
1508
+ var showContextMenuComponent = (position, contextMenu) => {
1509
+ PopupService.showPopup({
1510
+ focus: true,
1511
+ left: 0,
1512
+ top: 0,
1513
+ component: cloneElement(contextMenu, { position })
1514
+ });
1515
+ };
1516
+ var showContextMenu = (e, menuDescriptors, handleContextMenuAction, {
1517
+ position: positionProp,
1518
+ ...contextMenuProps
1519
+ } = NO_OPTIONS) => {
1520
+ const menuItems = (menuDescriptors2) => {
1521
+ const fromDescriptor = (menuItem, i) => isGroupMenuItemDescriptor(menuItem) ? /* @__PURE__ */ jsx8(MenuItemGroup, { label: menuItem.label, children: menuItem.children.map(fromDescriptor) }, i) : /* @__PURE__ */ jsx8(
1522
+ MenuItem,
1523
+ {
1524
+ action: menuItem.action,
1525
+ className: menuItem.className,
1526
+ "data-icon": menuItem.icon,
1527
+ options: menuItem.options,
1528
+ children: menuItem.label
1529
+ },
1530
+ i
1531
+ );
1532
+ return menuDescriptors2.map(fromDescriptor);
1533
+ };
1534
+ const handleClose = (reason) => {
1535
+ var _a;
1536
+ if (reasonIsMenuAction(reason)) {
1537
+ handleContextMenuAction(reason);
1538
+ PopupService.hidePopup();
1539
+ }
1540
+ (_a = contextMenuProps == null ? void 0 : contextMenuProps.onClose) == null ? void 0 : _a.call(contextMenuProps, reason);
1541
+ };
1542
+ const position = positionProp != null ? positionProp : {
1543
+ x: e.clientX,
1544
+ y: e.clientY
1545
+ };
1546
+ const component = /* @__PURE__ */ jsx8(
1547
+ ContextMenu,
1548
+ {
1549
+ ...contextMenuProps,
1550
+ onClose: handleClose,
1551
+ position,
1552
+ children: menuItems(menuDescriptors)
1553
+ }
1554
+ );
1555
+ PopupService.showPopup({ left: 0, top: 0, component, focus: true });
1556
+ };
1557
+
1558
+ // src/popup-menu/PopupMenu.tsx
1559
+ import { useId as useId3 } from "@vuu-ui/vuu-utils";
1560
+ import { Button as Button2 } from "@salt-ds/core";
1561
+ import cx7 from "clsx";
1562
+ import {
1563
+ useCallback as useCallback10,
1564
+ useRef as useRef8,
1565
+ useState as useState6
1566
+ } from "react";
1567
+ import { jsx as jsx9 } from "react/jsx-runtime";
1568
+ var classBase4 = "vuuPopupMenu";
1569
+ var getPosition2 = (element) => {
1570
+ if (element) {
1571
+ const { bottom, left } = element.getBoundingClientRect();
1572
+ return { x: left, y: bottom + 6 };
1573
+ }
1574
+ };
1575
+ var PopupMenu = ({
1576
+ className,
1577
+ label,
1578
+ icon = label ? "chevron-down" : "more-vert",
1579
+ id: idProp,
1580
+ menuActionHandler,
1581
+ menuBuilder,
1582
+ menuLocation = "header",
1583
+ menuOptions,
1584
+ onMenuClose,
1585
+ tabIndex = 0,
1586
+ ...htmlAttributes
1587
+ }) => {
1588
+ const rootRef = useRef8(null);
1589
+ const suppressShowMenuRef = useRef8(false);
1590
+ const [menuOpen, setMenuOpen] = useState6(false);
1591
+ const id = useId3(idProp);
1592
+ const [showContextMenu2] = useContextMenu(menuBuilder, menuActionHandler);
1593
+ const handleOpenMenu = useCallback10((el) => {
1594
+ console.log(`menu Open `, {
1595
+ el
1596
+ });
1597
+ }, []);
1598
+ const handleMenuClose = useCallback10(
1599
+ (reason) => {
1600
+ setMenuOpen(false);
1601
+ if (reasonIsClickAway(reason)) {
1602
+ const target = reason.mouseEvt.target;
1603
+ if (target === rootRef.current) {
1604
+ suppressShowMenuRef.current = true;
1605
+ }
1606
+ } else {
1607
+ requestAnimationFrame(() => {
1608
+ var _a;
1609
+ onMenuClose == null ? void 0 : onMenuClose(reason);
1610
+ if (tabIndex !== -1) {
1611
+ (_a = rootRef.current) == null ? void 0 : _a.focus();
1612
+ }
1613
+ });
1614
+ }
1615
+ },
1616
+ [onMenuClose, tabIndex]
1617
+ );
1618
+ const showMenu = useCallback10(
1619
+ (e) => {
1620
+ if (suppressShowMenuRef.current) {
1621
+ suppressShowMenuRef.current = false;
1622
+ } else {
1623
+ setMenuOpen(true);
1624
+ showContextMenu2(e, menuLocation, {
1625
+ ContextMenuProps: {
1626
+ id: `${id}-menu`,
1627
+ onClose: handleMenuClose,
1628
+ openMenu: handleOpenMenu,
1629
+ position: getPosition2(rootRef.current)
1630
+ },
1631
+ ...menuOptions
1632
+ });
1633
+ }
1634
+ },
1635
+ [
1636
+ handleMenuClose,
1637
+ handleOpenMenu,
1638
+ id,
1639
+ menuLocation,
1640
+ menuOptions,
1641
+ showContextMenu2
1642
+ ]
1643
+ );
1644
+ return /* @__PURE__ */ jsx9(
1645
+ Button2,
1646
+ {
1647
+ ...htmlAttributes,
1648
+ "aria-controls": `${id}-menu-root`,
1649
+ "aria-expanded": menuOpen,
1650
+ "aria-haspopup": "menu",
1651
+ className: cx7(classBase4, className, {
1652
+ [`${classBase4}-withCaption`]: label !== void 0,
1653
+ [`${classBase4}-open`]: menuOpen
1654
+ }),
1655
+ "data-icon": icon,
1656
+ id,
1657
+ onClick: showMenu,
1658
+ ref: rootRef,
1659
+ tabIndex,
1660
+ variant: "secondary",
1661
+ children: label
1662
+ }
1663
+ );
1664
+ };
1665
+
1666
+ // src/prompt/Prompt.tsx
1667
+ import { useThemeAttributes as useThemeAttributes4 } from "@vuu-ui/vuu-utils";
1668
+ import { Button as Button3 } from "@salt-ds/core";
1669
+ import cx8 from "clsx";
1670
+ import { useLayoutEffect as useLayoutEffect5, useRef as useRef9 } from "react";
1671
+ import { jsx as jsx10, jsxs as jsxs3 } from "react/jsx-runtime";
1672
+ var classBase5 = "vuuPrompt";
1673
+ var AnchorBody2 = { current: document.body };
1674
+ var EMPTY_PROPS2 = {};
1675
+ var Prompt = ({
1676
+ PopupProps = EMPTY_PROPS2,
1677
+ cancelButtonLabel = "Cancel",
1678
+ confirmButtonLabel = "Confirm",
1679
+ icon,
1680
+ onCancel,
1681
+ onConfirm,
1682
+ style,
1683
+ text,
1684
+ title,
1685
+ variant = "info",
1686
+ ...htmlAttributes
1687
+ }) => {
1688
+ const {
1689
+ anchorElement = AnchorBody2,
1690
+ offsetLeft = 0,
1691
+ offsetTop = 0,
1692
+ placement = "below"
1693
+ } = PopupProps;
1694
+ const [themeClass, densityClass, dataMode] = useThemeAttributes4();
1695
+ const { position } = useAnchoredPosition({
1696
+ anchorElement,
1697
+ offsetLeft,
1698
+ offsetTop,
1699
+ placement
1700
+ });
1701
+ const rootRef = useRef9(null);
1702
+ const confirmRef = useRef9(null);
1703
+ useLayoutEffect5(() => {
1704
+ if (rootRef.current) {
1705
+ rootRef.current.showModal();
1706
+ if (confirmRef.current) {
1707
+ confirmRef.current.focus();
1708
+ }
1709
+ if (placement.endsWith("center")) {
1710
+ const { width } = rootRef.current.getBoundingClientRect();
1711
+ rootRef.current.style.marginLeft = `-${width / 2}px`;
1712
+ }
1713
+ }
1714
+ }, [placement]);
1715
+ return /* @__PURE__ */ jsx10(
1716
+ "dialog",
1717
+ {
1718
+ ...htmlAttributes,
1719
+ className: cx8(classBase5, `${classBase5}-${variant}`, themeClass),
1720
+ "data-mode": dataMode,
1721
+ ref: rootRef,
1722
+ style: { ...style, ...position },
1723
+ children: /* @__PURE__ */ jsxs3("form", { className: `${classBase5}-form`, children: [
1724
+ /* @__PURE__ */ jsx10("div", { className: `${classBase5}-header`, "data-icon": icon, children: title }),
1725
+ /* @__PURE__ */ jsx10("div", { className: `${classBase5}-text`, children: text }),
1726
+ /* @__PURE__ */ jsxs3("div", { className: `${classBase5}-buttonBar`, children: [
1727
+ /* @__PURE__ */ jsx10(Button3, { onClick: onCancel, variant: "secondary", children: cancelButtonLabel }),
1728
+ /* @__PURE__ */ jsx10(Button3, { onClick: onConfirm, ref: confirmRef, value: "default", children: confirmButtonLabel })
1729
+ ] })
1730
+ ] })
1731
+ }
1732
+ );
1733
+ };
1734
+
1735
+ // src/tooltip/useAnchoredPosition.ts
1736
+ import { useLayoutEffect as useLayoutEffect6, useState as useState7 } from "react";
1737
+ var getPositionRelativeToAnchor2 = (anchorElement, placement, offsetLeft, offsetTop) => {
1738
+ const { bottom, height, left, right, top, width } = anchorElement.getBoundingClientRect();
1739
+ const midX = left + width / 2;
1740
+ const midY = top + height / 2;
1741
+ switch (placement) {
1742
+ case "above":
1743
+ return { left: midX + offsetLeft, top: top + offsetTop };
1744
+ case "below":
1745
+ return { left: midX + offsetLeft, top: bottom + offsetTop };
1746
+ case "right":
1747
+ return { left: right + offsetLeft, top: midY + offsetLeft };
1748
+ case "left":
1749
+ return { left: left + offsetLeft, top: midY + offsetLeft };
1750
+ default:
1751
+ throw Error(
1752
+ "Tooltip getPositionRelativeToAnchor only supported placement values are left, right, below and right"
1753
+ );
1754
+ }
1755
+ };
1756
+ var useAnchoredPosition2 = ({
1757
+ anchorElement,
1758
+ offsetLeft = 0,
1759
+ offsetTop = 0,
1760
+ placement
1761
+ }) => {
1762
+ const [position, setPosition] = useState7();
1763
+ useLayoutEffect6(() => {
1764
+ if (anchorElement.current) {
1765
+ const position2 = getPositionRelativeToAnchor2(
1766
+ anchorElement.current,
1767
+ placement,
1768
+ offsetLeft,
1769
+ offsetTop
1770
+ );
1771
+ setPosition(position2);
1772
+ }
1773
+ }, [anchorElement, offsetLeft, offsetTop, placement]);
1774
+ return position;
1775
+ };
1776
+
1777
+ // src/tooltip/Tooltip.tsx
1778
+ import cx9 from "clsx";
1779
+ import { jsx as jsx11 } from "react/jsx-runtime";
1780
+ var classBase6 = "vuuTooltip";
1781
+ var Tooltip = ({
1782
+ anchorElement,
1783
+ children,
1784
+ id,
1785
+ onMouseEnter,
1786
+ onMouseLeave,
1787
+ placement,
1788
+ status,
1789
+ style: styleProp
1790
+ }) => {
1791
+ const position = useAnchoredPosition2({ anchorElement, placement });
1792
+ if (position === void 0) {
1793
+ return null;
1794
+ }
1795
+ return /* @__PURE__ */ jsx11(Portal, { children: /* @__PURE__ */ jsx11(
1796
+ "div",
1797
+ {
1798
+ className: cx9(classBase6, {
1799
+ [`${classBase6}-error`]: status === "error"
1800
+ }),
1801
+ "data-align": placement,
1802
+ id,
1803
+ style: { ...styleProp, ...position },
1804
+ children: /* @__PURE__ */ jsx11(
1805
+ "span",
1806
+ {
1807
+ className: `${classBase6}-content`,
1808
+ onMouseEnter,
1809
+ onMouseLeave,
1810
+ children
1811
+ }
1812
+ )
1813
+ }
1814
+ ) });
1815
+ };
1816
+
1817
+ // src/tooltip/useTooltip.ts
1818
+ import { useId as useId4 } from "@vuu-ui/vuu-utils";
1819
+ import { useCallback as useCallback11, useRef as useRef10, useState as useState8 } from "react";
1820
+ var useTooltip = ({
1821
+ id: idProp,
1822
+ placement = "right",
1823
+ tooltipContent
1824
+ }) => {
1825
+ const hideTooltipRef = useRef10();
1826
+ const anchorElementRef = useRef10(null);
1827
+ const mouseEnterTimerRef = useRef10();
1828
+ const mouseLeaveTimerRef = useRef10();
1829
+ const [tooltipProps, setTooltipProps] = useState8();
1830
+ const id = useId4(idProp);
1831
+ const escapeListener = useCallback11((evt) => {
1832
+ var _a;
1833
+ if (evt.key === "Escape") {
1834
+ (_a = hideTooltipRef.current) == null ? void 0 : _a.call(hideTooltipRef);
1835
+ }
1836
+ }, []);
1837
+ hideTooltipRef.current = useCallback11(() => {
1838
+ setTooltipProps(void 0);
1839
+ document.removeEventListener("keydown", escapeListener);
1840
+ }, [escapeListener]);
1841
+ const handleMouseEnterTooltip = useCallback11(() => {
1842
+ window.clearTimeout(mouseLeaveTimerRef.current);
1843
+ }, []);
1844
+ const handleMouseLeaveTooltip = useCallback11(() => {
1845
+ var _a;
1846
+ (_a = hideTooltipRef.current) == null ? void 0 : _a.call(hideTooltipRef);
1847
+ }, []);
1848
+ const showTooltip = useCallback11(() => {
1849
+ const { current: anchorEl } = anchorElementRef;
1850
+ if (anchorEl) {
1851
+ setTooltipProps({
1852
+ anchorElement: anchorElementRef,
1853
+ children: tooltipContent,
1854
+ id: `${id}-tooltip`,
1855
+ onMouseEnter: handleMouseEnterTooltip,
1856
+ onMouseLeave: handleMouseLeaveTooltip,
1857
+ placement
1858
+ });
1859
+ document.addEventListener("keydown", escapeListener);
1860
+ }
1861
+ mouseEnterTimerRef.current = void 0;
1862
+ }, [
1863
+ escapeListener,
1864
+ handleMouseEnterTooltip,
1865
+ handleMouseLeaveTooltip,
1866
+ id,
1867
+ placement,
1868
+ tooltipContent
1869
+ ]);
1870
+ const handleMouseEnter = useCallback11(
1871
+ (evt) => {
1872
+ const el = evt.target;
1873
+ if (el) {
1874
+ anchorElementRef.current = el;
1875
+ mouseEnterTimerRef.current = window.setTimeout(showTooltip, 800);
1876
+ }
1877
+ },
1878
+ [showTooltip]
1879
+ );
1880
+ const handleMouseLeave = useCallback11(() => {
1881
+ if (anchorElementRef.current)
1882
+ if (mouseEnterTimerRef.current) {
1883
+ window.clearTimeout(mouseEnterTimerRef.current);
1884
+ mouseEnterTimerRef.current = void 0;
1885
+ } else {
1886
+ if (hideTooltipRef.current) {
1887
+ mouseLeaveTimerRef.current = window.setTimeout(
1888
+ hideTooltipRef.current,
1889
+ 200
1890
+ );
1891
+ }
1892
+ }
1893
+ }, []);
1894
+ const anchorProps = {
1895
+ "aria-describedby": `${id}-tooltip`,
1896
+ onMouseEnter: handleMouseEnter,
1897
+ onMouseLeave: handleMouseLeave
1898
+ };
1899
+ return {
1900
+ anchorProps,
1901
+ tooltipProps
1902
+ };
1903
+ };
1904
+
1905
+ // src/notifications/NotificationsProvider.tsx
1906
+ import React4, { useState as useState9, useContext as useContext2, useCallback as useCallback12, useEffect } from "react";
1907
+ import classNames from "clsx";
1908
+ import { getUniqueId } from "@vuu-ui/vuu-utils";
1909
+ import { jsx as jsx12, jsxs as jsxs4 } from "react/jsx-runtime";
1910
+ var toastOffsetTop = 60;
1911
+ var toastDisplayDuration = 6e3;
1912
+ var horizontalTransitionDuration = 1e3;
1913
+ var verticalTransitionDuration = 300;
1914
+ var toastHeight = 56;
1915
+ var toastWidth = 300;
1916
+ var toastContainerContentGap = 10;
1917
+ var toastContainerLeftPadding = 10;
1918
+ var toastContainerRightPadding = 50;
1919
+ var classBase7 = "vuuToastNotifications";
1920
+ var NotificationLevel = /* @__PURE__ */ ((NotificationLevel2) => {
1921
+ NotificationLevel2["Info"] = "info";
1922
+ NotificationLevel2["Success"] = "success";
1923
+ NotificationLevel2["Warning"] = "warning";
1924
+ NotificationLevel2["Error"] = "error";
1925
+ return NotificationLevel2;
1926
+ })(NotificationLevel || {});
1927
+ var NotificationsContext = React4.createContext({
1928
+ notify: () => "have you forgotten to provide a NotificationProvider?"
1929
+ });
1930
+ var NotificationsProvider = (props) => {
1931
+ const [notifications, setNotifications] = useState9([]);
1932
+ const notify = useCallback12((notification) => {
1933
+ const newNotification = { ...notification, id: getUniqueId() };
1934
+ setNotifications((prev) => [...prev, newNotification]);
1935
+ setTimeout(() => {
1936
+ setNotifications((prev) => prev.filter((n) => n !== newNotification));
1937
+ }, toastDisplayDuration + horizontalTransitionDuration * 2);
1938
+ }, []);
1939
+ return /* @__PURE__ */ jsxs4(NotificationsContext.Provider, { value: { notify }, children: [
1940
+ /* @__PURE__ */ jsx12(
1941
+ "div",
1942
+ {
1943
+ style: {
1944
+ width: toastWidth + toastContainerRightPadding + toastContainerLeftPadding
1945
+ },
1946
+ children: notifications.map((notification, i) => /* @__PURE__ */ jsx12(
1947
+ ToastNotification,
1948
+ {
1949
+ top: toastOffsetTop + (toastHeight + toastContainerContentGap) * i,
1950
+ notification
1951
+ },
1952
+ notification.id
1953
+ ))
1954
+ }
1955
+ ),
1956
+ props.children
1957
+ ] });
1958
+ };
1959
+ var useNotifications = () => useContext2(NotificationsContext);
1960
+ var ToastNotification = (props) => {
1961
+ const { top, notification, animated = true } = props;
1962
+ const [right, setRight] = useState9(-toastWidth - toastContainerRightPadding);
1963
+ useEffect(() => {
1964
+ setTimeout(() => setRight(toastContainerRightPadding));
1965
+ if (animated) {
1966
+ setTimeout(
1967
+ () => setRight(-toastWidth - toastContainerRightPadding),
1968
+ toastDisplayDuration + horizontalTransitionDuration
1969
+ );
1970
+ }
1971
+ }, [animated]);
1972
+ return /* @__PURE__ */ jsx12(Portal, { children: /* @__PURE__ */ jsxs4(
1973
+ "div",
1974
+ {
1975
+ className: classNames(`${classBase7}-toast`, notification.type),
1976
+ style: {
1977
+ height: toastHeight,
1978
+ right,
1979
+ width: toastWidth,
1980
+ top,
1981
+ transition: animated ? `right ${horizontalTransitionDuration}ms, top ${verticalTransitionDuration}ms ` : "none"
1982
+ },
1983
+ children: [
1984
+ /* @__PURE__ */ jsx12(
1985
+ "div",
1986
+ {
1987
+ className: classNames(
1988
+ `${classBase7}-toastIcon`,
1989
+ `${notification.type}-icon`
1990
+ )
1991
+ }
1992
+ ),
1993
+ /* @__PURE__ */ jsxs4("div", { className: `${classBase7}-toastContent`, children: [
1994
+ /* @__PURE__ */ jsx12("strong", { className: `${classBase7}-toastHeader`, children: notification.header }),
1995
+ /* @__PURE__ */ jsx12("div", { children: notification.body })
1996
+ ] })
1997
+ ]
1998
+ }
1999
+ ) });
2000
+ };
2001
+ export {
2002
+ ContextMenu,
2003
+ ContextMenuContext,
2004
+ ContextMenuProvider,
2005
+ Dialog,
2006
+ DialogHeader,
2007
+ DialogService,
2008
+ MenuItem,
2009
+ MenuItemGroup,
2010
+ MenuList,
2011
+ NotificationLevel,
2012
+ NotificationsContext,
2013
+ NotificationsProvider,
2014
+ PopupComponent2 as PopupComponent,
2015
+ PopupMenu,
2016
+ PopupService,
2017
+ Portal,
2018
+ Prompt,
2019
+ Separator,
2020
+ ToastNotification,
2021
+ Tooltip,
2022
+ createContainer,
2023
+ isMenuItemLabel,
2024
+ reasonIsClickAway,
2025
+ reasonIsMenuAction,
2026
+ renderPortal,
2027
+ useAnchoredPosition,
2028
+ useContextMenu,
2029
+ useDialog,
2030
+ useNotifications,
2031
+ useTooltip
2032
+ };
2
2033
  //# sourceMappingURL=index.js.map