@elastic/eui
Version:
Elastic UI Component Library
363 lines (359 loc) • 17.9 kB
JavaScript
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
var _excluded = ["children", "responsive", "restrictWidth", "paddingSize", "grow", "bottomBorder", "offset", "panelled", "contentBorder", "component", "mainProps", "className", "minHeight"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
/*
* 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.
*/
import React, { createContext, useContext, useEffect, useState } from 'react';
import PropTypes from "prop-types";
import classNames from 'classnames';
import { _EuiPageOuter as EuiPageOuter } from './outer';
import { _EuiPageInner as EuiPageInner } from './inner';
import { _EuiPageBottomBar as EuiPageBottomBar } from './bottom_bar/page_bottom_bar';
import { _EuiPageEmptyPrompt as EuiPageEmptyPrompt } from './empty_prompt/page_empty_prompt';
import { EuiPageHeader, EuiPageSection, EuiPageSidebar } from '../page';
import { useEuiTheme, useGeneratedHtmlId } from '../../services';
import { logicalStyle } from '../../global_styling';
import { jsx as ___EmotionJSX } from "@emotion/react";
export var TemplateContext = /*#__PURE__*/createContext({
section: {},
header: {},
emptyPrompt: {},
bottomBar: {}
});
/**
* Consumed via `EuiPageTemplate`,
* it controls and propogates most of the shared props per direct child
*/
export var _EuiPageTemplate = function _EuiPageTemplate(_ref) {
var children = _ref.children,
_ref$responsive = _ref.responsive,
responsive = _ref$responsive === void 0 ? ['xs', 's'] : _ref$responsive,
_ref$restrictWidth = _ref.restrictWidth,
restrictWidth = _ref$restrictWidth === void 0 ? true : _ref$restrictWidth,
_ref$paddingSize = _ref.paddingSize,
paddingSize = _ref$paddingSize === void 0 ? 'l' : _ref$paddingSize,
_ref$grow = _ref.grow,
grow = _ref$grow === void 0 ? true : _ref$grow,
bottomBorder = _ref.bottomBorder,
_offset = _ref.offset,
panelled = _ref.panelled,
contentBorder = _ref.contentBorder,
component = _ref.component,
mainProps = _ref.mainProps,
className = _ref.className,
_ref$minHeight = _ref.minHeight,
minHeight = _ref$minHeight === void 0 ? '460px' : _ref$minHeight,
rest = _objectWithoutProperties(_ref, _excluded);
var _useEuiTheme = useEuiTheme(),
euiTheme = _useEuiTheme.euiTheme;
var _useState = useState(_offset),
_useState2 = _slicedToArray(_useState, 2),
offset = _useState2[0],
setOffset = _useState2[1];
var templateContext = useContext(TemplateContext);
// Used as a target to insert the bottom bar component
var pageInnerId = useGeneratedHtmlId({
prefix: 'EuiPageTemplateInner',
conditionalId: mainProps === null || mainProps === void 0 ? void 0 : mainProps.id
});
useEffect(function () {
if (_offset === undefined) {
var _document$body$datase;
var euiHeaderFixedCounter = Number((_document$body$datase = document.body.dataset.fixedHeaders) !== null && _document$body$datase !== void 0 ? _document$body$datase : 0);
setOffset(euiTheme.base * 3 * euiHeaderFixedCounter);
}
}, [_offset, euiTheme.base]);
// Sections include page header
var sections = [];
var sidebar = [];
var getBottomBorder = function getBottomBorder() {
if (bottomBorder !== undefined) {
return bottomBorder;
} else {
return sidebar.length ? true : 'extended';
}
};
var getHeaderProps = function getHeaderProps() {
return {
restrictWidth: restrictWidth,
paddingSize: paddingSize,
bottomBorder: getBottomBorder()
};
};
var getSectionProps = function getSectionProps() {
return {
restrictWidth: restrictWidth,
paddingSize: paddingSize,
color: panelled === false ? 'transparent' : 'plain'
};
};
var getSideBarProps = function getSideBarProps() {
return {
paddingSize: paddingSize,
responsive: responsive
};
};
var getBottomBarProps = function getBottomBarProps() {
return {
restrictWidth: restrictWidth,
paddingSize: paddingSize,
parent: "#".concat(pageInnerId)
};
};
var innerPanelled = function innerPanelled() {
return panelled === false ? false : Boolean(sidebar.length > 0);
};
var innerBordered = function innerBordered() {
return contentBorder !== undefined ? contentBorder : Boolean(sidebar.length > 0);
};
React.Children.toArray(children).forEach(function (child, index) {
if (! /*#__PURE__*/React.isValidElement(child)) return; // Skip non-components
if (child.type === EuiPageSidebar || child.props.__EMOTION_TYPE_PLEASE_DO_NOT_USE__ === EuiPageSidebar) {
sidebar.push( /*#__PURE__*/React.cloneElement(child, _objectSpread(_objectSpread({
key: "sidebar".concat(index)
}, getSideBarProps()), child.props)));
} else {
sections.push(child);
}
});
var _minHeight = grow ? "max(".concat(minHeight, ", 100vh)") : minHeight;
var classes = classNames('euiPageTemplate', className);
var pageStyle = _objectSpread(_objectSpread(_objectSpread({}, logicalStyle('min-height', _minHeight)), logicalStyle('padding-top', offset)), rest.style);
templateContext.header = getHeaderProps();
templateContext.section = getSectionProps();
templateContext.emptyPrompt = {
panelled: innerPanelled() ? true : panelled,
grow: true
};
templateContext.bottomBar = getBottomBarProps();
return ___EmotionJSX(TemplateContext.Provider, {
value: templateContext
}, ___EmotionJSX(EuiPageOuter, _extends({}, rest, {
responsive: responsive,
style: pageStyle,
className: classes
}), sidebar, ___EmotionJSX(EuiPageInner, _extends({}, mainProps, {
component: component,
id: pageInnerId,
border: innerBordered(),
panelled: innerPanelled(),
responsive: responsive
}), sections)));
};
_EuiPageTemplate.propTypes = {
/**
* Adds `flex-grow: 1` to the whole page for stretching to fit vertically.
* Must be wrapped inside a flexbox, preferrably with `min-height: 100vh`
*/
grow: PropTypes.bool,
/**
* Changes the `flex-direction` property.
* Flip to `column` when not including a sidebar.
*/
direction: PropTypes.oneOf(["row", "column"]),
/**
* Decides at which point the main content wrapper will be 100vw.
*/
/**
* When direction is `row`, it will flip to `column` when within these breakpoints.
*/
responsive: PropTypes.arrayOf(PropTypes.any.isRequired),
className: PropTypes.string,
"aria-label": PropTypes.string,
"data-test-subj": PropTypes.string,
css: PropTypes.any,
/**
* Adds a white background and shadow to define the area.
*/
panelled: PropTypes.bool,
/**
* Adjust the overall padding.
*/
paddingSize: PropTypes.any,
/**
* Sets the max-width of the page,
* set to `true` to use the default size of `1200px`,
* set to `false` to not restrict the width,
* set to a number for a custom width in px,
* set to a string for a custom width in custom measurement.
*/
restrictWidth: PropTypes.oneOfType([PropTypes.bool.isRequired, PropTypes.number.isRequired, PropTypes.string.isRequired]),
/**
* Adds a bottom border to separate it from the content after;
* Passing `extended` will ensure the border touches the sides of the parent container.
*/
bottomBorder: PropTypes.oneOfType([PropTypes.bool.isRequired, PropTypes.oneOf(["extended"])]),
/**
* Applies a top or left border to the inner contents
* to add separation between content and sidebar when a sidebar exists.
*/
contentBorder: PropTypes.any,
/**
* Minimum height in which to enforce scrolling
*/
minHeight: PropTypes.any,
/**
* To account for any fixed elements like headers,
* pass in the value of the total height of those fixed elements.
* Otherwise they will be calculated based on the data attributes on the body element.
*/
offset: PropTypes.number,
/**
* Passes through some common HTML attributes to the `main` content wrapper
*/
mainProps: PropTypes.shape({
className: PropTypes.string,
"aria-label": PropTypes.string,
"data-test-subj": PropTypes.string,
css: PropTypes.any
}),
/**
* Sets which HTML element to render for the `main` content wrapper
* @default main
*/
component: PropTypes.oneOfType([PropTypes.any.isRequired, PropTypes.elementType.isRequired])
};
var _EuiPageSection = function _EuiPageSection(props) {
var templateContext = useContext(TemplateContext);
return ___EmotionJSX(EuiPageSection, _extends({}, templateContext.section, {
grow: true
}, props));
};
_EuiPageSection.propTypes = {
className: PropTypes.string,
"aria-label": PropTypes.string,
"data-test-subj": PropTypes.string,
css: PropTypes.any,
/**
* Sets the max-width of the page,
* set to `true` to use the default size of `1200px`,
* set to `false` to not restrict the width,
* set to a number for a custom width in px,
* set to a string for a custom width in custom measurement.
*/
restrictWidth: PropTypes.oneOfType([PropTypes.bool.isRequired, PropTypes.number.isRequired, PropTypes.string.isRequired]),
/**
* Background color of the section;
* Usually a lightened form of the brand colors
*/
color: PropTypes.any,
/**
* Padding for all four sides
*/
paddingSize: PropTypes.any,
/**
* Horizontal and/or vertical alignment of the section contents
*/
alignment: PropTypes.any,
/**
* When true the panel will grow in height to fill container if parent is a flex group
*/
grow: PropTypes.bool,
/**
* Passed down to the div wrapper of the section contents
*/
contentProps: PropTypes.shape({
className: PropTypes.string,
"aria-label": PropTypes.string,
"data-test-subj": PropTypes.string,
css: PropTypes.any
}),
/**
* Sets which HTML element to render.
*/
component: PropTypes.oneOfType([PropTypes.any.isRequired, PropTypes.elementType.isRequired])
};
var _EuiPageHeader = function _EuiPageHeader(props) {
var templateContext = useContext(TemplateContext);
return ___EmotionJSX(EuiPageHeader, _extends({}, templateContext.header, props));
};
_EuiPageHeader.propTypes = {
/**
* Adjust the overall padding.
*/
paddingSize: PropTypes.any,
className: PropTypes.string,
"aria-label": PropTypes.string,
"data-test-subj": PropTypes.string,
css: PropTypes.any,
/**
* Sets the max-width of the page,
* set to `true` to use the default size of `1200px`,
* set to `false` to not restrict the width,
* set to a number for a custom width in px,
* set to a string for a custom width in custom measurement.
*/
restrictWidth: PropTypes.oneOfType([PropTypes.bool.isRequired, PropTypes.number.isRequired, PropTypes.string.isRequired]),
/**
* Adds a bottom border to separate it from the content after;
* Passing `extended` will ensure the border touches the sides of the parent container.
*/
bottomBorder: PropTypes.oneOfType([PropTypes.bool.isRequired, PropTypes.oneOf(["extended"])])
};
var _EuiPageEmptyPrompt = function _EuiPageEmptyPrompt(props) {
var templateContext = useContext(TemplateContext);
return ___EmotionJSX(EuiPageEmptyPrompt, _extends({}, templateContext.emptyPrompt, props));
};
_EuiPageEmptyPrompt.propTypes = {
className: PropTypes.string,
"aria-label": PropTypes.string,
"data-test-subj": PropTypes.string,
css: PropTypes.any,
restrictWidth: PropTypes.oneOfType([PropTypes.bool.isRequired, PropTypes.number.isRequired, PropTypes.string.isRequired]),
color: PropTypes.any,
paddingSize: PropTypes.any,
alignment: PropTypes.any,
grow: PropTypes.bool,
contentProps: PropTypes.shape({
className: PropTypes.string,
"aria-label": PropTypes.string,
"data-test-subj": PropTypes.string,
css: PropTypes.any
}),
component: PropTypes.oneOfType([PropTypes.any.isRequired, PropTypes.elementType.isRequired]),
panelled: PropTypes.bool
};
var _EuiPageBottomBar = function _EuiPageBottomBar(props) {
var _useContext = useContext(TemplateContext),
bottomBar = _useContext.bottomBar;
return ___EmotionJSX(EuiPageBottomBar, _extends({}, bottomBar, props));
};
_EuiPageBottomBar.propTypes = {
/**
* The reference id of the element to insert into
*/
parent: PropTypes.string,
/**
* Sets the max-width of the page,
* set to `true` to use the default size of `1200px`,
* set to `false` to not restrict the width,
* set to a number for a custom width in px,
* set to a string for a custom width in custom measurement.
*/
restrictWidth: PropTypes.oneOfType([PropTypes.bool.isRequired, PropTypes.number.isRequired, PropTypes.string.isRequired])
};
export var EuiPageTemplate = Object.assign(_EuiPageTemplate, {
Sidebar: EuiPageSidebar,
Header: _EuiPageHeader,
Section: _EuiPageSection,
BottomBar: _EuiPageBottomBar,
EmptyPrompt: _EuiPageEmptyPrompt
});