Skip to content

Commit

Permalink
Merge pull request #183 from garronej/issue_182
Browse files Browse the repository at this point in the history
Issue 182
  • Loading branch information
garronej authored Jul 14, 2023
2 parents 07a7c83 + 1504d7a commit 374c300
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tss-react",
"version": "4.8.6",
"version": "4.8.7-rc.0",
"description": "Type safe CSS-in-JS API heavily inspired by react-jss",
"repository": {
"type": "git",
Expand Down
59 changes: 45 additions & 14 deletions src/next/appDir.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ export function NextAppDirEmotionCacheProvider(
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);
};
Expand All @@ -46,20 +49,48 @@ export function NextAppDirEmotionCacheProvider(
});

useServerInsertedHTML(() => {
const names = flush();
if (names.length === 0) return null;
const inserted = flush();
if (inserted.length === 0) {
return null;
}
let styles = "";
for (const name of names) {
styles += cache.inserted[name];
let dataEmotionAttribute = `${cache.key}`;
let globalStyles = "";
let globalDataEmotionAttribute = `${cache.key}-global`;

for (const { name, isGlobal } of inserted) {
const style = cache.inserted[name];

if (isGlobal) {
globalStyles += style;
globalDataEmotionAttribute += ` ${name}`;
} else {
styles += style;
dataEmotionAttribute += ` ${name}`;
}
}

return (
<style
key={cache.key}
data-emotion={`${cache.key} ${names.join(" ")}`}
dangerouslySetInnerHTML={{
"__html": styles
}}
/>
<>
{globalStyles !== "" && (
<style
key={`${cache.key}-global`}
data-emotion={globalDataEmotionAttribute}
dangerouslySetInnerHTML={{
"__html": globalStyles
}}
/>
)}
{styles !== "" && (
<style
key={cache.key}
data-emotion={dataEmotionAttribute}
dangerouslySetInnerHTML={{
"__html": styles
}}
/>
)}
</>
);
});

Expand Down

0 comments on commit 374c300

Please sign in to comment.