UNPKG

@instructure/ui-themeable

Version:

A UI component library made by Instructure Inc.

123 lines (100 loc) 4.25 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.ApplyTheme = exports.default = void 0; var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")); var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper")); var _console = require("@instructure/console"); var _react = require("react"); var _propTypes = _interopRequireDefault(require("prop-types")); var _mergeDeep = require("@instructure/ui-utils/lib/mergeDeep.js"); var _ensureSingleChild = require("@instructure/ui-react-utils/lib/ensureSingleChild.js"); var _ThemeContext = require("../ThemeContext.js"); var _themeable = require("../themeable.js"); /* * 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. */ /** --- category: components/utilities --- **/ var ApplyTheme = /*#__PURE__*/function (_Component) { (0, _inherits2.default)(ApplyTheme, _Component); var _super = (0, _createSuper2.default)(ApplyTheme); function ApplyTheme() { (0, _classCallCheck2.default)(this, ApplyTheme); return _super.apply(this, arguments); } (0, _createClass2.default)(ApplyTheme, [{ key: "getChildContext", value: function getChildContext() { var theme = this.props.theme || {}; var parentThemeContext = _ThemeContext.ThemeContext.getThemeContext(this.context) || {}; if (parentThemeContext.immutable && parentThemeContext.theme) { /*#__PURE__*/ ( /*#__PURE__*/0, _console.warn)(!this.props.theme, '[ApplyTheme] Parent theme is immutable. Cannot apply theme: %O', this.props.theme); theme = parentThemeContext.theme; } else if (parentThemeContext.theme) { theme = (0, _mergeDeep.mergeDeep)(parentThemeContext.theme, theme); } return _ThemeContext.ThemeContext.makeThemeContext(theme, parentThemeContext.immutable || this.props.immutable); } }, { key: "render", value: function render() { return (0, _ensureSingleChild.ensureSingleChild)(this.props.children); } }]); ApplyTheme.displayName = "ApplyTheme"; return ApplyTheme; }(_react.Component); exports.ApplyTheme = ApplyTheme; ApplyTheme.propTypes = { /** * set theme variables to override the defaults */ theme: _propTypes.default.object, /** * accepts only one child (children must be wrapped in a single component/element) */ children: _propTypes.default.node, /** * Prevent overriding this theme via a child ApplyTheme component or theme props */ immutable: _propTypes.default.bool }; ApplyTheme.defaultProps = { theme: void 0, children: null, immutable: false }; ApplyTheme.childContextTypes = _ThemeContext.ThemeContext.types; ApplyTheme.contextTypes = _ThemeContext.ThemeContext.types; ApplyTheme.generateTheme = _themeable.themeable.generateTheme; var _default = ApplyTheme; exports.default = _default;