UNPKG

@progress/kendo-react-popup

Version:

React Popup positions a piece of content next to a specific anchor component. KendoReact Popup package

9 lines (8 loc) 8.77 kB
/** * @license *------------------------------------------------------------------------------------------- * Copyright © 2026 Progress Software Corporation. All rights reserved. * Licensed under commercial license. See LICENSE.md in the package root for more information *------------------------------------------------------------------------------------------- */ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const De=require("react"),Re=require("react-dom"),t=require("prop-types"),xe=require("./animation.js"),p=require("@progress/kendo-react-common"),n=require("@progress/kendo-popup-common"),ie=require("./util.js");function se(r){const c=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(r){for(const g in r)if(g!=="default"){const m=Object.getOwnPropertyDescriptor(r,g);Object.defineProperty(c,g,m.get?m:{enumerable:!0,get:()=>r[g]})}}return c.default=r,Object.freeze(c)}const l=se(De),Te=se(Re),le=l.createContext(r=>r),Ne=12e3,ze=1;function b(r,c){if(r===c)return!0;if(!!r!=!!c)return!1;const g=Object.getOwnPropertyNames(r),m=Object.getOwnPropertyNames(c);if(g.length!==m.length)return!1;for(let P=0;P<g.length;P++){const y=g[P];if(r[y]!==c[y])return!1}return!0}const Se={left:-1e3,top:0},K=l.forwardRef((r,c)=>{const m=l.useContext(le).call(void 0,r),{collision:P=h.collision,anchorAlign:y=h.anchorAlign,popupAlign:v=h.popupAlign,offset:T=h.offset,animate:N=h.animate,show:C=h.show,margin:H=h.margin,positionMode:B=h.positionMode,appendTo:A,contentKey:z,anchor:f,scale:ce,role:ae,ariaLabelledBy:ue,ariaDescribedBy:pe,onKeyDown:fe,onPosition:V,onOpen:W,onMouseDownOutside:X,onClose:G,className:S,popupClass:M,id:de,style:L,children:me}=m,he=p.useUnstyled(),j=m.unstyled||he,x=j&&j.uPopup,[o,I]=l.useState({current:"hidden",previous:"hidden",props:{}}),_=N&&typeof N!="object"?re:{...re,...N},ge=e=>{window==null||window.addEventListener("mousedown",Q),k(e),J(e.firstChild,"enter",Pe),I({...i,current:"shown",previous:i.current})},k=e=>{const{width:s,height:d}=e.style;if(e.offsetWidth===0&&e.offsetHeight===0)return;const u=getComputedStyle(e),O=parseFloat(u.width),U=O>0?e.getBoundingClientRect().width/O:1,R=(ce||1)*U;e.style.width=u.width,e.style.height=u.height;const oe=n.alignElement({anchor:f,anchorAlign:y,element:e,elementAlign:v,offset:T,margin:H,positionMode:B,scale:R}),w=n.positionElement({anchor:f,anchorAlign:y,element:e,elementAlign:v,collisions:P,currentLocation:oe,margin:H,scale:R});if(e.style.top=w.offset.top+"px",e.style.left=w.offset.left+"px",e.style.width=s,e.style.height=d,ee.current={fit:w.fit,fitted:w.fitted,flip:w.flip,flipped:w.flipped},V){const Ee={target:D.current,flipped:w.flipped,fitted:w.fitted};V.call(void 0,Ee)}},Pe=()=>{const e=a.current;e&&(C&&e.classList.add(...p.classNames(p.uPopup.animationContainerShown({c:x})).split(" ").filter(s=>s)),ve(e),W&&W.call(void 0,{target:D.current}))},J=(e,s,d)=>{var O;if(!v)return;let u=_.direction;if(_.type==="slide"){const{horizontal:U,vertical:R}=v;typeof r.animate=="object"&&r.animate.direction?u=_.direction:U==="left"&&R==="center"?u="right":U==="right"&&R==="center"?u="left":R==="top"?u="down":u="up",(O=ee.current)!=null&&O.flipped&&(u={down:"up",up:"down",left:"right",right:"left"}[u])}xe.animations(e,we()[s],s,d,x,_.type,u)},Q=e=>{var u;const s=((u=e==null?void 0:e.target)==null?void 0:u.closest(".k-animation-container"))===null,d=(f==null?void 0:f.contains(e==null?void 0:e.target))||!1;if(s){if(X){const O={target:D.current,event:e,state:o,isAnchorClicked:d};X.call(void 0,O)}window==null||window.removeEventListener("mousedown",Q)}},ye=e=>{if(!C){const s=j&&j.uPopup;e.classList.remove(...p.classNames(p.uPopup.animationContainerShown({c:s})).split(" ").filter(d=>d))}Z()},Y=()=>{i.current==="hiding"&&(i.previous==="shown"||i.previous==="reposition")&&I({...i,current:"hidden",previous:i.current}),G&&G.call(void 0,{target:D.current})},we=()=>{const e=N;let s=0,d=0;return e&&(e===!0?s=d=300:(s=e.openDuration||0,d=e.closeDuration||0)),{enter:s,exit:d}},ve=e=>{Z(),E.current=n.domUtils.scrollableParents(f||e),E.current&&E.current.map(s=>s.addEventListener("scroll",q)),window.addEventListener("resize",q)},Z=()=>{E.current&&(E.current.map(e=>e.removeEventListener("scroll",q)),E.current=void 0),window.removeEventListener("resize",q)},q=ie.throttle(()=>{a.current&&i.current!=="hidden"&&i.current!=="hiding"&&I({...o,current:"reposition",previous:i.current})},ie.FRAME_DURATION),Oe=()=>$?$+ze:Ne,$=p.useZIndexContext()||0,ee=l.useRef(void 0),E=l.useRef(void 0),a=l.useRef(null),D=l.useRef(null),te=l.useRef({}),F=l.useRef(!1);l.useImperativeHandle(D,()=>({element:a.current,setPosition:k,props:r})),l.useImperativeHandle(c,()=>D.current);const i=l.useMemo(()=>{const e={...o,props:{show:C,anchor:f,anchorAlign:y,appendTo:A,collision:P,popupAlign:v,className:S,popupClass:M,style:L,offset:T,contentKey:z}};return C?o.current==="hidden"||o.current==="hiding"?{...e,current:"showing",previous:o.current}:o.current==="showing"?{...e,current:"shown",previous:o.current}:o.current==="shown"&&(!b(T,o.props.offset)||!b(y,o.props.anchorAlign)||!b(A,o.props.appendTo)||!b(P,o.props.collision)||!b(v,o.props.popupAlign)||!b(L,o.props.style)||f!==o.props.anchor||M!==o.props.popupClass||S!==o.props.className)?{...e,current:"reposition",previous:o.current}:e:o.current==="hiding"||o.current==="hidden"?{...e,current:"hidden",previous:o.current}:{...e,current:"hiding",previous:o.current}},[C,f,y,A,P,v,S,M,L,T,z,o]);l.useEffect(()=>{i.current==="hiding"&&!F.current&&a.current?(F.current=!0,ye(a.current),J(a.current.firstChild,"exit",Y)):(F.current=!1,i.current==="showing"&&a.current&&!a.current.classList.contains("k-animation-container-shown")?ge(a.current):i.current==="hiding"&&a.current?Y():i.current==="reposition"&&i.previous==="shown"?setTimeout(()=>{I({...i,current:"shown",previous:i.current})},0):i.current==="shown"&&te.current.contentKey!==z&&a.current&&k(a.current)),i.current==="reposition"&&(i.previous==="shown"||i.previous==="showing")&&a.current&&k(a.current),te.current={contentKey:z}},[i]),l.useEffect(()=>()=>{Z()},[]);const ne=A||(p.canUseDOM?f&&f.ownerDocument?f.ownerDocument.body:document.body:void 0),be=Object.assign({},{position:B,top:0,left:-1e4},L||{}),Ce=m.offset!==void 0&&!b(m.offset,h.offset)||m.margin!==void 0,Ae=i.current==="hiding";if((C||Ae)&&ne){const e=Oe(),s=l.createElement(p.ZIndexContext.Provider,{value:e},l.createElement("div",{onKeyDown:fe,className:p.classNames(p.uPopup.animationContainer({c:x}),S),id:de,ref:a,style:{zIndex:e,...be}},l.createElement("div",{className:p.classNames(p.uPopup.animationChild({c:x})),style:{transitionDelay:"0ms"}},l.createElement("div",{role:ae,"aria-labelledby":ue,"aria-describedby":pe,className:p.classNames(p.uPopup.popup({c:x}),M),style:Ce?{margin:0}:void 0},me))));return A!==null?Te.createPortal(s,ne):s}return null}),h={collision:{horizontal:n.Collision.fit,vertical:n.Collision.flip},anchorAlign:{horizontal:n.AlignPoint.left,vertical:n.AlignPoint.bottom},popupAlign:{horizontal:n.AlignPoint.left,vertical:n.AlignPoint.top},offset:Se,animate:!0,show:!1,margin:{horizontal:0,vertical:0},positionMode:"absolute"},re={type:"slide",direction:"down"};K.displayName="Popup";K.propTypes={anchor:function(r){const c=r.anchor;return c&&typeof c.nodeType!="number"?new Error("Invalid prop `anchor` supplied to `Kendo React Popup`. Validation failed."):null},appendTo:function(r){const c=r.appendTo;return c&&typeof c.nodeType!="number"?new Error("Invalid prop `appendTo` supplied to `Kendo React Popup`. Validation failed."):null},className:t.oneOfType([t.string,t.arrayOf(t.string.isRequired)]),id:t.string,popupClass:t.oneOfType([t.string,t.arrayOf(t.string),t.object]),collision:t.shape({horizontal:t.oneOf([n.Collision.fit,n.Collision.flip,n.Collision.none]),vertical:t.oneOf([n.Collision.fit,n.Collision.flip,n.Collision.none])}),anchorAlign:t.shape({horizontal:t.oneOf([n.AlignPoint.left,n.AlignPoint.center,n.AlignPoint.right]),vertical:t.oneOf([n.AlignPoint.top,n.AlignPoint.center,n.AlignPoint.bottom])}),popupAlign:t.shape({horizontal:t.oneOf([n.AlignPoint.left,n.AlignPoint.center,n.AlignPoint.right]),vertical:t.oneOf([n.AlignPoint.top,n.AlignPoint.center,n.AlignPoint.bottom])}),offset:t.shape({left:t.number,top:t.number}),children:t.oneOfType([t.element,t.node]),show:t.bool,animate:t.oneOfType([t.bool,t.shape({type:t.oneOf(["slide","zoom","push","expand","fade"]),openDuration:t.number,closeDuration:t.number})]),margin:t.shape({horizontal:t.number,vertical:t.number}),positionMode:t.oneOf(["fixed","absolute"]),scale:t.number,style:t.object,onClose:t.func,onPosition:t.func,onOpen:t.func,onKeyDown:t.func,onMouseDownOutside:t.func};exports.Popup=K;exports.PopupPropsContext=le;exports.popupDefaultProps=h;