UNPKG

@bootstrap-styled/provider

Version:

BootstrapProvider and UtilityProvider components help you manage reset, global styles and original bootstrap class utilities

255 lines (191 loc) 11.2 kB
"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } Object.defineProperty(exports, "__esModule", { value: true }); exports.default = exports.propTypes = exports.defaultProps = void 0; var _react = _interopRequireDefault(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _styledComponents = require("styled-components"); var _bootstrapStyled = require("bootstrap-styled"); var _UtilityProvider = _interopRequireWildcard(require("../UtilityProvider")); var _GlobalStyle = _interopRequireDefault(require("./GlobalStyle")); function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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 _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } var defaultProps = { theme: _bootstrapStyled.theme, injectGlobal: false, reset: false, utils: _UtilityProvider.defaultProps.utils }; exports.defaultProps = defaultProps; var propTypes = { /** Specified node element will be passed as children of `<BootstrapProvider />` component. */ children: _propTypes.default.node.isRequired, /** Define if BootstrapProvider is in charge of calling getGlobalStyles for you, it can only be run once. */ injectGlobal: _propTypes.default.bool, /** Define if BootstrapProvider is in charge of injecting the reset css for you, see http://meyerweb.com/eric/tools/css/reset/ for more details */ reset: _propTypes.default.bool, /** Theme variables. */ theme: _propTypes.default.object, /** Utilities variables. */ utils: _propTypes.default.shape({ /** v4.4.0 implementation for all screen utilities */ screen: _propTypes.default.bool, /** v4.4.0 implementation for all print utilities */ print: _propTypes.default.bool, /** v4.0.0 implementation align theme variables. */ align: _propTypes.default.bool, /** v4.0.0 implementation background theme variables. */ background: _propTypes.default.bool, /** v4.0.0 implementation border theme variables. */ border: _propTypes.default.bool, /** v4.0.0 implementation display theme variables. */ display: _propTypes.default.bool, /** v4.0.0 implementation flex theme variables. */ flex: _propTypes.default.bool, /** v4.0.0 implementation float theme variables. */ float: _propTypes.default.bool, /** v4.0.0 implementation position theme variables. */ position: _propTypes.default.bool, /** v4.0.0 implementation screenreaders theme variables. */ screenreaders: _propTypes.default.bool, /** v4.0.0 implementation sizing theme variables. */ sizing: _propTypes.default.bool, /** v4.0.0 implementation spacing theme variables. */ spacing: _propTypes.default.bool, /** v4.0.0 implementation text theme variables. */ text: _propTypes.default.bool, /** v4.0.0 implementation visibility theme variables. */ visibility: _propTypes.default.bool, /** add a .clearfix class. */ clearfix: _propTypes.default.bool, /** add cursor utilities. */ cursor: _propTypes.default.bool, /** add transition utilities. */ transition: _propTypes.default.bool }) }; exports.propTypes = propTypes; var BootstrapProvider = /*#__PURE__*/function (_React$Component) { _inherits(BootstrapProvider, _React$Component); var _super = _createSuper(BootstrapProvider); function BootstrapProvider() { var _this; _classCallCheck(this, BootstrapProvider); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _super.call.apply(_super, [this].concat(args)); _defineProperty(_assertThisInitialized(_this), "state", { theme: {}, // isWindowPhone8Fixed is used here for testing purposes isWindowPhone8Fixed: null // eslint-disable-line react/no-unused-state }); _defineProperty(_assertThisInitialized(_this), "setTheme", function (_ref, cb) { var userTheme = _ref.theme; var theme = _this.makeTheme({ theme: userTheme }); _this.setState({ theme: theme }, cb); }); _defineProperty(_assertThisInitialized(_this), "makeTheme", function (_ref2) { var userTheme = _ref2.theme; var theme = (0, _bootstrapStyled.makeTheme)(userTheme); var metaKeyList = Object.keys(theme).filter(function (f) { return f[0] === '_'; }); metaKeyList.forEach(function (k) { delete theme[k]; }); return theme; }); return _this; } _createClass(BootstrapProvider, [{ key: "UNSAFE_componentWillMount", // eslint-disable-next-line camelcase value: function UNSAFE_componentWillMount() { this.setTheme(this.props, this.injectGlobal); } }, { key: "componentDidMount", value: function componentDidMount() { this.windowPhone8Fix(); } /** * On update props * @param nextProps */ // eslint-disable-next-line camelcase }, { key: "UNSAFE_componentWillReceiveProps", value: function UNSAFE_componentWillReceiveProps(nextProps) { /** * We only replace the theme if the filtered theme is different from the current one */ if (JSON.stringify(this.makeTheme(this.props)) !== JSON.stringify(nextProps.theme)) { this.setTheme(nextProps); } } /** * setTheme is in charge of replacing the theme, it does not filter it, use makeTheme for that * @param userTheme * @param cb */ }, { key: "windowPhone8Fix", value: function windowPhone8Fix() { // Copyright 2014-2017 The Bootstrap Authors // Copyright 2014-2017 Twitter, Inc. // Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE) if (navigator.userAgent.match(/IEMobile\/10\.0/)) { var msViewportStyle = document.createElement('style'); msViewportStyle.appendChild(document.createTextNode('@-ms-viewport{width:auto!important}')); document.head.appendChild(msViewportStyle); this.setState({ isWindowPhone8Fixed: true // eslint-disable-line react/no-unused-state }); } } }, { key: "render", value: function render() { var _this$props = this.props, children = _this$props.children, utils = _this$props.utils, reset = _this$props.reset, injectGlobal = _this$props.injectGlobal; var theme = this.state.theme; return /*#__PURE__*/_react.default.createElement(_styledComponents.ThemeProvider, { theme: theme }, /*#__PURE__*/_react.default.createElement(_UtilityProvider.default, { utils: utils }, (reset || injectGlobal) && /*#__PURE__*/_react.default.createElement(_GlobalStyle.default, { reset: reset, injectGlobal: injectGlobal }), children)); } }]); return BootstrapProvider; }(_react.default.Component); _defineProperty(BootstrapProvider, "propTypes", propTypes); _defineProperty(BootstrapProvider, "defaultProps", defaultProps); BootstrapProvider.defaultProps = defaultProps; BootstrapProvider.propTypes = propTypes; var _default = BootstrapProvider; exports.default = _default;