File tree Expand file tree Collapse file tree 3 files changed +13
-6
lines changed
mui-system/src/useMediaQuery Expand file tree Collapse file tree 3 files changed +13
-6
lines changed Original file line number Diff line number Diff line change @@ -30,6 +30,7 @@ export interface UseMediaQueryOptions {
30
30
ssrMatchMedia ?: ( query : string ) => { matches : boolean } ;
31
31
}
32
32
33
+ // TODO React 17: Remove `useMediaQueryOld` once React 17 support is removed
33
34
function useMediaQueryOld (
34
35
query : string ,
35
36
defaultMatches : boolean ,
@@ -71,8 +72,9 @@ function useMediaQueryOld(
71
72
return match ;
72
73
}
73
74
74
- // eslint-disable-next-line no-useless-concat -- Workaround for https://github.com/webpack/webpack/issues/14814
75
- const maybeReactUseSyncExternalStore : undefined | any = ( React as any ) [ 'useSyncExternalStore' + '' ] ;
75
+ // See https://github.com/mui/material-ui/issues/41190#issuecomment-2040873379 for why
76
+ const safeReact = { ...React } ;
77
+ const maybeReactUseSyncExternalStore : undefined | any = safeReact . useSyncExternalStore ;
76
78
77
79
function useMediaQueryNew (
78
80
query : string ,
@@ -148,7 +150,6 @@ export default function useMediaQuery<Theme = unknown>(
148
150
let query = typeof queryInput === 'function' ? queryInput ( theme ) : queryInput ;
149
151
query = query . replace ( / ^ @ m e d i a ( ? ) / m, '' ) ;
150
152
151
- // TODO: Drop `useMediaQueryOld` and use `use-sync-external-store` shim in `useMediaQueryNew` once the package is stable
152
153
const useMediaQueryImplementation =
153
154
maybeReactUseSyncExternalStore !== undefined ? useMediaQueryNew : useMediaQueryOld ;
154
155
const match = useMediaQueryImplementation (
Original file line number Diff line number Diff line change 1
1
import * as React from 'react' ;
2
2
import { expect } from 'chai' ;
3
3
import { createRenderer , screen } from '@mui/internal-test-utils' ;
4
- import useId from '. /useId' ;
4
+ import useId from '@mui/utils /useId' ;
5
5
6
6
describe ( 'useId' , ( ) => {
7
7
const { render, renderToString } = createRenderer ( ) ;
Original file line number Diff line number Diff line change 2
2
import * as React from 'react' ;
3
3
4
4
let globalId = 0 ;
5
+
6
+ // TODO React 17: Remove `useGlobalId` once React 17 support is removed
5
7
function useGlobalId ( idOverride ?: string ) : string | undefined {
6
8
const [ defaultId , setDefaultId ] = React . useState ( idOverride ) ;
7
9
const id = idOverride || defaultId ;
@@ -18,19 +20,23 @@ function useGlobalId(idOverride?: string): string | undefined {
18
20
return id ;
19
21
}
20
22
21
- // downstream bundlers may remove unnecessary concatenation, but won't remove toString call -- Workaround for https://github.com/webpack/webpack/issues/14814
22
- const maybeReactUseId : undefined | ( ( ) => string ) = ( React as any ) [ 'useId' . toString ( ) ] ;
23
+ // See https://github.com/mui/material-ui/issues/41190#issuecomment-2040873379 for why
24
+ const safeReact = { ...React } ;
25
+ const maybeReactUseId : undefined | ( ( ) => string ) = safeReact . useId ;
26
+
23
27
/**
24
28
*
25
29
* @example <div id={useId()} />
26
30
* @param idOverride
27
31
* @returns {string }
28
32
*/
29
33
export default function useId ( idOverride ?: string ) : string | undefined {
34
+ // React.useId() is only available from React 17.0.0.
30
35
if ( maybeReactUseId !== undefined ) {
31
36
const reactId = maybeReactUseId ( ) ;
32
37
return idOverride ?? reactId ;
33
38
}
39
+
34
40
// TODO: uncomment once we enable eslint-plugin-react-compiler // eslint-disable-next-line react-compiler/react-compiler
35
41
// eslint-disable-next-line react-hooks/rules-of-hooks -- `React.useId` is invariant at runtime.
36
42
return useGlobalId ( idOverride ) ;
You can’t perform that action at this time.
0 commit comments