Files
twenty/packages/twenty-website/src/app/emotion-root-style-registry.js
Charles Bochet c15e138d72 Fix nx lint setup (#3234)
* Fix nx lint setup

* Fixes

* Fixes

* Add missing metadata

Fixes

Fix

Fixes

* Fix
2024-01-04 16:39:57 +01:00

48 lines
1.2 KiB
JavaScript

'use client';
import { useState } from 'react';
import createCache from '@emotion/cache';
import { CacheProvider } from '@emotion/react';
import { useServerInsertedHTML } from 'next/navigation';
export default function RootStyleRegistry({ children }) {
const [{ cache, flush }] = useState(() => {
const cache = createCache({ key: 'emotion-cache' });
cache.compat = true;
const prevInsert = cache.insert;
let inserted = [];
cache.insert = (...args) => {
const serialized = args[1];
if (cache.inserted[serialized.name] === undefined) {
inserted.push(serialized.name);
}
return prevInsert(...args);
};
const flush = () => {
const prevInserted = inserted;
inserted = [];
return prevInserted;
};
return { cache, flush };
});
useServerInsertedHTML(() => {
const names = flush();
if (names.length === 0) return null;
let styles = '';
for (const name of names) {
styles += cache.inserted[name];
}
return (
<style
data-emotion={`${cache.key} ${names.join(' ')}`}
dangerouslySetInnerHTML={{
__html: styles,
}}
/>
);
});
return <CacheProvider value={cache}>{children}</CacheProvider>;
}