UNPKG

@elastic/eui

Version:

Elastic UI Component Library

104 lines (96 loc) 5.35 kB
"use strict"; function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } Object.defineProperty(exports, "__esModule", { value: true }); exports.withEuiTheme = exports.useIsDarkMode = exports.useEuiThemeCSSVariables = exports.useEuiTheme = exports.RenderWithEuiTheme = void 0; var _react = _interopRequireWildcard(require("react")); var _euiThemeCommon = require("@elastic/eui-theme-common"); var _context = require("./context"); var _warning = require("./warning"); var _react2 = require("@emotion/react"); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } /* * 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. */ var providerMessage = "`EuiProvider` is missing which can result in negative effects.\nWrap your component in `EuiProvider`: https://ela.st/euiprovider."; /** * Hook for function components */ var useEuiTheme = exports.useEuiTheme = function useEuiTheme() { var theme = (0, _react.useContext)(_context.EuiThemeContext); var colorMode = (0, _react.useContext)(_context.EuiColorModeContext); var highContrastMode = (0, _react.useContext)(_context.EuiHighContrastModeContext); var modifications = (0, _react.useContext)(_context.EuiModificationsContext); var isFallback = theme === _context.defaultComputedTheme; if (isFallback) { (0, _warning.emitEuiProviderWarning)(providerMessage); } var assembledTheme = (0, _react.useMemo)(function () { return { euiTheme: theme, colorMode: colorMode, highContrastMode: highContrastMode, modifications: modifications }; }, [theme, colorMode, highContrastMode, modifications]); return assembledTheme; }; /** * HOC for class components */ // Provide the component props interface as the generic to allow the docs props table to populate. // e.g., `const EuiComponent = withEuiTheme<EuiComponentProps>(_EuiComponent)` var withEuiTheme = exports.withEuiTheme = function withEuiTheme(Component) { var componentName = Component.displayName || Component.name || 'ComponentWithTheme'; var Render = function Render(props, ref) { var theme = useEuiTheme(); return (0, _react2.jsx)(Component, _extends({ theme: theme, ref: ref }, props)); }; var WithEuiTheme = /*#__PURE__*/(0, _react.forwardRef)(Render); WithEuiTheme.displayName = componentName; return WithEuiTheme; }; /** * Render prop alternative for complex class components * Most useful for scenarios where a HOC may interfere with typing */ var RenderWithEuiTheme = exports.RenderWithEuiTheme = function RenderWithEuiTheme(_ref) { var children = _ref.children; var theme = useEuiTheme(); return children(theme); }; /** * Minor syntactical sugar hook for theme CSS variables. * Primarily meant for internal EUI usage. */ var useEuiThemeCSSVariables = exports.useEuiThemeCSSVariables = function useEuiThemeCSSVariables() { var _useContext = (0, _react.useContext)(_context.EuiNestedThemeContext), setGlobalCSSVariables = _useContext.setGlobalCSSVariables, globalCSSVariables = _useContext.globalCSSVariables, setNearestThemeCSSVariables = _useContext.setNearestThemeCSSVariables, themeCSSVariables = _useContext.themeCSSVariables; return { setGlobalCSSVariables: setGlobalCSSVariables, globalCSSVariables: globalCSSVariables, setNearestThemeCSSVariables: setNearestThemeCSSVariables, themeCSSVariables: themeCSSVariables }; }; /** * Checks whether the current active `colorMode` is set to `DARK`; * In case of nested providers this returns the value of the nearest provider context. */ var useIsDarkMode = exports.useIsDarkMode = function useIsDarkMode() { var _useEuiTheme = useEuiTheme(), colorMode = _useEuiTheme.colorMode; return colorMode === _euiThemeCommon.COLOR_MODES_STANDARD.dark; };