UNPKG

@instructure/ui-themeable

Version:

A UI component library made by Instructure Inc.

108 lines (94 loc) 3.63 kB
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck"; import _createClass from "@babel/runtime/helpers/esm/createClass"; import _inherits from "@babel/runtime/helpers/esm/inherits"; import _createSuper from "@babel/runtime/helpers/esm/createSuper"; import { warn as _warn } from "@instructure/console"; /* * The MIT License (MIT) * * Copyright (c) 2015 - present Instructure, Inc. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ import { Component } from 'react'; import PropTypes from 'prop-types'; import { mergeDeep } from '@instructure/ui-utils'; import { ensureSingleChild } from '@instructure/ui-react-utils'; import { ThemeContext } from "../ThemeContext.js"; import { themeable } from "../themeable.js"; /** --- category: components/utilities --- **/ var ApplyTheme = /*#__PURE__*/function (_Component) { _inherits(ApplyTheme, _Component); var _super = _createSuper(ApplyTheme); function ApplyTheme() { _classCallCheck(this, ApplyTheme); return _super.apply(this, arguments); } _createClass(ApplyTheme, [{ key: "getChildContext", value: function getChildContext() { var theme = this.props.theme || {}; var parentThemeContext = ThemeContext.getThemeContext(this.context) || {}; if (parentThemeContext.immutable && parentThemeContext.theme) { /*#__PURE__*/ /*#__PURE__*/ _warn(!this.props.theme, '[ApplyTheme] Parent theme is immutable. Cannot apply theme: %O', this.props.theme); theme = parentThemeContext.theme; } else if (parentThemeContext.theme) { theme = mergeDeep(parentThemeContext.theme, theme); } return ThemeContext.makeThemeContext(theme, parentThemeContext.immutable || this.props.immutable); } }, { key: "render", value: function render() { return ensureSingleChild(this.props.children); } }]); ApplyTheme.displayName = "ApplyTheme"; return ApplyTheme; }(Component); ApplyTheme.propTypes = { /** * set theme variables to override the defaults */ theme: PropTypes.object, /** * accepts only one child (children must be wrapped in a single component/element) */ children: PropTypes.node, /** * Prevent overriding this theme via a child ApplyTheme component or theme props */ immutable: PropTypes.bool }; ApplyTheme.defaultProps = { theme: void 0, children: null, immutable: false }; ApplyTheme.childContextTypes = ThemeContext.types; ApplyTheme.contextTypes = ThemeContext.types; ApplyTheme.generateTheme = themeable.generateTheme; export default ApplyTheme; export { ApplyTheme };