@@ -36,46 +36,65 @@ export const Hidden = ({
3636 const theme = useTheme ( ) ;
3737 const onlyValues = Array . isArray ( only ) ? only : only ? [ only ] : [ ] ;
3838
39- const xsOnly = useMediaQuery ( theme . breakpoints . only ( 'xs' ) ) ;
40- const smOnly = useMediaQuery ( theme . breakpoints . only ( 'sm' ) ) ;
41- const mdOnly = useMediaQuery ( theme . breakpoints . only ( 'md' ) ) ;
42- const lgOnly = useMediaQuery ( theme . breakpoints . only ( 'lg' ) ) ;
43- const xlOnly = useMediaQuery ( theme . breakpoints . only ( 'xl' ) ) ;
39+ const conditions : string [ ] = [ ] ;
4440
45- const xsUpMatch = useMediaQuery ( theme . breakpoints . up ( 'xs' ) ) ;
46- const smUpMatch = useMediaQuery ( theme . breakpoints . up ( 'sm' ) ) ;
47- const mdUpMatch = useMediaQuery ( theme . breakpoints . up ( 'md' ) ) ;
48- const lgUpMatch = useMediaQuery ( theme . breakpoints . up ( 'lg' ) ) ;
49- const xlUpMatch = useMediaQuery ( theme . breakpoints . up ( 'xl' ) ) ;
41+ const extractCondition = ( mediaQuery : string ) =>
42+ mediaQuery . replace ( / ^ @ m e d i a \s * / i, '' ) ;
5043
51- const xsDownMatch = useMediaQuery ( theme . breakpoints . down ( 'xs' ) ) ;
52- const smDownMatch = useMediaQuery ( theme . breakpoints . down ( 'sm' ) ) ;
53- const mdDownMatch = useMediaQuery ( theme . breakpoints . down ( 'md' ) ) ;
54- const lgDownMatch = useMediaQuery ( theme . breakpoints . down ( 'lg' ) ) ;
55- const xlDownMatch = useMediaQuery ( theme . breakpoints . down ( 'xl' ) ) ;
44+ if ( onlyValues . includes ( 'xs' ) ) {
45+ conditions . push ( extractCondition ( theme . breakpoints . only ( 'xs' ) ) ) ;
46+ }
47+ if ( onlyValues . includes ( 'sm' ) ) {
48+ conditions . push ( extractCondition ( theme . breakpoints . only ( 'sm' ) ) ) ;
49+ }
50+ if ( onlyValues . includes ( 'md' ) ) {
51+ conditions . push ( extractCondition ( theme . breakpoints . only ( 'md' ) ) ) ;
52+ }
53+ if ( onlyValues . includes ( 'lg' ) ) {
54+ conditions . push ( extractCondition ( theme . breakpoints . only ( 'lg' ) ) ) ;
55+ }
56+ if ( onlyValues . includes ( 'xl' ) ) {
57+ conditions . push ( extractCondition ( theme . breakpoints . only ( 'xl' ) ) ) ;
58+ }
59+
60+ if ( xsUp ) {
61+ conditions . push ( extractCondition ( theme . breakpoints . up ( 'xs' ) ) ) ;
62+ }
63+ if ( smUp ) {
64+ conditions . push ( extractCondition ( theme . breakpoints . up ( 'sm' ) ) ) ;
65+ }
66+ if ( mdUp ) {
67+ conditions . push ( extractCondition ( theme . breakpoints . up ( 'md' ) ) ) ;
68+ }
69+ if ( lgUp ) {
70+ conditions . push ( extractCondition ( theme . breakpoints . up ( 'lg' ) ) ) ;
71+ }
72+ if ( xlUp ) {
73+ conditions . push ( extractCondition ( theme . breakpoints . up ( 'xl' ) ) ) ;
74+ }
5675
57- const onlyMatches =
58- ( onlyValues . includes ( 'xs' ) && xsOnly ) ||
59- ( onlyValues . includes ( 'sm' ) && smOnly ) ||
60- ( onlyValues . includes ( 'md' ) && mdOnly ) ||
61- ( onlyValues . includes ( 'lg' ) && lgOnly ) ||
62- ( onlyValues . includes ( 'xl' ) && xlOnly ) ;
76+ if ( xsDown ) {
77+ conditions . push ( extractCondition ( theme . breakpoints . down ( 'xs' ) ) ) ;
78+ }
79+ if ( smDown ) {
80+ conditions . push ( extractCondition ( theme . breakpoints . down ( 'sm' ) ) ) ;
81+ }
82+ if ( mdDown ) {
83+ conditions . push ( extractCondition ( theme . breakpoints . down ( 'md' ) ) ) ;
84+ }
85+ if ( lgDown ) {
86+ conditions . push ( extractCondition ( theme . breakpoints . down ( 'lg' ) ) ) ;
87+ }
88+ if ( xlDown ) {
89+ conditions . push ( extractCondition ( theme . breakpoints . down ( 'xl' ) ) ) ;
90+ }
6391
64- const upMatches =
65- ( xsUp && xsUpMatch ) ||
66- ( smUp && smUpMatch ) ||
67- ( mdUp && mdUpMatch ) ||
68- ( lgUp && lgUpMatch ) ||
69- ( xlUp && xlUpMatch ) ;
92+ const mediaQuery =
93+ conditions . length > 0 ? `@media ${ conditions . join ( ', ' ) } ` : '@media not all' ;
7094
71- const downMatches =
72- ( xsDown && xsDownMatch ) ||
73- ( smDown && smDownMatch ) ||
74- ( mdDown && mdDownMatch ) ||
75- ( lgDown && lgDownMatch ) ||
76- ( xlDown && xlDownMatch ) ;
95+ const matches = useMediaQuery ( mediaQuery ) ;
7796
78- if ( onlyMatches || upMatches || downMatches ) {
97+ if ( matches ) {
7998 return null ;
8099 }
81100
0 commit comments