UNPKG

4.35 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
4
5var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
6
7Object.defineProperty(exports, "__esModule", {
8 value: true
9});
10exports.default = void 0;
11
12var _propTypes = _interopRequireDefault(require("prop-types"));
13
14var _utils = require("@material-ui/utils");
15
16var _withWidth = _interopRequireWildcard(require("../withWidth"));
17
18var _useTheme = _interopRequireDefault(require("../styles/useTheme"));
19
20/**
21 * @ignore - internal component.
22 */
23function HiddenJs(props) {
24 var children = props.children,
25 only = props.only,
26 width = props.width;
27 var theme = (0, _useTheme.default)();
28 var visible = true; // `only` check is faster to get out sooner if used.
29
30 if (only) {
31 if (Array.isArray(only)) {
32 for (var i = 0; i < only.length; i += 1) {
33 var breakpoint = only[i];
34
35 if (width === breakpoint) {
36 visible = false;
37 break;
38 }
39 }
40 } else if (only && width === only) {
41 visible = false;
42 }
43 } // Allow `only` to be combined with other props. If already hidden, no need to check others.
44
45
46 if (visible) {
47 // determine visibility based on the smallest size up
48 for (var _i = 0; _i < theme.breakpoints.keys.length; _i += 1) {
49 var _breakpoint = theme.breakpoints.keys[_i];
50 var breakpointUp = props["".concat(_breakpoint, "Up")];
51 var breakpointDown = props["".concat(_breakpoint, "Down")];
52
53 if (breakpointUp && (0, _withWidth.isWidthUp)(_breakpoint, width) || breakpointDown && (0, _withWidth.isWidthDown)(_breakpoint, width)) {
54 visible = false;
55 break;
56 }
57 }
58 }
59
60 if (!visible) {
61 return null;
62 }
63
64 return children;
65}
66
67HiddenJs.propTypes = {
68 /**
69 * The content of the component.
70 */
71 children: _propTypes.default.node,
72
73 /**
74 * @ignore
75 */
76 className: _propTypes.default.string,
77
78 /**
79 * Specify which implementation to use. 'js' is the default, 'css' works better for
80 * server-side rendering.
81 */
82 implementation: _propTypes.default.oneOf(['js', 'css']),
83
84 /**
85 * You can use this prop when choosing the `js` implementation with server-side rendering.
86 *
87 * As `window.innerWidth` is unavailable on the server,
88 * we default to rendering an empty component during the first mount.
89 * You might want to use an heuristic to approximate
90 * the screen width of the client browser screen width.
91 *
92 * For instance, you could be using the user-agent or the client-hints.
93 * https://caniuse.com/#search=client%20hint
94 */
95 initialWidth: _propTypes.default.oneOf(['xs', 'sm', 'md', 'lg', 'xl']),
96
97 /**
98 * If `true`, screens this size and down will be hidden.
99 */
100 lgDown: _propTypes.default.bool,
101
102 /**
103 * If `true`, screens this size and up will be hidden.
104 */
105 lgUp: _propTypes.default.bool,
106
107 /**
108 * If `true`, screens this size and down will be hidden.
109 */
110 mdDown: _propTypes.default.bool,
111
112 /**
113 * If `true`, screens this size and up will be hidden.
114 */
115 mdUp: _propTypes.default.bool,
116
117 /**
118 * Hide the given breakpoint(s).
119 */
120 only: _propTypes.default.oneOfType([_propTypes.default.oneOf(['xs', 'sm', 'md', 'lg', 'xl']), _propTypes.default.arrayOf(_propTypes.default.oneOf(['xs', 'sm', 'md', 'lg', 'xl']))]),
121
122 /**
123 * If `true`, screens this size and down will be hidden.
124 */
125 smDown: _propTypes.default.bool,
126
127 /**
128 * If `true`, screens this size and up will be hidden.
129 */
130 smUp: _propTypes.default.bool,
131
132 /**
133 * @ignore
134 * width prop provided by withWidth decorator.
135 */
136 width: _propTypes.default.string.isRequired,
137
138 /**
139 * If `true`, screens this size and down will be hidden.
140 */
141 xlDown: _propTypes.default.bool,
142
143 /**
144 * If `true`, screens this size and up will be hidden.
145 */
146 xlUp: _propTypes.default.bool,
147
148 /**
149 * If `true`, screens this size and down will be hidden.
150 */
151 xsDown: _propTypes.default.bool,
152
153 /**
154 * If `true`, screens this size and up will be hidden.
155 */
156 xsUp: _propTypes.default.bool
157};
158
159if (process.env.NODE_ENV !== 'production') {
160 HiddenJs.propTypes = (0, _utils.exactProp)(HiddenJs.propTypes);
161}
162
163var _default = (0, _withWidth.default)()(HiddenJs);
164
165exports.default = _default;
\No newline at end of file