Skip to content

Commit 7c5aba8

Browse files
authored
(split)React-JSS id prop docs and improvements (cssinjs#1147)
* keep only specific to the old HOC parts in the legacy doc * update all TOCs * section about class name generator * flatten sheetOptions, generate new generateId once id options changed
1 parent f6bd43d commit 7c5aba8

File tree

6 files changed

+117
-57
lines changed

6 files changed

+117
-57
lines changed

.size-snapshot.json

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
{
22
"dist/react-jss.js": {
3-
"bundled": 168182,
4-
"minified": 58162,
5-
"gzipped": 18963
3+
"bundled": 168271,
4+
"minified": 58142,
5+
"gzipped": 18993
66
},
77
"dist/react-jss.min.js": {
8-
"bundled": 111457,
9-
"minified": 41516,
10-
"gzipped": 14027
8+
"bundled": 111603,
9+
"minified": 41537,
10+
"gzipped": 14077
1111
},
1212
"dist/react-jss.cjs.js": {
13-
"bundled": 26174,
14-
"minified": 11471,
15-
"gzipped": 3751
13+
"bundled": 26210,
14+
"minified": 11429,
15+
"gzipped": 3784
1616
},
1717
"dist/react-jss.esm.js": {
18-
"bundled": 25212,
19-
"minified": 10636,
20-
"gzipped": 3624,
18+
"bundled": 25248,
19+
"minified": 10594,
20+
"gzipped": 3662,
2121
"treeshaked": {
2222
"rollup": {
23-
"code": 1946,
23+
"code": 1841,
2424
"import_statements": 538
2525
},
2626
"webpack": {
27-
"code": 3544
27+
"code": 3439
2828
}
2929
}
3030
}

src/JssContext.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import React, {type Context} from 'react'
33
import type {Context as JssContextValue} from './types'
44

55
const JssContext: Context<JssContextValue> = React.createContext({
6-
sheetOptions: {classNamePrefix: ''},
6+
classNamePrefix: '',
77
disableStylesGeneration: false
88
})
99

src/JssProvider.js

+34-27
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// @flow
2+
/* eslint-disable react/require-default-props, react/no-unused-prop-types */
3+
24
import React, {Component, type Node} from 'react'
35
import PropTypes from 'prop-types'
46
import {shallowEqualObjects} from 'shallow-equal'
@@ -12,19 +14,19 @@ import defaultJss, {
1214
import type {Context, Managers} from './types'
1315
import JssContext from './JssContext'
1416

15-
/* eslint-disable react/require-default-props, react/no-unused-prop-types */
16-
1717
type Props = {|
1818
jss?: Jss,
1919
registry?: SheetsRegistry,
2020
generateId?: GenerateId,
2121
classNamePrefix?: string,
2222
disableStylesGeneration?: boolean,
2323
media?: string,
24-
children: Node,
25-
id: CreateGenerateIdOptions
24+
id?: CreateGenerateIdOptions,
25+
children: Node
2626
|}
2727

28+
const initialContext: Object = {}
29+
2830
export default class JssProvider extends Component<Props> {
2931
static propTypes = {
3032
registry: PropTypes.instanceOf(SheetsRegistry),
@@ -37,15 +39,20 @@ export default class JssProvider extends Component<Props> {
3739
id: PropTypes.shape({minify: PropTypes.bool})
3840
}
3941

40-
static defaultProps = {id: {minify: false}}
41-
4242
managers: Managers = {}
4343

44-
createContext = (outerContext: Context) => {
45-
const {registry, classNamePrefix, jss, generateId, disableStylesGeneration, media} = this.props
44+
createContext = (parentContext: Context, prevContext?: Context = initialContext) => {
45+
const {
46+
registry,
47+
classNamePrefix,
48+
jss,
49+
generateId,
50+
disableStylesGeneration,
51+
media,
52+
id
53+
} = this.props
4654

47-
// Clone the outer context
48-
const context = {...outerContext}
55+
const context = {...parentContext}
4956

5057
if (registry) {
5158
context.registry = registry
@@ -61,22 +68,24 @@ export default class JssProvider extends Component<Props> {
6168

6269
context.managers = this.managers
6370

64-
if (generateId) {
65-
context.sheetOptions.generateId = generateId
66-
} else if (!context.sheetOptions.generateId) {
67-
if (!this.generateId) {
68-
this.generateId = createGenerateId(this.props.id)
69-
}
70-
context.sheetOptions.generateId = this.generateId
71+
if (id !== undefined) {
72+
context.id = id
73+
}
74+
75+
if (generateId !== undefined) {
76+
context.generateId = generateId
77+
}
78+
79+
if (!context.generateId || !prevContext || context.id !== prevContext.id) {
80+
context.generateId = createGenerateId(context.id)
7181
}
7282

73-
// Merge the classname prefix
7483
if (classNamePrefix) {
75-
context.sheetOptions.classNamePrefix += classNamePrefix
84+
context.classNamePrefix += classNamePrefix
7685
}
7786

7887
if (media !== undefined) {
79-
context.sheetOptions.media = media
88+
context.media = media
8089
}
8190

8291
if (jss) {
@@ -87,12 +96,10 @@ export default class JssProvider extends Component<Props> {
8796
context.disableStylesGeneration = disableStylesGeneration
8897
}
8998

90-
if (this.prevContext && shallowEqualObjects(this.prevContext, context)) {
91-
return this.prevContext
99+
if (prevContext && shallowEqualObjects(prevContext, context)) {
100+
return prevContext
92101
}
93102

94-
this.prevContext = context
95-
96103
return context
97104
}
98105

@@ -102,10 +109,10 @@ export default class JssProvider extends Component<Props> {
102109

103110
registry: ?SheetsRegistry
104111

105-
renderProvider = (outerContext: Context) => {
112+
renderProvider = (parentContext: Context) => {
106113
const {children} = this.props
107-
const context: Context = this.createContext(outerContext)
108-
114+
const context: Context = this.createContext(parentContext, this.prevContext)
115+
this.prevContext = context
109116
return <JssContext.Provider value={context}>{children}</JssContext.Provider>
110117
}
111118

src/JssProvider.test.js

+42
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,48 @@ describe('React-JSS: JssProvider', () => {
191191
`)
192192
})
193193
})
194+
195+
describe('id prop', () => {
196+
it('should forward from context', () => {
197+
const MyComponent = withStyles({a: {color: 'red'}})()
198+
199+
TestRenderer.create(
200+
<JssProvider registry={registry} id={{minify: true}}>
201+
<JssProvider>
202+
<MyComponent />
203+
</JssProvider>
204+
</JssProvider>
205+
)
206+
expect(registry.toString().substr(0, 2)).to.be('.c')
207+
})
208+
209+
it('should overwrite over child props to `true`', () => {
210+
const MyComponent = withStyles({a: {color: 'red'}})()
211+
212+
TestRenderer.create(
213+
<JssProvider registry={registry} id={{minify: false}}>
214+
<JssProvider id={{minify: true}}>
215+
<MyComponent />
216+
</JssProvider>
217+
</JssProvider>
218+
)
219+
expect(registry.toString().substr(0, 2)).to.be('.c')
220+
})
221+
222+
it('should overwrite over child props to `false`', () => {
223+
const MyComponent = withStyles({a: {color: 'red'}})()
224+
225+
TestRenderer.create(
226+
<JssProvider registry={registry} id={{minify: true}}>
227+
<JssProvider id={{minify: false}}>
228+
<MyComponent />
229+
</JssProvider>
230+
</JssProvider>
231+
)
232+
233+
expect(registry.toString().substr(0, 2)).to.be('.N')
234+
})
235+
})
194236
})
195237

196238
describe('JssProvider in a stateful component', () => {

src/types.js

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
// @flow
2-
import type {StyleSheetFactoryOptions, Jss, SheetsRegistry, SheetsManager, BaseRule} from 'jss'
2+
import type {
3+
StyleSheetFactoryOptions,
4+
Jss,
5+
SheetsRegistry,
6+
SheetsManager,
7+
BaseRule,
8+
CreateGenerateIdOptions,
9+
GenerateId
10+
} from 'jss'
311
import type {Node} from 'react'
412
import type {Theming} from 'theming'
513

614
export type Managers = {[key: number]: SheetsManager}
715

8-
type StyleSheetOptions = {
9-
...StyleSheetFactoryOptions,
10-
classNamePrefix: string
11-
}
12-
1316
export type HookOptions<Theme> = StyleSheetFactoryOptions & {
1417
index?: number,
1518
name?: string,
@@ -26,8 +29,11 @@ export type Context = {|
2629
jss?: Jss,
2730
registry?: SheetsRegistry,
2831
managers?: Managers,
29-
sheetOptions: StyleSheetOptions,
30-
disableStylesGeneration: boolean
32+
id?: CreateGenerateIdOptions,
33+
classNamePrefix?: string,
34+
disableStylesGeneration?: boolean,
35+
media?: string,
36+
generateId?: GenerateId
3137
|}
3238

3339
export type HOCProps<Theme, Props> = Props & {

src/utils/sheets.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,27 @@ const getStyles = <Theme>(options: Options<Theme>) => {
3232
}
3333

3434
function getSheetOptions<Theme>(options: Options<Theme>, link: boolean) {
35-
const classNamePrefix =
36-
process.env.NODE_ENV === 'production' || !options.name
37-
? ''
38-
: `${options.name.replace(/\s/g, '-')}-`
35+
let minify
36+
if (options.context.id && options.context.id.minify != null) {
37+
minify = options.context.id.minify
38+
}
39+
40+
let classNamePrefix = options.context.classNamePrefix || ''
41+
if (options.name && !minify) {
42+
classNamePrefix += `${options.name.replace(/\s/g, '-')}-`
43+
}
3944

4045
let meta = ''
4146
if (options.name) meta = `${options.name}, `
4247
meta += typeof options.styles === 'function' ? 'Themed' : 'Unthemed'
4348

4449
return {
4550
...options.sheetOptions,
46-
...options.context.sheetOptions,
4751
index: options.index,
4852
meta,
49-
classNamePrefix: options.context.sheetOptions.classNamePrefix + classNamePrefix,
50-
link
53+
classNamePrefix,
54+
link,
55+
generateId: options.context.generateId
5156
}
5257
}
5358

0 commit comments

Comments
 (0)