1 | 'use client';
|
2 |
|
3 | import * as React from 'react';
|
4 | import PropTypes from 'prop-types';
|
5 | import getDisplayName from '@mui/utils/getDisplayName';
|
6 | import { getThemeProps } from '@mui/system/useThemeProps';
|
7 | import useTheme from "../styles/useTheme.js";
|
8 | import useEnhancedEffect from "../utils/useEnhancedEffect.js";
|
9 | import useMediaQuery from "../useMediaQuery/index.js";
|
10 | import { jsx as _jsx } from "react/jsx-runtime";
|
11 | const breakpointKeys = ['xs', 'sm', 'md', 'lg', 'xl'];
|
12 |
|
13 |
|
14 | export const isWidthUp = (breakpoint, width, inclusive = true) => {
|
15 | if (inclusive) {
|
16 | return breakpointKeys.indexOf(breakpoint) <= breakpointKeys.indexOf(width);
|
17 | }
|
18 | return breakpointKeys.indexOf(breakpoint) < breakpointKeys.indexOf(width);
|
19 | };
|
20 |
|
21 |
|
22 | export const isWidthDown = (breakpoint, width, inclusive = false) => {
|
23 | if (inclusive) {
|
24 | return breakpointKeys.indexOf(width) <= breakpointKeys.indexOf(breakpoint);
|
25 | }
|
26 | return breakpointKeys.indexOf(width) < breakpointKeys.indexOf(breakpoint);
|
27 | };
|
28 | const withWidth = (options = {}) => Component => {
|
29 | const {
|
30 | withTheme: withThemeOption = false,
|
31 | noSSR = false,
|
32 | initialWidth: initialWidthOption
|
33 | } = options;
|
34 | function WithWidth(props) {
|
35 | const contextTheme = useTheme();
|
36 | const theme = props.theme || contextTheme;
|
37 | const {
|
38 | initialWidth,
|
39 | width,
|
40 | ...other
|
41 | } = getThemeProps({
|
42 | theme,
|
43 | name: 'MuiWithWidth',
|
44 | props
|
45 | });
|
46 | const [mountedState, setMountedState] = React.useState(false);
|
47 | useEnhancedEffect(() => {
|
48 | setMountedState(true);
|
49 | }, []);
|
50 |
|
51 | |
52 |
|
53 |
|
54 |
|
55 |
|
56 | const keys = theme.breakpoints.keys.slice().reverse();
|
57 | const widthComputed = keys.reduce((output, key) => {
|
58 |
|
59 |
|
60 | const matches = useMediaQuery(theme.breakpoints.up(key));
|
61 | return !output && matches ? key : output;
|
62 | }, null);
|
63 | const more = {
|
64 | width: width || (mountedState || noSSR ? widthComputed : undefined) || initialWidth || initialWidthOption,
|
65 | ...(withThemeOption ? {
|
66 | theme
|
67 | } : {}),
|
68 | ...other
|
69 | };
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | if (more.width === undefined) {
|
78 | return null;
|
79 | }
|
80 | return _jsx(Component, {
|
81 | ...more
|
82 | });
|
83 | }
|
84 | process.env.NODE_ENV !== "production" ? WithWidth.propTypes = {
|
85 | |
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | initialWidth: PropTypes.oneOf(['xs', 'sm', 'md', 'lg', 'xl']),
|
95 | |
96 |
|
97 |
|
98 | theme: PropTypes.object,
|
99 | |
100 |
|
101 |
|
102 | width: PropTypes.oneOf(['xs', 'sm', 'md', 'lg', 'xl'])
|
103 | } : void 0;
|
104 | if (process.env.NODE_ENV !== 'production') {
|
105 | WithWidth.displayName = `WithWidth(${getDisplayName(Component)})`;
|
106 | }
|
107 | return WithWidth;
|
108 | };
|
109 | export default withWidth; |
\ | No newline at end of file |