Repository navigation
Expand file tree
/
Copy pathThemeProvider.js
More file actions
132 lines (109 loc) · 4.18 KB
/
Copy pathThemeProvider.js
File metadata and controls
132 lines (109 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// @flow
import React, { Component } from 'react';
import type { Node } from 'react';
// external libraries
import { isEmpty, isEqual, cloneDeep } from 'lodash';
// contains default theme and context provider
import { ThemeContext } from './HOC/ThemeContext';
// imports the Root Theme API object which specifies the shape
// of a complete theme for every component in this library, used in this.composeLibraryTheme
import { ROOT_THEME_API } from '../themes/API';
// internal utility functions
import { appendToProperty } from '../utils/themes';
import { hasProperty } from '../utils/props';
type Props = {
children: Node,
theme: Object,
themeOverrides: Object // custom css/scss from user that adheres to shape of ROOT_THEME_API
};
type State = {
theme: Object
};
export class ThemeProvider extends Component<Props, State> {
static defaultProps = {
themeOverrides: {}
};
constructor(props: Props) {
super(props);
const { theme, themeOverrides } = props;
this.state = {
theme: this._composeLibraryTheme(theme, themeOverrides)
};
}
componentWillReceiveProps(nextProps: Props) {
const { theme, themeOverrides } = this.props;
const { theme: nextTheme, themeOverrides: nextOverrides } = nextProps;
if (!isEqual(theme, nextTheme) || !isEqual(themeOverrides, nextOverrides)) {
this.setState(() => ({
theme: this._composeLibraryTheme(nextTheme, nextOverrides)
}));
}
}
// composeLibraryTheme returns a single obj containing theme definitions
// for every component in the library. Every key on the returned obj is named
// in conjunction with a component in the library and each key's value is structured
// to contain the css definitions for each element in that component.
// Which is just a string via CSS-Modules. Looks like this:
// {
// button: { root: '', disabled: '' },
// input: { input: '', disabled: '', error: '' },
// formField: { root: '', label: '', error: '' },
// ... and so on, creating a complete theme for the library,
// }
_composeLibraryTheme = (theme: Object, themeOverrides: Object) => {
// if themeOverrides is empty, no need for composition
if (isEmpty(themeOverrides)) { return theme; }
// final object to be returned
const composedTheme = {};
for (const componentName in ROOT_THEME_API) {
// check if ROOT_THEME_API contains the key of componentName
if (hasProperty(ROOT_THEME_API, componentName)) {
// check if theme contains a key of componentName
if (hasProperty(theme, componentName)) {
// add componentName as a key to final return obj
composedTheme[componentName] = theme[componentName];
}
// also check if themeOverrides contains the key componentName
if (hasProperty(themeOverrides, componentName)) {
// compose theme styles with user's themeOverrides
composedTheme[componentName] = this._applyThemeOverrides(
theme[componentName],
themeOverrides[componentName],
ROOT_THEME_API[componentName]
);
}
}
}
return composedTheme;
};
_applyThemeOverrides = (
componentTheme: Object,
componentThemeOverrides: Object,
componentThemeAPI: Object
) => {
// Return componentTheme if there are no overrides provided
if (isEmpty(componentThemeOverrides)) { return componentTheme; }
// final composed theme obj to be returned at end
const composedComponentTheme = cloneDeep(componentThemeAPI);
for (const className in componentThemeAPI) {
if (hasProperty(componentThemeAPI, className)) {
if (hasProperty(componentTheme, className)) {
appendToProperty(composedComponentTheme, className, componentTheme[className]);
}
if (hasProperty(componentThemeOverrides, className)) {
appendToProperty(composedComponentTheme, className, componentThemeOverrides[className]);
}
}
}
return composedComponentTheme;
}
render() {
const { theme } = this.state;
const providerState = { theme, ROOT_THEME_API };
return (
<ThemeContext.Provider value={providerState}>
{this.props.children}
</ThemeContext.Provider>
);
}
}