UNPKG

4.79 kBJavaScriptView Raw
1'use client';
2
3import * as React from 'react';
4import clsx from 'clsx';
5import PropTypes from 'prop-types';
6import composeClasses from '@mui/utils/composeClasses';
7import capitalize from "../utils/capitalize.js";
8import styled from "../styles/styled.js";
9import useTheme from "../styles/useTheme.js";
10import { getHiddenCssUtilityClass } from "./hiddenCssClasses.js";
11import { jsx as _jsx } from "react/jsx-runtime";
12const useUtilityClasses = ownerState => {
13 const {
14 classes,
15 breakpoints
16 } = ownerState;
17 const slots = {
18 root: ['root', ...breakpoints.map(({
19 breakpoint,
20 dir
21 }) => {
22 return dir === 'only' ? `${dir}${capitalize(breakpoint)}` : `${breakpoint}${capitalize(dir)}`;
23 })]
24 };
25 return composeClasses(slots, getHiddenCssUtilityClass, classes);
26};
27
28// FIXME(romgrk): Can't use memoTheme here, should we memo also on ownerState?
29const HiddenCssRoot = styled('div', {
30 name: 'PrivateHiddenCss',
31 slot: 'Root'
32})(({
33 theme,
34 ownerState
35}) => {
36 const hidden = {
37 display: 'none'
38 };
39 return {
40 ...ownerState.breakpoints.map(({
41 breakpoint,
42 dir
43 }) => {
44 if (dir === 'only') {
45 return {
46 [theme.breakpoints.only(breakpoint)]: hidden
47 };
48 }
49 return dir === 'up' ? {
50 [theme.breakpoints.up(breakpoint)]: hidden
51 } : {
52 [theme.breakpoints.down(breakpoint)]: hidden
53 };
54 }).reduce((r, o) => {
55 Object.keys(o).forEach(k => {
56 r[k] = o[k];
57 });
58 return r;
59 }, {})
60 };
61});
62
63/**
64 * @ignore - internal component.
65 */
66function HiddenCss(props) {
67 const {
68 children,
69 className,
70 only,
71 ...other
72 } = props;
73 const theme = useTheme();
74 if (process.env.NODE_ENV !== 'production') {
75 const unknownProps = Object.keys(other).filter(propName => {
76 const isUndeclaredBreakpoint = !theme.breakpoints.keys.some(breakpoint => {
77 return `${breakpoint}Up` === propName || `${breakpoint}Down` === propName;
78 });
79 return !['classes', 'theme', 'isRtl', 'sx'].includes(propName) && isUndeclaredBreakpoint;
80 });
81 if (unknownProps.length > 0) {
82 console.error(`MUI: Unsupported props received by \`<Hidden implementation="css" />\`: ${unknownProps.join(', ')}. Did you forget to wrap this component in a ThemeProvider declaring these breakpoints?`);
83 }
84 }
85 const breakpoints = [];
86 for (let i = 0; i < theme.breakpoints.keys.length; i += 1) {
87 const breakpoint = theme.breakpoints.keys[i];
88 const breakpointUp = other[`${breakpoint}Up`];
89 const breakpointDown = other[`${breakpoint}Down`];
90 if (breakpointUp) {
91 breakpoints.push({
92 breakpoint,
93 dir: 'up'
94 });
95 }
96 if (breakpointDown) {
97 breakpoints.push({
98 breakpoint,
99 dir: 'down'
100 });
101 }
102 }
103 if (only) {
104 const onlyBreakpoints = Array.isArray(only) ? only : [only];
105 onlyBreakpoints.forEach(breakpoint => {
106 breakpoints.push({
107 breakpoint,
108 dir: 'only'
109 });
110 });
111 }
112 const ownerState = {
113 ...props,
114 breakpoints
115 };
116 const classes = useUtilityClasses(ownerState);
117 return /*#__PURE__*/_jsx(HiddenCssRoot, {
118 className: clsx(classes.root, className),
119 ownerState: ownerState,
120 children: children
121 });
122}
123process.env.NODE_ENV !== "production" ? HiddenCss.propTypes = {
124 /**
125 * The content of the component.
126 */
127 children: PropTypes.node,
128 /**
129 * @ignore
130 */
131 className: PropTypes.string,
132 /**
133 * Specify which implementation to use. 'js' is the default, 'css' works better for
134 * server-side rendering.
135 */
136 implementation: PropTypes.oneOf(['js', 'css']),
137 /**
138 * If `true`, screens this size and down are hidden.
139 */
140 lgDown: PropTypes.bool,
141 /**
142 * If `true`, screens this size and up are hidden.
143 */
144 lgUp: PropTypes.bool,
145 /**
146 * If `true`, screens this size and down are hidden.
147 */
148 mdDown: PropTypes.bool,
149 /**
150 * If `true`, screens this size and up are hidden.
151 */
152 mdUp: PropTypes.bool,
153 /**
154 * Hide the given breakpoint(s).
155 */
156 only: PropTypes.oneOfType([PropTypes.oneOf(['xs', 'sm', 'md', 'lg', 'xl']), PropTypes.arrayOf(PropTypes.oneOf(['xs', 'sm', 'md', 'lg', 'xl']))]),
157 /**
158 * If `true`, screens this size and down are hidden.
159 */
160 smDown: PropTypes.bool,
161 /**
162 * If `true`, screens this size and up are hidden.
163 */
164 smUp: PropTypes.bool,
165 /**
166 * If `true`, screens this size and down are hidden.
167 */
168 xlDown: PropTypes.bool,
169 /**
170 * If `true`, screens this size and up are hidden.
171 */
172 xlUp: PropTypes.bool,
173 /**
174 * If `true`, screens this size and down are hidden.
175 */
176 xsDown: PropTypes.bool,
177 /**
178 * If `true`, screens this size and up are hidden.
179 */
180 xsUp: PropTypes.bool
181} : void 0;
182export default HiddenCss;
\No newline at end of file