UNPKG

@carbon/react

Version:

React components for the Carbon Design System

50 lines (46 loc) 1.83 kB
/** * Copyright IBM Corp. 2016, 2023 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ import { extends as _extends } from '../../_virtual/_rollupPluginBabelHelpers.js'; import PropTypes from 'prop-types'; import React, { useState, useCallback } from 'react'; import { Escape } from '../../internal/keyboard/keys.js'; import { match } from '../../internal/keyboard/match.js'; import { useWindowEvent } from '../../internal/useEvent.js'; function HeaderContainer({ render: Children, isSideNavExpanded = false, ...rest }) { //state for expandable sidenav const [isSideNavExpandedState, setIsSideNavExpandedState] = useState(isSideNavExpanded); useWindowEvent('keydown', event => { if (match(event, Escape)) { setIsSideNavExpandedState(false); } }); const handleHeaderMenuButtonClick = useCallback(() => { setIsSideNavExpandedState(prevIsSideNavExpanded => !prevIsSideNavExpanded); }, [setIsSideNavExpandedState]); return /*#__PURE__*/React.createElement(Children, _extends({}, rest, { isSideNavExpanded: isSideNavExpandedState, onClickSideNavExpand: handleHeaderMenuButtonClick })); } HeaderContainer.propTypes = { /** * Optionally provide a custom class name that is applied to the underlying <header> */ isSideNavExpanded: PropTypes.bool, /** * A function or a component that is invoked with `isSideNavExpanded` and `onClickSideNavExpand`. * The function or component can then use those properties to within the components it * returns, such as with the HeaderMenuButton and SideNav components. Additional props will also be passed * into this component for convenience. */ render: PropTypes.elementType.isRequired }; export { HeaderContainer as default };