Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[system] Use a custom sheet to set prepend for GlobalStyles #43632

Merged
merged 4 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/mui-styled-engine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"dependencies": {
"@babel/runtime": "^7.25.0",
"@emotion/cache": "^11.13.1",
"@emotion/sheet": "^1.4.0",
"csstype": "^3.1.3",
"prop-types": "^15.8.1"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,41 @@ import * as React from 'react';
import PropTypes from 'prop-types';
import { CacheProvider } from '@emotion/react';
import createCache from '@emotion/cache';
import { StyleSheet } from '@emotion/sheet';

const createEmotionCache = (options) => {
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved
const cache = createCache(options);

/**
* This is for client-side apps only.
* A custom sheet is required to make the GlobalStyles API work with `prepend: true`.
* 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.
*/
class MyStyleSheet extends StyleSheet {
constructor(args) {
super(args);
this.prepend = cache.sheet.prepend;
}
}

// Do the same as https://github.com/emotion-js/emotion/blob/main/packages/cache/src/index.js#L238-L245
cache.sheet = new MyStyleSheet({
key: cache.key,
nonce: cache.sheet.nonce,
container: cache.sheet.container,
speedy: cache.sheet.isSpeedy,
prepend: cache.sheet.prepend,
insertionPoint: cache.sheet.insertionPoint,
});

return cache;
};

// prepend: true moves MUI styles to the top of the <head> so they're loaded first.
// It allows developers to easily override MUI styles with other styling solutions, like CSS modules.
let cache;
if (typeof document === 'object') {
cache = createCache({ key: 'css', prepend: true });
cache = createEmotionCache({ key: 'css', prepend: true });
}

export default function StyledEngineProvider(props) {
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.