@material-ui/core
Version:
React components that implement Google's Material Design.
154 lines (117 loc) • 5.86 kB
JavaScript
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.isWidthDown = exports.isWidthUp = void 0;
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _react = _interopRequireDefault(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.default.useEffect : _react.default.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.default.useState(false),
_React$useState2 = (0, _slicedToArray2.default)(_React$useState, 2),
mountedState = _React$useState2[0],
setMountedState = _React$useState2[1];
useEnhancedEffect(function () {
setMountedState(true);
}, []);
/**
* innerWidth |xs sm md lg xl
* |-------|-------|-------|-------|------>
* width | xs | sm | md | lg | xl
*/
var keys = (0, _toConsumableArray2.default)(theme.breakpoints.keys).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 _react.default.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;
;