@instructure/emotion 8.23.1-snapshot.1 → 8.23.1-snapshot.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -6
- package/es/EmotionThemeProvider/index.js +3 -108
- package/es/InstUISettingsProvider/index.js +1 -57
- package/es/getTheme.js +84 -0
- package/es/useTheme.js +9 -1
- package/lib/EmotionThemeProvider/index.js +4 -112
- package/lib/InstUISettingsProvider/index.js +2 -58
- package/lib/getTheme.js +96 -0
- package/lib/useTheme.js +10 -1
- package/package.json +12 -11
- package/src/EmotionThemeProvider/index.tsx +3 -145
- package/src/InstUISettingsProvider/index.tsx +1 -57
- package/src/getTheme.ts +109 -0
- package/src/useTheme.ts +9 -1
- package/tsconfig.build.json +3 -0
- package/tsconfig.build.tsbuildinfo +1 -1
- package/types/EmotionThemeProvider/index.d.ts +1 -56
- package/types/EmotionThemeProvider/index.d.ts.map +1 -1
- package/types/InstUISettingsProvider/index.d.ts +0 -56
- package/types/InstUISettingsProvider/index.d.ts.map +1 -1
- package/types/getTheme.d.ts +20 -0
- package/types/getTheme.d.ts.map +1 -0
- package/types/useTheme.d.ts +2 -1
- package/types/useTheme.d.ts.map +1 -1
package/README.md
CHANGED
|
@@ -24,12 +24,6 @@ With this framework, each UI component can be used in isolation and support mult
|
|
|
24
24
|
|
|
25
25
|
7. Use a popular, well maintained and broadly adopted JS design and theming library that supports runtime theme switching ([Emotion](https://emotion.sh/)).
|
|
26
26
|
|
|
27
|
-
### Installation
|
|
28
|
-
|
|
29
|
-
```sh
|
|
30
|
-
yarn add @instructure/emotion
|
|
31
|
-
```
|
|
32
|
-
|
|
33
27
|
### Usage
|
|
34
28
|
|
|
35
29
|
Make a component themeable with the [withStyle](#withStyle) decorator. It adds a `makeStyles` function and the generated `styles` object to the decorated Component's props.
|
|
@@ -22,58 +22,15 @@
|
|
|
22
22
|
* SOFTWARE.
|
|
23
23
|
*/
|
|
24
24
|
import React from 'react';
|
|
25
|
-
import { merge, cloneDeep } from 'lodash';
|
|
26
25
|
import { ThemeProvider } from '@emotion/react';
|
|
27
|
-
import {
|
|
28
|
-
|
|
26
|
+
import { getTheme } from '../getTheme';
|
|
27
|
+
|
|
29
28
|
/**
|
|
30
29
|
* ---
|
|
31
30
|
* category: components/utilities
|
|
32
31
|
* ---
|
|
33
|
-
*
|
|
34
|
-
* #### DEPRECATED Please use [InstUISettingsProvider](#InstUISettingsProvider)
|
|
35
|
-
* instead. It has the same functionality and adds a text direction context.
|
|
36
|
-
*
|
|
37
|
-
* Wrapper for the [ThemeProvider](https://emotion.sh/docs/theming#themeprovider-reactcomponenttype) of emotion js.
|
|
38
|
-
*
|
|
39
|
-
* Applies the given theme. It handles either a full theme, or an overrides object.
|
|
40
|
-
*
|
|
41
|
-
* ```js
|
|
42
|
-
* import { canvas, instructure } from '@instructure/ui-themes'
|
|
43
|
-
* import { EmotionThemeProvider } from '@instructure/emotion'
|
|
44
|
-
*
|
|
45
|
-
* @example
|
|
46
|
-
* <EmotionThemeProvider theme={canvas}>
|
|
47
|
-
* <div>Canvas themed part</div>
|
|
48
|
-
*
|
|
49
|
-
* <EmotionThemeProvider
|
|
50
|
-
* theme={{
|
|
51
|
-
* themeOverrides: {
|
|
52
|
-
* canvas: {
|
|
53
|
-
* colors: {
|
|
54
|
-
* backgroundLightest: '#fefefe'
|
|
55
|
-
* },
|
|
56
|
-
* borders: {
|
|
57
|
-
* style: 'dashed'
|
|
58
|
-
* }
|
|
59
|
-
* }
|
|
60
|
-
* }
|
|
61
|
-
* }}
|
|
62
|
-
* >
|
|
63
|
-
* <div>Canvas with new 'backgroundLightest'</div>
|
|
64
|
-
* </EmotionThemeProvider>
|
|
65
|
-
*
|
|
66
|
-
* <EmotionThemeProvider theme={instructure}>
|
|
67
|
-
* <div>Instructure themed part</div>
|
|
68
|
-
* </EmotionThemeProvider>
|
|
69
|
-
* </EmotionThemeProvider>
|
|
70
|
-
* ```
|
|
71
|
-
*
|
|
72
|
-
* @param {object} children
|
|
73
|
-
* @param {object} theme - A full theme or an override object
|
|
74
32
|
* @module EmotionThemeProvider
|
|
75
33
|
*/
|
|
76
|
-
|
|
77
34
|
function EmotionThemeProvider(_ref) {
|
|
78
35
|
let children = _ref.children,
|
|
79
36
|
_ref$theme = _ref.theme,
|
|
@@ -86,67 +43,5 @@ function EmotionThemeProvider(_ref) {
|
|
|
86
43
|
EmotionThemeProvider.defaultProps = {
|
|
87
44
|
theme: {}
|
|
88
45
|
};
|
|
89
|
-
/**
|
|
90
|
-
* Gives back the theme object for the the provider.
|
|
91
|
-
*
|
|
92
|
-
* If a valid InstUI theme is given, it just returns the theme.
|
|
93
|
-
*
|
|
94
|
-
* If an override object is given, it returns the ancestor theme and
|
|
95
|
-
* the overrides merged together.
|
|
96
|
-
*
|
|
97
|
-
* @param {object} themeOrOverride - A full theme or an override object
|
|
98
|
-
* @returns {function} A function that returns with the theme object for the [ThemeProvider](https://emotion.sh/docs/theming#themeprovider-reactcomponenttype)
|
|
99
|
-
* @module getTheme
|
|
100
|
-
*/
|
|
101
|
-
|
|
102
|
-
const getTheme = themeOrOverride => function () {
|
|
103
|
-
var _themeOrOverride, _themeOrOverride$them;
|
|
104
|
-
|
|
105
|
-
let ancestorTheme = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
|
|
106
|
-
|
|
107
|
-
try {
|
|
108
|
-
// If a valid InstUI theme is given, it just returns the theme.
|
|
109
|
-
if (isBaseTheme(themeOrOverride)) {
|
|
110
|
-
return themeOrOverride;
|
|
111
|
-
}
|
|
112
|
-
} catch {
|
|
113
|
-
// If the prop passed is not an Object, it will throw an error.
|
|
114
|
-
// We are using this fail-safe here for the non-TS users,
|
|
115
|
-
// because the whole page can break without a theme.
|
|
116
|
-
if (process.env.NODE_ENV !== 'production') {} // eslint-disable-next-line no-param-reassign
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
themeOrOverride = {};
|
|
120
|
-
} // we need to clone the ancestor theme not to override it
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
let currentTheme;
|
|
124
|
-
|
|
125
|
-
if (Object.keys(ancestorTheme).length === 0) {
|
|
126
|
-
if (process.env.NODE_ENV !== 'production') {}
|
|
127
|
-
|
|
128
|
-
currentTheme = cloneDeep(canvas);
|
|
129
|
-
} else {
|
|
130
|
-
currentTheme = cloneDeep(ancestorTheme);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
const themeName = currentTheme.key; // we pick the overrides for the current theme from the override object
|
|
134
|
-
|
|
135
|
-
const currentThemeOverrides = ((_themeOrOverride = themeOrOverride) === null || _themeOrOverride === void 0 ? void 0 : (_themeOrOverride$them = _themeOrOverride.themeOverrides) === null || _themeOrOverride$them === void 0 ? void 0 : _themeOrOverride$them[themeName]) || {};
|
|
136
|
-
return merge({}, currentTheme, merge({}, themeOrOverride, currentThemeOverrides));
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
const isBaseTheme = theme => {
|
|
140
|
-
if (Array.isArray(theme) || typeof theme === 'function') {
|
|
141
|
-
throw new Error();
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
try {
|
|
145
|
-
return 'key' in theme && baseThemeProps.every(prop => prop in theme);
|
|
146
|
-
} catch {
|
|
147
|
-
throw new Error();
|
|
148
|
-
}
|
|
149
|
-
};
|
|
150
|
-
|
|
151
46
|
export default EmotionThemeProvider;
|
|
152
|
-
export { EmotionThemeProvider
|
|
47
|
+
export { EmotionThemeProvider };
|
|
@@ -26,7 +26,7 @@ import PropTypes from 'prop-types';
|
|
|
26
26
|
import { ThemeProvider } from '@emotion/react';
|
|
27
27
|
import { TextDirectionContext } from '@instructure/ui-i18n';
|
|
28
28
|
import { DeterministicIdContextProvider, getElementType } from '@instructure/ui-react-utils';
|
|
29
|
-
import { getTheme } from '../
|
|
29
|
+
import { getTheme } from '../getTheme';
|
|
30
30
|
|
|
31
31
|
/**
|
|
32
32
|
* ---
|
|
@@ -34,62 +34,6 @@ import { getTheme } from '../EmotionThemeProvider';
|
|
|
34
34
|
* ---
|
|
35
35
|
* @module InstUISettingsProvider
|
|
36
36
|
* @tsProps
|
|
37
|
-
*
|
|
38
|
-
* Wrapper for emotion js's [ThemeProvider](https://emotion.sh/docs/theming#themeprovider-reactcomponenttype).
|
|
39
|
-
*
|
|
40
|
-
* Applies the given theme. It handles either a full theme, or an overrides object.
|
|
41
|
-
* You can also specify the requested text direction for its descendants.
|
|
42
|
-
*
|
|
43
|
-
* It accepts the following props:
|
|
44
|
-
* - theme - A full theme or an override object.
|
|
45
|
-
* - dir - The text direction to use in the descendants. If not
|
|
46
|
-
* given it uses the following in this priority order:
|
|
47
|
-
* - The value given in a parent `TextDirectionContext`
|
|
48
|
-
* - The `dir` prop of `document.documentElement` or its `direction` CSS prop
|
|
49
|
-
* - `ltr`
|
|
50
|
-
* - instanceCounterMap - A [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map)
|
|
51
|
-
* which keeps track of specific InstUI components. (generally this is used for deterministic id generation for [SSR](/#server-side-rendering))
|
|
52
|
-
* - as - InstUISettingsProvider will wrap it's children with a HTML element when the its `dir` property is also set, this can be changed with the `as` property
|
|
53
|
-
*
|
|
54
|
-
* ```js
|
|
55
|
-
*
|
|
56
|
-
* import { canvas, instructure } from '@instructure/ui-themes'
|
|
57
|
-
* import { InstUISettingsProvider } from '@instructure/emotion'
|
|
58
|
-
*
|
|
59
|
-
* <InstUISettingsProvider theme={canvas}>
|
|
60
|
-
* <div>Canvas themed part</div>
|
|
61
|
-
*
|
|
62
|
-
* <InstUISettingsProvider
|
|
63
|
-
* theme={{
|
|
64
|
-
* themeOverrides: {
|
|
65
|
-
* canvas: {
|
|
66
|
-
* colors: {
|
|
67
|
-
* backgroundLightest: '#fefefe'
|
|
68
|
-
* },
|
|
69
|
-
* borders: {
|
|
70
|
-
* style: 'dashed'
|
|
71
|
-
* }
|
|
72
|
-
* }
|
|
73
|
-
* }
|
|
74
|
-
* }}
|
|
75
|
-
* >
|
|
76
|
-
* <div>Canvas with new 'backgroundLightest'</div>
|
|
77
|
-
* </InstUISettingsProvider>
|
|
78
|
-
*
|
|
79
|
-
* <InstUISettingsProvider theme={instructure} dir="rtl">
|
|
80
|
-
* <div>Instructure themed part with RTL text</div>
|
|
81
|
-
* </InstUISettingsProvider>
|
|
82
|
-
*
|
|
83
|
-
* //this is mostly needed for Server Side Rendering, to read more:
|
|
84
|
-
* //read our [SSR](/#server-side-rendering) guide
|
|
85
|
-
* const counter = generateInstanceCounterMap()
|
|
86
|
-
* counter.set("Alert", 5)
|
|
87
|
-
* <InstUISettingsProvider instanceCounterMap={counter}>
|
|
88
|
-
* //this Alert's rendered DOM Node will have [id="Alert_5"] on it
|
|
89
|
-
* <Alert>Test!</Alert>
|
|
90
|
-
* </InstUISettingsProvider>
|
|
91
|
-
* </InstUISettingsProvider>
|
|
92
|
-
* ```
|
|
93
37
|
*/
|
|
94
38
|
function InstUISettingsProvider(_ref) {
|
|
95
39
|
let children = _ref.children,
|
package/es/getTheme.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License (MIT)
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2015 - present Instructure, Inc.
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
import { merge, cloneDeep } from 'lodash';
|
|
25
|
+
import { canvas } from '@instructure/ui-themes';
|
|
26
|
+
import { ThemeRegistry } from '@instructure/theme-registry';
|
|
27
|
+
import { isBaseTheme } from '@instructure/ui-utils';
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* ---
|
|
31
|
+
* private: true
|
|
32
|
+
* ---
|
|
33
|
+
* Gives back the theme object for the the provider.
|
|
34
|
+
*
|
|
35
|
+
* If a valid InstUI theme is given, it just returns the theme.
|
|
36
|
+
*
|
|
37
|
+
* If an override object is given, it returns the ancestor theme and
|
|
38
|
+
* the overrides merged together.
|
|
39
|
+
*
|
|
40
|
+
* @param {object} themeOrOverride - A full theme or an override object
|
|
41
|
+
* @returns {function} A function that returns with the theme object for the [ThemeProvider](https://emotion.sh/docs/theming#themeprovider-reactcomponenttype)
|
|
42
|
+
*/
|
|
43
|
+
const getTheme = themeOrOverride => function () {
|
|
44
|
+
var _themeOrOverride, _themeOrOverride$them;
|
|
45
|
+
|
|
46
|
+
let ancestorTheme = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
// If a valid InstUI theme is given, it just returns the theme.
|
|
50
|
+
if (isBaseTheme(themeOrOverride)) {
|
|
51
|
+
return themeOrOverride;
|
|
52
|
+
}
|
|
53
|
+
} catch {
|
|
54
|
+
// If the prop passed is not an Object, it will throw an error.
|
|
55
|
+
// We are using this fail-safe here for the non-TS users,
|
|
56
|
+
// because the whole page can break without a theme.
|
|
57
|
+
if (process.env.NODE_ENV !== 'production') {} // eslint-disable-next-line no-param-reassign
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
themeOrOverride = {};
|
|
61
|
+
} // we need to clone the ancestor theme not to override it
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
let currentTheme;
|
|
65
|
+
|
|
66
|
+
if (Object.keys(ancestorTheme).length === 0) {
|
|
67
|
+
const globalTheme = ThemeRegistry.getCurrentTheme();
|
|
68
|
+
|
|
69
|
+
if (process.env.NODE_ENV !== 'production' && !globalTheme) {}
|
|
70
|
+
|
|
71
|
+
currentTheme = cloneDeep(globalTheme || canvas);
|
|
72
|
+
} else {
|
|
73
|
+
currentTheme = cloneDeep(ancestorTheme);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const themeName = currentTheme.key; // we pick the overrides for the current theme from the override object
|
|
77
|
+
|
|
78
|
+
const currentThemeOverrides = ((_themeOrOverride = themeOrOverride) === null || _themeOrOverride === void 0 ? void 0 : (_themeOrOverride$them = _themeOrOverride.themeOverrides) === null || _themeOrOverride$them === void 0 ? void 0 : _themeOrOverride$them[themeName]) || themeOrOverride.themeOverrides || {};
|
|
79
|
+
const finalTheme = merge({}, currentTheme, merge({}, themeOrOverride, currentThemeOverrides));
|
|
80
|
+
return finalTheme;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export default getTheme;
|
|
84
|
+
export { getTheme };
|
package/es/useTheme.js
CHANGED
|
@@ -24,19 +24,27 @@
|
|
|
24
24
|
import { useTheme as useEmotionTheme } from '@emotion/react';
|
|
25
25
|
import { canvas } from '@instructure/ui-themes';
|
|
26
26
|
import { isEmpty } from '@instructure/ui-utils';
|
|
27
|
+
import { ThemeRegistry } from '@instructure/theme-registry';
|
|
27
28
|
|
|
28
29
|
/**
|
|
29
30
|
* ---
|
|
30
31
|
* private: true
|
|
31
32
|
* ---
|
|
32
33
|
* A hook that will return the currently applied theme object from the nearest Context.
|
|
33
|
-
* If there is no
|
|
34
|
+
* If there is no Context, then it tries to get the current theme from the global ThemeRegistry.
|
|
35
|
+
* If there is no theme provided to the Context and ThemeRegistry it will return the default `canvas` theme.
|
|
34
36
|
* @returns {object} the theme object
|
|
35
37
|
*/
|
|
36
38
|
const useTheme = () => {
|
|
37
39
|
let theme = useEmotionTheme();
|
|
38
40
|
|
|
39
41
|
if (isEmpty(theme)) {
|
|
42
|
+
const globalTheme = ThemeRegistry.getCurrentTheme();
|
|
43
|
+
|
|
44
|
+
if (globalTheme) {
|
|
45
|
+
return globalTheme;
|
|
46
|
+
}
|
|
47
|
+
|
|
40
48
|
if (process.env.NODE_ENV !== 'production') {}
|
|
41
49
|
|
|
42
50
|
theme = canvas;
|
|
@@ -6,15 +6,13 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
value: true
|
|
7
7
|
});
|
|
8
8
|
exports.EmotionThemeProvider = EmotionThemeProvider;
|
|
9
|
-
exports.
|
|
9
|
+
exports.default = void 0;
|
|
10
10
|
|
|
11
11
|
var _react = _interopRequireDefault(require("react"));
|
|
12
12
|
|
|
13
|
-
var _lodash = require("lodash");
|
|
14
|
-
|
|
15
13
|
var _react2 = require("@emotion/react");
|
|
16
14
|
|
|
17
|
-
var
|
|
15
|
+
var _getTheme = require("../getTheme");
|
|
18
16
|
|
|
19
17
|
/*
|
|
20
18
|
* The MIT License (MIT)
|
|
@@ -39,130 +37,24 @@ var _uiThemes = require("@instructure/ui-themes");
|
|
|
39
37
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
40
38
|
* SOFTWARE.
|
|
41
39
|
*/
|
|
42
|
-
|
|
40
|
+
|
|
43
41
|
/**
|
|
44
42
|
* ---
|
|
45
43
|
* category: components/utilities
|
|
46
44
|
* ---
|
|
47
|
-
*
|
|
48
|
-
* #### DEPRECATED Please use [InstUISettingsProvider](#InstUISettingsProvider)
|
|
49
|
-
* instead. It has the same functionality and adds a text direction context.
|
|
50
|
-
*
|
|
51
|
-
* Wrapper for the [ThemeProvider](https://emotion.sh/docs/theming#themeprovider-reactcomponenttype) of emotion js.
|
|
52
|
-
*
|
|
53
|
-
* Applies the given theme. It handles either a full theme, or an overrides object.
|
|
54
|
-
*
|
|
55
|
-
* ```js
|
|
56
|
-
* import { canvas, instructure } from '@instructure/ui-themes'
|
|
57
|
-
* import { EmotionThemeProvider } from '@instructure/emotion'
|
|
58
|
-
*
|
|
59
|
-
* @example
|
|
60
|
-
* <EmotionThemeProvider theme={canvas}>
|
|
61
|
-
* <div>Canvas themed part</div>
|
|
62
|
-
*
|
|
63
|
-
* <EmotionThemeProvider
|
|
64
|
-
* theme={{
|
|
65
|
-
* themeOverrides: {
|
|
66
|
-
* canvas: {
|
|
67
|
-
* colors: {
|
|
68
|
-
* backgroundLightest: '#fefefe'
|
|
69
|
-
* },
|
|
70
|
-
* borders: {
|
|
71
|
-
* style: 'dashed'
|
|
72
|
-
* }
|
|
73
|
-
* }
|
|
74
|
-
* }
|
|
75
|
-
* }}
|
|
76
|
-
* >
|
|
77
|
-
* <div>Canvas with new 'backgroundLightest'</div>
|
|
78
|
-
* </EmotionThemeProvider>
|
|
79
|
-
*
|
|
80
|
-
* <EmotionThemeProvider theme={instructure}>
|
|
81
|
-
* <div>Instructure themed part</div>
|
|
82
|
-
* </EmotionThemeProvider>
|
|
83
|
-
* </EmotionThemeProvider>
|
|
84
|
-
* ```
|
|
85
|
-
*
|
|
86
|
-
* @param {object} children
|
|
87
|
-
* @param {object} theme - A full theme or an override object
|
|
88
45
|
* @module EmotionThemeProvider
|
|
89
46
|
*/
|
|
90
|
-
|
|
91
47
|
function EmotionThemeProvider(_ref) {
|
|
92
48
|
let children = _ref.children,
|
|
93
49
|
_ref$theme = _ref.theme,
|
|
94
50
|
theme = _ref$theme === void 0 ? {} : _ref$theme;
|
|
95
51
|
return /*#__PURE__*/_react.default.createElement(_react2.ThemeProvider, {
|
|
96
|
-
theme: getTheme(theme)
|
|
52
|
+
theme: (0, _getTheme.getTheme)(theme)
|
|
97
53
|
}, children);
|
|
98
54
|
}
|
|
99
55
|
|
|
100
56
|
EmotionThemeProvider.defaultProps = {
|
|
101
57
|
theme: {}
|
|
102
58
|
};
|
|
103
|
-
/**
|
|
104
|
-
* Gives back the theme object for the the provider.
|
|
105
|
-
*
|
|
106
|
-
* If a valid InstUI theme is given, it just returns the theme.
|
|
107
|
-
*
|
|
108
|
-
* If an override object is given, it returns the ancestor theme and
|
|
109
|
-
* the overrides merged together.
|
|
110
|
-
*
|
|
111
|
-
* @param {object} themeOrOverride - A full theme or an override object
|
|
112
|
-
* @returns {function} A function that returns with the theme object for the [ThemeProvider](https://emotion.sh/docs/theming#themeprovider-reactcomponenttype)
|
|
113
|
-
* @module getTheme
|
|
114
|
-
*/
|
|
115
|
-
|
|
116
|
-
const getTheme = themeOrOverride => function () {
|
|
117
|
-
var _themeOrOverride, _themeOrOverride$them;
|
|
118
|
-
|
|
119
|
-
let ancestorTheme = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
|
|
120
|
-
|
|
121
|
-
try {
|
|
122
|
-
// If a valid InstUI theme is given, it just returns the theme.
|
|
123
|
-
if (isBaseTheme(themeOrOverride)) {
|
|
124
|
-
return themeOrOverride;
|
|
125
|
-
}
|
|
126
|
-
} catch {
|
|
127
|
-
// If the prop passed is not an Object, it will throw an error.
|
|
128
|
-
// We are using this fail-safe here for the non-TS users,
|
|
129
|
-
// because the whole page can break without a theme.
|
|
130
|
-
if (process.env.NODE_ENV !== 'production') {} // eslint-disable-next-line no-param-reassign
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
themeOrOverride = {};
|
|
134
|
-
} // we need to clone the ancestor theme not to override it
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
let currentTheme;
|
|
138
|
-
|
|
139
|
-
if (Object.keys(ancestorTheme).length === 0) {
|
|
140
|
-
if (process.env.NODE_ENV !== 'production') {}
|
|
141
|
-
|
|
142
|
-
currentTheme = (0, _lodash.cloneDeep)(_uiThemes.canvas);
|
|
143
|
-
} else {
|
|
144
|
-
currentTheme = (0, _lodash.cloneDeep)(ancestorTheme);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
const themeName = currentTheme.key; // we pick the overrides for the current theme from the override object
|
|
148
|
-
|
|
149
|
-
const currentThemeOverrides = ((_themeOrOverride = themeOrOverride) === null || _themeOrOverride === void 0 ? void 0 : (_themeOrOverride$them = _themeOrOverride.themeOverrides) === null || _themeOrOverride$them === void 0 ? void 0 : _themeOrOverride$them[themeName]) || {};
|
|
150
|
-
return (0, _lodash.merge)({}, currentTheme, (0, _lodash.merge)({}, themeOrOverride, currentThemeOverrides));
|
|
151
|
-
};
|
|
152
|
-
|
|
153
|
-
exports.getTheme = getTheme;
|
|
154
|
-
|
|
155
|
-
const isBaseTheme = theme => {
|
|
156
|
-
if (Array.isArray(theme) || typeof theme === 'function') {
|
|
157
|
-
throw new Error();
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
try {
|
|
161
|
-
return 'key' in theme && baseThemeProps.every(prop => prop in theme);
|
|
162
|
-
} catch {
|
|
163
|
-
throw new Error();
|
|
164
|
-
}
|
|
165
|
-
};
|
|
166
|
-
|
|
167
59
|
var _default = EmotionThemeProvider;
|
|
168
60
|
exports.default = _default;
|
|
@@ -22,7 +22,7 @@ var _DeterministicIdContextProvider = require("@instructure/ui-react-utils/lib/D
|
|
|
22
22
|
|
|
23
23
|
var _getElementType = require("@instructure/ui-react-utils/lib/getElementType.js");
|
|
24
24
|
|
|
25
|
-
var
|
|
25
|
+
var _getTheme = require("../getTheme");
|
|
26
26
|
|
|
27
27
|
/*
|
|
28
28
|
* The MIT License (MIT)
|
|
@@ -54,62 +54,6 @@ var _EmotionThemeProvider = require("../EmotionThemeProvider");
|
|
|
54
54
|
* ---
|
|
55
55
|
* @module InstUISettingsProvider
|
|
56
56
|
* @tsProps
|
|
57
|
-
*
|
|
58
|
-
* Wrapper for emotion js's [ThemeProvider](https://emotion.sh/docs/theming#themeprovider-reactcomponenttype).
|
|
59
|
-
*
|
|
60
|
-
* Applies the given theme. It handles either a full theme, or an overrides object.
|
|
61
|
-
* You can also specify the requested text direction for its descendants.
|
|
62
|
-
*
|
|
63
|
-
* It accepts the following props:
|
|
64
|
-
* - theme - A full theme or an override object.
|
|
65
|
-
* - dir - The text direction to use in the descendants. If not
|
|
66
|
-
* given it uses the following in this priority order:
|
|
67
|
-
* - The value given in a parent `TextDirectionContext`
|
|
68
|
-
* - The `dir` prop of `document.documentElement` or its `direction` CSS prop
|
|
69
|
-
* - `ltr`
|
|
70
|
-
* - instanceCounterMap - A [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map)
|
|
71
|
-
* which keeps track of specific InstUI components. (generally this is used for deterministic id generation for [SSR](/#server-side-rendering))
|
|
72
|
-
* - as - InstUISettingsProvider will wrap it's children with a HTML element when the its `dir` property is also set, this can be changed with the `as` property
|
|
73
|
-
*
|
|
74
|
-
* ```js
|
|
75
|
-
*
|
|
76
|
-
* import { canvas, instructure } from '@instructure/ui-themes'
|
|
77
|
-
* import { InstUISettingsProvider } from '@instructure/emotion'
|
|
78
|
-
*
|
|
79
|
-
* <InstUISettingsProvider theme={canvas}>
|
|
80
|
-
* <div>Canvas themed part</div>
|
|
81
|
-
*
|
|
82
|
-
* <InstUISettingsProvider
|
|
83
|
-
* theme={{
|
|
84
|
-
* themeOverrides: {
|
|
85
|
-
* canvas: {
|
|
86
|
-
* colors: {
|
|
87
|
-
* backgroundLightest: '#fefefe'
|
|
88
|
-
* },
|
|
89
|
-
* borders: {
|
|
90
|
-
* style: 'dashed'
|
|
91
|
-
* }
|
|
92
|
-
* }
|
|
93
|
-
* }
|
|
94
|
-
* }}
|
|
95
|
-
* >
|
|
96
|
-
* <div>Canvas with new 'backgroundLightest'</div>
|
|
97
|
-
* </InstUISettingsProvider>
|
|
98
|
-
*
|
|
99
|
-
* <InstUISettingsProvider theme={instructure} dir="rtl">
|
|
100
|
-
* <div>Instructure themed part with RTL text</div>
|
|
101
|
-
* </InstUISettingsProvider>
|
|
102
|
-
*
|
|
103
|
-
* //this is mostly needed for Server Side Rendering, to read more:
|
|
104
|
-
* //read our [SSR](/#server-side-rendering) guide
|
|
105
|
-
* const counter = generateInstanceCounterMap()
|
|
106
|
-
* counter.set("Alert", 5)
|
|
107
|
-
* <InstUISettingsProvider instanceCounterMap={counter}>
|
|
108
|
-
* //this Alert's rendered DOM Node will have [id="Alert_5"] on it
|
|
109
|
-
* <Alert>Test!</Alert>
|
|
110
|
-
* </InstUISettingsProvider>
|
|
111
|
-
* </InstUISettingsProvider>
|
|
112
|
-
* ```
|
|
113
57
|
*/
|
|
114
58
|
function InstUISettingsProvider(_ref) {
|
|
115
59
|
let children = _ref.children,
|
|
@@ -125,7 +69,7 @@ function InstUISettingsProvider(_ref) {
|
|
|
125
69
|
let providers = /*#__PURE__*/_react.default.createElement(_DeterministicIdContextProvider.DeterministicIdContextProvider, {
|
|
126
70
|
instanceCounterMap: instanceCounterMap
|
|
127
71
|
}, /*#__PURE__*/_react.default.createElement(_react2.ThemeProvider, {
|
|
128
|
-
theme: (0,
|
|
72
|
+
theme: (0, _getTheme.getTheme)(theme)
|
|
129
73
|
}, /*#__PURE__*/_react.default.createElement(_TextDirectionContext.TextDirectionContext.Provider, {
|
|
130
74
|
value: finalDir
|
|
131
75
|
}, children)));
|
package/lib/getTheme.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.getTheme = exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _lodash = require("lodash");
|
|
9
|
+
|
|
10
|
+
var _uiThemes = require("@instructure/ui-themes");
|
|
11
|
+
|
|
12
|
+
var _themeRegistry = require("@instructure/theme-registry");
|
|
13
|
+
|
|
14
|
+
var _isBaseTheme = require("@instructure/ui-utils/lib/isBaseTheme.js");
|
|
15
|
+
|
|
16
|
+
/*
|
|
17
|
+
* The MIT License (MIT)
|
|
18
|
+
*
|
|
19
|
+
* Copyright (c) 2015 - present Instructure, Inc.
|
|
20
|
+
*
|
|
21
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
22
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
23
|
+
* in the Software without restriction, including without limitation the rights
|
|
24
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
25
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
26
|
+
* furnished to do so, subject to the following conditions:
|
|
27
|
+
*
|
|
28
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
29
|
+
* copies or substantial portions of the Software.
|
|
30
|
+
*
|
|
31
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
32
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
33
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
34
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
35
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
36
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
37
|
+
* SOFTWARE.
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* ---
|
|
42
|
+
* private: true
|
|
43
|
+
* ---
|
|
44
|
+
* Gives back the theme object for the the provider.
|
|
45
|
+
*
|
|
46
|
+
* If a valid InstUI theme is given, it just returns the theme.
|
|
47
|
+
*
|
|
48
|
+
* If an override object is given, it returns the ancestor theme and
|
|
49
|
+
* the overrides merged together.
|
|
50
|
+
*
|
|
51
|
+
* @param {object} themeOrOverride - A full theme or an override object
|
|
52
|
+
* @returns {function} A function that returns with the theme object for the [ThemeProvider](https://emotion.sh/docs/theming#themeprovider-reactcomponenttype)
|
|
53
|
+
*/
|
|
54
|
+
const getTheme = themeOrOverride => function () {
|
|
55
|
+
var _themeOrOverride, _themeOrOverride$them;
|
|
56
|
+
|
|
57
|
+
let ancestorTheme = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
|
|
58
|
+
|
|
59
|
+
try {
|
|
60
|
+
// If a valid InstUI theme is given, it just returns the theme.
|
|
61
|
+
if ((0, _isBaseTheme.isBaseTheme)(themeOrOverride)) {
|
|
62
|
+
return themeOrOverride;
|
|
63
|
+
}
|
|
64
|
+
} catch {
|
|
65
|
+
// If the prop passed is not an Object, it will throw an error.
|
|
66
|
+
// We are using this fail-safe here for the non-TS users,
|
|
67
|
+
// because the whole page can break without a theme.
|
|
68
|
+
if (process.env.NODE_ENV !== 'production') {} // eslint-disable-next-line no-param-reassign
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
themeOrOverride = {};
|
|
72
|
+
} // we need to clone the ancestor theme not to override it
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
let currentTheme;
|
|
76
|
+
|
|
77
|
+
if (Object.keys(ancestorTheme).length === 0) {
|
|
78
|
+
const globalTheme = _themeRegistry.ThemeRegistry.getCurrentTheme();
|
|
79
|
+
|
|
80
|
+
if (process.env.NODE_ENV !== 'production' && !globalTheme) {}
|
|
81
|
+
|
|
82
|
+
currentTheme = (0, _lodash.cloneDeep)(globalTheme || _uiThemes.canvas);
|
|
83
|
+
} else {
|
|
84
|
+
currentTheme = (0, _lodash.cloneDeep)(ancestorTheme);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const themeName = currentTheme.key; // we pick the overrides for the current theme from the override object
|
|
88
|
+
|
|
89
|
+
const currentThemeOverrides = ((_themeOrOverride = themeOrOverride) === null || _themeOrOverride === void 0 ? void 0 : (_themeOrOverride$them = _themeOrOverride.themeOverrides) === null || _themeOrOverride$them === void 0 ? void 0 : _themeOrOverride$them[themeName]) || themeOrOverride.themeOverrides || {};
|
|
90
|
+
const finalTheme = (0, _lodash.merge)({}, currentTheme, (0, _lodash.merge)({}, themeOrOverride, currentThemeOverrides));
|
|
91
|
+
return finalTheme;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
exports.getTheme = getTheme;
|
|
95
|
+
var _default = getTheme;
|
|
96
|
+
exports.default = _default;
|
package/lib/useTheme.js
CHANGED
|
@@ -11,6 +11,8 @@ var _uiThemes = require("@instructure/ui-themes");
|
|
|
11
11
|
|
|
12
12
|
var _isEmpty = require("@instructure/ui-utils/lib/isEmpty.js");
|
|
13
13
|
|
|
14
|
+
var _themeRegistry = require("@instructure/theme-registry");
|
|
15
|
+
|
|
14
16
|
/*
|
|
15
17
|
* The MIT License (MIT)
|
|
16
18
|
*
|
|
@@ -40,13 +42,20 @@ var _isEmpty = require("@instructure/ui-utils/lib/isEmpty.js");
|
|
|
40
42
|
* private: true
|
|
41
43
|
* ---
|
|
42
44
|
* A hook that will return the currently applied theme object from the nearest Context.
|
|
43
|
-
* If there is no
|
|
45
|
+
* If there is no Context, then it tries to get the current theme from the global ThemeRegistry.
|
|
46
|
+
* If there is no theme provided to the Context and ThemeRegistry it will return the default `canvas` theme.
|
|
44
47
|
* @returns {object} the theme object
|
|
45
48
|
*/
|
|
46
49
|
const useTheme = () => {
|
|
47
50
|
let theme = (0, _react.useTheme)();
|
|
48
51
|
|
|
49
52
|
if ((0, _isEmpty.isEmpty)(theme)) {
|
|
53
|
+
const globalTheme = _themeRegistry.ThemeRegistry.getCurrentTheme();
|
|
54
|
+
|
|
55
|
+
if (globalTheme) {
|
|
56
|
+
return globalTheme;
|
|
57
|
+
}
|
|
58
|
+
|
|
50
59
|
if (process.env.NODE_ENV !== 'production') {}
|
|
51
60
|
|
|
52
61
|
theme = _uiThemes.canvas;
|