UNPKG

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