UNPKG

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