UNPKG

4.67 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
3import * as React from 'react';
4import PropTypes from 'prop-types';
5import getDisplayName from '@mui/utils/getDisplayName';
6import { getThemeProps } from '@mui/system/useThemeProps';
7import useTheme from '../styles/useTheme';
8import useEnhancedEffect from '../utils/useEnhancedEffect';
9import useMediaQuery from '../useMediaQuery';
10import { jsx as _jsx } from "react/jsx-runtime";
11var breakpointKeys = ['xs', 'sm', 'md', 'lg', 'xl'];
12
13// By default, returns true if screen width is the same or greater than the given breakpoint.
14export var isWidthUp = function isWidthUp(breakpoint, width) {
15 var inclusive = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
16 if (inclusive) {
17 return breakpointKeys.indexOf(breakpoint) <= breakpointKeys.indexOf(width);
18 }
19 return breakpointKeys.indexOf(breakpoint) < breakpointKeys.indexOf(width);
20};
21
22// By default, returns true if screen width is less than the given breakpoint.
23export var isWidthDown = function isWidthDown(breakpoint, width) {
24 var inclusive = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
25 if (inclusive) {
26 return breakpointKeys.indexOf(width) <= breakpointKeys.indexOf(breakpoint);
27 }
28 return breakpointKeys.indexOf(width) < breakpointKeys.indexOf(breakpoint);
29};
30var withWidth = function withWidth() {
31 var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
32 return function (Component) {
33 var _options$withTheme = options.withTheme,
34 withThemeOption = _options$withTheme === void 0 ? false : _options$withTheme,
35 _options$noSSR = options.noSSR,
36 noSSR = _options$noSSR === void 0 ? false : _options$noSSR,
37 initialWidthOption = options.initialWidth;
38 function WithWidth(props) {
39 var contextTheme = useTheme();
40 var theme = props.theme || contextTheme;
41 var _getThemeProps = getThemeProps({
42 theme: theme,
43 name: 'MuiWithWidth',
44 props: props
45 }),
46 initialWidth = _getThemeProps.initialWidth,
47 width = _getThemeProps.width,
48 other = _objectWithoutProperties(_getThemeProps, ["initialWidth", "width"]);
49 var _React$useState = React.useState(false),
50 mountedState = _React$useState[0],
51 setMountedState = _React$useState[1];
52 useEnhancedEffect(function () {
53 setMountedState(true);
54 }, []);
55
56 /**
57 * innerWidth |xs sm md lg xl
58 * |-------|-------|-------|-------|------>
59 * width | xs | sm | md | lg | xl
60 */
61 var keys = theme.breakpoints.keys.slice().reverse();
62 var widthComputed = keys.reduce(function (output, key) {
63 // eslint-disable-next-line react-hooks/rules-of-hooks
64 var matches = useMediaQuery(theme.breakpoints.up(key));
65 return !output && matches ? key : output;
66 }, null);
67 var more = _extends({
68 width: width || (mountedState || noSSR ? widthComputed : undefined) || initialWidth || initialWidthOption
69 }, withThemeOption ? {
70 theme: theme
71 } : {}, other);
72
73 // When rendering the component on the server,
74 // we have no idea about the client browser screen width.
75 // In order to prevent blinks and help the reconciliation of the React tree
76 // we are not rendering the child component.
77 //
78 // An alternative is to use the `initialWidth` property.
79 if (more.width === undefined) {
80 return null;
81 }
82 return /*#__PURE__*/_jsx(Component, _extends({}, more));
83 }
84 process.env.NODE_ENV !== "production" ? WithWidth.propTypes = {
85 /**
86 * As `window.innerWidth` is unavailable on the server,
87 * we default to rendering an empty component during the first mount.
88 * You might want to use a heuristic to approximate
89 * the screen width of the client browser screen width.
90 *
91 * For instance, you could be using the user-agent or the client-hints.
92 * https://caniuse.com/#search=client%20hint
93 */
94 initialWidth: PropTypes.oneOf(['xs', 'sm', 'md', 'lg', 'xl']),
95 /**
96 * @ignore
97 */
98 theme: PropTypes.object,
99 /**
100 * Bypass the width calculation logic.
101 */
102 width: PropTypes.oneOf(['xs', 'sm', 'md', 'lg', 'xl'])
103 } : void 0;
104 if (process.env.NODE_ENV !== 'production') {
105 WithWidth.displayName = "WithWidth(".concat(getDisplayName(Component), ")");
106 }
107 return WithWidth;
108 };
109};
110export default withWidth;
\No newline at end of file