UNPKG

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