151 lines
5.5 KiB
JavaScript
151 lines
5.5 KiB
JavaScript
"use strict";
|
|
|
|
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
|
|
|
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.default = exports.isWidthDown = exports.isWidthUp = void 0;
|
|
|
|
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
|
|
|
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
|
|
|
|
var React = _interopRequireWildcard(require("react"));
|
|
|
|
var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
|
|
var _utils = require("@material-ui/utils");
|
|
|
|
var _styles = require("@material-ui/styles");
|
|
|
|
var _hoistNonReactStatics = _interopRequireDefault(require("hoist-non-react-statics"));
|
|
|
|
var _useTheme = _interopRequireDefault(require("../styles/useTheme"));
|
|
|
|
var _createBreakpoints = require("../styles/createBreakpoints");
|
|
|
|
var _useMediaQuery = _interopRequireDefault(require("../useMediaQuery"));
|
|
|
|
// By default, returns true if screen width is the same or greater than the given breakpoint.
|
|
var isWidthUp = function isWidthUp(breakpoint, width) {
|
|
var inclusive = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
|
|
|
|
if (inclusive) {
|
|
return _createBreakpoints.keys.indexOf(breakpoint) <= _createBreakpoints.keys.indexOf(width);
|
|
}
|
|
|
|
return _createBreakpoints.keys.indexOf(breakpoint) < _createBreakpoints.keys.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(breakpoint, width) {
|
|
var inclusive = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
|
|
|
|
if (inclusive) {
|
|
return _createBreakpoints.keys.indexOf(width) <= _createBreakpoints.keys.indexOf(breakpoint);
|
|
}
|
|
|
|
return _createBreakpoints.keys.indexOf(width) < _createBreakpoints.keys.indexOf(breakpoint);
|
|
};
|
|
|
|
exports.isWidthDown = isWidthDown;
|
|
var useEnhancedEffect = typeof window === 'undefined' ? React.useEffect : React.useLayoutEffect;
|
|
|
|
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;
|
|
|
|
function WithWidth(props) {
|
|
var contextTheme = (0, _useTheme.default)();
|
|
var theme = props.theme || contextTheme;
|
|
|
|
var _getThemeProps = (0, _styles.getThemeProps)({
|
|
theme: theme,
|
|
name: 'MuiWithWidth',
|
|
props: (0, _extends2.default)({}, props)
|
|
}),
|
|
initialWidth = _getThemeProps.initialWidth,
|
|
width = _getThemeProps.width,
|
|
other = (0, _objectWithoutProperties2.default)(_getThemeProps, ["initialWidth", "width"]);
|
|
|
|
var _React$useState = React.useState(false),
|
|
mountedState = _React$useState[0],
|
|
setMountedState = _React$useState[1];
|
|
|
|
useEnhancedEffect(function () {
|
|
setMountedState(true);
|
|
}, []);
|
|
/**
|
|
* innerWidth |xs sm md lg xl
|
|
* |-------|-------|-------|-------|------>
|
|
* width | xs | sm | md | lg | xl
|
|
*/
|
|
|
|
var keys = theme.breakpoints.keys.slice().reverse();
|
|
var widthComputed = keys.reduce(function (output, key) {
|
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
var matches = (0, _useMediaQuery.default)(theme.breakpoints.up(key));
|
|
return !output && matches ? key : output;
|
|
}, null);
|
|
var more = (0, _extends2.default)({
|
|
width: width || (mountedState || noSSR ? widthComputed : undefined) || initialWidth || initialWidthOption
|
|
}, withThemeOption ? {
|
|
theme: theme
|
|
} : {}, 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;
|
|
}
|
|
|
|
return /*#__PURE__*/React.createElement(Component, more);
|
|
}
|
|
|
|
process.env.NODE_ENV !== "production" ? WithWidth.propTypes = {
|
|
/**
|
|
* As `window.innerWidth` is unavailable on the server,
|
|
* we default to rendering an empty component during the first mount.
|
|
* 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']),
|
|
|
|
/**
|
|
* @ignore
|
|
*/
|
|
theme: _propTypes.default.object,
|
|
|
|
/**
|
|
* Bypass the width calculation logic.
|
|
*/
|
|
width: _propTypes.default.oneOf(['xs', 'sm', 'md', 'lg', 'xl'])
|
|
} : void 0;
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
WithWidth.displayName = "WithWidth(".concat((0, _utils.getDisplayName)(Component), ")");
|
|
}
|
|
|
|
(0, _hoistNonReactStatics.default)(WithWidth, Component);
|
|
return WithWidth;
|
|
};
|
|
};
|
|
|
|
var _default = withWidth;
|
|
exports.default = _default; |