-
-
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
Add ClassNames component #828
Merged
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3b05125
Add Style component
emmatown 26e7460
Change error message
emmatown 9f40a14
Rename to ClassNames
emmatown 7882699
Fix a type
emmatown 74cc3cc
Merge branch 'reimplement-vanilla-emotion' into style-component
emmatown 99fd29b
Fix it mostly
emmatown 9f8b62a
Merge branch 'reimplement-vanilla-emotion' into style-component
emmatown ecfb58e
Format
emmatown 71346ff
Fix flow error
emmatown File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`css 1`] = ` | ||
.emotion-0 { | ||
color: hotpink; | ||
} | ||
|
||
<div | ||
className="emotion-0" | ||
/> | ||
`; | ||
|
||
exports[`cx 1`] = ` | ||
.emotion-0 { | ||
color: hotpink; | ||
color: green; | ||
} | ||
|
||
<div | ||
className="some-other-class emotion-0" | ||
/> | ||
`; | ||
|
||
exports[`should get the theme 1`] = ` | ||
.emotion-0 { | ||
color: green; | ||
} | ||
|
||
<div | ||
className="emotion-0" | ||
/> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// @flow | ||
import * as React from 'react' | ||
import 'test-utils/next-env' | ||
import { Style } from '@emotion/core' | ||
import { ThemeProvider } from 'emotion-theming' | ||
import renderer from 'react-test-renderer' | ||
|
||
test('css', () => { | ||
const tree = renderer.create( | ||
<Style> | ||
{({ css }) => ( | ||
<div | ||
className={css` | ||
color: hotpink; | ||
`} | ||
/> | ||
)} | ||
</Style> | ||
) | ||
|
||
expect(tree.toJSON()).toMatchSnapshot() | ||
}) | ||
|
||
it('should get the theme', () => { | ||
const tree = renderer.create( | ||
<ThemeProvider theme={{ color: 'green' }}> | ||
<Style> | ||
{({ css, theme }) => ( | ||
<div | ||
className={css` | ||
color: ${theme.color}; | ||
`} | ||
/> | ||
)} | ||
</Style> | ||
</ThemeProvider> | ||
) | ||
|
||
expect(tree.toJSON()).toMatchSnapshot() | ||
}) | ||
|
||
test('cx', () => { | ||
const tree = renderer.create( | ||
<Style> | ||
{({ css, cx }) => { | ||
let secondClassButItsInsertedFirst = css` | ||
color: green; | ||
` | ||
let firstClassButItsInsertedSecond = css` | ||
color: hotpink; | ||
` | ||
|
||
return ( | ||
<div | ||
className={cx( | ||
firstClassButItsInsertedSecond, | ||
'some-other-class', | ||
secondClassButItsInsertedFirst | ||
)} | ||
/> | ||
) | ||
}} | ||
</Style> | ||
) | ||
|
||
expect(tree.toJSON()).toMatchSnapshot() | ||
}) | ||
|
||
test('css and cx throws when used after render', () => { | ||
let cx, css | ||
renderer.create( | ||
<Style> | ||
{arg => { | ||
;({ cx, css } = arg) | ||
return null | ||
}} | ||
</Style> | ||
) | ||
|
||
expect(cx).toThrowErrorMatchingInlineSnapshot( | ||
`"cx can only be used during render"` | ||
) | ||
expect(css).toThrowErrorMatchingInlineSnapshot( | ||
`"css can only be used during render"` | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// @flow | ||
import * as React from 'react' | ||
import { | ||
getRegisteredStyles, | ||
insertStyles, | ||
isBrowser, | ||
getClassName | ||
} from '@emotion/utils' | ||
import { serializeStyles } from '@emotion/serialize' | ||
import { withCSSContext } from '@emotion/core' | ||
|
||
type ClassNameArg = | ||
| string | ||
| boolean | ||
| (() => ClassNameArg) | ||
| { [key: string]: boolean } | ||
| Array<ClassNameArg> | ||
|
||
let classnames = (args: Array<ClassNameArg>): string => { | ||
let len = args.length | ||
let i = 0 | ||
let cls = '' | ||
for (; i < len; i++) { | ||
let arg = args[i] | ||
if (arg == null) continue | ||
|
||
let toAdd | ||
switch (typeof arg) { | ||
case 'boolean': | ||
break | ||
case 'function': | ||
if (process.env.NODE_ENV !== 'production') { | ||
console.error( | ||
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. is this needed? it's already "next major version of Emotion" :D 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. Nope, I made this a while ago and copied |
||
'Passing functions to cx is deprecated and will be removed in the next major version of Emotion.\n' + | ||
'Please call the function before passing it to cx.' | ||
) | ||
} | ||
toAdd = classnames([arg()]) | ||
break | ||
case 'object': { | ||
if (Array.isArray(arg)) { | ||
toAdd = classnames(arg) | ||
} else { | ||
toAdd = '' | ||
for (const k in arg) { | ||
if (arg[k] && k) { | ||
toAdd && (toAdd += ' ') | ||
toAdd += k | ||
} | ||
} | ||
} | ||
break | ||
} | ||
default: { | ||
toAdd = arg | ||
} | ||
} | ||
if (toAdd) { | ||
cls && (cls += ' ') | ||
cls += toAdd | ||
} | ||
} | ||
return cls | ||
} | ||
function merge(registered: Object, css: (*) => string, className: string) { | ||
const registeredStyles = [] | ||
|
||
const rawClassName = getRegisteredStyles( | ||
registered, | ||
registeredStyles, | ||
className | ||
) | ||
|
||
if (registeredStyles.length < 2) { | ||
return className | ||
} | ||
return rawClassName + css(registeredStyles) | ||
} | ||
|
||
export const Style = withCSSContext((props, context) => { | ||
let rules = '' | ||
let serializedHashes = '' | ||
let hasRendered = false | ||
|
||
let css = (...args) => { | ||
if (hasRendered && process.env.NODE_ENV !== 'production') { | ||
throw new Error('css can only be used during render') | ||
} | ||
let serialized = serializeStyles(context.registered, args) | ||
if (isBrowser) { | ||
insertStyles(context, serialized, false) | ||
} else { | ||
let res = insertStyles(context, serialized, false) | ||
if (res !== undefined) { | ||
rules += res | ||
} | ||
} | ||
if (!isBrowser) { | ||
serializedHashes += ` ${serialized.name}` | ||
} | ||
return getClassName(context, serialized) | ||
} | ||
let cx = (...args) => { | ||
if (hasRendered && process.env.NODE_ENV !== 'production') { | ||
throw new Error('cx can only be used during render') | ||
} | ||
return merge(context.registered, css, classnames(args)) | ||
} | ||
let content = { css, cx, theme: context.theme } | ||
let ele = props.children(content) | ||
hasRendered = true | ||
if (!isBrowser && rules !== undefined) { | ||
return ( | ||
<React.Fragment> | ||
<style | ||
{...{ | ||
[`data-emotion-${context.key}`]: serializedHashes.substring(1), | ||
dangerouslySetInnerHTML: { __html: rules }, | ||
nonce: context.sheet.nonce | ||
}} | ||
/> | ||
{ele} | ||
</React.Fragment> | ||
) | ||
} | ||
return ele | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
can u explain more why this is duplicated? I suspect it's like a global caching registry issue, but couldnt this share the implementation with original cx, css & merge?
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’m just lazy, I think I’ll move it and merge into
@emotion/utils
. There’s no point in sharing css and cx since they need slightly different things in here and in create-emotion.