UNPKG

@carbon/react

Version:

React components for the Carbon Design System

88 lines (86 loc) 3.66 kB
/** * Copyright IBM Corp. 2016, 2026 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ const require_runtime = require("../../_virtual/_rolldown/runtime.js"); const require_usePrefix = require("../../internal/usePrefix.js"); const require_keys = require("../../internal/keyboard/keys.js"); const require_match = require("../../internal/keyboard/match.js"); const require_noopFn = require("../../internal/noopFn.js"); const require_utils = require("../../internal/utils.js"); const require_useMergedRefs = require("../../internal/useMergedRefs.js"); const require_useEvent = require("../../internal/useEvent.js"); const require_Switcher = require("./Switcher.js"); let classnames = require("classnames"); classnames = require_runtime.__toESM(classnames); let react = require("react"); react = require_runtime.__toESM(react); let prop_types = require("prop-types"); prop_types = require_runtime.__toESM(prop_types); let react_jsx_runtime = require("react/jsx-runtime"); //#region src/components/UIShell/HeaderPanel.tsx /** * Copyright IBM Corp. 2016, 2026 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ const HeaderPanel = react.default.forwardRef(({ children, className: customClassName, expanded, addFocusListeners = true, onHeaderPanelFocus = require_noopFn.noopFn, href, ...rest }, ref) => { const prefix = require_usePrefix.usePrefix(); const headerPanelReference = (0, react.useRef)(null); const headerPanelRef = require_useMergedRefs.useMergedRefs([headerPanelReference, ref]); const controlled = (0, react.useRef)(expanded !== void 0).current; const [expandedState, setExpandedState] = (0, react.useState)(expanded); const expandedProp = controlled ? expanded : expandedState; const [lastClickedElement, setLastClickedElement] = (0, react.useState)(null); const className = (0, classnames.default)(`${prefix}--header-panel`, { [`${prefix}--header-panel--expanded`]: expandedProp, [customClassName]: !!customClassName }); const eventHandlers = {}; if (addFocusListeners) { eventHandlers.onBlur = (event) => { if (!event.currentTarget.contains(event.relatedTarget) && !lastClickedElement?.classList?.contains(`${prefix}--switcher__item-link`)) { setExpandedState(false); setLastClickedElement(null); if (expanded) onHeaderPanelFocus(); } }; eventHandlers.onKeyDown = (event) => { if (require_match.match(event, require_keys.Escape)) { setExpandedState(false); onHeaderPanelFocus(); if (href) window.location.href = href; } }; } require_useEvent.useWindowEvent("click", (event) => { const { target } = event; if (!(target instanceof Element)) return; setLastClickedElement(target); if (require_utils.isComponentElement(children, require_Switcher.default) && !target.closest(`.${prefix}--header-panel--expanded`) && !target.closest(`.${prefix}--header__action`) && !headerPanelReference?.current?.classList.contains(`${prefix}--switcher`) && expanded) { setExpandedState(false); onHeaderPanelFocus(); } }); return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { ...rest, className, ref: headerPanelRef, ...eventHandlers, children }); }); HeaderPanel.propTypes = { addFocusListeners: prop_types.default.bool, children: prop_types.default.node, className: prop_types.default.string, expanded: prop_types.default.bool, href: prop_types.default.string, onHeaderPanelFocus: prop_types.default.func }; HeaderPanel.displayName = "HeaderPanel"; //#endregion exports.default = HeaderPanel;