1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4 |
|
5 | Object.defineProperty(exports, "__esModule", {
|
6 | value: true
|
7 | });
|
8 | exports.default = void 0;
|
9 |
|
10 | var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
11 |
|
12 | var _react = _interopRequireDefault(require("react"));
|
13 |
|
14 | var _styles = require("@material-ui/styles");
|
15 |
|
16 | function 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, '');
|
33 |
|
34 |
|
35 |
|
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 | }
|
57 |
|
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 |
|
76 |
|
77 |
|
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 |
|
94 | var _default = useMediaQuery;
|
95 | exports.default = _default; |
\ | No newline at end of file |