@progress/kendo-react-popup
Version:
React Popup positions a piece of content next to a specific anchor component. KendoReact Popup package
16 lines (15 loc) • 10.5 kB
JavaScript
/**
* @license
*-------------------------------------------------------------------------------------------
* Copyright © 2026 Progress Software Corporation. All rights reserved.
* Licensed under commercial license. See LICENSE.md in the package root for more information
*-------------------------------------------------------------------------------------------
*/
/**
* @license
*-------------------------------------------------------------------------------------------
* Copyright © 2026 Progress Software Corporation. All rights reserved.
* Licensed under commercial license. See LICENSE.md in the package root for more information
*-------------------------------------------------------------------------------------------
*/
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("react"),require("react-dom"),require("prop-types"),require("@progress/kendo-react-common"),require("@progress/kendo-popup-common")):"function"==typeof define&&define.amd?define(["exports","react","react-dom","prop-types","@progress/kendo-react-common","@progress/kendo-popup-common"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self).KendoReactPopup={},e.React,e.ReactDOM,e.PropTypes,e.KendoReactCommon,e.KendoPopupCommon)}(this,function(e,n,t,o,i,r){"use strict";function s(e){var n=Object.create(null);return e&&Object.keys(e).forEach(function(t){if("default"!==t){var o=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(n,t,o.get?o:{enumerable:!0,get:function(){return e[t]}})}}),n.default=e,Object.freeze(n)}var l=s(n),c=s(t);const a=(e,n,t)=>{e.style.transitionDuration="",n&&e.classList.remove(...n.split(" ")),t&&e.classList.remove(...t.split(" "))},u="undefined"!=typeof window&&/Firefox/.test(window.navigator.userAgent),p=(e,n,t,o,r,s,l)=>{if(0===n)return o();const c=`${s}Active`,p=i.classNames(i.uPopup[s]({direction:l,type:t,c:r})),d=i.classNames(i.uPopup[c]({direction:l,type:t,c:r})),f=n=>{n.target===e&&(e.removeEventListener("transitionend",f),a(e,p,d),e.style.display="exit"===t?"none":"",o())};e.addEventListener("transitionend",f);const m=e.ownerDocument;if(!m)return;const h=m.defaultView;if(!h)return;const g=()=>{a(e,p,d),"enter"===t&&(e.style.display=""),p&&e.classList.add(...p.split(" ").filter(e=>e)),h.requestAnimationFrame(()=>{e.style.transitionDuration=n+"ms",e.classList.add(...d.split(" ").filter(e=>e))})};u?h.requestAnimationFrame(g):g()},d=l.createContext(e=>e);function f(e,n){if(e===n)return!0;if(!!e!=!!n)return!1;const t=Object.getOwnPropertyNames(e),o=Object.getOwnPropertyNames(n);if(t.length!==o.length)return!1;for(let o=0;o<t.length;o++){const i=t[o];if(e[i]!==n[i])return!1}return!0}const m=l.forwardRef((e,n)=>{const t=l.useContext(d).call(void 0,e),{collision:o=h.collision,anchorAlign:s=h.anchorAlign,popupAlign:a=h.popupAlign,offset:u=h.offset,animate:m=h.animate,show:v=h.show,margin:y=h.margin,positionMode:w=h.positionMode,appendTo:P,contentKey:b,anchor:A,scale:O,role:C,ariaLabelledBy:D,ariaDescribedBy:T,onKeyDown:x,onPosition:E,onOpen:L,onMouseDownOutside:N,onClose:R,className:z,popupClass:j,id:K,style:M,children:k}=t,q=i.useUnstyled(),I=t.unstyled||q,F=I&&I.uPopup,[S,B]=l.useState({current:"hidden",previous:"hidden",props:{}}),H=m&&"object"!=typeof m?g:{...g,...m},U=e=>{const{width:n,height:t}=e.style;if(0===e.offsetWidth&&0===e.offsetHeight)return;const i=getComputedStyle(e),l=parseFloat(i.width),c=l>0?e.getBoundingClientRect().width/l:1,p=(O||1)*c;e.style.width=i.width,e.style.height=i.height;const d=r.alignElement({anchor:A,anchorAlign:s,element:e,elementAlign:a,offset:u,margin:y,positionMode:w,scale:p}),f=r.positionElement({anchor:A,anchorAlign:s,element:e,elementAlign:a,collisions:o,currentLocation:d,margin:y,scale:p});if(e.style.top=f.offset.top+"px",e.style.left=f.offset.left+"px",e.style.width=n,e.style.height=t,_.current={fit:f.fit,fitted:f.fitted,flip:f.flip,flipped:f.flipped},E){const e={target:te.current,flipped:f.flipped,fitted:f.fitted};E.call(void 0,e)}},V=()=>{const e=ne.current;e&&(v&&e.classList.add(...i.classNames(i.uPopup.animationContainerShown({c:F})).split(" ").filter(e=>e)),J(e),L&&L.call(void 0,{target:te.current}))},Z=(n,t,o)=>{var i;if(!a)return;let r=H.direction;if("slide"===H.type){const{horizontal:n,vertical:t}=a;r="object"==typeof e.animate&&e.animate.direction?H.direction:"left"===n&&"center"===t?"right":"right"===n&&"center"===t?"left":"top"===t?"down":"up",null!=(i=_.current)&&i.flipped&&(r={down:"up",up:"down",left:"right",right:"left"}[r])}p(n,G()[t],t,o,F,H.type,r)},W=e=>{var n;const t=null===(null==(n=null==e?void 0:e.target)?void 0:n.closest(".k-animation-container")),o=(null==A?void 0:A.contains(null==e?void 0:e.target))||!1;if(t){if(N){const n={target:te.current,event:e,state:S,isAnchorClicked:o};N.call(void 0,n)}null==window||window.removeEventListener("mousedown",W)}},$=()=>{"hiding"===re.current&&("shown"===re.previous||"reposition"===re.previous)&&B({...re,current:"hidden",previous:re.current}),R&&R.call(void 0,{target:te.current})},G=()=>{const e=m;let n=0,t=0;return e&&(!0===e?n=t=300:(n=e.openDuration||0,t=e.closeDuration||0)),{enter:n,exit:t}},J=e=>{Q(),ee.current=r.domUtils.scrollableParents(A||e),ee.current&&ee.current.map(e=>e.addEventListener("scroll",X)),window.addEventListener("resize",X)},Q=()=>{ee.current&&(ee.current.map(e=>e.removeEventListener("scroll",X)),ee.current=void 0),window.removeEventListener("resize",X)},X=function(e,n,t={}){let o,i,r,s,l=0;t=t||{};const c=function(){l=!1===t.leading?0:(new Date).getTime(),o=void 0,s=e.apply(i,r),o||(i=r=null)};return function(){const a=(new Date).getTime();!l&&!1===t.leading&&(l=a);const u=n-(a-l);return i=void 0,r=arguments,u<=0||u>n?(o&&(clearTimeout(o),o=void 0),l=a,s=e.apply(i,r),o||(i=r=null)):!o&&!1!==t.trailing&&(o=window.setTimeout(c,u)),s}}(()=>{ne.current&&"hidden"!==re.current&&"hiding"!==re.current&&B({...S,current:"reposition",previous:re.current})},16.666666666666668),Y=i.useZIndexContext()||0,_=l.useRef(void 0),ee=l.useRef(void 0),ne=l.useRef(null),te=l.useRef(null),oe=l.useRef({}),ie=l.useRef(!1);l.useImperativeHandle(te,()=>({element:ne.current,setPosition:U,props:e})),l.useImperativeHandle(n,()=>te.current);const re=l.useMemo(()=>{const e={...S,props:{show:v,anchor:A,anchorAlign:s,appendTo:P,collision:o,popupAlign:a,className:z,popupClass:j,style:M,offset:u,contentKey:b}};return v?"hidden"===S.current||"hiding"===S.current?{...e,current:"showing",previous:S.current}:"showing"===S.current?{...e,current:"shown",previous:S.current}:"shown"!==S.current||f(u,S.props.offset)&&f(s,S.props.anchorAlign)&&f(P,S.props.appendTo)&&f(o,S.props.collision)&&f(a,S.props.popupAlign)&&f(M,S.props.style)&&A===S.props.anchor&&j===S.props.popupClass&&z===S.props.className?e:{...e,current:"reposition",previous:S.current}:"hiding"===S.current||"hidden"===S.current?{...e,current:"hidden",previous:S.current}:{...e,current:"hiding",previous:S.current}},[v,A,s,P,o,a,z,j,M,u,b,S]);l.useEffect(()=>{var e;"hiding"===re.current&&!ie.current&&ne.current?(ie.current=!0,(e=>{if(!v){const n=I&&I.uPopup;e.classList.remove(...i.classNames(i.uPopup.animationContainerShown({c:n})).split(" ").filter(e=>e))}Q()})(ne.current),Z(ne.current.firstChild,"exit",$)):(ie.current=!1,"showing"===re.current&&ne.current&&!ne.current.classList.contains("k-animation-container-shown")?(e=ne.current,null==window||window.addEventListener("mousedown",W),U(e),Z(e.firstChild,"enter",V),B({...re,current:"shown",previous:re.current})):"hiding"===re.current&&ne.current?$():"reposition"===re.current&&"shown"===re.previous?setTimeout(()=>{B({...re,current:"shown",previous:re.current})},0):"shown"===re.current&&oe.current.contentKey!==b&&ne.current&&U(ne.current)),"reposition"===re.current&&("shown"===re.previous||"showing"===re.previous)&&ne.current&&U(ne.current),oe.current={contentKey:b}},[re]),l.useEffect(()=>()=>{Q()},[]);const se=P||(i.canUseDOM?A&&A.ownerDocument?A.ownerDocument.body:document.body:void 0),le=Object.assign({},{position:w,top:0,left:-1e4},M||{}),ce=void 0!==t.offset&&!f(t.offset,h.offset)||void 0!==t.margin,ae="hiding"===re.current;if((v||ae)&&se){const e=Y?Y+1:12e3,n=l.createElement(i.ZIndexContext.Provider,{value:e},l.createElement("div",{onKeyDown:x,className:i.classNames(i.uPopup.animationContainer({c:F}),z),id:K,ref:ne,style:{zIndex:e,...le}},l.createElement("div",{className:i.classNames(i.uPopup.animationChild({c:F})),style:{transitionDelay:"0ms"}},l.createElement("div",{role:C,"aria-labelledby":D,"aria-describedby":T,className:i.classNames(i.uPopup.popup({c:F}),j),style:ce?{margin:0}:void 0},k))));return null!==P?c.createPortal(n,se):n}return null}),h={collision:{horizontal:r.Collision.fit,vertical:r.Collision.flip},anchorAlign:{horizontal:r.AlignPoint.left,vertical:r.AlignPoint.bottom},popupAlign:{horizontal:r.AlignPoint.left,vertical:r.AlignPoint.top},offset:{left:-1e3,top:0},animate:!0,show:!1,margin:{horizontal:0,vertical:0},positionMode:"absolute"},g={type:"slide",direction:"down"};m.displayName="Popup",m.propTypes={anchor:function(e){const n=e.anchor;return n&&"number"!=typeof n.nodeType?new Error("Invalid prop `anchor` supplied to `Kendo React Popup`. Validation failed."):null},appendTo:function(e){const n=e.appendTo;return n&&"number"!=typeof n.nodeType?new Error("Invalid prop `appendTo` supplied to `Kendo React Popup`. Validation failed."):null},className:o.oneOfType([o.string,o.arrayOf(o.string.isRequired)]),id:o.string,popupClass:o.oneOfType([o.string,o.arrayOf(o.string),o.object]),collision:o.shape({horizontal:o.oneOf([r.Collision.fit,r.Collision.flip,r.Collision.none]),vertical:o.oneOf([r.Collision.fit,r.Collision.flip,r.Collision.none])}),anchorAlign:o.shape({horizontal:o.oneOf([r.AlignPoint.left,r.AlignPoint.center,r.AlignPoint.right]),vertical:o.oneOf([r.AlignPoint.top,r.AlignPoint.center,r.AlignPoint.bottom])}),popupAlign:o.shape({horizontal:o.oneOf([r.AlignPoint.left,r.AlignPoint.center,r.AlignPoint.right]),vertical:o.oneOf([r.AlignPoint.top,r.AlignPoint.center,r.AlignPoint.bottom])}),offset:o.shape({left:o.number,top:o.number}),children:o.oneOfType([o.element,o.node]),show:o.bool,animate:o.oneOfType([o.bool,o.shape({type:o.oneOf(["slide","zoom","push","expand","fade"]),openDuration:o.number,closeDuration:o.number})]),margin:o.shape({horizontal:o.number,vertical:o.number}),positionMode:o.oneOf(["fixed","absolute"]),scale:o.number,style:o.object,onClose:o.func,onPosition:o.func,onOpen:o.func,onKeyDown:o.func,onMouseDownOutside:o.func},e.Popup=m,e.PopupPropsContext=d});