102 lines
3.4 KiB
JavaScript
102 lines
3.4 KiB
JavaScript
"use strict";
|
|
|
|
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
|
|
|
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.default = useMediaQuery;
|
|
|
|
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
|
|
|
var React = _interopRequireWildcard(require("react"));
|
|
|
|
var _styles = require("@material-ui/styles");
|
|
|
|
function useMediaQuery(queryInput) {
|
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
var theme = (0, _styles.useTheme)();
|
|
var props = (0, _styles.getThemeProps)({
|
|
theme: theme,
|
|
name: 'MuiUseMediaQuery',
|
|
props: {}
|
|
});
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
if (typeof queryInput === 'function' && theme === null) {
|
|
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'));
|
|
}
|
|
}
|
|
|
|
var query = typeof queryInput === 'function' ? queryInput(theme) : queryInput;
|
|
query = query.replace(/^@media( ?)/m, ''); // Wait for jsdom to support the match media feature.
|
|
// All the browsers Material-UI 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.
|
|
|
|
var supportMatchMedia = typeof window !== 'undefined' && typeof window.matchMedia !== 'undefined';
|
|
|
|
var _props$options = (0, _extends2.default)({}, props, options),
|
|
_props$options$defaul = _props$options.defaultMatches,
|
|
defaultMatches = _props$options$defaul === void 0 ? false : _props$options$defaul,
|
|
_props$options$matchM = _props$options.matchMedia,
|
|
matchMedia = _props$options$matchM === void 0 ? supportMatchMedia ? window.matchMedia : null : _props$options$matchM,
|
|
_props$options$noSsr = _props$options.noSsr,
|
|
noSsr = _props$options$noSsr === void 0 ? false : _props$options$noSsr,
|
|
_props$options$ssrMat = _props$options.ssrMatchMedia,
|
|
ssrMatchMedia = _props$options$ssrMat === void 0 ? null : _props$options$ssrMat;
|
|
|
|
var _React$useState = React.useState(function () {
|
|
if (noSsr && supportMatchMedia) {
|
|
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;
|
|
}),
|
|
match = _React$useState[0],
|
|
setMatch = _React$useState[1];
|
|
|
|
React.useEffect(function () {
|
|
var active = true;
|
|
|
|
if (!supportMatchMedia) {
|
|
return undefined;
|
|
}
|
|
|
|
var queryList = matchMedia(query);
|
|
|
|
var updateMatch = function updateMatch() {
|
|
// Workaround Safari wrong implementation of matchMedia
|
|
// TODO can we remove it?
|
|
// https://github.com/mui-org/material-ui/pull/17315#issuecomment-528286677
|
|
if (active) {
|
|
setMatch(queryList.matches);
|
|
}
|
|
};
|
|
|
|
updateMatch();
|
|
queryList.addListener(updateMatch);
|
|
return function () {
|
|
active = false;
|
|
queryList.removeListener(updateMatch);
|
|
};
|
|
}, [query, matchMedia, supportMatchMedia]);
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
React.useDebugValue({
|
|
query: query,
|
|
match: match
|
|
});
|
|
}
|
|
|
|
return match;
|
|
} |