@instructure/emotion 11.6.0 → 11.6.1-snapshot-129

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.
Files changed (69) hide show
  1. package/CHANGELOG.md +51 -303
  2. package/es/InstUISettingsProvider/index.js +1 -1
  3. package/es/getComponentThemeOverride.js +5 -4
  4. package/es/getTheme.js +2 -2
  5. package/es/index.js +4 -1
  6. package/es/styleUtils/calcFocusOutlineStyles.js +71 -0
  7. package/es/styleUtils/calcSpacingFromShorthand.js +112 -0
  8. package/es/styleUtils/index.js +2 -1
  9. package/es/useStyle.js +27 -6
  10. package/es/useStyleLegacy.js +49 -0
  11. package/es/useTheme.js +1 -1
  12. package/es/withStyle.js +13 -31
  13. package/es/withStyleLegacy.js +116 -0
  14. package/lib/InstUISettingsProvider/index.js +1 -1
  15. package/lib/getComponentThemeOverride.js +5 -4
  16. package/lib/getTheme.js +2 -2
  17. package/lib/index.js +38 -7
  18. package/lib/styleUtils/calcFocusOutlineStyles.js +78 -0
  19. package/lib/styleUtils/calcSpacingFromShorthand.js +118 -0
  20. package/lib/styleUtils/index.js +14 -7
  21. package/lib/useStyle.js +28 -6
  22. package/lib/useStyleLegacy.js +59 -0
  23. package/lib/useTheme.js +1 -1
  24. package/lib/withStyle.js +13 -31
  25. package/lib/withStyleLegacy.js +125 -0
  26. package/package.json +11 -9
  27. package/src/EmotionTypes.ts +10 -1
  28. package/src/InstUISettingsProvider/index.tsx +5 -1
  29. package/src/getComponentThemeOverride.ts +9 -8
  30. package/src/getTheme.ts +8 -2
  31. package/src/index.ts +7 -2
  32. package/src/styleUtils/calcFocusOutlineStyles.ts +106 -0
  33. package/src/styleUtils/calcSpacingFromShorthand.ts +127 -0
  34. package/src/styleUtils/index.ts +2 -1
  35. package/src/useStyle.ts +63 -32
  36. package/src/useStyleLegacy.ts +92 -0
  37. package/src/useTheme.ts +4 -1
  38. package/src/withStyle.tsx +29 -39
  39. package/src/withStyleLegacy.tsx +212 -0
  40. package/tsconfig.build.json +3 -0
  41. package/tsconfig.build.tsbuildinfo +1 -1
  42. package/types/EmotionTypes.d.ts +4 -2
  43. package/types/EmotionTypes.d.ts.map +1 -1
  44. package/types/InstUISettingsProvider/index.d.ts.map +1 -1
  45. package/types/getComponentThemeOverride.d.ts +4 -5
  46. package/types/getComponentThemeOverride.d.ts.map +1 -1
  47. package/types/getTheme.d.ts.map +1 -1
  48. package/types/index.d.ts +6 -2
  49. package/types/index.d.ts.map +1 -1
  50. package/types/styleUtils/calcFocusOutlineStyles.d.ts +51 -0
  51. package/types/styleUtils/calcFocusOutlineStyles.d.ts.map +1 -0
  52. package/types/styleUtils/calcSpacingFromShorthand.d.ts +33 -0
  53. package/types/styleUtils/calcSpacingFromShorthand.d.ts.map +1 -0
  54. package/types/styleUtils/index.d.ts +2 -1
  55. package/types/styleUtils/index.d.ts.map +1 -1
  56. package/types/useStyle.d.ts +15 -13
  57. package/types/useStyle.d.ts.map +1 -1
  58. package/types/useStyleLegacy.d.ts +22 -0
  59. package/types/useStyleLegacy.d.ts.map +1 -0
  60. package/types/useTheme.d.ts.map +1 -1
  61. package/types/withStyle.d.ts +2 -22
  62. package/types/withStyle.d.ts.map +1 -1
  63. package/types/withStyleLegacy.d.ts +22 -0
  64. package/types/withStyleLegacy.d.ts.map +1 -0
  65. package/es/styleUtils/mapSpacingToShorthand.js +0 -29
  66. package/lib/styleUtils/mapSpacingToShorthand.js +0 -35
  67. package/src/styleUtils/mapSpacingToShorthand.ts +0 -35
  68. package/types/styleUtils/mapSpacingToShorthand.d.ts +0 -5
  69. package/types/styleUtils/mapSpacingToShorthand.d.ts.map +0 -1
package/lib/index.js CHANGED
@@ -5,13 +5,17 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  var _exportNames = {
7
7
  InstUISettingsProvider: true,
8
+ withStyleLegacy: true,
9
+ getComponentThemeOverride: true,
8
10
  withStyle: true,
9
11
  ThemeablePropValues: true,
10
12
  makeThemeVars: true,
11
13
  getShorthandPropValue: true,
12
14
  mirrorShorthandCorners: true,
13
15
  mirrorShorthandEdges: true,
14
- mapSpacingToShorthand: true,
16
+ calcSpacingFromShorthand: true,
17
+ calcFocusOutlineStyles: true,
18
+ useStyleLegacy: true,
15
19
  useStyle: true,
16
20
  useTheme: true
17
21
  };
@@ -27,22 +31,34 @@ Object.defineProperty(exports, "ThemeablePropValues", {
27
31
  return _styleUtils.ThemeablePropValues;
28
32
  }
29
33
  });
30
- Object.defineProperty(exports, "getShorthandPropValue", {
34
+ Object.defineProperty(exports, "calcFocusOutlineStyles", {
31
35
  enumerable: true,
32
36
  get: function () {
33
- return _styleUtils.getShorthandPropValue;
37
+ return _styleUtils.calcFocusOutlineStyles;
34
38
  }
35
39
  });
36
- Object.defineProperty(exports, "makeThemeVars", {
40
+ Object.defineProperty(exports, "calcSpacingFromShorthand", {
37
41
  enumerable: true,
38
42
  get: function () {
39
- return _styleUtils.makeThemeVars;
43
+ return _styleUtils.calcSpacingFromShorthand;
44
+ }
45
+ });
46
+ Object.defineProperty(exports, "getComponentThemeOverride", {
47
+ enumerable: true,
48
+ get: function () {
49
+ return _getComponentThemeOverride.getComponentThemeOverride;
50
+ }
51
+ });
52
+ Object.defineProperty(exports, "getShorthandPropValue", {
53
+ enumerable: true,
54
+ get: function () {
55
+ return _styleUtils.getShorthandPropValue;
40
56
  }
41
57
  });
42
- Object.defineProperty(exports, "mapSpacingToShorthand", {
58
+ Object.defineProperty(exports, "makeThemeVars", {
43
59
  enumerable: true,
44
60
  get: function () {
45
- return _styleUtils.mapSpacingToShorthand;
61
+ return _styleUtils.makeThemeVars;
46
62
  }
47
63
  });
48
64
  Object.defineProperty(exports, "mirrorShorthandCorners", {
@@ -63,6 +79,12 @@ Object.defineProperty(exports, "useStyle", {
63
79
  return _useStyle.useStyle;
64
80
  }
65
81
  });
82
+ Object.defineProperty(exports, "useStyleLegacy", {
83
+ enumerable: true,
84
+ get: function () {
85
+ return _useStyleLegacy.useStyleLegacy;
86
+ }
87
+ });
66
88
  Object.defineProperty(exports, "useTheme", {
67
89
  enumerable: true,
68
90
  get: function () {
@@ -75,6 +97,12 @@ Object.defineProperty(exports, "withStyle", {
75
97
  return _withStyle.withStyle;
76
98
  }
77
99
  });
100
+ Object.defineProperty(exports, "withStyleLegacy", {
101
+ enumerable: true,
102
+ get: function () {
103
+ return _withStyleLegacy.withStyleLegacy;
104
+ }
105
+ });
78
106
  var _react = require("@emotion/react");
79
107
  Object.keys(_react).forEach(function (key) {
80
108
  if (key === "default" || key === "__esModule") return;
@@ -88,7 +116,10 @@ Object.keys(_react).forEach(function (key) {
88
116
  });
89
117
  });
90
118
  var _InstUISettingsProvider = require("./InstUISettingsProvider");
119
+ var _withStyleLegacy = require("./withStyleLegacy");
120
+ var _getComponentThemeOverride = require("./getComponentThemeOverride");
91
121
  var _withStyle = require("./withStyle");
92
122
  var _styleUtils = require("./styleUtils");
123
+ var _useStyleLegacy = require("./useStyleLegacy");
93
124
  var _useStyle = require("./useStyle");
94
125
  var _useTheme = require("./useTheme");
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = exports.calcFocusOutlineStyles = void 0;
7
+ var _alpha = require("@instructure/ui-color-utils/lib/alpha.js");
8
+ /*
9
+ * The MIT License (MIT)
10
+ *
11
+ * Copyright (c) 2015 - present Instructure, Inc.
12
+ *
13
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
14
+ * of this software and associated documentation files (the "Software"), to deal
15
+ * in the Software without restriction, including without limitation the rights
16
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
+ * copies of the Software, and to permit persons to whom the Software is
18
+ * furnished to do so, subject to the following conditions:
19
+ *
20
+ * The above copyright notice and this permission notice shall be included in all
21
+ * copies or substantial portions of the Software.
22
+ *
23
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
+ * SOFTWARE.
30
+ */
31
+
32
+ /**
33
+ * This function creates CSS-in-JS styles for focus indicators.
34
+ *
35
+ * @returns CSS-in-JS style object containing focus outline styles ready for use with emotion or similar libraries.
36
+ */
37
+ const calcFocusOutlineStyles = (theme, params) => {
38
+ var _params$focusColor, _params$focusPosition, _params$shouldAnimate, _params$focusWithin, _params$withFocusOutl, _params$customCSSSele;
39
+ const focusColor = (_params$focusColor = params === null || params === void 0 ? void 0 : params.focusColor) !== null && _params$focusColor !== void 0 ? _params$focusColor : 'info';
40
+ const focusPosition = (_params$focusPosition = params === null || params === void 0 ? void 0 : params.focusPosition) !== null && _params$focusPosition !== void 0 ? _params$focusPosition : 'offset';
41
+ const shouldAnimateFocus = (_params$shouldAnimate = params === null || params === void 0 ? void 0 : params.shouldAnimateFocus) !== null && _params$shouldAnimate !== void 0 ? _params$shouldAnimate : true;
42
+ const focusWithin = (_params$focusWithin = params === null || params === void 0 ? void 0 : params.focusWithin) !== null && _params$focusWithin !== void 0 ? _params$focusWithin : false;
43
+ const withFocusOutline = (_params$withFocusOutl = params === null || params === void 0 ? void 0 : params.withFocusOutline) !== null && _params$withFocusOutl !== void 0 ? _params$withFocusOutl : false;
44
+ const selector = (_params$customCSSSele = params === null || params === void 0 ? void 0 : params.customCSSSelector) !== null && _params$customCSSSele !== void 0 ? _params$customCSSSele : '&:focus';
45
+ const focusColorVariants = {
46
+ info: theme.infoColor,
47
+ inverse: theme.onColor,
48
+ success: theme.successColor,
49
+ danger: theme.dangerColor
50
+ };
51
+ const outlineStyle = {
52
+ outlineColor: focusColorVariants[focusColor],
53
+ outlineStyle: 'solid',
54
+ outlineWidth: theme.width,
55
+ outlineOffset: theme[focusPosition]
56
+ };
57
+ return {
58
+ ...(shouldAnimateFocus && {
59
+ transition: 'outline-color 0.2s, outline-offset 0.25s'
60
+ }),
61
+ outlineOffset: '-0.8rem',
62
+ outlineStyle: 'solid',
63
+ outlineColor: (0, _alpha.alpha)(outlineStyle.outlineColor, 0),
64
+ ...(withFocusOutline && outlineStyle),
65
+ [selector]: {
66
+ ...outlineStyle,
67
+ '&:hover, &:active': {
68
+ // apply the same style so it's not overridden by some global style
69
+ ...outlineStyle
70
+ }
71
+ },
72
+ ...(focusWithin && {
73
+ '&:focus-within': outlineStyle
74
+ })
75
+ };
76
+ };
77
+ exports.calcFocusOutlineStyles = calcFocusOutlineStyles;
78
+ var _default = exports.default = calcFocusOutlineStyles;
@@ -0,0 +1,118 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.calcSpacingFromShorthand = calcSpacingFromShorthand;
7
+ /*
8
+ * The MIT License (MIT)
9
+ *
10
+ * Copyright (c) 2015 - present Instructure, Inc.
11
+ *
12
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
13
+ * of this software and associated documentation files (the "Software"), to deal
14
+ * in the Software without restriction, including without limitation the rights
15
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
+ * copies of the Software, and to permit persons to whom the Software is
17
+ * furnished to do so, subject to the following conditions:
18
+ *
19
+ * The above copyright notice and this permission notice shall be included in all
20
+ * copies or substantial portions of the Software.
21
+ *
22
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
+ * SOFTWARE.
29
+ */
30
+
31
+ /**
32
+ * Converts hyphen-case strings to camelCase
33
+ * Example: 'medium-small' -> 'mediumSmall'
34
+ */
35
+ function camelize(str) {
36
+ return str.replace(/-([a-z])/g, g => g[1].toUpperCase());
37
+ }
38
+
39
+ /**
40
+ * Converts shorthand spacing values into CSS strings using theme spacing tokens.
41
+ *
42
+ * This function parses space-separated spacing values (margin, padding) and resolves theme
43
+ * tokens to their actual CSS values. It supports CSS shorthand syntax (1-4 values), nested
44
+ * theme token paths using dot notation, and automatically converts hyphen-case tokens to camelCase.
45
+ *
46
+ * @param {Spacing | undefined} value - The shorthand spacing value string containing space-separated tokens or CSS values.
47
+ * Tokens can be in camelCase (mediumSmall) or hyphen-case (medium-small).
48
+ * Can be undefined, in which case '0' is returned.
49
+ * @param {Record<string, string>} spacingMap - The spacing theme object containing spacing tokens and nested values.
50
+ * Typically comes from `sharedTokens.margin.spacing` or `sharedTokens.padding.spacing` in the component theme.
51
+ *
52
+ * @returns {string} The resolved CSS spacing string ready to be used in styles.
53
+ *
54
+ * @example
55
+ * // Hyphen-case tokens are converted to camelCase
56
+ * calcSpacingFromShorthand('medium-small', spacingMap) // resolves to spacingMap.mediumSmall
57
+ * calcSpacingFromShorthand('x-large small', spacingMap) // resolves to spacingMap.xLarge + spacingMap.small
58
+ *
59
+ * // Dot notation paths are NOT converted
60
+ * calcSpacingFromShorthand('gap.nested-value', spacingMap) // resolves to spacingMap.gap['nested-value']
61
+ *
62
+ * // CSS values like 'none', 'auto', '10px' are returned as-is
63
+ * calcSpacingFromShorthand('none', spacingMap) // returns 'none'
64
+ */
65
+ function calcSpacingFromShorthand(value, spacingMap) {
66
+ // return undefined when there is no value -> this is important when a component (like View)
67
+ // doesn't have a prop like `padding` but has inline css for padding
68
+ // this makes sure to not overwrite the inline style
69
+ if (!value) return;
70
+ const tokens = value.trim().split(' ');
71
+
72
+ // Handle whitespace-only strings
73
+ if (tokens.length === 1 && tokens[0] === '') return '';
74
+
75
+ // Map each token to its resolved CSS value
76
+ const resolvedValues = tokens.map(token => {
77
+ // Handle special CSS value 'none' - convert to 0 for valid CSS
78
+ if (token === 'none') {
79
+ return '0';
80
+ }
81
+
82
+ // Handle valid CSS numeric and keyword values
83
+ if (token === '0' || token === 'auto') {
84
+ return token;
85
+ }
86
+
87
+ // Handle dot notation for nested theme token paths (no camelization)
88
+ if (token.includes('.')) {
89
+ const path = token.split('.');
90
+ let currentLevel = spacingMap;
91
+ for (const key of path) {
92
+ if (currentLevel && typeof currentLevel === 'object' && key in currentLevel) {
93
+ currentLevel = currentLevel[key];
94
+ } else {
95
+ // If path doesn't resolve, return the original token as fallback
96
+ return token;
97
+ }
98
+ }
99
+ if (typeof currentLevel === 'string') {
100
+ return currentLevel;
101
+ }
102
+ }
103
+
104
+ // For direct tokens, try camelized version
105
+ const camelizedToken = camelize(token);
106
+ const directValue = spacingMap[camelizedToken];
107
+ if (typeof directValue === 'string') {
108
+ return directValue;
109
+ }
110
+
111
+ // Return the original token if not found (could be a direct CSS value like 'auto', '10px', etc.)
112
+
113
+ return token;
114
+ });
115
+
116
+ // Return the space-separated resolved values
117
+ return resolvedValues.join(' ');
118
+ }
@@ -9,22 +9,28 @@ Object.defineProperty(exports, "ThemeablePropValues", {
9
9
  return _ThemeablePropValues.ThemeablePropValues;
10
10
  }
11
11
  });
12
- Object.defineProperty(exports, "getShorthandPropValue", {
12
+ Object.defineProperty(exports, "calcFocusOutlineStyles", {
13
13
  enumerable: true,
14
14
  get: function () {
15
- return _getShorthandPropValue.getShorthandPropValue;
15
+ return _calcFocusOutlineStyles.calcFocusOutlineStyles;
16
16
  }
17
17
  });
18
- Object.defineProperty(exports, "makeThemeVars", {
18
+ Object.defineProperty(exports, "calcSpacingFromShorthand", {
19
19
  enumerable: true,
20
20
  get: function () {
21
- return _makeThemeVars.makeThemeVars;
21
+ return _calcSpacingFromShorthand.calcSpacingFromShorthand;
22
+ }
23
+ });
24
+ Object.defineProperty(exports, "getShorthandPropValue", {
25
+ enumerable: true,
26
+ get: function () {
27
+ return _getShorthandPropValue.getShorthandPropValue;
22
28
  }
23
29
  });
24
- Object.defineProperty(exports, "mapSpacingToShorthand", {
30
+ Object.defineProperty(exports, "makeThemeVars", {
25
31
  enumerable: true,
26
32
  get: function () {
27
- return _mapSpacingToShorthand.mapSpacingToShorthand;
33
+ return _makeThemeVars.makeThemeVars;
28
34
  }
29
35
  });
30
36
  Object.defineProperty(exports, "mirrorShorthandCorners", {
@@ -44,4 +50,5 @@ var _makeThemeVars = require("./makeThemeVars");
44
50
  var _getShorthandPropValue = require("./getShorthandPropValue");
45
51
  var _mirrorShorthandCorners = require("./mirrorShorthandCorners");
46
52
  var _mirrorShorthandEdges = require("./mirrorShorthandEdges");
47
- var _mapSpacingToShorthand = require("./mapSpacingToShorthand");
53
+ var _calcSpacingFromShorthand = require("./calcSpacingFromShorthand");
54
+ var _calcFocusOutlineStyles = require("./calcFocusOutlineStyles");
package/lib/useStyle.js CHANGED
@@ -32,20 +32,42 @@ var _getComponentThemeOverride = require("./getComponentThemeOverride");
32
32
 
33
33
  // returns the second parameter of a function
34
34
 
35
+ /**
36
+ * Type for a theme override
37
+ */
38
+
39
+ const isNewThemeObject = obj => {
40
+ return typeof (obj === null || obj === void 0 ? void 0 : obj.newTheme) === 'object';
41
+ };
42
+
43
+ /**
44
+ * new useStyle syntax, use this with v12 themes
45
+ */
46
+
47
+ // TODO: improve useStyle to handle generateStyle functions that don't
48
+ // have a theme.
35
49
  const useStyle = useStyleParams => {
50
+ var _ref, _ref2;
36
51
  const generateStyle = useStyleParams.generateStyle,
37
- generateComponentTheme = useStyleParams.generateComponentTheme,
38
52
  params = useStyleParams.params,
39
53
  componentId = useStyleParams.componentId,
40
- displayName = useStyleParams.displayName;
54
+ displayName = useStyleParams.displayName,
55
+ themeOverride = useStyleParams.themeOverride;
56
+ const useTokensFrom = useStyleParams.useTokensFrom;
41
57
  const theme = (0, _useTheme.useTheme)();
42
- const baseComponentTheme = generateComponentTheme ? generateComponentTheme(theme) : {};
43
- const themeOverride = (0, _getComponentThemeOverride.getComponentThemeOverride)(theme, displayName ? displayName : componentId || '', componentId, params, baseComponentTheme);
58
+ let baseComponentTheme = {};
59
+ const componentWithTokensId = useTokensFrom !== null && useTokensFrom !== void 0 ? useTokensFrom : componentId;
60
+ if (isNewThemeObject(theme) &&
61
+ // TODO: is it possible not to have a theme object here?
62
+ theme.newTheme.components[componentWithTokensId]) {
63
+ baseComponentTheme = theme.newTheme.components[componentWithTokensId];
64
+ }
65
+ const finalOverride = (0, _getComponentThemeOverride.getComponentThemeOverride)(theme, (_ref = (_ref2 = useTokensFrom !== null && useTokensFrom !== void 0 ? useTokensFrom : displayName) !== null && _ref2 !== void 0 ? _ref2 : componentId) !== null && _ref !== void 0 ? _ref : '', componentWithTokensId, themeOverride, baseComponentTheme);
44
66
  const componentTheme = {
45
67
  ...baseComponentTheme,
46
- ...themeOverride
68
+ ...finalOverride
47
69
  };
48
- return generateStyle(componentTheme, params ? params : {});
70
+ return generateStyle(componentTheme, params, theme.newTheme.sharedTokens);
49
71
  };
50
72
  exports.useStyle = useStyle;
51
73
  var _default = exports.default = useStyle;
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useStyleLegacy = exports.default = void 0;
7
+ var _useTheme = require("./useTheme");
8
+ var _getComponentThemeOverride = require("./getComponentThemeOverride");
9
+ /*
10
+ * The MIT License (MIT)
11
+ *
12
+ * Copyright (c) 2015 - present Instructure, Inc.
13
+ *
14
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
15
+ * of this software and associated documentation files (the "Software"), to deal
16
+ * in the Software without restriction, including without limitation the rights
17
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18
+ * copies of the Software, and to permit persons to whom the Software is
19
+ * furnished to do so, subject to the following conditions:
20
+ *
21
+ * The above copyright notice and this permission notice shall be included in all
22
+ * copies or substantial portions of the Software.
23
+ *
24
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30
+ * SOFTWARE.
31
+ */
32
+
33
+ // returns the second parameter of a function
34
+
35
+ // TODO this is only used by the old themes, remove when everything uses the new
36
+ // theming system
37
+
38
+ /*
39
+ * This is only used by the **old themes**, remove when everything uses the new
40
+ * theming system (InstUI v12)
41
+ */
42
+ const useStyleLegacy = useStyleParams => {
43
+ var _ref;
44
+ const generateStyle = useStyleParams.generateStyle,
45
+ params = useStyleParams.params,
46
+ componentId = useStyleParams.componentId,
47
+ displayName = useStyleParams.displayName;
48
+ const generateComponentTheme = useStyleParams === null || useStyleParams === void 0 ? void 0 : useStyleParams.generateComponentTheme;
49
+ const theme = (0, _useTheme.useTheme)();
50
+ const baseComponentTheme = typeof generateComponentTheme === 'function' ? generateComponentTheme(theme) : {};
51
+ const themeOverride = (0, _getComponentThemeOverride.getComponentThemeOverride)(theme, (_ref = displayName !== null && displayName !== void 0 ? displayName : componentId) !== null && _ref !== void 0 ? _ref : '', componentId, params === null || params === void 0 ? void 0 : params.themeOverride, baseComponentTheme);
52
+ const componentTheme = {
53
+ ...baseComponentTheme,
54
+ ...themeOverride
55
+ };
56
+ return generateStyle(componentTheme, params ? params : {}, theme);
57
+ };
58
+ exports.useStyleLegacy = useStyleLegacy;
59
+ var _default = exports.default = useStyleLegacy;
package/lib/useTheme.js CHANGED
@@ -44,7 +44,7 @@ const useTheme = () => {
44
44
  // This reads the theme from Emotion's ThemeContext
45
45
  let theme = (0, _react.useTheme)();
46
46
  if ((0, _isEmpty.isEmpty)(theme)) {
47
- if (process.env.NODE_ENV !== 'production') {}
47
+ if (process.env.NODE_ENV !== 'production' || process.env.GITHUB_PULL_REQUEST_PREVIEW === 'true') {}
48
48
  theme = _uiThemes.default;
49
49
  }
50
50
  return theme;
package/lib/withStyle.js CHANGED
@@ -62,9 +62,8 @@ const defaultValues = {
62
62
  * ```js-code
63
63
  * import { withStyle } from '@instructure/emotion'
64
64
  * import generateStyle from './styles'
65
- * import generateComponentTheme from './theme'
66
65
  *
67
- * export default withStyle(generateStyle, generateComponentTheme)(ExampleComponent)
66
+ * export default withStyle(generateStyle)(ExampleComponent)
68
67
  * ```
69
68
  *
70
69
  * Themeable components inject their themed styles into the document
@@ -83,24 +82,6 @@ const defaultValues = {
83
82
  *
84
83
  * See more about the overrides on the [Using theme overrides](/#using-theme-overrides) docs page.
85
84
  *
86
- * ```js-code
87
- * // ExampleComponent/theme.js
88
- * const generateComponentTheme = (theme) => {
89
- * const { colors } = theme
90
- *
91
- * const componentVariables = {
92
- * background: colors?.backgroundMedium,
93
- * color: colors?.textDarkest,
94
- *
95
- * hoverColor: colors?.textLightest,
96
- * hoverBackground: colors?.backgroundDarkest
97
- * }
98
- *
99
- * return componentVariables
100
- * }
101
- * export default generateComponentTheme
102
- * ```
103
- *
104
85
  * ```jsx-code
105
86
  * {// global theme override}
106
87
  * <InstUISettingsProvider theme={{
@@ -120,11 +101,12 @@ const defaultValues = {
120
101
  * @module withStyle
121
102
  *
122
103
  * @param {function} generateStyle - The function that returns the component's style object
123
- * @param {function} generateComponentTheme - The function that returns the component's theme variables object
124
104
  * @returns {ReactElement} The decorated WithStyle Component
125
105
  */
126
- const withStyle = exports.withStyle = (0, _decorator.decorator)((ComposedComponent, generateStyle, generateComponentTheme) => {
106
+ const withStyle = exports.withStyle = (0, _decorator.decorator)((ComposedComponent, generateStyle, useTokensFrom) => {
107
+ var _ComposedComponent$co;
127
108
  const displayName = ComposedComponent.displayName || ComposedComponent.name;
109
+ const componentId = (_ComposedComponent$co = ComposedComponent.componentId) === null || _ComposedComponent$co === void 0 ? void 0 : _ComposedComponent$co.replace('.', '');
128
110
  const WithStyle = /*#__PURE__*/(0, _react.forwardRef)((props, ref) => {
129
111
  const theme = (0, _useTheme.useTheme)();
130
112
  if (props.styles) {
@@ -138,18 +120,21 @@ const withStyle = exports.withStyle = (0, _decorator.decorator)((ComposedCompone
138
120
  ...props,
139
121
  ...defaultValues
140
122
  };
141
- let componentTheme = typeof generateComponentTheme === 'function' ? generateComponentTheme(theme) : {};
142
- const themeOverride = (0, _getComponentThemeOverride.getComponentThemeOverride)(theme, displayName, ComposedComponent.componentId, componentProps, componentTheme);
143
- componentTheme = {
144
- ...componentTheme,
123
+ const componentWithTokensId = useTokensFrom !== null && useTokensFrom !== void 0 ? useTokensFrom : componentId;
124
+ const baseComponentTheme = theme.newTheme.components[componentWithTokensId];
125
+ const themeOverride = (0, _getComponentThemeOverride.getComponentThemeOverride)(theme, displayName, ComposedComponent.componentId, componentProps.themeOverride, baseComponentTheme);
126
+ const componentTheme = {
127
+ ...baseComponentTheme,
145
128
  ...themeOverride
146
129
  };
147
- const _useState = (0, _react.useState)(generateStyle ? generateStyle(componentTheme, componentProps, {}) : {}),
130
+
131
+ // TODO do not call here generateStyle, it does not receive the extraArgs
132
+ const _useState = (0, _react.useState)(generateStyle ? generateStyle(componentTheme, componentProps, theme.newTheme.sharedTokens, {}) : {}),
148
133
  _useState2 = (0, _slicedToArray2.default)(_useState, 2),
149
134
  styles = _useState2[0],
150
135
  setStyles = _useState2[1];
151
136
  const makeStyleHandler = extraArgs => {
152
- const calculatedStyles = generateStyle(componentTheme, componentProps, extraArgs);
137
+ const calculatedStyles = generateStyle(componentTheme, componentProps, theme.newTheme.sharedTokens, extraArgs);
153
138
  if (!(0, _deepEqual.deepEqual)(calculatedStyles, styles)) {
154
139
  setStyles(calculatedStyles);
155
140
  }
@@ -175,9 +160,6 @@ const withStyle = exports.withStyle = (0, _decorator.decorator)((ComposedCompone
175
160
  // These static fields exist on InstUI components
176
161
  WithStyle.allowedProps = ComposedComponent.allowedProps;
177
162
 
178
- // we are exposing the theme generator for the docs generation
179
- WithStyle.generateComponentTheme = generateComponentTheme;
180
-
181
163
  // we have to add defaults to makeStyles and styles added by this decorator
182
164
  // eslint-disable-next-line no-param-reassign
183
165
  ComposedComponent.defaultProps = {
@@ -0,0 +1,125 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.withStyleLegacy = exports.default = void 0;
8
+ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
9
+ var _react = require("react");
10
+ var _hoistNonReactStatics = _interopRequireDefault(require("hoist-non-react-statics"));
11
+ var _deepEqual = require("@instructure/ui-utils/lib/deepEqual.js");
12
+ var _console = require("@instructure/console");
13
+ var _decorator = require("@instructure/ui-decorator/lib/decorator.js");
14
+ var _getComponentThemeOverride = require("./getComponentThemeOverride");
15
+ var _useTheme = require("./useTheme");
16
+ var _jsxRuntime = require("@emotion/react/jsx-runtime");
17
+ /*
18
+ * The MIT License (MIT)
19
+ *
20
+ * Copyright (c) 2015 - present Instructure, Inc.
21
+ *
22
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
23
+ * of this software and associated documentation files (the "Software"), to deal
24
+ * in the Software without restriction, including without limitation the rights
25
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
26
+ * copies of the Software, and to permit persons to whom the Software is
27
+ * furnished to do so, subject to the following conditions:
28
+ *
29
+ * The above copyright notice and this permission notice shall be included in all
30
+ * copies or substantial portions of the Software.
31
+ *
32
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38
+ * SOFTWARE.
39
+ */
40
+
41
+ // Extract is needed because it would allow number otherwise
42
+ // https://stackoverflow.com/a/51808262/319473
43
+
44
+ // Unique name of an InstUI component
45
+
46
+ const defaultValues = {
47
+ styles: {},
48
+ makeStyles: () => {}
49
+ };
50
+
51
+ /**
52
+ * ---
53
+ * category: utilities/themes
54
+ * ---
55
+ * used for old (v11 and eariler) theming system
56
+ * TODO delete when the theme migration is complete
57
+ */
58
+ const withStyleLegacy = exports.withStyleLegacy = (0, _decorator.decorator)((ComposedComponent, generateStyle, generateComponentTheme) => {
59
+ const displayName = ComposedComponent.displayName || ComposedComponent.name;
60
+ const WithStyle = /*#__PURE__*/(0, _react.forwardRef)((props, ref) => {
61
+ const theme = (0, _useTheme.useTheme)();
62
+ if (props.styles) {
63
+ (0, _console.warn)(false, `Manually passing the "styles" property is not allowed on the ${displayName} component. Using the default styles calculated by the @withStyle decorator instead.\n`, props.styles);
64
+ }
65
+ if (props.makeStyles) {
66
+ (0, _console.warn)(false, `Manually passing the "makeStyles" property is not allowed on the ${displayName} component. Styles are calculated by the @withStyle decorator.`);
67
+ }
68
+ const componentProps = {
69
+ ...ComposedComponent.defaultProps,
70
+ ...props,
71
+ ...defaultValues
72
+ };
73
+ let componentTheme = typeof generateComponentTheme === 'function' ? generateComponentTheme(theme) : {};
74
+ const themeOverride = (0, _getComponentThemeOverride.getComponentThemeOverride)(theme, displayName, ComposedComponent.componentId, componentProps.themeOverride, componentTheme);
75
+ componentTheme = {
76
+ ...componentTheme,
77
+ ...themeOverride
78
+ };
79
+ const _useState = (0, _react.useState)(generateStyle ? generateStyle(componentTheme, componentProps, {}) : {}),
80
+ _useState2 = (0, _slicedToArray2.default)(_useState, 2),
81
+ styles = _useState2[0],
82
+ setStyles = _useState2[1];
83
+ const makeStyleHandler = extraArgs => {
84
+ const calculatedStyles = generateStyle(componentTheme, componentProps, extraArgs);
85
+ if (!(0, _deepEqual.deepEqual)(calculatedStyles, styles)) {
86
+ setStyles(calculatedStyles);
87
+ }
88
+ };
89
+ return (0, _jsxRuntime.jsx)(ComposedComponent, {
90
+ ref: ref,
91
+ ...props,
92
+ makeStyles: makeStyleHandler,
93
+ styles: styles
94
+ // passing themeOverrides is needed for components like Button
95
+ // that have no makeStyles of their own and only pass themeOverrides
96
+ // to the underlying component (e.g.: BaseButton)
97
+ ,
98
+ themeOverride: themeOverride
99
+ });
100
+ });
101
+ (0, _hoistNonReactStatics.default)(WithStyle, ComposedComponent);
102
+
103
+ // added so it can be tested with ReactTestUtils
104
+ // more info: https://github.com/facebook/react/issues/13455
105
+ WithStyle.originalType = ComposedComponent.originalType || ComposedComponent;
106
+ WithStyle.defaultProps = ComposedComponent.defaultProps;
107
+ // These static fields exist on InstUI components
108
+ WithStyle.allowedProps = ComposedComponent.allowedProps;
109
+
110
+ // we are exposing the theme generator for the docs generation
111
+ WithStyle.generateComponentTheme = generateComponentTheme;
112
+
113
+ // we have to add defaults to makeStyles and styles added by this decorator
114
+ // eslint-disable-next-line no-param-reassign
115
+ ComposedComponent.defaultProps = {
116
+ ...ComposedComponent.defaultProps,
117
+ makeStyles: defaultValues.makeStyles,
118
+ styles: defaultValues.styles
119
+ };
120
+ if (process.env.NODE_ENV !== 'production') {
121
+ WithStyle.displayName = `WithStyle(${displayName})`;
122
+ }
123
+ return WithStyle;
124
+ });
125
+ var _default = exports.default = withStyleLegacy;