@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/src/withStyle.tsx CHANGED
@@ -46,11 +46,12 @@ import type {
46
46
  import type {
47
47
  ComponentStyle,
48
48
  ComponentOverride,
49
- GenerateComponentTheme,
50
- GenerateStyle,
49
+ GenerateStyleRework,
51
50
  Props
52
51
  } from './EmotionTypes'
53
52
 
53
+ import type { NewComponentTypes, Theme } from '@instructure/ui-themes'
54
+
54
55
  // Extract is needed because it would allow number otherwise
55
56
  // https://stackoverflow.com/a/51808262/319473
56
57
 
@@ -102,9 +103,8 @@ const defaultValues = {
102
103
  * ```js-code
103
104
  * import { withStyle } from '@instructure/emotion'
104
105
  * import generateStyle from './styles'
105
- * import generateComponentTheme from './theme'
106
106
  *
107
- * export default withStyle(generateStyle, generateComponentTheme)(ExampleComponent)
107
+ * export default withStyle(generateStyle)(ExampleComponent)
108
108
  * ```
109
109
  *
110
110
  * Themeable components inject their themed styles into the document
@@ -123,24 +123,6 @@ const defaultValues = {
123
123
  *
124
124
  * See more about the overrides on the [Using theme overrides](/#using-theme-overrides) docs page.
125
125
  *
126
- * ```js-code
127
- * // ExampleComponent/theme.js
128
- * const generateComponentTheme = (theme) => {
129
- * const { colors } = theme
130
- *
131
- * const componentVariables = {
132
- * background: colors?.backgroundMedium,
133
- * color: colors?.textDarkest,
134
- *
135
- * hoverColor: colors?.textLightest,
136
- * hoverBackground: colors?.backgroundDarkest
137
- * }
138
- *
139
- * return componentVariables
140
- * }
141
- * export default generateComponentTheme
142
- * ```
143
- *
144
126
  * ```jsx-code
145
127
  * {// global theme override}
146
128
  * <InstUISettingsProvider theme={{
@@ -160,26 +142,26 @@ const defaultValues = {
160
142
  * @module withStyle
161
143
  *
162
144
  * @param {function} generateStyle - The function that returns the component's style object
163
- * @param {function} generateComponentTheme - The function that returns the component's theme variables object
164
145
  * @returns {ReactElement} The decorated WithStyle Component
165
146
  */
166
147
  const withStyle = decorator(
167
148
  (
168
149
  ComposedComponent: WithStyleComponent,
169
- generateStyle: GenerateStyle,
170
- generateComponentTheme: GenerateComponentTheme
150
+ generateStyle: GenerateStyleRework,
151
+ useTokensFrom?: keyof NewComponentTypes
171
152
  ) => {
172
153
  const displayName = ComposedComponent.displayName || ComposedComponent.name
173
154
 
155
+ const componentId = ComposedComponent.componentId?.replace('.', '')
156
+
174
157
  const WithStyle: ForwardRefExoticComponent<
175
158
  PropsWithoutRef<Props> & RefAttributes<any>
176
159
  > & {
177
- generateComponentTheme?: GenerateComponentTheme
178
160
  allowedProps?: string[]
179
161
  originalType?: WithStyleComponent
180
162
  defaultProps?: Partial<any>
181
163
  } = forwardRef((props, ref) => {
182
- const theme = useTheme()
164
+ const theme = useTheme() as Theme
183
165
 
184
166
  if (props.styles) {
185
167
  warn(
@@ -202,29 +184,40 @@ const withStyle = decorator(
202
184
  ...defaultValues
203
185
  }
204
186
 
205
- let componentTheme: ComponentTheme =
206
- typeof generateComponentTheme === 'function'
207
- ? generateComponentTheme(theme as BaseTheme)
208
- : {}
187
+ const componentWithTokensId = useTokensFrom ?? componentId
188
+
189
+ const baseComponentTheme =
190
+ theme.newTheme.components[
191
+ componentWithTokensId as keyof NewComponentTypes
192
+ ]
209
193
 
210
194
  const themeOverride = getComponentThemeOverride(
211
195
  theme,
212
196
  displayName,
213
197
  ComposedComponent.componentId,
214
- componentProps,
215
- componentTheme
198
+ (componentProps as ThemeOverrideProp).themeOverride,
199
+ baseComponentTheme
216
200
  )
217
201
 
218
- componentTheme = { ...componentTheme, ...themeOverride }
202
+ const componentTheme = { ...baseComponentTheme, ...themeOverride }
219
203
 
204
+ // TODO do not call here generateStyle, it does not receive the extraArgs
220
205
  const [styles, setStyles] = useState(
221
- generateStyle ? generateStyle(componentTheme, componentProps, {}) : {}
206
+ generateStyle
207
+ ? generateStyle(
208
+ componentTheme,
209
+ componentProps,
210
+ (theme as Theme).newTheme.sharedTokens,
211
+ {}
212
+ )
213
+ : {}
222
214
  )
223
215
 
224
216
  const makeStyleHandler: WithStyleProps['makeStyles'] = (extraArgs) => {
225
217
  const calculatedStyles = generateStyle(
226
218
  componentTheme,
227
219
  componentProps,
220
+ (theme as Theme).newTheme.sharedTokens,
228
221
  extraArgs
229
222
  )
230
223
  if (!isEqual(calculatedStyles, styles)) {
@@ -256,9 +249,6 @@ const withStyle = decorator(
256
249
  // These static fields exist on InstUI components
257
250
  WithStyle.allowedProps = ComposedComponent.allowedProps
258
251
 
259
- // we are exposing the theme generator for the docs generation
260
- WithStyle.generateComponentTheme = generateComponentTheme
261
-
262
252
  // we have to add defaults to makeStyles and styles added by this decorator
263
253
  // eslint-disable-next-line no-param-reassign
264
254
  ComposedComponent.defaultProps = {
@@ -277,4 +267,4 @@ const withStyle = decorator(
277
267
 
278
268
  export default withStyle
279
269
  export { withStyle }
280
- export type { WithStyleProps }
270
+ export type { WithStyleProps, ThemeOverrideProp }
@@ -0,0 +1,212 @@
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
+
25
+ import { forwardRef, useState } from 'react'
26
+ import type {
27
+ ForwardRefExoticComponent,
28
+ PropsWithoutRef,
29
+ RefAttributes
30
+ } from 'react'
31
+
32
+ import hoistNonReactStatics from 'hoist-non-react-statics'
33
+
34
+ import { deepEqual as isEqual } from '@instructure/ui-utils'
35
+ import { warn } from '@instructure/console'
36
+ import { decorator } from '@instructure/ui-decorator'
37
+
38
+ import { getComponentThemeOverride } from './getComponentThemeOverride'
39
+ import { useTheme } from './useTheme'
40
+
41
+ import type {
42
+ BaseTheme,
43
+ ComponentTheme,
44
+ InstUIComponent
45
+ } from '@instructure/shared-types'
46
+ import type {
47
+ ComponentStyle,
48
+ ComponentOverride,
49
+ GenerateComponentTheme,
50
+ GenerateStyle,
51
+ Props
52
+ } from './EmotionTypes'
53
+
54
+ // Extract is needed because it would allow number otherwise
55
+ // https://stackoverflow.com/a/51808262/319473
56
+
57
+ // Unique name of an InstUI component
58
+ type ComponentName = Extract<keyof ComponentOverride, string>
59
+
60
+ interface WithStyleComponent extends InstUIComponent {
61
+ componentId?: ComponentName
62
+ }
63
+
64
+ type WithStylePrivateProps<
65
+ Style extends ComponentStyle | null = ComponentStyle
66
+ > = Style extends null
67
+ ? object
68
+ : {
69
+ styles?: Style
70
+ makeStyles?: (extraArgs?: Record<string, unknown>) => void
71
+ }
72
+
73
+ type ThemeOverrideProp<Theme extends ComponentTheme | null = ComponentTheme> = {
74
+ themeOverride?:
75
+ | Partial<Theme>
76
+ | ((componentTheme: Theme, currentTheme: BaseTheme) => Partial<Theme>)
77
+ }
78
+
79
+ type WithStyleProps<
80
+ Theme extends ComponentTheme | null = ComponentTheme,
81
+ Style extends ComponentStyle | null = ComponentStyle
82
+ > = Theme extends null
83
+ ? WithStylePrivateProps<Style>
84
+ : WithStylePrivateProps<Style> & ThemeOverrideProp<Theme>
85
+
86
+ const defaultValues = {
87
+ styles: {},
88
+ makeStyles: () => {}
89
+ }
90
+
91
+ /**
92
+ * ---
93
+ * category: utilities/themes
94
+ * ---
95
+ * used for old (v11 and eariler) theming system
96
+ * TODO delete when the theme migration is complete
97
+ */
98
+ const withStyleLegacy = decorator(
99
+ (
100
+ ComposedComponent: WithStyleComponent,
101
+ generateStyle: GenerateStyle,
102
+ generateComponentTheme: GenerateComponentTheme
103
+ ) => {
104
+ const displayName = ComposedComponent.displayName || ComposedComponent.name
105
+
106
+ const WithStyle: ForwardRefExoticComponent<
107
+ PropsWithoutRef<Props> & RefAttributes<any>
108
+ > & {
109
+ generateComponentTheme?: GenerateComponentTheme
110
+ allowedProps?: string[]
111
+ originalType?: WithStyleComponent
112
+ defaultProps?: Partial<any>
113
+ } = forwardRef((props, ref) => {
114
+ const theme = useTheme()
115
+
116
+ if (props.styles) {
117
+ warn(
118
+ false,
119
+ `Manually passing the "styles" property is not allowed on the ${displayName} component. Using the default styles calculated by the @withStyle decorator instead.\n`,
120
+ props.styles
121
+ )
122
+ }
123
+
124
+ if (props.makeStyles) {
125
+ warn(
126
+ false,
127
+ `Manually passing the "makeStyles" property is not allowed on the ${displayName} component. Styles are calculated by the @withStyle decorator.`
128
+ )
129
+ }
130
+
131
+ const componentProps: Props = {
132
+ ...ComposedComponent.defaultProps,
133
+ ...props,
134
+ ...defaultValues
135
+ }
136
+
137
+ let componentTheme: ComponentTheme =
138
+ typeof generateComponentTheme === 'function'
139
+ ? generateComponentTheme(theme as BaseTheme)
140
+ : {}
141
+
142
+ const themeOverride = getComponentThemeOverride(
143
+ theme,
144
+ displayName,
145
+ ComposedComponent.componentId,
146
+ (componentProps as ThemeOverrideProp).themeOverride,
147
+ componentTheme
148
+ )
149
+
150
+ componentTheme = { ...componentTheme, ...themeOverride }
151
+
152
+ const [styles, setStyles] = useState(
153
+ generateStyle ? generateStyle(componentTheme, componentProps, {}) : {}
154
+ )
155
+
156
+ const makeStyleHandler: WithStyleProps['makeStyles'] = (extraArgs) => {
157
+ const calculatedStyles = generateStyle(
158
+ componentTheme,
159
+ componentProps,
160
+ extraArgs
161
+ )
162
+ if (!isEqual(calculatedStyles, styles)) {
163
+ setStyles(calculatedStyles)
164
+ }
165
+ }
166
+
167
+ return (
168
+ <ComposedComponent
169
+ ref={ref}
170
+ {...props}
171
+ makeStyles={makeStyleHandler}
172
+ styles={styles}
173
+ // passing themeOverrides is needed for components like Button
174
+ // that have no makeStyles of their own and only pass themeOverrides
175
+ // to the underlying component (e.g.: BaseButton)
176
+ themeOverride={themeOverride}
177
+ />
178
+ )
179
+ })
180
+
181
+ hoistNonReactStatics(WithStyle, ComposedComponent)
182
+
183
+ // added so it can be tested with ReactTestUtils
184
+ // more info: https://github.com/facebook/react/issues/13455
185
+ WithStyle.originalType = ComposedComponent.originalType || ComposedComponent
186
+
187
+ WithStyle.defaultProps = ComposedComponent.defaultProps
188
+ // These static fields exist on InstUI components
189
+ WithStyle.allowedProps = ComposedComponent.allowedProps
190
+
191
+ // we are exposing the theme generator for the docs generation
192
+ WithStyle.generateComponentTheme = generateComponentTheme
193
+
194
+ // we have to add defaults to makeStyles and styles added by this decorator
195
+ // eslint-disable-next-line no-param-reassign
196
+ ComposedComponent.defaultProps = {
197
+ ...ComposedComponent.defaultProps,
198
+ makeStyles: defaultValues.makeStyles,
199
+ styles: defaultValues.styles
200
+ }
201
+
202
+ if (process.env.NODE_ENV !== 'production') {
203
+ WithStyle.displayName = `WithStyle(${displayName})`
204
+ }
205
+
206
+ return WithStyle
207
+ }
208
+ )
209
+
210
+ export default withStyleLegacy
211
+ export { withStyleLegacy }
212
+ export type { WithStyleProps }
@@ -30,6 +30,9 @@
30
30
  },
31
31
  {
32
32
  "path": "../ui-react-utils/tsconfig.build.json"
33
+ },
34
+ {
35
+ "path": "../ui-color-utils/tsconfig.build.json"
33
36
  }
34
37
  ]
35
38
  }