-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
chore: custom sheet #2677
chore: custom sheet #2677
Conversation
🦋 Changeset detectedLatest commit: ea74c7a The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
hi! I cannot run
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit ea74c7a:
|
if (Reflect.get(options.container, 'sheet')) { | ||
return new GlobalSheet(options) | ||
} else { | ||
Reflect.set(options.container, 'sheet', this) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder - any particular reason why Reflect
is used here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To bypass the flow type check. sheet
is not a property on the HTMLElement
That is somewhat weird but don't worry - I will update them before merging |
Codecov Report
|
I've wanted to push out changes to this branch - but it seems like I don't have sufficient permissions to push to your fork. You can check how to allow this for future PRs here - https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/allowing-changes-to-a-pull-request-branch-created-from-a-fork . At the moment, you can try applying this patch: diff --git a/packages/react/__tests__/custom-cache.js b/packages/react/__tests__/custom-cache.js
index cab71fc3..86f6d89f 100644
--- a/packages/react/__tests__/custom-cache.js
+++ b/packages/react/__tests__/custom-cache.js
@@ -1,10 +1,11 @@
// @flow
/** @jsx jsx */
-import 'test-utils/next-env'
import createCache from '@emotion/cache'
-import { jsx, CacheProvider, Global } from '@emotion/react'
+import { CacheProvider, Global, jsx } from '@emotion/react'
import { StyleSheet } from '@emotion/sheet'
import renderer from 'react-test-renderer'
+import { safeQuerySelector } from 'test-utils'
+import 'test-utils/next-env'
function stylisPlugin(element) {
if (element.type === 'decl' && element.value.startsWith('color:')) {
@@ -16,6 +17,11 @@ function render(ele) {
return renderer.create(ele).toJSON()
}
+beforeEach(() => {
+ safeQuerySelector('head').innerHTML = ''
+ safeQuerySelector('body').innerHTML = ''
+})
+
test('with custom plugins', () => {
let cache = createCache({
key: 'custom-plugins',
@@ -40,40 +46,42 @@ test('with custom plugins', () => {
`)
})
-test('with custom sheet', () => {
+test('Global should "inherit" sheet class from the cache', () => {
// https://github.com/emotion-js/emotion/issues/2675
let cache = createCache({
key: 'test',
speedy: false
})
-
- class GlobalSheet extends StyleSheet {
- insert(rule) {
- return super.insert('/** global */' + rule)
- }
- }
class MySheet extends StyleSheet {
- constructor(options) {
- super(options)
- if (Reflect.get(options.container, 'sheet')) {
- return new GlobalSheet(options)
- } else {
- Reflect.set(options.container, 'sheet', this)
- }
- }
-
insert(rule) {
super.insert(`/** ${this.key} */${rule}`)
}
}
cache.sheet = new MySheet({ key: 'test', container: document.head })
- expect(
- render(
- <CacheProvider value={cache}>
- <div css={{ display: 'flex', color: 'blue' }} />
- <Global styles={{ body: { width: '0' } }} />
- </CacheProvider>
- )
- ).toMatchInlineSnapshot()
+ render(
+ <CacheProvider value={cache}>
+ <div css={{ color: 'hotpink' }} />
+ <Global styles={{ body: { width: '0' } }} />
+ </CacheProvider>
+ )
+
+ expect(safeQuerySelector('head')).toMatchInlineSnapshot(`
+ <head>
+ <style
+ data-emotion="test-global"
+ data-s=""
+ >
+
+ /** test-global */body{width:0;}
+ </style>
+ <style
+ data-emotion="test"
+ data-s=""
+ >
+
+ /** test */.test-1lrxbo5{color:hotpink;}
+ </style>
+ </head>
+ `)
})
|
That was weird and I've also experienced that. The problem is when you are using I didn't solve this problem, I just have provided a dummy value as a snapshot manually and then constructed the correct value manually based on the CLI report. I'll have to look into this later but that's a completely separate issue from this PR. |
key: 'test', | ||
speedy: false | ||
}) | ||
class MySheet extends StyleSheet { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've noticed that you were constructing different sheet classes in the constructor of your custom implementation but perhaps that's not really needed. You can easily differentiate hashed/main and global sheets by just checking the options.key.endsWith('-global')
. Based on that you can just assign some private property and branch the logic in the insert
method. This way you only have to write a single StyleSheet
class which might simplify your implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, I need to get the un-global version of the sheet so that's the easiest way, any way, thanks!
Maybe because I'm using Windows? 🤔 |
I have meant that the same thing has happened to me - so it was definitely not specific to your machine etc. |
What: change
new StyleSheet
tonew cache.sheet.constructor
Why: close #2675
How:
Checklist: