diff --git a/docs/src/pages/components/use-media-query/use-media-query.md b/docs/src/pages/components/use-media-query/use-media-query.md index e1fa0ab49c7f22..ca29e673cd981e 100644 --- a/docs/src/pages/components/use-media-query/use-media-query.md +++ b/docs/src/pages/components/use-media-query/use-media-query.md @@ -164,12 +164,13 @@ You can reproduce the same behavior with a `useWidth` hook: - `options.defaultMatches` (*Boolean* [optional]): As `window.matchMedia()` is unavailable on the server, we return a default matches during the first mount. The default value is `false`. + - `options.matchMedia` (*Function* [optional]) You can provide your own implementation of *matchMedia*. This can be used for handling an iframe content window. - `options.noSsr` (*Boolean* [optional]): Defaults to `false`. In order to perform the server-side rendering reconciliation, it needs to render twice. A first time with nothing and a second time with the children. This double pass rendering cycle comes with a drawback. It's slower. You can set this flag to `true` if you are **not doing server-side rendering**. - - `options.ssrMatchMedia` (*Function* [optional]) You can provide your own implementation of *matchMedia*. This especially useful for [server-side rendering support](#server-side-rendering). + - `options.ssrMatchMedia` (*Function* [optional]) You can provide your own implementation of *matchMedia* in a [server-side rendering context](#server-side-rendering). Note: You can change the default options using the [`default props`](/customization/globals/#default-props) feature of the theme with the `MuiUseMediaQuery` key. diff --git a/packages/material-ui/src/useMediaQuery/useMediaQuery.js b/packages/material-ui/src/useMediaQuery/useMediaQuery.js index 2cb368de0bed09..eda8ec7b38b5c2 100644 --- a/packages/material-ui/src/useMediaQuery/useMediaQuery.js +++ b/packages/material-ui/src/useMediaQuery/useMediaQuery.js @@ -31,14 +31,19 @@ function useMediaQuery(queryInput, options = {}) { const supportMatchMedia = typeof window !== 'undefined' && typeof window.matchMedia !== 'undefined'; - const { defaultMatches = false, noSsr = false, ssrMatchMedia = null } = { + const { + defaultMatches = false, + matchMedia = supportMatchMedia ? window.matchMedia : null, + noSsr = false, + ssrMatchMedia = null, + } = { ...props, ...options, }; const [match, setMatch] = React.useState(() => { if (noSsr && supportMatchMedia) { - return window.matchMedia(query).matches; + return matchMedia(query).matches; } if (ssrMatchMedia) { return ssrMatchMedia(query).matches; @@ -56,7 +61,7 @@ function useMediaQuery(queryInput, options = {}) { return undefined; } - const queryList = window.matchMedia(query); + const queryList = matchMedia(query); const updateMatch = () => { // Workaround Safari wrong implementation of matchMedia // TODO can we remove it? @@ -71,7 +76,7 @@ function useMediaQuery(queryInput, options = {}) { active = false; queryList.removeListener(updateMatch); }; - }, [query, supportMatchMedia]); + }, [query, matchMedia, supportMatchMedia]); return match; }