-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add insertionPoint option in EmotionCache
#2521
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
Changes from all commits
84cf0a4
a8a38cc
5e801e0
221c298
60bf5bd
b13a72c
3fc9c88
c8fa22d
1c94b62
74ab823
218ea87
ef3412f
61fcc7f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| --- | ||
| '@emotion/cache': minor | ||
| '@emotion/sheet': minor | ||
| --- | ||
|
|
||
| Add insertionPoint option to the EmotionCache, to insert rules after the specified element. | ||
|
|
||
| ```jsx | ||
| const head = document.querySelector('head') | ||
|
|
||
| // <meta name="emotion-insertion-point" content=""> | ||
| const emotionInsertionPoint = document.createElement('meta') | ||
| emotionInsertionPoint.setAttribute('name', 'emotion-insertion-point') | ||
| emotionInsertionPoint.setAttribute('content', '') | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI: I've checked the spec and it seems that
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Kindly, does this work with SSR, 'cause
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to create such a tag (or similar) in your SSR response and then select it on the client's side and pass it as the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, @Andarist. Thanks for your response. I saw and did as you suggested and it worked. Thank you. But then, there's another issue which I'm noticing. I'm using Framer Motion's page transition feature--AnimatePresence--with MUI, and it happens that on route change start, the styles of the unmounting page/component (seem) to get removed from the DOM causing a FOUC of the unmounting page (before it transitions out). Is there a way to preserve the styles of the pages as they get mounted, so they don't have to get removed and re-added everytime they get mounted/unmounted. I hope you understand what I mean? Cc: @oliverturner
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What styles are removed on the route change? By default Emotion doesn't unmount any styles - except global ones when used through So while I understand what you are observing there, I don't understand how this can happen. Do you have a repro case (even a complex one, like a full deployed page) that I could take a look at? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ...versus what it currently looks like in prod: prod-480x.movThere was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Andarist , so you'll notice the FOUC in the production (build) video. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, @Andarist . Seen these yet?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you perhaps record this with https://www.replay.io/ ? I'd like to debug this and can't do that with just a standard recording of the problem. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alright, I will. Thanks for your support. 🤲🏼 |
||
|
|
||
| head.appendChild(emotionInsertionPoint) | ||
|
|
||
| // the emotion sheets should be inserted right after the meta tag | ||
| const cache = createCache({ | ||
| key: 'my-app', | ||
| insertionPoint: emotionInsertionPoint | ||
| }) | ||
|
|
||
| function App() { | ||
| return ( | ||
| <CacheProvider value={cache}> | ||
| <Main /> | ||
| </CacheProvider> | ||
| ) | ||
| } | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,27 @@ | ||
| // Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
|
||
| exports[`should accept insertionPoint option 1`] = ` | ||
| <head> | ||
|
|
||
|
|
||
| <style | ||
| id="first" | ||
| /> | ||
| <style | ||
| data-emotion="test-insertion-point" | ||
| data-s="" | ||
| > | ||
|
|
||
| .test-insertion-point-83n355{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;color:blue;} | ||
| </style> | ||
|
|
||
|
|
||
| <style | ||
| id="last" | ||
| /> | ||
|
|
||
|
|
||
| </head> | ||
| `; | ||
|
|
||
| exports[`throws correct error with invalid key 1`] = `"Emotion key must only contain lower case alphabetical characters and - but \\".\\" was passed"`; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,36 @@ | ||
| // @flow | ||
| /** @jsx jsx */ | ||
| import 'test-utils/next-env' | ||
| import { safeQuerySelector } from 'test-utils' | ||
| import createCache from '@emotion/cache' | ||
| import { jsx, CacheProvider } from '@emotion/react' | ||
| import { render } from '@testing-library/react' | ||
|
|
||
| test('throws correct error with invalid key', () => { | ||
| expect(() => { | ||
| createCache({ key: '.' }) | ||
| }).toThrowErrorMatchingSnapshot() | ||
| }) | ||
|
|
||
| it('should accept insertionPoint option', () => { | ||
| const head = safeQuerySelector('head') | ||
|
|
||
| head.innerHTML = ` | ||
| <style id="first"></style> | ||
| <style id="last"></style> | ||
| ` | ||
|
|
||
| // the sheet should be inserted between the first and last style nodes | ||
| const cache = createCache({ | ||
| key: 'test-insertion-point', | ||
| insertionPoint: safeQuerySelector('#first') | ||
| }) | ||
|
|
||
| render( | ||
| <CacheProvider value={cache}> | ||
| <div css={{ display: 'flex', color: 'blue' }} /> | ||
| </CacheProvider> | ||
| ) | ||
|
|
||
| expect(document.head).toMatchSnapshot() | ||
| }) |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -36,7 +36,9 @@ export interface Options { | |||||||
| key: string | ||||||||
| container?: HTMLElement | ||||||||
| speedy?: boolean | ||||||||
| /** @deprecate use `insertionPoint` instead */ | ||||||||
| prepend?: boolean | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
a note about this deprecation should both go into the docs and into the changeset
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would keep the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Btw I am adding the style rules after the node specified in the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I was thinking about this in the last few days and I think I would still prefer to deprecate the Removing this option would simplify the code (the cost ain't high here though), but importantly it would simplify the docs, teachability and would allow us to drop conflicting options in the future (
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated |
||||||||
| insertionPoint?: HTMLElement | ||||||||
| } | ||||||||
|
|
||||||||
| export default function createCache(options: Options): EmotionCache | ||||||||
Uh oh!
There was an error while loading. Please reload this page.