@@ -3,12 +3,43 @@ import * as React from 'react';
33import PropTypes from 'prop-types' ;
44import { CacheProvider } from '@emotion/react' ;
55import createCache from '@emotion/cache' ;
6+ import { StyleSheet } from '@emotion/sheet' ;
7+
8+ // We might be able to remove this when this issue is fixed:
9+ // https://github.com/emotion-js/emotion/issues/2790
10+ const createEmotionCache = ( options ) => {
11+ const cache = createCache ( options ) ;
12+
13+ /**
14+ * This is for client-side apps only.
15+ * A custom sheet is required to make the GlobalStyles API work with `prepend: true`.
16+ * This is because the [sheet](https://github.com/emotion-js/emotion/blob/main/packages/react/src/global.js#L94-L99) does not consume the options.
17+ */
18+ class MyStyleSheet extends StyleSheet {
19+ constructor ( args ) {
20+ super ( args ) ;
21+ this . prepend = cache . sheet . prepend ;
22+ }
23+ }
24+
25+ // Do the same as https://github.com/emotion-js/emotion/blob/main/packages/cache/src/index.js#L238-L245
26+ cache . sheet = new MyStyleSheet ( {
27+ key : cache . key ,
28+ nonce : cache . sheet . nonce ,
29+ container : cache . sheet . container ,
30+ speedy : cache . sheet . isSpeedy ,
31+ prepend : cache . sheet . prepend ,
32+ insertionPoint : cache . sheet . insertionPoint ,
33+ } ) ;
34+
35+ return cache ;
36+ } ;
637
738// prepend: true moves MUI styles to the top of the <head> so they're loaded first.
839// It allows developers to easily override MUI styles with other styling solutions, like CSS modules.
940let cache ;
1041if ( typeof document === 'object' ) {
11- cache = createCache ( { key : 'css' , prepend : true } ) ;
42+ cache = createEmotionCache ( { key : 'css' , prepend : true } ) ;
1243}
1344
1445export default function StyledEngineProvider ( props ) {
0 commit comments