UNPKG

carbon-react

Version:

A library of reusable React components for easily building user interfaces.

2 lines (1 loc) 5.97 kB
import{jsx as e,jsxs as r}from"react/jsx-runtime";import n,{forwardRef as t,useState as o,useRef as i,useImperativeHandle as a,useCallback as l,useLayoutEffect as s,useEffect as c}from"react";import{DepthProvider as u}from"./__internal__/depth.context.js";import{ResponsiveVerticalMenuProvider as p,useResponsiveVerticalMenu as d}from"./responsive-vertical-menu.context.js";import{StyledBackdrop as v,StyledButton as m,ModalContainer as f,StyledCloseButton as h,StyledResponsiveMenu as b,StyledGlobalVerticalMenuWrapper as y}from"./responsive-vertical-menu.style.js";import{Box as g}from"../../box/box.component.js";import O from"../../../__internal__/modal/modal.component.js";import j from"../../../hooks/__internal__/useIsAboveBreakpoint/useIsAboveBreakpoint.js";import w from"../../../hooks/useMediaQuery/useMediaQuery.js";import _ from"../../../hooks/__internal__/useLocale/useLocale.js";import x from"../../../__internal__/focus-trap/focus-trap.component.js";import M from"../../../__internal__/utils/helpers/tags/tags.js";function k(e,r,n){return r in e?Object.defineProperty(e,r,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[r]=n,e}function L(e){for(var r=1;r<arguments.length;r++){var n=null!=arguments[r]?arguments[r]:{},t=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(t=t.concat(Object.getOwnPropertySymbols(n).filter((function(e){return Object.getOwnPropertyDescriptor(n,e).enumerable})))),t.forEach((function(r){k(e,r,n[r])}))}return e}function P(e,r){return r=null!=r?r:{},Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):function(e){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r.push.apply(r,n)}return r}(Object(r)).forEach((function(n){Object.defineProperty(e,n,Object.getOwnPropertyDescriptor(r,n))})),e}function E(e,r){if(null==e)return{};var n,t,o=function(e,r){if(null==e)return{};var n,t,o={},i=Object.keys(e);for(t=0;t<i.length;t++)n=i[t],r.indexOf(n)>=0||(o[n]=e[n]);return o}(e,r);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);for(t=0;t<i.length;t++)n=i[t],r.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(o[n]=e[n])}return o}const B=t(((t,u)=>{var p,{children:k,height:B,responsiveBreakpoint:C=700,width:D,launcherButtonDataProps:R}=t,S=E(t,["children","height","responsiveBreakpoint","width","launcherButtonDataProps"]);const I=_(),{active:A,activeMenuItem:T,buttonRef:z,containerRef:$,menuRef:q,responsiveMode:Q,top:V,setActive:G,setActiveMenuItem:F,setReducedMotion:H,setResponsiveMode:J,setLeft:K,setTop:N}=d(),[U,W]=o(0),X=j(C),[Y,Z]=o("100%"),ee=!w("screen and (prefers-reduced-motion: no-preference)"),re=i(null),{current:ne}=q,{current:te}=z;a(u,(()=>({focusLaunchButton(){null==te||te.focus({preventScroll:!0})}})),[te]);const oe=l((()=>{if(A&&Q){const e=ne||document.querySelector("[id='responsive-vertical-menu-primary']");e&&Z(`${e.getBoundingClientRect().width}px`)}K(T&&ne?`${ne.getBoundingClientRect().right}px`:"auto"),N(T&&te?`${te.getBoundingClientRect().bottom}px`:"auto")}),[A,Q,T,ne,te,K,N]);s((()=>(oe(),window.addEventListener("resize",oe),()=>{var e;null===(e=window)||void 0===e||e.removeEventListener("resize",oe)})),[A,oe,ne,Q]),c((()=>{const e=$.current,r=r=>{!A||Q||(null==e?void 0:e.contains(r.relatedTarget))||G(!1)},n=e=>{A&&!Q&&"Escape"===e.key&&(e.preventDefault(),G(!1))};return null==e||e.addEventListener("focusout",r),document.addEventListener("keydown",n),()=>{null==e||e.removeEventListener("focusout",r),document.removeEventListener("keydown",n)}}),[A,$,Q,G]),c((()=>{null==H||H(ee),null==J||J(!X)}),[X,ee,H,J]);const ie=l((e=>{if(!e)return 0;if(n.isValidElement(e))return 1+ie(e.props.children);const r=n.Children.toArray(e);return r.length?r.reduce(((e,r)=>n.isValidElement(r)?e+1+ie(r.props.children):e),0):0}),[]);return c((()=>{const e=U,r=ie(k);e!==r&&(W(r),F(null))}),[U,k,ie,F]),r("div",{ref:$,children:[A&&!Q&&e(v,{onMouseDown:e=>{e.preventDefault(),G(!1)}}),e(m,L({active:A,buttonType:"tertiary",iconType:"squares_nine",id:"responsive-vertical-menu-launcher",onClick:()=>{G((e=>!e)),F(null)},ref:z},M("responsive-vertical-menu-launcher",L({"data-role":"responsive-vertical-menu-launcher"},R)),(()=>{var e,r;return Q?{"aria-label":null===(r=I.verticalMenu.ariaLabels)||void 0===r?void 0:r.responsiveMenuLauncher()}:{"aria-expanded":A,"aria-controls":"responsive-vertical-menu-primary","aria-label":null===(e=I.verticalMenu.ariaLabels)||void 0===e?void 0:e.responsiveMenuLauncher()}})())),Q&&e(O,{open:A,children:e(x,{wrapperRef:re,isOpen:A,children:r(f,P(L({ref:re,width:Y,tabIndex:-1,id:"responsive-vertical-menu-dialog"},(()=>{var e;return{role:"dialog","aria-modal":!0,"aria-label":null===(e=I.verticalMenu.ariaLabels)||void 0===e?void 0:e.responsiveMenuAria()}})()),{children:[e(g,{boxSizing:"border-box",display:"flex",justifyContent:"flex-end",width:"100%",backgroundColor:"var(--colorsGray850)",p:1,children:e(h,{"aria-label":null===(p=I.verticalMenu.ariaLabels)||void 0===p?void 0:p.responsiveMenuCloseButton(),"data-component":"responsive-vertical-menu-close","data-role":"responsive-vertical-menu-close",iconType:"close",size:"small",buttonType:"tertiary",onClick:()=>{G(!1),F(null)}})}),e(b,{height:B,id:"responsive-vertical-menu-primary",menu:"primary",reduceMotion:ee,ref:q,responsive:!0,tabIndex:-1,top:"48px",width:D,children:k})]}))})}),A&&!Q&&e(y,P(L({},S,M("responsive-vertical-menu",S)),{children:e(b,{childOpen:!!T,"data-component":"responsive-vertical-menu-primary","data-role":"responsive-vertical-menu-primary",height:B||"100%",id:"responsive-vertical-menu-primary",menu:"primary",reduceMotion:ee,ref:q,responsive:!1,tabIndex:-1,top:V,width:D,children:k})}))]})})),C=t(((r,n)=>{var{children:t,width:o,height:i}=r,a=E(r,["children","width","height"]);return e(u,{children:e(p,{width:o,height:i,children:e(B,P(L({ref:n,width:o,height:i},a),{children:t}))})})}));export{C as ResponsiveVerticalMenu,C as default};