Skip to content
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

[material-ui][useMediaQuery] Add warning for using useMediaQuery('print') #44790

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ The media query string can be any valid CSS media query, for example [`'(prefers

{{"demo": "SimpleMediaQuery.js", "defaultCodeOpen": true}}

⚠️ You can't use `'print'` per browsers limitation, for example [Firefox](https://bugzilla.mozilla.org/show_bug.cgi?id=774398).
⚠️ Using the query `'print'` to modify a printing document isn't recommended, as it may not accurately reflect re-rendering changes.

## Using breakpoint helpers

Expand Down
10 changes: 10 additions & 0 deletions packages/mui-system/src/useMediaQuery/useMediaQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ export function unstable_createUseMediaQuery(params: { themeId?: string } = {})
let query = typeof queryInput === 'function' ? queryInput(theme) : queryInput;
query = query.replace(/^@media( ?)/m, '');

if (query.includes('print')) {
console.warn(
[
`MUI: You have provided a query \`print\` for the useMediaQuery.`,
'Using the print media query to modify print styles might lead to unexpected results.',
'Consider using the `displayPrint` field in the `sx` prop instead.',
].join('\n'),
);
}

const useMediaQueryImplementation =
maybeReactUseSyncExternalStore !== undefined ? useMediaQueryNew : useMediaQueryOld;
const match = useMediaQueryImplementation(
Expand Down
Loading