Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move conditional useInsertionEffect declarations into separate package #2867

Merged
merged 9 commits into from Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/react/package.json
Expand Up @@ -66,6 +66,7 @@
"@emotion/babel-plugin": "^11.10.0",
"@emotion/cache": "^11.10.0",
"@emotion/serialize": "^1.1.0",
"@emotion/use-insertion-effect-with-fallbacks": "^1.0.0",
"@emotion/utils": "^1.2.0",
"@emotion/weak-memoize": "^0.3.0",
"hoist-non-react-statics": "^3.3.1"
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/class-names.js
Expand Up @@ -8,7 +8,7 @@ import {
import { serializeStyles } from '@emotion/serialize'
import { withEmotionCache } from './context'
import { ThemeContext } from './theming'
import useInsertionEffectMaybe from './useInsertionEffectMaybe'
import { useInsertionEffectAlwaysWithSyncFallback } from '@emotion/use-insertion-effect-with-fallbacks'
import { isBrowser } from './utils'

type ClassNameArg =
Expand Down Expand Up @@ -94,7 +94,7 @@ type Props = {
}

const Insertion = ({ cache, serializedArr }) => {
let rules = useInsertionEffectMaybe(() => {
let rules = useInsertionEffectAlwaysWithSyncFallback(() => {
let rules = ''
for (let i = 0; i < serializedArr.length; i++) {
let res = insertStyles(cache, serializedArr[i], false)
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/emotion-element.js
Expand Up @@ -10,7 +10,7 @@ import {
import { hasOwnProperty, isBrowser } from './utils'
import { serializeStyles } from '@emotion/serialize'
import { getLabelFromStackTrace } from './get-label-from-stack-trace'
import useInsertionEffectMaybe from './useInsertionEffectMaybe'
import { useInsertionEffectAlwaysWithSyncFallback } from '@emotion/use-insertion-effect-with-fallbacks'

let typePropName = '__EMOTION_TYPE_PLEASE_DO_NOT_USE__'

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

const rules = useInsertionEffectMaybe(() =>
const rules = useInsertionEffectAlwaysWithSyncFallback(() =>
insertStyles(cache, serialized, isStringTag)
)

Expand Down
9 changes: 3 additions & 6 deletions packages/react/src/global.js
Expand Up @@ -4,6 +4,7 @@ import { withEmotionCache } from './context'
import { ThemeContext } from './theming'
import { insertStyles } from '@emotion/utils'
import { isBrowser } from './utils'
import { useInsertionEffectWithLayoutFallback } from '@emotion/use-insertion-effect-with-fallbacks'

import { serializeStyles } from '@emotion/serialize'

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

const useInsertionEffect = React['useInsertion' + 'Effect']
? React['useInsertion' + 'Effect']
: React.useLayoutEffect

let warnedAboutCssPropForGlobal = false

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

let sheetRef = React.useRef()

useInsertionEffect(() => {
useInsertionEffectWithLayoutFallback(() => {
const key = `${cache.key}-global`

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

useInsertionEffect(() => {
useInsertionEffectWithLayoutFallback(() => {
let sheetRefCurrent = (sheetRef.current: any)
let [sheet, rehydrating] = sheetRefCurrent
if (rehydrating) {
Expand Down
16 changes: 0 additions & 16 deletions packages/react/src/useInsertionEffectMaybe.js

This file was deleted.

1 change: 1 addition & 0 deletions packages/styled/package.json
Expand Up @@ -15,6 +15,7 @@
"@emotion/babel-plugin": "^11.10.0",
"@emotion/is-prop-valid": "^1.2.0",
"@emotion/serialize": "^1.1.0",
"@emotion/use-insertion-effect-with-fallbacks": "^1.0.0",
"@emotion/utils": "^1.2.0"
},
"peerDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions packages/styled/src/base.js
Expand Up @@ -15,7 +15,7 @@ import {
registerStyles
} from '@emotion/utils'
import { serializeStyles } from '@emotion/serialize'
import useInsertionEffectMaybe from './useInsertionEffectMaybe'
import { useInsertionEffectAlwaysWithSyncFallback } from '@emotion/use-insertion-effect-with-fallbacks'

const ILLEGAL_ESCAPE_SEQUENCE_ERROR = `You have illegal escape sequence in your template literal, most likely inside content's property value.
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';".
Expand All @@ -27,7 +27,7 @@ let isBrowser = typeof document !== 'undefined'
const Insertion = ({ cache, serialized, isStringTag }) => {
registerStyles(cache, serialized, isStringTag)

const rules = useInsertionEffectMaybe(() =>
const rules = useInsertionEffectAlwaysWithSyncFallback(() =>
insertStyles(cache, serialized, isStringTag)
)

Expand Down
16 changes: 0 additions & 16 deletions packages/styled/src/useInsertionEffectMaybe.js

This file was deleted.

21 changes: 21 additions & 0 deletions packages/use-insertion-effect-with-fallbacks/LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) Emotion team and other contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions packages/use-insertion-effect-with-fallbacks/README.md
@@ -0,0 +1 @@
# @emotion/use-insertion-effect-with-fallbacks
38 changes: 38 additions & 0 deletions packages/use-insertion-effect-with-fallbacks/package.json
@@ -0,0 +1,38 @@
{
"name": "@emotion/use-insertion-effect-with-fallbacks",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be *-with-fallback or *-with-fallbacks? 😅 #realproblems

"version": "1.0.0",
Copy link
Member

@emmatown emmatown Aug 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"version": "1.0.0",
"version": "0.0.0",

+a changeset

"description": "",
"main": "dist/emotion-use-insertion-effect-with-fallbacks.cjs.js",
"module": "dist/emotion-use-insertion-effect-with-fallbacks.esm.js",
"browser": {
"./dist/emotion-use-insertion-effect-with-fallbacks.esm.js": "./dist/emotion-use-insertion-effect-with-fallbacks.browser.esm.js"
},
"license": "MIT",
"repository": "https://github.com/emotion-js/emotion/tree/main/packages/use-insertion-effect-with-fallbacks",
"publishConfig": {
"access": "public"
},
"files": [
"src",
"dist"
],
"exports": {
".": {
"module": {
"worker": "./dist/emotion-use-insertion-effect-with-fallbacks.worker.esm.js",
"browser": "./dist/emotion-use-insertion-effect-with-fallbacks.browser.esm.js",
"default": "./dist/emotion-use-insertion-effect-with-fallbacks.esm.js"
},
"default": "./dist/emotion-use-insertion-effect-with-fallbacks.cjs.js"
},
"./package.json": "./package.json"
},
"preconstruct": {
"exports": {
"envConditions": [
"browser",
"worker"
]
}
}
}
16 changes: 16 additions & 0 deletions packages/use-insertion-effect-with-fallbacks/src/index.js
@@ -0,0 +1,16 @@
import * as React from 'react'

const isBrowser = typeof document !== 'undefined'

const syncFallback = create => create()

const useInsertionEffect = React['useInsertion' + 'Effect']
? React['useInsertion' + 'Effect']
: false

export const useInsertionEffectAlwaysWithSyncFallback = !isBrowser
? syncFallback
: useInsertionEffect || syncFallback
Comment on lines +11 to +13
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please be extra careful when reviewing this as the shape of this has slightly changed when I moved this. I think that the current code is functionally equivalent to the old version though.


export const useInsertionEffectWithLayoutFallback =
useInsertionEffect || React.useLayoutEffect
8 changes: 8 additions & 0 deletions yarn.lock
Expand Up @@ -2584,6 +2584,7 @@ __metadata:
"@emotion/serialize": ^1.1.0
"@emotion/server": 11.10.0
"@emotion/styled": 11.10.0
"@emotion/use-insertion-effect-with-fallbacks": ^1.0.0
"@emotion/utils": ^1.2.0
"@emotion/weak-memoize": ^0.3.0
hoist-non-react-statics: ^3.3.1
Expand Down Expand Up @@ -2658,6 +2659,7 @@ __metadata:
"@emotion/is-prop-valid": ^1.2.0
"@emotion/react": 11.10.0
"@emotion/serialize": ^1.1.0
"@emotion/use-insertion-effect-with-fallbacks": ^1.0.0
"@emotion/utils": ^1.2.0
react: 16.14.0
typescript: ^4.5.5
Expand All @@ -2679,6 +2681,12 @@ __metadata:
languageName: unknown
linkType: soft

"@emotion/use-insertion-effect-with-fallbacks@^1.0.0, @emotion/use-insertion-effect-with-fallbacks@workspace:packages/use-insertion-effect-with-fallbacks":
version: 0.0.0-use.local
resolution: "@emotion/use-insertion-effect-with-fallbacks@workspace:packages/use-insertion-effect-with-fallbacks"
languageName: unknown
linkType: soft

"@emotion/utils@^1.2.0, @emotion/utils@workspace:packages/utils":
version: 0.0.0-use.local
resolution: "@emotion/utils@workspace:packages/utils"
Expand Down