diff --git a/examples/joy-next-app-router-ts/src/components/ThemeRegistry/EmotionCache.tsx b/examples/joy-next-app-router-ts/src/components/ThemeRegistry/EmotionCache.tsx
index 2aeb43e64957c4..871d3c98ebb21e 100644
--- a/examples/joy-next-app-router-ts/src/components/ThemeRegistry/EmotionCache.tsx
+++ b/examples/joy-next-app-router-ts/src/components/ThemeRegistry/EmotionCache.tsx
@@ -17,24 +17,25 @@ export type NextAppDirEmotionCacheProviderProps = {
children: React.ReactNode;
};
-// This implementation is taken from https://github.com/garronej/tss-react/blob/main/src/next/appDir.tsx
+// Adapted from https://github.com/garronej/tss-react/blob/main/src/next/appDir.tsx
export default function NextAppDirEmotionCacheProvider(props: NextAppDirEmotionCacheProviderProps) {
const { options, CacheProvider = DefaultCacheProvider, children } = props;
- const [{ cache, flush }] = React.useState(() => {
- // eslint-disable-next-line @typescript-eslint/no-shadow
+ const [registry] = React.useState(() => {
const cache = createCache(options);
cache.compat = true;
const prevInsert = cache.insert;
- let inserted: string[] = [];
+ let inserted: { name: string; isGlobal: boolean }[] = [];
cache.insert = (...args) => {
- const serialized = args[1];
+ const [selector, serialized] = args;
if (cache.inserted[serialized.name] === undefined) {
- inserted.push(serialized.name);
+ inserted.push({
+ name: serialized.name,
+ isGlobal: !selector,
+ });
}
return prevInsert(...args);
};
- // eslint-disable-next-line @typescript-eslint/no-shadow
const flush = () => {
const prevInserted = inserted;
inserted = [];
@@ -44,26 +45,51 @@ export default function NextAppDirEmotionCacheProvider(props: NextAppDirEmotionC
});
useServerInsertedHTML(() => {
- const names = flush();
- if (names.length === 0) {
+ const inserted = registry.flush();
+ if (inserted.length === 0) {
return null;
}
let styles = '';
- // eslint-disable-next-line no-restricted-syntax
- for (const name of names) {
- styles += cache.inserted[name];
- }
+ let dataEmotionAttribute = registry.cache.key;
+
+ const globals: {
+ name: string;
+ style: string;
+ }[] = [];
+
+ inserted.forEach(({ name, isGlobal }) => {
+ const style = registry.cache.inserted[name];
+
+ if (typeof style !== 'boolean') {
+ if (isGlobal) {
+ globals.push({ name, style });
+ } else {
+ styles += style;
+ dataEmotionAttribute += ` ${name}`;
+ }
+ }
+ });
+
return (
-
+
+ {globals.map(({ name, style }) => (
+
+ ))}
+ {styles && (
+
+ )}
+
);
});
- return {children};
+ return {children};
}
diff --git a/examples/material-next-app-router-ts/src/components/ThemeRegistry/EmotionCache.tsx b/examples/material-next-app-router-ts/src/components/ThemeRegistry/EmotionCache.tsx
index 2aeb43e64957c4..871d3c98ebb21e 100644
--- a/examples/material-next-app-router-ts/src/components/ThemeRegistry/EmotionCache.tsx
+++ b/examples/material-next-app-router-ts/src/components/ThemeRegistry/EmotionCache.tsx
@@ -17,24 +17,25 @@ export type NextAppDirEmotionCacheProviderProps = {
children: React.ReactNode;
};
-// This implementation is taken from https://github.com/garronej/tss-react/blob/main/src/next/appDir.tsx
+// Adapted from https://github.com/garronej/tss-react/blob/main/src/next/appDir.tsx
export default function NextAppDirEmotionCacheProvider(props: NextAppDirEmotionCacheProviderProps) {
const { options, CacheProvider = DefaultCacheProvider, children } = props;
- const [{ cache, flush }] = React.useState(() => {
- // eslint-disable-next-line @typescript-eslint/no-shadow
+ const [registry] = React.useState(() => {
const cache = createCache(options);
cache.compat = true;
const prevInsert = cache.insert;
- let inserted: string[] = [];
+ let inserted: { name: string; isGlobal: boolean }[] = [];
cache.insert = (...args) => {
- const serialized = args[1];
+ const [selector, serialized] = args;
if (cache.inserted[serialized.name] === undefined) {
- inserted.push(serialized.name);
+ inserted.push({
+ name: serialized.name,
+ isGlobal: !selector,
+ });
}
return prevInsert(...args);
};
- // eslint-disable-next-line @typescript-eslint/no-shadow
const flush = () => {
const prevInserted = inserted;
inserted = [];
@@ -44,26 +45,51 @@ export default function NextAppDirEmotionCacheProvider(props: NextAppDirEmotionC
});
useServerInsertedHTML(() => {
- const names = flush();
- if (names.length === 0) {
+ const inserted = registry.flush();
+ if (inserted.length === 0) {
return null;
}
let styles = '';
- // eslint-disable-next-line no-restricted-syntax
- for (const name of names) {
- styles += cache.inserted[name];
- }
+ let dataEmotionAttribute = registry.cache.key;
+
+ const globals: {
+ name: string;
+ style: string;
+ }[] = [];
+
+ inserted.forEach(({ name, isGlobal }) => {
+ const style = registry.cache.inserted[name];
+
+ if (typeof style !== 'boolean') {
+ if (isGlobal) {
+ globals.push({ name, style });
+ } else {
+ styles += style;
+ dataEmotionAttribute += ` ${name}`;
+ }
+ }
+ });
+
return (
-
+
+ {globals.map(({ name, style }) => (
+
+ ))}
+ {styles && (
+
+ )}
+
);
});
- return {children};
+ return {children};
}