Skip to content

Commit 07aa35d

Browse files
committed
Optimize Hidden component: compute only required media queries
Signed-off-by: Sbragul26 <sbragul26@gmail.com>
1 parent 8ca4849 commit 07aa35d

File tree

1 file changed

+25
-55
lines changed

1 file changed

+25
-55
lines changed

src/base/Hidden/Hidden.tsx

Lines changed: 25 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useMediaQuery, useTheme } from '@mui/material';
2-
import React from 'react';
2+
import React, { useMemo } from 'react';
33

44
type 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(/^@media\s*/i, '');
25+
2226
export const Hidden = ({
2327
children,
2428
only,
@@ -34,63 +38,29 @@ export const Hidden = ({
3438
xlDown = false
3539
}: HiddenProps) => {
3640
const theme = useTheme();
37-
const onlyValues = Array.isArray(only) ? only : only ? [only] : [];
38-
39-
const conditions: string[] = [];
40-
41-
const extractCondition = (mediaQuery: string) =>
42-
mediaQuery.replace(/^@media\s*/i, '');
4341

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-
}
42+
const upProps: Record<Breakpoint, boolean> = { xs: xsUp, sm: smUp, md: mdUp, lg: lgUp, xl: xlUp };
43+
const downProps: Record<Breakpoint, boolean> = { xs: xsDown, sm: smDown, md: mdDown, lg: lgDown, xl: xlDown };
5944

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-
}
45+
const mediaQuery = useMemo(() => {
46+
const onlyValues = Array.isArray(only) ? only : only ? [only] : [];
47+
const conditions: string[] = [];
7548

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-
}
49+
for (const bp of BREAKPOINTS) {
50+
if (onlyValues.includes(bp)) {
51+
conditions.push(extractCondition(theme.breakpoints.only(bp)));
52+
}
53+
if (upProps[bp]) {
54+
conditions.push(extractCondition(theme.breakpoints.up(bp)));
55+
}
56+
if (downProps[bp]) {
57+
conditions.push(extractCondition(theme.breakpoints.down(bp)));
58+
}
59+
}
9160

92-
const mediaQuery =
93-
conditions.length > 0 ? `@media ${conditions.join(', ')}` : '@media not all';
61+
return conditions.length > 0 ? `@media ${conditions.join(', ')}` : '@media not all';
62+
// eslint-disable-next-line react-hooks/exhaustive-deps
63+
}, [only, xsUp, smUp, mdUp, lgUp, xlUp, xsDown, smDown, mdDown, lgDown, xlDown, theme.breakpoints]);
9464

9565
const matches = useMediaQuery(mediaQuery);
9666

0 commit comments

Comments
 (0)