Skip to content

Commit 89b6dbb

Browse files
authored
Move conditional useInsertionEffect declarations into separate package (#2867)
* Move conditional `useInsertionEffect` declarations into separate package * add missing dependecies * regenerate lockfile * Add missing peer dep * regenerate lockfile * add missing dev dep for a peer dep * regenerate lockfile * add changesets
1 parent 28522cd commit 89b6dbb

File tree

15 files changed

+118
-44
lines changed

15 files changed

+118
-44
lines changed

.changeset/blue-pears-explode.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'@emotion/react': patch
3+
'@emotion/styled': patch
4+
---
5+
6+
Externalized code referencing `React.useInsertionEffect` to a separate `@emotion/use-insertion-effect-with-fallbacks` package. This package should be used in your defined externals if you bundle Emotion for whatever reason. It references `useInsertionEffect` in a very specific way that allows us to use it conditionally. However, if the code consuming Emotion is bundled as a library with Emotion in it then some bundlers might change the way in which we reference `useInsertionEffect` and that might create problems for bundlers used to consume the said library code. By externalizing this new package you can still bundle Emotion if you want to without running into this problem as you won't "destroy" the carefully crafted reference to `useInsertionEffect` in the process.
7+
8+
Note that we don't recommend bundling Emotion. You should have very specific reasons to do so.

.changeset/lemon-emus-applaud.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@emotion/use-insertion-effect-with-fallbacks': major
3+
---
4+
5+
A wrapper package that uses `useInsertionEffect` or a specific fallback for it. It comes with two exports: `useInsertionEffectAlwaysWithSyncFallback` and `useInsertionEffectWithLayoutFallback`.

packages/react/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"@emotion/babel-plugin": "^11.10.0",
6767
"@emotion/cache": "^11.10.0",
6868
"@emotion/serialize": "^1.1.0",
69+
"@emotion/use-insertion-effect-with-fallbacks": "^0.0.0",
6970
"@emotion/utils": "^1.2.0",
7071
"@emotion/weak-memoize": "^0.3.0",
7172
"hoist-non-react-statics": "^3.3.1"

packages/react/src/class-names.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
import { serializeStyles } from '@emotion/serialize'
99
import { withEmotionCache } from './context'
1010
import { ThemeContext } from './theming'
11-
import useInsertionEffectMaybe from './useInsertionEffectMaybe'
11+
import { useInsertionEffectAlwaysWithSyncFallback } from '@emotion/use-insertion-effect-with-fallbacks'
1212
import { isBrowser } from './utils'
1313

1414
type ClassNameArg =
@@ -94,7 +94,7 @@ type Props = {
9494
}
9595

9696
const Insertion = ({ cache, serializedArr }) => {
97-
let rules = useInsertionEffectMaybe(() => {
97+
let rules = useInsertionEffectAlwaysWithSyncFallback(() => {
9898
let rules = ''
9999
for (let i = 0; i < serializedArr.length; i++) {
100100
let res = insertStyles(cache, serializedArr[i], false)

packages/react/src/emotion-element.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
import { hasOwnProperty, isBrowser } from './utils'
1111
import { serializeStyles } from '@emotion/serialize'
1212
import { getLabelFromStackTrace } from './get-label-from-stack-trace'
13-
import useInsertionEffectMaybe from './useInsertionEffectMaybe'
13+
import { useInsertionEffectAlwaysWithSyncFallback } from '@emotion/use-insertion-effect-with-fallbacks'
1414

1515
let typePropName = '__EMOTION_TYPE_PLEASE_DO_NOT_USE__'
1616

@@ -57,7 +57,7 @@ export const createEmotionProps = (type: React.ElementType, props: Object) => {
5757
const Insertion = ({ cache, serialized, isStringTag }) => {
5858
registerStyles(cache, serialized, isStringTag)
5959

60-
const rules = useInsertionEffectMaybe(() =>
60+
const rules = useInsertionEffectAlwaysWithSyncFallback(() =>
6161
insertStyles(cache, serialized, isStringTag)
6262
)
6363

packages/react/src/global.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { withEmotionCache } from './context'
44
import { ThemeContext } from './theming'
55
import { insertStyles } from '@emotion/utils'
66
import { isBrowser } from './utils'
7+
import { useInsertionEffectWithLayoutFallback } from '@emotion/use-insertion-effect-with-fallbacks'
78

89
import { serializeStyles } from '@emotion/serialize'
910

@@ -13,10 +14,6 @@ type GlobalProps = {
1314
+styles: Styles | (Object => Styles)
1415
}
1516

16-
const useInsertionEffect = React['useInsertion' + 'Effect']
17-
? React['useInsertion' + 'Effect']
18-
: React.useLayoutEffect
19-
2017
let warnedAboutCssPropForGlobal = false
2118

2219
// maintain place over rerenders.
@@ -87,7 +84,7 @@ export let Global: React.AbstractComponent<GlobalProps> =
8784

8885
let sheetRef = React.useRef()
8986

90-
useInsertionEffect(() => {
87+
useInsertionEffectWithLayoutFallback(() => {
9188
const key = `${cache.key}-global`
9289

9390
// use case of https://github.com/emotion-js/emotion/issues/2675
@@ -117,7 +114,7 @@ export let Global: React.AbstractComponent<GlobalProps> =
117114
}
118115
}, [cache])
119116

120-
useInsertionEffect(() => {
117+
useInsertionEffectWithLayoutFallback(() => {
121118
let sheetRefCurrent = (sheetRef.current: any)
122119
let [sheet, rehydrating] = sheetRefCurrent
123120
if (rehydrating) {

packages/react/src/useInsertionEffectMaybe.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

packages/styled/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"@emotion/babel-plugin": "^11.10.0",
1616
"@emotion/is-prop-valid": "^1.2.0",
1717
"@emotion/serialize": "^1.1.0",
18+
"@emotion/use-insertion-effect-with-fallbacks": "^0.0.0",
1819
"@emotion/utils": "^1.2.0"
1920
},
2021
"peerDependencies": {

packages/styled/src/base.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
registerStyles
1616
} from '@emotion/utils'
1717
import { serializeStyles } from '@emotion/serialize'
18-
import useInsertionEffectMaybe from './useInsertionEffectMaybe'
18+
import { useInsertionEffectAlwaysWithSyncFallback } from '@emotion/use-insertion-effect-with-fallbacks'
1919

2020
const ILLEGAL_ESCAPE_SEQUENCE_ERROR = `You have illegal escape sequence in your template literal, most likely inside content's property value.
2121
Because you write your CSS inside a JavaScript string you actually have to do double escaping, so for example "content: '\\00d7';" should become "content: '\\\\00d7';".
@@ -27,7 +27,7 @@ let isBrowser = typeof document !== 'undefined'
2727
const Insertion = ({ cache, serialized, isStringTag }) => {
2828
registerStyles(cache, serialized, isStringTag)
2929

30-
const rules = useInsertionEffectMaybe(() =>
30+
const rules = useInsertionEffectAlwaysWithSyncFallback(() =>
3131
insertStyles(cache, serialized, isStringTag)
3232
)
3333

packages/styled/src/useInsertionEffectMaybe.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)