@elastic/eui
Version:
Elastic UI Component Library
143 lines (142 loc) • 7.17 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.EuiHeader = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _classnames = _interopRequireDefault(require("classnames"));
var _services = require("../../services");
var _header_breadcrumbs = require("./header_breadcrumbs");
var _header_section = require("./header_section");
var _header = require("./header.styles");
var _react2 = require("@emotion/react");
var _excluded = ["children", "className", "sections", "position", "theme"];
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function createHeaderSection(sections, border) {
return sections.map(function (section, index) {
return (0, _react2.jsx)(_header_section.EuiHeaderSectionItem, {
key: index,
border: border
}, section);
});
}
// Start a counter to manage the total number of fixed headers that need the body class
var euiHeaderFixedCounter = 0;
var EuiHeader = function EuiHeader(_ref) {
var children = _ref.children,
className = _ref.className,
sections = _ref.sections,
_ref$position = _ref.position,
position = _ref$position === void 0 ? 'static' : _ref$position,
_ref$theme = _ref.theme,
theme = _ref$theme === void 0 ? 'default' : _ref$theme,
rest = (0, _objectWithoutProperties2.default)(_ref, _excluded);
var classes = (0, _classnames.default)('euiHeader', className);
var euiTheme = (0, _services.useEuiTheme)();
var styles = (0, _header.euiHeaderStyles)(euiTheme);
var cssStyles = [styles.euiHeader, styles[position], styles[theme]];
(0, _react.useEffect)(function () {
if (position === 'fixed') {
// Increment fixed header counter for each fixed header
euiHeaderFixedCounter++;
document.body.classList.add('euiBody--headerIsFixed');
document.body.dataset.fixedHeaders = String(euiHeaderFixedCounter);
return function () {
// Both decrement the fixed counter AND then check if there are none
if (--euiHeaderFixedCounter === 0) {
// If there are none, THEN remove class
document.body.classList.remove('euiBody--headerIsFixed');
delete document.body.dataset.fixedHeaders;
}
};
}
}, [position]);
var contents;
if (sections) {
if (children) {
// In case both children and sections are passed, warn in the console that the children will be disregarded
console.warn('EuiHeader cannot accept both `children` and `sections`. It will disregard the `children`.');
}
contents = sections.map(function (section, index) {
var content = [];
if (section.items) {
// Items get wrapped in EuiHeaderSection and each item in a EuiHeaderSectionItem
content.push((0, _react2.jsx)(_header_section.EuiHeaderSection, {
key: "items-".concat(index)
}, createHeaderSection(section.items, section.borders)));
}
if (section.breadcrumbs) {
content.push(
// Breadcrumbs are separate and cannot be contained in a EuiHeaderSection
// in order for truncation to work
(0, _react2.jsx)(_header_breadcrumbs.EuiHeaderBreadcrumbs, (0, _extends2.default)({
key: "breadcrumbs-".concat(index),
breadcrumbs: section.breadcrumbs
}, section.breadcrumbProps)));
}
return content;
});
} else {
contents = children;
}
return (0, _react2.jsx)("div", (0, _extends2.default)({
css: cssStyles,
className: classes,
"data-fixed-header": position === 'fixed' || undefined // Used by EuiFlyouts as a query selector
}, rest), contents);
};
exports.EuiHeader = EuiHeader;
EuiHeader.propTypes = {
className: _propTypes.default.string,
"aria-label": _propTypes.default.string,
"data-test-subj": _propTypes.default.string,
css: _propTypes.default.any,
/**
* An array of objects to wrap in a #EuiHeaderSection.
* Each section is spaced using `space-between`.
* See #EuiHeaderSectionsProp for object details.
* This prop disregards the prop `children` if both are passed.
*/
sections: _propTypes.default.arrayOf(_propTypes.default.shape({
/**
* An arry of items that will be wrapped in a #EuiHeaderSectionItem
*/
items: _propTypes.default.arrayOf(_propTypes.default.node.isRequired),
/**
* Apply the passed border side to each #EuiHeaderSectionItem
*/
borders: _propTypes.default.oneOf(["left", "right", "none"]),
/**
* Breadcrumbs in the header cannot be wrapped in a #EuiHeaderSection in order for truncation to work.
* Simply pass the array of EuiBreadcrumb objects
*/
breadcrumbs: _propTypes.default.arrayOf(_propTypes.default.any.isRequired),
/**
* Other props to pass to #EuiHeaderBreadcrumbs
*/
breadcrumbProps: _propTypes.default.any
}).isRequired),
/**
* Helper that positions the header against the window body and
* adds the correct amount of top padding to the window when in `fixed` mode
*/
position: _propTypes.default.oneOf(["static", "fixed"]),
/**
* The `default` will inherit its coloring from the light or dark theme.
* Or, force the header into pseudo `dark` theme for all themes.
*/
theme: _propTypes.default.oneOf(["default", "dark"])
};