Skip to content

Commit 5739514

Browse files
authored
Merge pull request #818 from deckchairlabs/custom-cache-docs
docs: added custom CacheProvider guide
2 parents 9b5f8ba + d9d0c5c commit 5739514

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"devDependencies": {
2020
"@babel/cli": "^7.4.4",
2121
"@babel/core": "^7.4.5",
22+
"@babel/helper-validator-identifier": "^7.9.0",
2223
"@babel/plugin-transform-runtime": "^7.7.6",
2324
"@babel/preset-env": "^7.4.5",
2425
"@babel/preset-react": "^7.0.0",
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
title: Custom CacheProvider
3+
---
4+
5+
# Custom CacheProvider
6+
7+
## Style container
8+
9+
You may come across a situation where you want to inject the generated styles into a
10+
different element than the current document head (an iframe perhaps).
11+
12+
By using the CacheProvider from `@emotion/core` and `createCache` from `@emotion/cache` you can
13+
specify the target container element.
14+
15+
```jsx
16+
import { ThemeProvider } from 'theme-ui'
17+
import { CacheProvider } from '@emotion/core'
18+
import createCache from '@emotion/cache'
19+
20+
/**
21+
* @see https://emotion.sh/docs/@emotion/cache
22+
*/
23+
const cache = createCache({
24+
container: document.getElementById('my-cool-iframe'),
25+
})
26+
27+
const theme = {
28+
colors: {
29+
text: 'tomato',
30+
},
31+
}
32+
33+
export default ({ children }) => {
34+
return (
35+
<CacheProvider value={cache}>
36+
<ThemeProvider theme={theme}>{children}</ThemeProvider>
37+
</CacheProvider>
38+
)
39+
}
40+
```
41+
42+
### With react-frame-component
43+
44+
```jsx
45+
import { ThemeProvider } from 'theme-ui'
46+
import { CacheProvider } from '@emotion/core'
47+
import createCache from '@emotion/cache'
48+
import Iframe, { FrameContextConsumer } from 'react-frame-component'
49+
50+
const theme = {
51+
colors: {
52+
text: 'tomato',
53+
},
54+
}
55+
56+
export default ({ children }) => {
57+
return (
58+
<Iframe initialContent="IFRAME_CONTENT">
59+
<FrameContextConsumer>
60+
{frameContext => {
61+
const cache = createCache({
62+
container: frameContext.document.head,
63+
})
64+
return (
65+
<CacheProvider value={cache}>
66+
<ThemeProvider theme={theme}>{children}</ThemeProvider>
67+
</CacheProvider>
68+
)
69+
}}
70+
</FrameContextConsumer>
71+
</Iframe>
72+
)
73+
}
74+
```

packages/docs/src/pages/guides/index.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import { Cards } from '../..'
2828
Style content responsively
2929
- [Nested ThemeProviders](/guides/nested-theme-providers)
3030
Add contextual theme and stylistic changes
31+
- [Custom CacheProvider](/guides/custom-cache-provider)
32+
Customise the styles generated and where they are injected
3133
- [Syntax Highlighting](/guides/syntax-highlighting)
3234
Add syntax highlighting to MDX code blocks
3335
- [Theme Color Meta Tag](/guides/theme-color-meta-tag)

packages/docs/src/sidebar.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
- [MDX Layout Components](/guides/mdx-layout-components)
7373
- [Responsive Typography](/guides/responsive-typography)
7474
- [Nested ThemeProviders](/guides/nested-theme-providers)
75+
- [Custom CacheProvider](/guides/custom-cache-provider)
7576
- [Syntax Highlighting](/guides/syntax-highlighting)
7677
- [Theme Color Meta Tag](/guides/theme-color-meta-tag)
7778
- [Color Mode Toggles](/guides/color-mode-toggles)

0 commit comments

Comments
 (0)