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

Pass in nonce to emotion (Experiment) #8

Open
wants to merge 1 commit into
base: text
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion demo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import style from './style.less'

ReactDOM.render((
<BrowserRouter>
<OIO fontFamily="Helvetica Neue, sans-serif">
<OIO fontFamily="Helvetica Neue, sans-serif" nonce="cd7b0a50-f223-4aaf-9aee-44fbd8329c69">
<div
style={{
position: 'absolute',
Expand Down
132 changes: 122 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
"babel-jest": "23.6.0",
"babel-loader": "8.0.4",
"classnames": "2.2.6",
"create-emotion": "10.0.0-beta.12",
"css-loader": "1.0.1",
"emotion": "9.1.1",
"eslint-config-mother": "2.0.11",
"eslint-plugin-jest": "21.27.1",
"express": "4.16.4",
Expand Down
5 changes: 3 additions & 2 deletions src/OIO/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import React from 'react'

const OIOContext = React.createContext()

// TODO: CLEAN THIS UP
const withOIOContext = WrappedComponent => props => (
<OIOContext.Consumer>
{theme => (
<WrappedComponent {...props} OIOContext={theme} />
{({ emotion, ...theme }) => (
<WrappedComponent {...props} emotion={emotion} OIOContext={theme} />
)}
</OIOContext.Consumer>
)
Expand Down
20 changes: 16 additions & 4 deletions src/OIO/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import React, { Component } from 'react'
import { injectGlobal } from 'emotion'
import createEmotion from 'create-emotion'
import PropTypes from 'prop-types'
import { OIOContext } from './context'
import normalize from './normalize'

// Normalize
injectGlobal`${normalize}` // eslint-disable-line no-unused-expressions

export default class OIO extends Component {
static propTypes = {
children: PropTypes.node,
className: PropTypes.string,
fontFamily: PropTypes.string,
fontSize: PropTypes.string,
fontWeights: PropTypes.object,
nonce: PropTypes.string,
style: PropTypes.object,
textSizeScaleRatio: PropTypes.number,
textSizeMultiplier: PropTypes.number
Expand All @@ -31,11 +29,24 @@ export default class OIO extends Component {
semibold: '600',
bold: '700'
},
nonce: undefined,
style: {},
textSizeScaleRatio: 1.125,
textSizeMultiplier: 1
}

constructor(props) {
super(props)
this.emotion = createEmotion({
nonce: props.nonce
})

// eslint-disable-next-line no-unused-expressions
this.emotion.injectGlobal`${normalize}`
}

emotion = null

render() {
const {
children,
Expand All @@ -49,6 +60,7 @@ export default class OIO extends Component {
} = this.props

const contextProps = {
emotion: this.emotion,
fontFamily,
fontSize,
fontWeights,
Expand Down
4 changes: 3 additions & 1 deletion src/Text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// =======================================================

import React, { Component } from 'react'
import { css, cx } from 'emotion'
import PropTypes from 'prop-types'
import { withOIOContext } from '../OIO/context'
import generateStyleObject from '../utils/generateStyleObject'
Expand Down Expand Up @@ -60,6 +59,8 @@ export default class Text extends Component {
children: PropTypes.node,
className: PropTypes.string,
color: PropTypes.string,
// TODO: USE REAL EMOTION INSTANCE
emotion: PropTypes.object.isRequired,
fontFamily: PropTypes.string,
generatedStyleObject: PropTypes.object.isRequired,
lineHeight: PropTypes.string,
Expand All @@ -84,6 +85,7 @@ export default class Text extends Component {
}

render() {
const { css, cx } = this.props.emotion
const styles = {
...this.props.style,
...this.props.generatedStyleObject
Expand Down