@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.
- package/CHANGELOG.md +51 -303
- package/es/InstUISettingsProvider/index.js +1 -1
- package/es/getComponentThemeOverride.js +5 -4
- package/es/getTheme.js +2 -2
- package/es/index.js +4 -1
- package/es/styleUtils/calcFocusOutlineStyles.js +71 -0
- package/es/styleUtils/calcSpacingFromShorthand.js +112 -0
- package/es/styleUtils/index.js +2 -1
- package/es/useStyle.js +27 -6
- package/es/useStyleLegacy.js +49 -0
- package/es/useTheme.js +1 -1
- package/es/withStyle.js +13 -31
- package/es/withStyleLegacy.js +116 -0
- package/lib/InstUISettingsProvider/index.js +1 -1
- package/lib/getComponentThemeOverride.js +5 -4
- package/lib/getTheme.js +2 -2
- package/lib/index.js +38 -7
- package/lib/styleUtils/calcFocusOutlineStyles.js +78 -0
- package/lib/styleUtils/calcSpacingFromShorthand.js +118 -0
- package/lib/styleUtils/index.js +14 -7
- package/lib/useStyle.js +28 -6
- package/lib/useStyleLegacy.js +59 -0
- package/lib/useTheme.js +1 -1
- package/lib/withStyle.js +13 -31
- package/lib/withStyleLegacy.js +125 -0
- package/package.json +11 -9
- package/src/EmotionTypes.ts +10 -1
- package/src/InstUISettingsProvider/index.tsx +5 -1
- package/src/getComponentThemeOverride.ts +9 -8
- package/src/getTheme.ts +8 -2
- package/src/index.ts +7 -2
- package/src/styleUtils/calcFocusOutlineStyles.ts +106 -0
- package/src/styleUtils/calcSpacingFromShorthand.ts +127 -0
- package/src/styleUtils/index.ts +2 -1
- package/src/useStyle.ts +63 -32
- package/src/useStyleLegacy.ts +92 -0
- package/src/useTheme.ts +4 -1
- package/src/withStyle.tsx +29 -39
- package/src/withStyleLegacy.tsx +212 -0
- package/tsconfig.build.json +3 -0
- package/tsconfig.build.tsbuildinfo +1 -1
- package/types/EmotionTypes.d.ts +4 -2
- package/types/EmotionTypes.d.ts.map +1 -1
- package/types/InstUISettingsProvider/index.d.ts.map +1 -1
- package/types/getComponentThemeOverride.d.ts +4 -5
- package/types/getComponentThemeOverride.d.ts.map +1 -1
- package/types/getTheme.d.ts.map +1 -1
- package/types/index.d.ts +6 -2
- package/types/index.d.ts.map +1 -1
- package/types/styleUtils/calcFocusOutlineStyles.d.ts +51 -0
- package/types/styleUtils/calcFocusOutlineStyles.d.ts.map +1 -0
- package/types/styleUtils/calcSpacingFromShorthand.d.ts +33 -0
- package/types/styleUtils/calcSpacingFromShorthand.d.ts.map +1 -0
- package/types/styleUtils/index.d.ts +2 -1
- package/types/styleUtils/index.d.ts.map +1 -1
- package/types/useStyle.d.ts +15 -13
- package/types/useStyle.d.ts.map +1 -1
- package/types/useStyleLegacy.d.ts +22 -0
- package/types/useStyleLegacy.d.ts.map +1 -0
- package/types/useTheme.d.ts.map +1 -1
- package/types/withStyle.d.ts +2 -22
- package/types/withStyle.d.ts.map +1 -1
- package/types/withStyleLegacy.d.ts +22 -0
- package/types/withStyleLegacy.d.ts.map +1 -0
- package/es/styleUtils/mapSpacingToShorthand.js +0 -29
- package/lib/styleUtils/mapSpacingToShorthand.js +0 -35
- package/src/styleUtils/mapSpacingToShorthand.ts +0 -35
- package/types/styleUtils/mapSpacingToShorthand.d.ts +0 -5
- 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
|
-
|
|
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
|
|
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:
|
|
170
|
-
|
|
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
|
-
|
|
206
|
-
|
|
207
|
-
|
|
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
|
-
|
|
198
|
+
(componentProps as ThemeOverrideProp).themeOverride,
|
|
199
|
+
baseComponentTheme
|
|
216
200
|
)
|
|
217
201
|
|
|
218
|
-
componentTheme = { ...
|
|
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
|
|
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 }
|