Skip to content

Commit

Permalink
[useMediaQuery] Convert to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Sep 17, 2021
1 parent 2a52c2a commit 67ac50f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
22 changes: 0 additions & 22 deletions packages/mui-material/src/useMediaQuery/useMediaQuery.d.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,39 @@ import * as React from 'react';
import { getThemeProps, useThemeWithoutDefault as useTheme } from '@mui/system';
import useEnhancedEffect from '../utils/useEnhancedEffect';

export default function useMediaQuery(queryInput, options = {}) {
const theme = useTheme();
/**
* @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 Options {
defaultMatches?: boolean;
matchMedia?: typeof window.matchMedia;
noSsr?: boolean;
ssrMatchMedia?: (query: string) => { matches: boolean };
}

export default function useMediaQuery<Theme = unknown>(
queryInput: string | ((theme: Theme) => string),
options: Options = {},
): 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.
Expand Down Expand Up @@ -34,7 +65,7 @@ export default function useMediaQuery(queryInput, options = {}) {

const [match, setMatch] = React.useState(() => {
if (noSsr && supportMatchMedia) {
return matchMedia(query).matches;
return matchMedia!(query).matches;
}
if (ssrMatchMedia) {
return ssrMatchMedia(query).matches;
Expand All @@ -52,7 +83,7 @@ export default function useMediaQuery(queryInput, options = {}) {
return undefined;
}

const queryList = matchMedia(query);
const queryList = matchMedia!(query);
const updateMatch = () => {
// Workaround Safari wrong implementation of matchMedia
// TODO can we remove it?
Expand Down

0 comments on commit 67ac50f

Please sign in to comment.