UNPKG

3.32 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.default = void 0;
9
10var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
11
12var _react = _interopRequireDefault(require("react"));
13
14var _styles = require("@material-ui/styles");
15
16function useMediaQuery(queryInput) {
17 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
18 var theme = (0, _styles.useTheme)();
19 var props = (0, _styles.getThemeProps)({
20 theme: theme,
21 name: 'MuiUseMediaQuery',
22 props: {}
23 });
24
25 if (process.env.NODE_ENV !== 'production') {
26 if (typeof queryInput === 'function' && theme === null) {
27 console.error(['Material-UI: 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'));
28 }
29 }
30
31 var query = typeof queryInput === 'function' ? queryInput(theme) : queryInput;
32 query = query.replace(/^@media( ?)/m, ''); // Wait for jsdom to support the match media feature.
33 // All the browsers Material-UI support have this built-in.
34 // This defensive check is here for simplicity.
35 // Most of the time, the match media logic isn't central to people tests.
36
37 var supportMatchMedia = typeof window !== 'undefined' && typeof window.matchMedia !== 'undefined';
38
39 var _props$options = (0, _extends2.default)({}, props, {}, options),
40 _props$options$defaul = _props$options.defaultMatches,
41 defaultMatches = _props$options$defaul === void 0 ? false : _props$options$defaul,
42 _props$options$matchM = _props$options.matchMedia,
43 matchMedia = _props$options$matchM === void 0 ? supportMatchMedia ? window.matchMedia : null : _props$options$matchM,
44 _props$options$noSsr = _props$options.noSsr,
45 noSsr = _props$options$noSsr === void 0 ? false : _props$options$noSsr,
46 _props$options$ssrMat = _props$options.ssrMatchMedia,
47 ssrMatchMedia = _props$options$ssrMat === void 0 ? null : _props$options$ssrMat;
48
49 var _React$useState = _react.default.useState(function () {
50 if (noSsr && supportMatchMedia) {
51 return matchMedia(query).matches;
52 }
53
54 if (ssrMatchMedia) {
55 return ssrMatchMedia(query).matches;
56 } // Once the component is mounted, we rely on the
57 // event listeners to return the correct matches value.
58
59
60 return defaultMatches;
61 }),
62 match = _React$useState[0],
63 setMatch = _React$useState[1];
64
65 _react.default.useEffect(function () {
66 var active = true;
67
68 if (!supportMatchMedia) {
69 return undefined;
70 }
71
72 var queryList = matchMedia(query);
73
74 var updateMatch = function updateMatch() {
75 // Workaround Safari wrong implementation of matchMedia
76 // TODO can we remove it?
77 // https://github.com/mui-org/material-ui/pull/17315#issuecomment-528286677
78 if (active) {
79 setMatch(queryList.matches);
80 }
81 };
82
83 updateMatch();
84 queryList.addListener(updateMatch);
85 return function () {
86 active = false;
87 queryList.removeListener(updateMatch);
88 };
89 }, [query, matchMedia, supportMatchMedia]);
90
91 return match;
92}
93
94var _default = useMediaQuery;
95exports.default = _default;
\No newline at end of file