UNPKG

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