carbon-react
Version:
A library of reusable React components for easily building user interfaces.
2 lines (1 loc) • 4.01 kB
JavaScript
import{jsxs as e,jsx as r}from"react/jsx-runtime";import{useRef as t,useState as n,useEffect as o,useCallback as i}from"react";import a from"invariant";import l from"../../__internal__/utils/helpers/guid/index.js";import d from"../../hooks/__internal__/usePrevious/index.js";import c from"../icon/icon.component.js";import{StyledDrawerWrapper as s,StyledDrawerContent as p,StyledSidebarTitle as u,StyledDrawerSidebar as b,StyledSidebarFooter as f,StyledSidebarToggleButton as m}from"./drawer.style.js";import h from"../../__internal__/utils/helpers/tags/tags.js";import g from"./__internal__/drawer-sidebar.context.js";import w from"../../__internal__/utils/logger/index.js";function y(e,r,t){return r in e?Object.defineProperty(e,r,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[r]=t,e}let O=!1,v=!1,j=!1;const _=_=>{var{"aria-label":x,sidebarAriaLabel:D,defaultExpanded:P,expanded:k,onChange:C,children:E,expandedWidth:S="30vw",sidebar:H,animationDuration:T="400ms",backgroundColor:A,title:F,footer:W,showControls:I,height:L="100%",stickyHeader:$,stickyFooter:q}=_,z=function(e,r){if(null==e)return{};var t,n,o=function(e,r){if(null==e)return{};var t,n,o={},i=Object.keys(e);for(n=0;n<i.length;n++)t=i[n],r.indexOf(t)>=0||(o[t]=e[t]);return o}(e,r);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);for(n=0;n<i.length;n++)t=i[n],r.indexOf(t)>=0||Object.prototype.propertyIsEnumerable.call(e,t)&&(o[t]=e[t])}return o}(_,["aria-label","sidebarAriaLabel","defaultExpanded","expanded","onChange","children","expandedWidth","sidebar","animationDuration","backgroundColor","title","footer","showControls","height","stickyHeader","stickyFooter"]);"400ms"===T||O||(w.deprecate("The `animationDuration` prop in `Drawer` is deprecated and will soon be removed."),O=!0),void 0===I||j||(w.deprecate("The `showControls` prop in `Drawer` is deprecated and will soon be removed."),j=!0),void 0===P||v||(w.deprecate("The `defaultExpanded` prop in `Drawer` is deprecated and will soon be removed."),v=!0);const B=t(l()),G=`DrawerSidebar_${B.current}`,J=`DrawerTitle_${B.current}`,K=t(null),M=t(null),N=t(void 0!==k),[Q,R]=n(N.current?k:null==P||P),U=d(k);o((()=>{a(N.current===(void 0!==k),"Drawer should not switch from uncontrolled to controlled (or vice versa). Decide between using a controlled or uncontrolled Drawer element for the lifetime of the component"),N.current&&U!==k&&R(k)}),[k,U,I]),o((()=>{const e=M.current;e&&(e.scrollHeight>e.clientHeight?e.setAttribute("tabindex","0"):e.removeAttribute("tabindex"))}),[H,Q]);const V=i((e=>{R(!Q),null==C||C(e,!Q),Q&&K.current&&(K.current.scrollTop=0)}),[Q,C]);return e(s,(X=function(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{},n=Object.keys(t);"function"==typeof Object.getOwnPropertySymbols&&(n=n.concat(Object.getOwnPropertySymbols(t).filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable})))),n.forEach((function(r){y(e,r,t[r])}))}return e}({"aria-label":x,height:L},h("drawer",z)),Y=null!=(Y={children:[e(p,{expandedWidth:S,animationDuration:T,isExpanded:Q,showControls:I,ref:K,backgroundColor:A,"data-element":"drawer-content","data-role":"drawer-content","aria-label":D,"aria-labelledby":F?J:void 0,children:[F&&r(u,{id:J,stickyHeader:$,children:F}),I&&r(m,{"aria-label":"toggle sidebar","aria-expanded":Q,"aria-controls":G,"data-element":"drawer-toggle",onClick:V,isExpanded:Q,children:r(c,{type:"chevron_right"})}),r(b,{"data-element":"drawer-sidebar",id:G,ref:M,children:r(g.Provider,{value:{isInSidebar:!0},children:H})}),W&&r(f,{stickyFooter:q,children:W})]}),E]})?Y:{},Object.getOwnPropertyDescriptors?Object.defineProperties(X,Object.getOwnPropertyDescriptors(Y)):function(e){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var t=Object.getOwnPropertySymbols(e);r.push.apply(r,t)}return r}(Object(Y)).forEach((function(e){Object.defineProperty(X,e,Object.getOwnPropertyDescriptor(Y,e))})),X));var X,Y};export{_ as Drawer,g as DrawerSidebarContext,_ as default};