Skip to content

Commit ad72f45

Browse files
committed
Move theme from context to style props
1 parent 2dd5de9 commit ad72f45

9 files changed

Lines changed: 80 additions & 100 deletions

File tree

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,21 @@ const ButtonContainer = styled(Container)({
5656
import {ThemeProvider} from 'theming'
5757
import styled from 'styled-jss'
5858

59-
const theme = {
59+
const Button = styled('button')(({theme}) => ({
60+
color: theme.color,
61+
'background-color': theme.backgroundColor,
62+
margin: props.margin,
63+
}))
64+
65+
const currentTheme = {
6066
primary: {
6167
color: 'black',
6268
backgroundColor: 'yellow',
6369
},
6470
}
6571

66-
const Button = styled('button')((props, {theme}) => ({
67-
color: theme.color,
68-
'background-color': theme.backgroundColor,
69-
margin: props.margin,
70-
}))
71-
7272
const App = () => (
73-
<ThemeProvider>
73+
<ThemeProvider theme={currentTheme}>
7474
<Button margin={20}>This is themed Button</Button>
7575
</ThemeProvider>
7676
)

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
"deep-extend": "^0.5.0",
4242
"is-observable": "^0.2.0",
4343
"is-react-prop": "^1.0.0",
44-
"jss": "^9.5.1",
45-
"jss-preset-default": "^4.0.0",
44+
"jss": "^9.8.0",
45+
"jss-preset-default": "^4.3.0",
4646
"theming": "^1.3.0"
4747
},
4848
"peerDependencies": {

src/errors/index.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/styled.js

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import composeClasses from './utils/composeClasses'
77
import generateTagName from './utils/generateTagName'
88
import getSeparatedStyles from './utils/getSeparatedStyles'
99

10-
import StyledJssError from './errors'
11-
1210
import type {
1311
JssSheet,
1412
TagNameOrStyledElementType,
@@ -131,29 +129,23 @@ const styled = ({element, ownStyle, mountSheet, jss}: StyledArgs) => {
131129
staticClassName = ''
132130
unsubscribe: ?Function
133131

134-
updateSheet(props: StyledElementPropsType, {theme}: StateType) {
132+
updateSheet(props: StyledElementPropsType, state: StateType) {
135133
let rule
136134
let ruleIndex = 0
137135

136+
const styleProps = state.theme
137+
? Object.assign({}, state, props)
138+
: props
139+
138140
// nested styles become to flatten rules, so we need to update each nested rule
139141
for (ruleIndex; ruleIndex < classMap[this.dynamicTagName].length; ruleIndex++) {
140142
rule = classMap[this.dynamicTagName][ruleIndex]
141143

142144
if (isFunctionStyle) {
143-
const context = {theme}
144-
145-
if (process.env.NODE_ENV !== 'production' && !context.theme) {
146-
Object.defineProperty(context, 'theme', ({
147-
get: () => {
148-
throw new StyledJssError('You should wrap your Application by ThemeProvider to use theme')
149-
}
150-
}: Object))
151-
}
152-
153-
this.sheet.update(rule.key, {props, context})
145+
this.sheet.update(rule.key, styleProps)
154146
}
155147
else {
156-
this.sheet.update(rule.key, props)
148+
this.sheet.update(rule.key, styleProps)
157149
}
158150
}
159151
}

src/tests/__snapshots__/functional.spec.jsx.snap

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,30 @@ exports[`functional tests composable styles should merge all object styles 1`] =
129129
}"
130130
`;
131131

132+
exports[`functional tests composable styles should merge and compose all styles 1`] = `
133+
".button-1-id {
134+
margin: 20px;
135+
padding: 20px;
136+
}
137+
.button-2-id {
138+
border-radius: 100%;
139+
color: red;
140+
background-color: green;
141+
}"
142+
`;
143+
144+
exports[`functional tests composable styles should merge and compose all styles 2`] = `
145+
".button-1-id {
146+
margin: 20px;
147+
padding: 20px;
148+
}
149+
.button-2-id {
150+
border-radius: 100%;
151+
color: black;
152+
background-color: white;
153+
}"
154+
`;
155+
132156
exports[`functional tests should update dynamic props for conditional rules 1`] = `
133157
".button-1-id {
134158
padding: 10px;
@@ -301,8 +325,6 @@ exports[`functional tests should use props on remount 2`] = `
301325
}"
302326
`;
303327

304-
exports[`functional tests theming should throw an exception without ThemeProvider 1`] = `[StyledJssError: You should wrap your Application by ThemeProvider to use theme]`;
305-
306328
exports[`functional tests theming should update theme 1`] = `
307329
".button-1-id {
308330
color: green;

src/tests/functional.spec.jsx

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -304,10 +304,7 @@ describe('functional tests', () => {
304304
wrapper.unmount()
305305
})
306306

307-
/**
308-
* TODO (@lttb): it looks like jss keeps previous props for function rules
309-
*/
310-
it.skip('should merge and compose all styles', () => {
307+
it('should merge and compose all styles', () => {
311308
const round = props => props.round && ({
312309
'border-radius': '100%'
313310
})
@@ -349,7 +346,7 @@ describe('functional tests', () => {
349346
}
350347
}
351348

352-
const Button = styled('button')((_, {theme}) => ({
349+
const Button = styled('button')(({theme}) => ({
353350
color: theme.color.primary,
354351
'background-color': theme.color.secondary,
355352
}))
@@ -374,7 +371,7 @@ describe('functional tests', () => {
374371
}
375372
}
376373

377-
const Button = styled('button')((_, {theme}) => ({
374+
const Button = styled('button')(({theme}) => ({
378375
color: theme.color.primary,
379376
'background-color': theme.color.secondary,
380377
}))
@@ -416,7 +413,7 @@ describe('functional tests', () => {
416413
}
417414
}]
418415

419-
const Button = styled('button')((_, {theme}) => ({
416+
const Button = styled('button')(({theme}) => ({
420417
color: theme.color.primary,
421418
'background-color': theme.color.secondary,
422419
}))
@@ -439,25 +436,5 @@ describe('functional tests', () => {
439436

440437
wrapper.unmount()
441438
})
442-
443-
it('should throw an exception without ThemeProvider', () => {
444-
const Button = styled('button')((_, {theme}) => ({
445-
color: theme.color.primary,
446-
'background-color': theme.color.secondary,
447-
}))
448-
449-
try {
450-
/** @see https://github.com/facebook/react/issues/11098 */
451-
// $FlowIgnore
452-
console.error = () => {}
453-
454-
mount(
455-
<Button />
456-
)
457-
}
458-
catch (e) {
459-
expect(e).toMatchSnapshot()
460-
}
461-
})
462439
})
463440
})

src/types/index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ export type JssStyles = Object
22
export type JssStyle = Object
33
export type JssSheet = Object
44

5-
export type contextType = Object
6-
75
export type BaseStylesType = JssStyles
8-
export type ComponentStyleType = JssStyle | (props: Object, context: contextType) => ?JssStyle
6+
export type ComponentStyleType = JssStyle | (props: Object) => ?JssStyle
97
export type StyledType = Function & {
108
mountSheet: Function,
119
styles: JssStyles

src/utils/getSeparatedStyles.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ const getSeparatedStyles = (...initialStyles: ComponentStyleType[]): separatedSt
4747
if (fns.length) {
4848
const {dynamicStyle} = result
4949

50-
result.dynamicStyle = ({props, context}) => {
50+
result.dynamicStyle = (props) => {
5151
const fnObjects = []
5252

5353
for (let i = 0; i < fns.length; i++) {
54-
fnObjects.push(fns[i](props, context))
54+
fnObjects.push(fns[i](props))
5555
}
5656

5757
return Object.assign({}, dynamicStyle, ...fnObjects)

yarn.lock

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2260,6 +2260,10 @@ https-proxy-agent@^1.0.0:
22602260
debug "2"
22612261
extend "3"
22622262

2263+
hyphenate-style-name@^1.0.2:
2264+
version "1.0.2"
2265+
resolved "https://registry.yarnpkg.com/hyphenate-style-name/-/hyphenate-style-name-1.0.2.tgz#31160a36930adaf1fc04c6074f7eb41465d4ec4b"
2266+
22632267
iconv-lite@0.4.13, iconv-lite@~0.4.13:
22642268
version "0.4.13"
22652269
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.13.tgz#1f88aba4ab0b1508e8312acc39345f36e992e2f2"
@@ -2991,33 +2995,30 @@ jsprim@^1.2.2:
29912995
json-schema "0.2.3"
29922996
verror "1.3.6"
29932997

2994-
jss-camel-case@^6.0.0:
2995-
version "6.0.0"
2996-
resolved "https://registry.yarnpkg.com/jss-camel-case/-/jss-camel-case-6.0.0.tgz#7cf8453e395c31fed931d11efbc885edcd61132e"
2998+
jss-camel-case@^6.1.0:
2999+
version "6.1.0"
3000+
resolved "https://registry.yarnpkg.com/jss-camel-case/-/jss-camel-case-6.1.0.tgz#ccb1ff8d6c701c02a1fed6fb6fb6b7896e11ce44"
3001+
dependencies:
3002+
hyphenate-style-name "^1.0.2"
29973003

29983004
jss-compose@^5.0.0:
29993005
version "5.0.0"
30003006
resolved "https://registry.yarnpkg.com/jss-compose/-/jss-compose-5.0.0.tgz#ce01b2e4521d65c37ea42cf49116e5f7ab596484"
30013007
dependencies:
30023008
warning "^3.0.0"
30033009

3004-
jss-default-unit@^8.0.0:
3005-
version "8.0.0"
3006-
resolved "https://registry.yarnpkg.com/jss-default-unit/-/jss-default-unit-8.0.0.tgz#a308ead4f587ebe17cc845f9870867400de90910"
3007-
dependencies:
3008-
is-observable "^0.2.0"
3010+
jss-default-unit@^8.0.2:
3011+
version "8.0.2"
3012+
resolved "https://registry.yarnpkg.com/jss-default-unit/-/jss-default-unit-8.0.2.tgz#cc1e889bae4c0b9419327b314ab1c8e2826890e6"
30093013

3010-
jss-expand@^5.0.0:
3011-
version "5.0.0"
3012-
resolved "https://registry.yarnpkg.com/jss-expand/-/jss-expand-5.0.0.tgz#8eadb782f29ec5f1d285451dd38052d5ac51644a"
3013-
dependencies:
3014-
is-observable "^0.2.0"
3014+
jss-expand@^5.1.0:
3015+
version "5.1.0"
3016+
resolved "https://registry.yarnpkg.com/jss-expand/-/jss-expand-5.1.0.tgz#b1ad74ec18631f34f65a2124fcfceb6400610e3d"
30153017

3016-
jss-extend@^6.0.1:
3017-
version "6.0.1"
3018-
resolved "https://registry.yarnpkg.com/jss-extend/-/jss-extend-6.0.1.tgz#e53430bc92a42e50d036883ccfecfbc4e1199147"
3018+
jss-extend@^6.2.0:
3019+
version "6.2.0"
3020+
resolved "https://registry.yarnpkg.com/jss-extend/-/jss-extend-6.2.0.tgz#4af09d0b72fb98ee229970f8ca852fec1ca2a8dc"
30193021
dependencies:
3020-
is-observable "^0.2.0"
30213022
warning "^3.0.0"
30223023

30233024
jss-global@^3.0.0:
@@ -3030,28 +3031,28 @@ jss-nested@^6.0.1:
30303031
dependencies:
30313032
warning "^3.0.0"
30323033

3033-
jss-preset-default@^4.0.0:
3034-
version "4.0.1"
3035-
resolved "https://registry.yarnpkg.com/jss-preset-default/-/jss-preset-default-4.0.1.tgz#822cecb87c27ff91633774422f4c221d61486b65"
3034+
jss-preset-default@^4.3.0:
3035+
version "4.3.0"
3036+
resolved "https://registry.yarnpkg.com/jss-preset-default/-/jss-preset-default-4.3.0.tgz#7bc91b0b282492557d36ed4e5c6d7c8cb3154bb8"
30363037
dependencies:
3037-
jss-camel-case "^6.0.0"
3038+
jss-camel-case "^6.1.0"
30383039
jss-compose "^5.0.0"
3039-
jss-default-unit "^8.0.0"
3040-
jss-expand "^5.0.0"
3041-
jss-extend "^6.0.1"
3040+
jss-default-unit "^8.0.2"
3041+
jss-expand "^5.1.0"
3042+
jss-extend "^6.2.0"
30423043
jss-global "^3.0.0"
30433044
jss-nested "^6.0.1"
30443045
jss-props-sort "^6.0.0"
3045-
jss-template "^1.0.0"
3046+
jss-template "^1.0.1"
30463047
jss-vendor-prefixer "^7.0.0"
30473048

30483049
jss-props-sort@^6.0.0:
30493050
version "6.0.0"
30503051
resolved "https://registry.yarnpkg.com/jss-props-sort/-/jss-props-sort-6.0.0.tgz#9105101a3b5071fab61e2d85ea74cc22e9b16323"
30513052

3052-
jss-template@^1.0.0:
3053-
version "1.0.0"
3054-
resolved "https://registry.yarnpkg.com/jss-template/-/jss-template-1.0.0.tgz#4b874608706ddceecacdb5567e254aecb6ea69b3"
3053+
jss-template@^1.0.1:
3054+
version "1.0.1"
3055+
resolved "https://registry.yarnpkg.com/jss-template/-/jss-template-1.0.1.tgz#09aed9d86cc547b07f53ef355d7e1777f7da430a"
30553056
dependencies:
30563057
warning "^3.0.0"
30573058

@@ -3061,9 +3062,9 @@ jss-vendor-prefixer@^7.0.0:
30613062
dependencies:
30623063
css-vendor "^0.3.8"
30633064

3064-
jss@^9.5.1:
3065-
version "9.5.1"
3066-
resolved "https://registry.yarnpkg.com/jss/-/jss-9.5.1.tgz#c869f38cdc44a32f4425fe007ea46b8891536326"
3065+
jss@^9.8.0:
3066+
version "9.8.0"
3067+
resolved "https://registry.yarnpkg.com/jss/-/jss-9.8.0.tgz#77830def563870103f8671ed31ce3a3d2f32aa2b"
30673068
dependencies:
30683069
is-in-browser "^1.1.3"
30693070
symbol-observable "^1.1.0"

0 commit comments

Comments
 (0)