UNPKG

3.4 kBJavaScriptView Raw
1'use client';
2
3import PropTypes from 'prop-types';
4import exactProp from '@mui/utils/exactProp';
5import withWidth, { isWidthDown, isWidthUp } from "./withWidth.js";
6import useTheme from "../styles/useTheme.js";
7
8/**
9 * @ignore - internal component.
10 */
11function HiddenJs(props) {
12 const {
13 children,
14 only,
15 width
16 } = props;
17 const theme = useTheme();
18 let visible = true;
19
20 // `only` check is faster to get out sooner if used.
21 if (only) {
22 if (Array.isArray(only)) {
23 for (let i = 0; i < only.length; i += 1) {
24 const breakpoint = only[i];
25 if (width === breakpoint) {
26 visible = false;
27 break;
28 }
29 }
30 } else if (only && width === only) {
31 visible = false;
32 }
33 }
34
35 // Allow `only` to be combined with other props. If already hidden, no need to check others.
36 if (visible) {
37 // determine visibility based on the smallest size up
38 for (let i = 0; i < theme.breakpoints.keys.length; i += 1) {
39 const breakpoint = theme.breakpoints.keys[i];
40 const breakpointUp = props[`${breakpoint}Up`];
41 const breakpointDown = props[`${breakpoint}Down`];
42 if (breakpointUp && isWidthUp(breakpoint, width) || breakpointDown && isWidthDown(breakpoint, width)) {
43 visible = false;
44 break;
45 }
46 }
47 }
48 if (!visible) {
49 return null;
50 }
51 return children;
52}
53HiddenJs.propTypes = {
54 /**
55 * The content of the component.
56 */
57 children: PropTypes.node,
58 /**
59 * If `true`, screens this size and down are hidden.
60 */
61 // eslint-disable-next-line react/no-unused-prop-types
62 lgDown: PropTypes.bool,
63 /**
64 * If `true`, screens this size and up are hidden.
65 */
66 // eslint-disable-next-line react/no-unused-prop-types
67 lgUp: PropTypes.bool,
68 /**
69 * If `true`, screens this size and down are hidden.
70 */
71 // eslint-disable-next-line react/no-unused-prop-types
72 mdDown: PropTypes.bool,
73 /**
74 * If `true`, screens this size and up are hidden.
75 */
76 // eslint-disable-next-line react/no-unused-prop-types
77 mdUp: PropTypes.bool,
78 /**
79 * Hide the given breakpoint(s).
80 */
81 only: PropTypes.oneOfType([PropTypes.oneOf(['xs', 'sm', 'md', 'lg', 'xl']), PropTypes.arrayOf(PropTypes.oneOf(['xs', 'sm', 'md', 'lg', 'xl']))]),
82 /**
83 * If `true`, screens this size and down are hidden.
84 */
85 // eslint-disable-next-line react/no-unused-prop-types
86 smDown: PropTypes.bool,
87 /**
88 * If `true`, screens this size and up are hidden.
89 */
90 // eslint-disable-next-line react/no-unused-prop-types
91 smUp: PropTypes.bool,
92 /**
93 * @ignore
94 * width prop provided by withWidth decorator.
95 */
96 width: PropTypes.string.isRequired,
97 /**
98 * If `true`, screens this size and down are hidden.
99 */
100 // eslint-disable-next-line react/no-unused-prop-types
101 xlDown: PropTypes.bool,
102 /**
103 * If `true`, screens this size and up are hidden.
104 */
105 // eslint-disable-next-line react/no-unused-prop-types
106 xlUp: PropTypes.bool,
107 /**
108 * If `true`, screens this size and down are hidden.
109 */
110 // eslint-disable-next-line react/no-unused-prop-types
111 xsDown: PropTypes.bool,
112 /**
113 * If `true`, screens this size and up are hidden.
114 */
115 // eslint-disable-next-line react/no-unused-prop-types
116 xsUp: PropTypes.bool
117};
118if (process.env.NODE_ENV !== 'production') {
119 HiddenJs.propTypes = exactProp(HiddenJs.propTypes);
120}
121export default withWidth()(HiddenJs);
\No newline at end of file