1 | import * as React from 'react';
|
2 | import PropTypes from 'prop-types';
|
3 | import getDisplayName from '@mui/utils/getDisplayName';
|
4 | import { getThemeProps } from '@mui/system/useThemeProps';
|
5 | import useTheme from "../styles/useTheme.js";
|
6 | import useEnhancedEffect from "../utils/useEnhancedEffect.js";
|
7 | import useMediaQuery from "../useMediaQuery/index.js";
|
8 | import { jsx as _jsx } from "react/jsx-runtime";
|
9 | const breakpointKeys = ['xs', 'sm', 'md', 'lg', 'xl'];
|
10 |
|
11 |
|
12 | export 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 |
|
20 | export 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 | };
|
26 | const 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 |
|
51 |
|
52 |
|
53 |
|
54 | const keys = theme.breakpoints.keys.slice().reverse();
|
55 | const widthComputed = keys.reduce((output, key) => {
|
56 |
|
57 |
|
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 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | if (more.width === undefined) {
|
76 | return null;
|
77 | }
|
78 | return _jsx(Component, {
|
79 | ...more
|
80 | });
|
81 | }
|
82 | process.env.NODE_ENV !== "production" ? WithWidth.propTypes = {
|
83 | |
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | initialWidth: PropTypes.oneOf(['xs', 'sm', 'md', 'lg', 'xl']),
|
93 | |
94 |
|
95 |
|
96 | theme: PropTypes.object,
|
97 | |
98 |
|
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 | };
|
107 | export default withWidth; |
\ | No newline at end of file |