-
Notifications
You must be signed in to change notification settings - Fork 189
Feature/hidden mediaquery optimization #1362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
c543d51
9f7eeaf
630f5c2
5ea5a38
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import { useMediaQuery, useTheme } from '@mui/material'; | ||
| import React from 'react'; | ||
| import React, { useMemo } from 'react'; | ||
|
|
||
| type Breakpoint = 'xs' | 'sm' | 'md' | 'lg' | 'xl'; | ||
|
|
||
|
|
@@ -16,9 +16,13 @@ export interface HiddenProps { | |
| mdDown?: boolean; | ||
| lgDown?: boolean; | ||
| xlDown?: boolean; | ||
|
|
||
| } | ||
|
|
||
| const BREAKPOINTS: Breakpoint[] = ['xs', 'sm', 'md', 'lg', 'xl']; | ||
|
|
||
| const extractCondition = (mediaQuery: string) => | ||
| mediaQuery.replace(/^@media\s*/i, ''); | ||
|
|
||
| export const Hidden = ({ | ||
| children, | ||
| only, | ||
|
|
@@ -34,63 +38,32 @@ export const Hidden = ({ | |
| xlDown = false | ||
| }: HiddenProps) => { | ||
| const theme = useTheme(); | ||
| const onlyValues = Array.isArray(only) ? only : only ? [only] : []; | ||
|
|
||
| const conditions: string[] = []; | ||
|
|
||
| const extractCondition = (mediaQuery: string) => | ||
| mediaQuery.replace(/^@media\s*/i, ''); | ||
|
|
||
| if (onlyValues.includes('xs')) { | ||
| conditions.push(extractCondition(theme.breakpoints.only('xs'))); | ||
| } | ||
| if (onlyValues.includes('sm')) { | ||
| conditions.push(extractCondition(theme.breakpoints.only('sm'))); | ||
| } | ||
| if (onlyValues.includes('md')) { | ||
| conditions.push(extractCondition(theme.breakpoints.only('md'))); | ||
| } | ||
| if (onlyValues.includes('lg')) { | ||
| conditions.push(extractCondition(theme.breakpoints.only('lg'))); | ||
| } | ||
| if (onlyValues.includes('xl')) { | ||
| conditions.push(extractCondition(theme.breakpoints.only('xl'))); | ||
| } | ||
| // Serialize `only` to a stable string so that array literals passed as props | ||
| // (e.g. only={['xs', 'md']}) don't defeat memoization due to new references. | ||
| const onlyKey = Array.isArray(only) ? only.join(',') : only ?? ''; | ||
|
|
||
| if (xsUp) { | ||
| conditions.push(extractCondition(theme.breakpoints.up('xs'))); | ||
| } | ||
| if (smUp) { | ||
| conditions.push(extractCondition(theme.breakpoints.up('sm'))); | ||
| } | ||
| if (mdUp) { | ||
| conditions.push(extractCondition(theme.breakpoints.up('md'))); | ||
| } | ||
| if (lgUp) { | ||
| conditions.push(extractCondition(theme.breakpoints.up('lg'))); | ||
| } | ||
| if (xlUp) { | ||
| conditions.push(extractCondition(theme.breakpoints.up('xl'))); | ||
| } | ||
| const mediaQuery = useMemo(() => { | ||
| const onlyValues = Array.isArray(only) ? only : only ? [only] : []; | ||
| const upProps: Record<Breakpoint, boolean> = { xs: xsUp, sm: smUp, md: mdUp, lg: lgUp, xl: xlUp }; | ||
| const downProps: Record<Breakpoint, boolean> = { xs: xsDown, sm: smDown, md: mdDown, lg: lgDown, xl: xlDown }; | ||
| const conditions: string[] = []; | ||
|
|
||
| if (xsDown) { | ||
| conditions.push(extractCondition(theme.breakpoints.down('xs'))); | ||
| } | ||
| if (smDown) { | ||
| conditions.push(extractCondition(theme.breakpoints.down('sm'))); | ||
| } | ||
| if (mdDown) { | ||
| conditions.push(extractCondition(theme.breakpoints.down('md'))); | ||
| } | ||
| if (lgDown) { | ||
| conditions.push(extractCondition(theme.breakpoints.down('lg'))); | ||
| } | ||
| if (xlDown) { | ||
| conditions.push(extractCondition(theme.breakpoints.down('xl'))); | ||
| } | ||
| for (const bp of BREAKPOINTS) { | ||
| if (onlyValues.includes(bp)) { | ||
| conditions.push(extractCondition(theme.breakpoints.only(bp))); | ||
| } | ||
| if (upProps[bp]) { | ||
| conditions.push(extractCondition(theme.breakpoints.up(bp))); | ||
| } | ||
| if (downProps[bp]) { | ||
| conditions.push(extractCondition(theme.breakpoints.down(bp))); | ||
| } | ||
| } | ||
|
|
||
| const mediaQuery = | ||
| conditions.length > 0 ? `@media ${conditions.join(', ')}` : '@media not all'; | ||
| return conditions.length > 0 ? `@media ${conditions.join(', ')}` : '@media not all'; | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
|
||
| }, [onlyKey, xsUp, smUp, mdUp, lgUp, xlUp, xsDown, smDown, mdDown, lgDown, xlDown, theme.breakpoints]); | ||
|
|
||
| const matches = useMediaQuery(mediaQuery); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
onlyKeyis order-sensitive, which defeats memoization for reordered arrays.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The inner loop iterates BREAKPOINTS in fixed order, so ['xs', 'sm'] and ['sm', 'xs'] produce identical media queries. But their onlyKeys differ, causing unnecessary useMemo cache invalidation.
Fix: Sort before joining: