11import { useMediaQuery , useTheme } from '@mui/material' ;
2- import React from 'react' ;
2+ import React , { useMemo } from 'react' ;
33
44type Breakpoint = 'xs' | 'sm' | 'md' | 'lg' | 'xl' ;
55
@@ -16,9 +16,13 @@ export interface HiddenProps {
1616 mdDown ?: boolean ;
1717 lgDown ?: boolean ;
1818 xlDown ?: boolean ;
19-
2019}
2120
21+ const BREAKPOINTS : Breakpoint [ ] = [ 'xs' , 'sm' , 'md' , 'lg' , 'xl' ] ;
22+
23+ const extractCondition = ( mediaQuery : string ) =>
24+ mediaQuery . replace ( / ^ @ m e d i a \s * / i, '' ) ;
25+
2226export const Hidden = ( {
2327 children,
2428 only,
@@ -34,63 +38,27 @@ export const Hidden = ({
3438 xlDown = false
3539} : HiddenProps ) => {
3640 const theme = useTheme ( ) ;
37- const onlyValues = Array . isArray ( only ) ? only : only ? [ only ] : [ ] ;
3841
39- const conditions : string [ ] = [ ] ;
42+ const mediaQuery = useMemo ( ( ) => {
43+ const onlyValues = Array . isArray ( only ) ? only : only ? [ only ] : [ ] ;
44+ const upProps : Record < Breakpoint , boolean > = { xs : xsUp , sm : smUp , md : mdUp , lg : lgUp , xl : xlUp } ;
45+ const downProps : Record < Breakpoint , boolean > = { xs : xsDown , sm : smDown , md : mdDown , lg : lgDown , xl : xlDown } ;
46+ const conditions : string [ ] = [ ] ;
4047
41- const extractCondition = ( mediaQuery : string ) =>
42- mediaQuery . replace ( / ^ @ m e d i a \s * / i, '' ) ;
43-
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- }
75-
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- }
48+ for ( const bp of BREAKPOINTS ) {
49+ if ( onlyValues . includes ( bp ) ) {
50+ conditions . push ( extractCondition ( theme . breakpoints . only ( bp ) ) ) ;
51+ }
52+ if ( upProps [ bp ] ) {
53+ conditions . push ( extractCondition ( theme . breakpoints . up ( bp ) ) ) ;
54+ }
55+ if ( downProps [ bp ] ) {
56+ conditions . push ( extractCondition ( theme . breakpoints . down ( bp ) ) ) ;
57+ }
58+ }
9159
92- const mediaQuery =
93- conditions . length > 0 ? `@media ${ conditions . join ( ', ' ) } ` : '@media not all' ;
60+ return conditions . length > 0 ? `@media ${ conditions . join ( ', ' ) } ` : '@media not all' ;
61+ } , [ only , xsUp , smUp , mdUp , lgUp , xlUp , xsDown , smDown , mdDown , lgDown , xlDown , theme . breakpoints ] ) ;
9462
9563 const matches = useMediaQuery ( mediaQuery ) ;
9664
0 commit comments