Skip to content

Commit 89b6dbb

Browse files
authoredAug 30, 2022
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

15 files changed

+118
-44
lines changed
 

‎.changeset/blue-pears-explode.md

+8
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

+5
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

+1
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

+2-2
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

+2-2
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

+3-6
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

-16
This file was deleted.

‎packages/styled/package.json

+1
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

+2-2
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

-16
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) Emotion team and other contributors
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# @emotion/use-insertion-effect-with-fallbacks
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "@emotion/use-insertion-effect-with-fallbacks",
3+
"version": "0.0.0",
4+
"description": "A wrapper package that uses `useInsertionEffect` or a fallback for it",
5+
"main": "dist/emotion-use-insertion-effect-with-fallbacks.cjs.js",
6+
"module": "dist/emotion-use-insertion-effect-with-fallbacks.esm.js",
7+
"browser": {
8+
"./dist/emotion-use-insertion-effect-with-fallbacks.esm.js": "./dist/emotion-use-insertion-effect-with-fallbacks.browser.esm.js"
9+
},
10+
"license": "MIT",
11+
"repository": "https://github.com/emotion-js/emotion/tree/main/packages/use-insertion-effect-with-fallbacks",
12+
"publishConfig": {
13+
"access": "public"
14+
},
15+
"files": [
16+
"src",
17+
"dist"
18+
],
19+
"peerDependencies": {
20+
"react": ">=16.8.0"
21+
},
22+
"devDependencies": {
23+
"react": "16.14.0"
24+
},
25+
"exports": {
26+
".": {
27+
"module": {
28+
"worker": "./dist/emotion-use-insertion-effect-with-fallbacks.worker.esm.js",
29+
"browser": "./dist/emotion-use-insertion-effect-with-fallbacks.browser.esm.js",
30+
"default": "./dist/emotion-use-insertion-effect-with-fallbacks.esm.js"
31+
},
32+
"default": "./dist/emotion-use-insertion-effect-with-fallbacks.cjs.js"
33+
},
34+
"./package.json": "./package.json"
35+
},
36+
"preconstruct": {
37+
"exports": {
38+
"envConditions": [
39+
"browser",
40+
"worker"
41+
]
42+
}
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import * as React from 'react'
2+
3+
const isBrowser = typeof document !== 'undefined'
4+
5+
const syncFallback = create => create()
6+
7+
const useInsertionEffect = React['useInsertion' + 'Effect']
8+
? React['useInsertion' + 'Effect']
9+
: false
10+
11+
export const useInsertionEffectAlwaysWithSyncFallback = !isBrowser
12+
? syncFallback
13+
: useInsertionEffect || syncFallback
14+
15+
export const useInsertionEffectWithLayoutFallback =
16+
useInsertionEffect || React.useLayoutEffect

‎yarn.lock

+12
Original file line numberDiff line numberDiff line change
@@ -2584,6 +2584,7 @@ __metadata:
25842584
"@emotion/serialize": ^1.1.0
25852585
"@emotion/server": 11.10.0
25862586
"@emotion/styled": 11.10.0
2587+
"@emotion/use-insertion-effect-with-fallbacks": ^0.0.0
25872588
"@emotion/utils": ^1.2.0
25882589
"@emotion/weak-memoize": ^0.3.0
25892590
hoist-non-react-statics: ^3.3.1
@@ -2658,6 +2659,7 @@ __metadata:
26582659
"@emotion/is-prop-valid": ^1.2.0
26592660
"@emotion/react": 11.10.0
26602661
"@emotion/serialize": ^1.1.0
2662+
"@emotion/use-insertion-effect-with-fallbacks": ^0.0.0
26612663
"@emotion/utils": ^1.2.0
26622664
react: 16.14.0
26632665
typescript: ^4.5.5
@@ -2679,6 +2681,16 @@ __metadata:
26792681
languageName: unknown
26802682
linkType: soft
26812683

2684+
"@emotion/use-insertion-effect-with-fallbacks@^0.0.0, @emotion/use-insertion-effect-with-fallbacks@workspace:packages/use-insertion-effect-with-fallbacks":
2685+
version: 0.0.0-use.local
2686+
resolution: "@emotion/use-insertion-effect-with-fallbacks@workspace:packages/use-insertion-effect-with-fallbacks"
2687+
dependencies:
2688+
react: 16.14.0
2689+
peerDependencies:
2690+
react: ">=16.8.0"
2691+
languageName: unknown
2692+
linkType: soft
2693+
26822694
"@emotion/utils@^1.2.0, @emotion/utils@workspace:packages/utils":
26832695
version: 0.0.0-use.local
26842696
resolution: "@emotion/utils@workspace:packages/utils"

0 commit comments

Comments
 (0)
Please sign in to comment.