Skip to content

Commit ff3cb16

Browse files
Jack-WorksAndarist
andauthored
Change Global component to use the StyleSheet constructor of the current cache.sheet (#2677)
* chore: add custom sheet * chore: add changeset * chore: remove unused import * chore: apply patch * fix inline snapshots * Fix Flow error * tweak changeset Co-authored-by: Mateusz Burzyński <[email protected]>
1 parent 4266aa0 commit ff3cb16

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

.changeset/green-dingos-design.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@emotion/react': patch
3+
---
4+
5+
Change `Global` component to use the `StyleSheet` constructor of the current `cache.sheet`. This is useful when `cache.sheet` is not the default implementation. Thanks to that the inner sheet constructed by `Global` can share the behavior with its "main" sheet that is hold by the `cache`.

packages/react/__tests__/custom-cache.js

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// @flow
22
/** @jsx jsx */
3-
import 'test-utils/next-env'
43
import createCache from '@emotion/cache'
5-
import { jsx, CacheProvider } from '@emotion/react'
4+
import { CacheProvider, Global, jsx } from '@emotion/react'
5+
import { StyleSheet } from '@emotion/sheet'
66
import renderer from 'react-test-renderer'
7+
import { safeQuerySelector } from 'test-utils'
8+
import 'test-utils/next-env'
79

810
function stylisPlugin(element) {
911
if (element.type === 'decl' && element.value.startsWith('color:')) {
@@ -15,6 +17,11 @@ function render(ele) {
1517
return renderer.create(ele).toJSON()
1618
}
1719

20+
beforeEach(() => {
21+
safeQuerySelector('head').innerHTML = ''
22+
safeQuerySelector('body').innerHTML = ''
23+
})
24+
1825
test('with custom plugins', () => {
1926
let cache = createCache({
2027
key: 'custom-plugins',
@@ -38,3 +45,46 @@ test('with custom plugins', () => {
3845
/>
3946
`)
4047
})
48+
49+
test('Global should "inherit" sheet class from the cache', () => {
50+
// https://github.com/emotion-js/emotion/issues/2675
51+
let cache = createCache({
52+
key: 'test',
53+
speedy: false
54+
})
55+
class MySheet extends StyleSheet {
56+
insert(rule) {
57+
super.insert(`/** ${this.key} */${rule}`)
58+
}
59+
}
60+
cache.sheet = new MySheet({
61+
key: 'test',
62+
container: safeQuerySelector('head')
63+
})
64+
65+
render(
66+
<CacheProvider value={cache}>
67+
<div css={{ color: 'hotpink' }} />
68+
<Global styles={{ body: { width: '0' } }} />
69+
</CacheProvider>
70+
)
71+
72+
expect(safeQuerySelector('head')).toMatchInlineSnapshot(`
73+
<head>
74+
<style
75+
data-emotion="test-global"
76+
data-s=""
77+
>
78+
79+
/** test-global */body{width:0;}
80+
</style>
81+
<style
82+
data-emotion="test"
83+
data-s=""
84+
>
85+
86+
/** test */.test-1lrxbo5{color:hotpink;}
87+
</style>
88+
</head>
89+
`)
90+
})

packages/react/src/global.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { ThemeContext } from './theming'
55
import { insertStyles } from '@emotion/utils'
66
import { isBrowser } from './utils'
77

8-
import { StyleSheet } from '@emotion/sheet'
98
import { serializeStyles } from '@emotion/serialize'
109

1110
type Styles = Object | Array<Object>
@@ -91,7 +90,8 @@ export let Global: React.AbstractComponent<GlobalProps> =
9190
useInsertionEffect(() => {
9291
const key = `${cache.key}-global`
9392

94-
let sheet = new StyleSheet({
93+
// use case of https://github.com/emotion-js/emotion/issues/2675
94+
let sheet = new cache.sheet.constructor({
9595
key,
9696
nonce: cache.sheet.nonce,
9797
container: cache.sheet.container,

0 commit comments

Comments
 (0)