-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
/
Copy pathuseMediaQuery.ts
197 lines (175 loc) · 6.25 KB
/
useMediaQuery.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
'use client';
import * as React from 'react';
import useEnhancedEffect from '@mui/utils/useEnhancedEffect';
import { getThemeProps } from '../useThemeProps';
import useTheme from '../useThemeWithoutDefault';
/**
* @deprecated Not used internally. Use `MediaQueryListEvent` from lib.dom.d.ts instead.
*/
export interface MuiMediaQueryListEvent {
matches: boolean;
}
/**
* @deprecated Not used internally. Use `MediaQueryList` from lib.dom.d.ts instead.
*/
export interface MuiMediaQueryList {
matches: boolean;
addListener: (listener: MuiMediaQueryListListener) => void;
removeListener: (listener: MuiMediaQueryListListener) => void;
}
/**
* @deprecated Not used internally. Use `(event: MediaQueryListEvent) => void` instead.
*/
export type MuiMediaQueryListListener = (event: MuiMediaQueryListEvent) => void;
export interface UseMediaQueryOptions {
/**
* As `window.matchMedia()` is unavailable on the server,
* it returns a default matches during the first mount.
* @default false
*/
defaultMatches?: boolean;
/**
* You can provide your own implementation of matchMedia.
* This can be used for handling an iframe content window.
*/
matchMedia?: typeof window.matchMedia;
/**
* To perform the server-side hydration, the hook needs to render twice.
* A first time with `defaultMatches`, the value of the server, and a second time with the resolved value.
* This double pass rendering cycle comes with a drawback: it's slower.
* You can set this option to `true` if you use the returned value **only** client-side.
* @default false
*/
noSsr?: boolean;
/**
* You can provide your own implementation of `matchMedia`, it's used when rendering server-side.
*/
ssrMatchMedia?: (query: string) => { matches: boolean };
}
function useMediaQueryOld(
query: string,
defaultMatches: boolean,
matchMedia: typeof window.matchMedia | null,
ssrMatchMedia: ((query: string) => { matches: boolean }) | null,
noSsr: boolean,
): boolean {
const [match, setMatch] = React.useState(() => {
if (noSsr && matchMedia) {
return matchMedia!(query).matches;
}
if (ssrMatchMedia) {
return ssrMatchMedia(query).matches;
}
// Once the component is mounted, we rely on the
// event listeners to return the correct matches value.
return defaultMatches;
});
useEnhancedEffect(() => {
let active = true;
if (!matchMedia) {
return undefined;
}
const queryList = matchMedia!(query);
const updateMatch = () => {
// Workaround Safari wrong implementation of matchMedia
// TODO can we remove it?
// https://github.com/mui/material-ui/pull/17315#issuecomment-528286677
if (active) {
setMatch(queryList.matches);
}
};
updateMatch();
// TODO: Use `addEventListener` once support for Safari < 14 is dropped
queryList.addListener(updateMatch);
return () => {
active = false;
queryList.removeListener(updateMatch);
};
}, [query, matchMedia]);
return match;
}
// eslint-disable-next-line no-useless-concat -- Workaround for https://github.com/webpack/webpack/issues/14814
const maybeReactUseSyncExternalStore: undefined | any = (React as any)['useSyncExternalStore' + ''];
function useMediaQueryNew(
query: string,
defaultMatches: boolean,
matchMedia: typeof window.matchMedia | null,
ssrMatchMedia: ((query: string) => { matches: boolean }) | null,
noSsr: boolean,
): boolean {
const getDefaultSnapshot = React.useCallback(() => defaultMatches, [defaultMatches]);
const getServerSnapshot = React.useMemo(() => {
if (noSsr && matchMedia) {
return () => matchMedia!(query).matches;
}
if (ssrMatchMedia !== null) {
const { matches } = ssrMatchMedia(query);
return () => matches;
}
return getDefaultSnapshot;
}, [getDefaultSnapshot, query, ssrMatchMedia, noSsr, matchMedia]);
const [getSnapshot, subscribe] = React.useMemo(() => {
if (matchMedia === null) {
return [getDefaultSnapshot, () => () => {}];
}
const mediaQueryList = matchMedia(query);
return [
() => mediaQueryList.matches,
(notify: () => void) => {
// TODO: Use `addEventListener` once support for Safari < 14 is dropped
mediaQueryList.addListener(notify);
return () => {
mediaQueryList.removeListener(notify);
};
},
];
}, [getDefaultSnapshot, matchMedia, query]);
const match = maybeReactUseSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
return match;
}
export default function useMediaQuery<Theme = unknown>(
queryInput: string | ((theme: Theme) => string),
options: UseMediaQueryOptions = {},
): boolean {
const theme = useTheme<Theme>();
// Wait for jsdom to support the match media feature.
// All the browsers MUI support have this built-in.
// This defensive check is here for simplicity.
// Most of the time, the match media logic isn't central to people tests.
const supportMatchMedia =
typeof window !== 'undefined' && typeof window.matchMedia !== 'undefined';
const {
defaultMatches = false,
matchMedia = supportMatchMedia ? window.matchMedia : null,
ssrMatchMedia = null,
noSsr = false,
} = getThemeProps({ name: 'MuiUseMediaQuery', props: options, theme });
if (process.env.NODE_ENV !== 'production') {
if (typeof queryInput === 'function' && theme === null) {
console.error(
[
'MUI: The `query` argument provided is invalid.',
'You are providing a function without a theme in the context.',
'One of the parent elements needs to use a ThemeProvider.',
].join('\n'),
);
}
}
let query = typeof queryInput === 'function' ? queryInput(theme) : queryInput;
query = query.replace(/^@media( ?)/m, '');
// TODO: Drop `useMediaQueryOld` and use `use-sync-external-store` shim in `useMediaQueryNew` once the package is stable
const useMediaQueryImplementation =
maybeReactUseSyncExternalStore !== undefined ? useMediaQueryNew : useMediaQueryOld;
const match = useMediaQueryImplementation(
query,
defaultMatches,
matchMedia,
ssrMatchMedia,
noSsr,
);
if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line react-hooks/rules-of-hooks
React.useDebugValue({ query, match });
}
return match;
}