Skip to content

[useMediaQuery] Add warning and docs for using useMediaQuery('print') (@good-jinu) #45886

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

Merged
merged 2 commits into from
Apr 12, 2025
Merged
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,11 @@ 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).
:::warning
Using the query `'print'` to modify a document for printing is not supported, as changes made in re-rendering may not be accurately reflected.
You can use the `sx` prop's `displayPrint` field for this purpose instead.
See [MUI System—Display in print](/system/display/#display-in-print) for more details.
:::

## Using breakpoint helpers

Expand Down
11 changes: 11 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,17 @@ 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 \`print\` query to the \`useMediaQuery\` hook.`,
'Using the print media query to modify print styles can lead to unexpected results.',
'Consider using the `displayPrint` field in the `sx` prop instead.',
'More information about `displayPrint` on our docs: https://mui.com/system/display/#display-in-print.',
].join('\n'),
);
}

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