bootstrap-styled
Version:
Bootstrap Styled is a front-end ecosystem for React made with Bootstrap 4 philosophy, using the power of css-in-js thanks to styled-components.
249 lines (187 loc) • 12.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.isWidthDown = exports.isWidthUp = void 0;
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _reactEventListener = _interopRequireDefault(require("react-event-listener"));
var _debounce = _interopRequireDefault(require("debounce"));
var _hoistNonReactStatics = _interopRequireDefault(require("hoist-non-react-statics"));
var _styledComponents = require("styled-components");
var _getDisplayName = _interopRequireDefault(require("./utils/getDisplayName"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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 _typeof(obj) { 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); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
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; }
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; }
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 _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 _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
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); }
/* eslint-disable react/destructuring-assignment, react/jsx-fragments, react/jsx-props-no-spreading */
// By default, returns true if screen width is the same or greater than the given breakpoint.
var isWidthUp = function isWidthUp(gridBreakpoints, breakpoint, width) {
var inclusive = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true;
var breakpointKeys = Object.keys(gridBreakpoints);
if (inclusive) {
return breakpointKeys.indexOf(breakpoint) <= breakpointKeys.indexOf(width);
}
return breakpointKeys.indexOf(breakpoint) < breakpointKeys.indexOf(width);
}; // By default, returns true if screen width is the same or less than the given breakpoint.
exports.isWidthUp = isWidthUp;
var isWidthDown = function isWidthDown(gridBreakpoints, breakpoint, width) {
var inclusive = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true;
var breakpointKeys = Object.keys(gridBreakpoints);
if (inclusive) {
return breakpointKeys.indexOf(width) <= breakpointKeys.indexOf(breakpoint);
}
return breakpointKeys.indexOf(width) < breakpointKeys.indexOf(breakpoint);
};
exports.isWidthDown = isWidthDown;
var withWidth = function withWidth() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return function (Component) {
var _options$withTheme = options.withTheme,
withThemeOption = _options$withTheme === void 0 ? false : _options$withTheme,
_options$noSSR = options.noSSR,
noSSR = _options$noSSR === void 0 ? false : _options$noSSR,
initialWidthOption = options.initialWidth,
_options$logging = options.logging,
logging = _options$logging === void 0 ? false : _options$logging,
_options$resizeInterv = options.resizeInterval,
resizeInterval = _options$resizeInterv === void 0 ? 166 : _options$resizeInterv;
var WithWidth =
/*#__PURE__*/
function (_React$Component) {
_inherits(WithWidth, _React$Component);
function WithWidth(props) {
var _this;
_classCallCheck(this, WithWidth);
_this = _possibleConstructorReturn(this, _getPrototypeOf(WithWidth).call(this, props));
_this.state = {
width: noSSR ? _this.getWidth() : undefined
};
if (typeof window !== 'undefined') {
_this.handleResize = (0, _debounce.default)(function () {
var width2 = _this.getWidth();
if (width2 !== _this.state.width) {
_this.setState({
width: width2
});
}
}, resizeInterval);
}
return _this;
}
_createClass(WithWidth, [{
key: "componentDidMount",
value: function componentDidMount() {
var width = this.getWidth();
if (width !== this.state.width) {
this.setState({
width: width
});
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.handleResize.clear();
}
}, {
key: "getWidth",
value: function getWidth() {
var innerWidth = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : window.innerWidth;
var breakpoints = this.props.theme['$grid-breakpoints'];
var breakpointKeys = Object.keys(breakpoints);
var width = null;
/**
* Start with the slowest value as low end devices often have a small screen.
*
* innerWidth |xs sm md lg xl xxl
* |-------|-------|-------|-------|------|------>
* width | xs | sm | md | lg | xl | xxl
*/
var index = 1;
while (width === null && index < breakpointKeys.length) {
var currentWidth = breakpointKeys[index]; // @media are inclusive, so reproduce the behavior here.
if (innerWidth < parseFloat(breakpoints[currentWidth])) {
width = breakpointKeys[index - 1];
break;
}
index += 1;
}
width = width || 'xl';
return width;
}
}, {
key: "render",
value: function render() {
var _this$props = this.props,
initialWidth = _this$props.initialWidth,
theme = _this$props.theme,
width = _this$props.width,
other = _objectWithoutProperties(_this$props, ["initialWidth", "theme", "width"]);
var more = _objectSpread({
width: width || this.state.width || initialWidth || initialWidthOption
}, other); // When rendering the component on the server,
// we have no idea about the client browser screen width.
// In order to prevent blinks and help the reconciliation of the React tree
// we are not rendering the child component.
//
// An alternative is to use the `initialWidth` property.
if (more.width === undefined) {
return null;
}
if (withThemeOption) {
more.theme = theme;
}
if (logging) {
console.log("widthWidth: [bp=".concat(more.width, "][width=").concat(window.innerWidth, "px]")); // eslint-disable-line no-console
}
return _react.default.createElement(_react.Fragment, null, _react.default.createElement(Component, more), _react.default.createElement(_reactEventListener.default, {
target: "window",
onResize: this.handleResize
}));
}
}]);
return WithWidth;
}(_react.default.Component);
WithWidth.propTypes = {
/**
* As `window.innerWidth` is unavailable on the server,
* we default to rendering an empty component during the first mount.
* In some situation, you might want to use an heuristic to approximate
* the screen width of the client browser screen width.
*
* For instance, you could be using the user-agent or the client-hints.
* https://caniuse.com/#search=client%20hint
*/
initialWidth: _propTypes.default.oneOf(['xs', 'sm', 'md', 'lg', 'xl', 'xxl']),
/**
* @ignore
*/
theme: _propTypes.default.object.isRequired,
/**
* Bypass the width calculation logic.
*/
width: _propTypes.default.oneOf(['xs', 'sm', 'md', 'lg', 'xl', 'xxl'])
};
if (process.env.NODE_ENV !== 'production') {
WithWidth.displayName = "WithWidth(".concat((0, _getDisplayName.default)(Component), ")");
}
(0, _hoistNonReactStatics.default)(WithWidth, Component);
return (0, _styledComponents.withTheme)(WithWidth);
};
};
var _default = withWidth;
exports.default = _default;