|
| 1 | +'use client'; |
| 2 | +import * as React from 'react'; |
| 3 | +import createCache from '@emotion/cache'; |
| 4 | +import { useServerInsertedHTML } from 'next/navigation'; |
| 5 | +import { CacheProvider as DefaultCacheProvider } from '@emotion/react'; |
| 6 | +import type { EmotionCache, Options as OptionsOfCreateCache } from '@emotion/cache'; |
| 7 | + |
| 8 | +export interface NextAppDirEmotionCacheProviderProps { |
| 9 | + /** This is the options passed to createCache() from 'import createCache from "@emotion/cache"' */ |
| 10 | + options: Omit<OptionsOfCreateCache, 'insertionPoint'>; |
| 11 | + /** By default <CacheProvider /> from 'import { CacheProvider } from "@emotion/react"' */ |
| 12 | + CacheProvider?: (props: { |
| 13 | + value: EmotionCache; |
| 14 | + children: React.ReactNode; |
| 15 | + }) => React.JSX.Element | null; |
| 16 | + children: React.ReactNode; |
| 17 | +} |
| 18 | + |
| 19 | +// Adapted from https://github.com/garronej/tss-react/blob/main/src/next/appDir.tsx |
| 20 | +export default function NextAppDirEmotionCacheProvider(props: NextAppDirEmotionCacheProviderProps) { |
| 21 | + const { options, CacheProvider = DefaultCacheProvider, children } = props; |
| 22 | + |
| 23 | + const [registry] = React.useState(() => { |
| 24 | + const cache = createCache(options); |
| 25 | + cache.compat = true; |
| 26 | + const prevInsert = cache.insert; |
| 27 | + let inserted: Array<{ name: string; isGlobal: boolean }> = []; |
| 28 | + cache.insert = (...args) => { |
| 29 | + const [selector, serialized] = args; |
| 30 | + if (cache.inserted[serialized.name] === undefined) { |
| 31 | + inserted.push({ |
| 32 | + name: serialized.name, |
| 33 | + isGlobal: !selector, |
| 34 | + }); |
| 35 | + } |
| 36 | + return prevInsert(...args); |
| 37 | + }; |
| 38 | + const flush = () => { |
| 39 | + const prevInserted = inserted; |
| 40 | + inserted = []; |
| 41 | + return prevInserted; |
| 42 | + }; |
| 43 | + return { cache, flush }; |
| 44 | + }); |
| 45 | + |
| 46 | + useServerInsertedHTML(() => { |
| 47 | + const inserted = registry.flush(); |
| 48 | + if (inserted.length === 0) { |
| 49 | + return null; |
| 50 | + } |
| 51 | + let styles = ''; |
| 52 | + let dataEmotionAttribute = registry.cache.key; |
| 53 | + |
| 54 | + const globals: Array<{ |
| 55 | + name: string; |
| 56 | + style: string; |
| 57 | + }> = []; |
| 58 | + |
| 59 | + inserted.forEach(({ name, isGlobal }) => { |
| 60 | + const style = registry.cache.inserted[name]; |
| 61 | + |
| 62 | + if (typeof style !== 'boolean') { |
| 63 | + if (isGlobal) { |
| 64 | + globals.push({ name, style }); |
| 65 | + } else { |
| 66 | + styles += style; |
| 67 | + dataEmotionAttribute += ` ${name}`; |
| 68 | + } |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + return ( |
| 73 | + <React.Fragment> |
| 74 | + {globals.map(({ name, style }) => ( |
| 75 | + <style |
| 76 | + key={name} |
| 77 | + data-emotion={`${registry.cache.key}-global ${name}`} |
| 78 | + // eslint-disable-next-line react/no-danger |
| 79 | + dangerouslySetInnerHTML={{ __html: style }} |
| 80 | + /> |
| 81 | + ))} |
| 82 | + {styles && ( |
| 83 | + <style |
| 84 | + data-emotion={dataEmotionAttribute} |
| 85 | + // eslint-disable-next-line react/no-danger |
| 86 | + dangerouslySetInnerHTML={{ __html: styles }} |
| 87 | + /> |
| 88 | + )} |
| 89 | + </React.Fragment> |
| 90 | + ); |
| 91 | + }); |
| 92 | + |
| 93 | + return <CacheProvider value={registry.cache}>{children}</CacheProvider>; |
| 94 | +} |
0 commit comments