@instructure/emotion 10.26.1-snapshot-2 → 10.26.1
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 +2 -23
- package/es/InstUISettingsProvider/index.js +15 -3
- package/es/getTheme.js +4 -2
- package/es/index.js +1 -1
- package/es/styleUtils/ThemeablePropTypes.js +74 -0
- package/es/styleUtils/index.js +1 -0
- package/es/useTheme.js +7 -1
- package/es/withStyle.js +7 -1
- package/lib/InstUISettingsProvider/index.js +16 -3
- package/lib/getTheme.js +4 -2
- package/lib/index.js +7 -0
- package/lib/styleUtils/ThemeablePropTypes.js +81 -0
- package/lib/styleUtils/index.js +7 -0
- package/lib/useTheme.js +7 -1
- package/lib/withStyle.js +7 -1
- package/package.json +15 -13
- package/src/InstUISettingsProvider/index.tsx +46 -11
- package/src/getTheme.ts +5 -2
- package/src/index.ts +1 -0
- package/src/styleUtils/ThemeablePropTypes.ts +121 -0
- package/src/styleUtils/index.ts +1 -0
- package/src/useTheme.ts +9 -1
- package/src/withStyle.tsx +6 -2
- package/tsconfig.build.json +3 -0
- package/tsconfig.build.tsbuildinfo +1 -1
- package/types/InstUISettingsProvider/index.d.ts +29 -3
- package/types/InstUISettingsProvider/index.d.ts.map +1 -1
- package/types/getTheme.d.ts.map +1 -1
- package/types/index.d.ts +1 -1
- package/types/index.d.ts.map +1 -1
- package/types/styleUtils/ThemeablePropTypes.d.ts +22 -0
- package/types/styleUtils/ThemeablePropTypes.d.ts.map +1 -0
- package/types/styleUtils/index.d.ts +1 -0
- package/types/styleUtils/index.d.ts.map +1 -1
- package/types/useTheme.d.ts +2 -1
- package/types/useTheme.d.ts.map +1 -1
- package/types/withStyle.d.ts +1 -1
- package/types/withStyle.d.ts.map +1 -1
|
@@ -0,0 +1,121 @@
|
|
|
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 PropTypes from 'prop-types'
|
|
26
|
+
|
|
27
|
+
import { ThemeablePropValues } from './ThemeablePropValues'
|
|
28
|
+
import type {
|
|
29
|
+
BorderRadiiValues,
|
|
30
|
+
BorderRadii,
|
|
31
|
+
BorderWidthValues,
|
|
32
|
+
BorderWidth,
|
|
33
|
+
SpacingValues
|
|
34
|
+
} from './ThemeablePropValues'
|
|
35
|
+
|
|
36
|
+
const {
|
|
37
|
+
SHADOW_TYPES,
|
|
38
|
+
STACKING_TYPES,
|
|
39
|
+
BORDER_WIDTHS,
|
|
40
|
+
BORDER_RADII,
|
|
41
|
+
BACKGROUNDS,
|
|
42
|
+
SIZES
|
|
43
|
+
} = ThemeablePropValues
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* ---
|
|
47
|
+
* category: utilities/themes
|
|
48
|
+
* ---
|
|
49
|
+
* Custom prop types for themeable React components.
|
|
50
|
+
* @module ThemeablePropTypes
|
|
51
|
+
*/
|
|
52
|
+
const ThemeablePropTypes = {
|
|
53
|
+
shadow: PropTypes.oneOf(Object.values(SHADOW_TYPES)),
|
|
54
|
+
stacking: PropTypes.oneOf(Object.values(STACKING_TYPES)),
|
|
55
|
+
borderWidth: shorthandPropType(Object.values(BORDER_WIDTHS)),
|
|
56
|
+
borderRadius: shorthandPropType(Object.values(BORDER_RADII)),
|
|
57
|
+
background: PropTypes.oneOf(Object.values(BACKGROUNDS)),
|
|
58
|
+
size: PropTypes.oneOf(Object.values(SIZES))
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
type ValueKeys =
|
|
62
|
+
| BorderWidthValues[]
|
|
63
|
+
| BorderRadiiValues[]
|
|
64
|
+
| SpacingValues[]
|
|
65
|
+
| string[]
|
|
66
|
+
|
|
67
|
+
function shorthandPropType<V extends ValueKeys>(
|
|
68
|
+
validValues: V
|
|
69
|
+
): PropTypes.Validator<
|
|
70
|
+
V extends BorderWidthValues[]
|
|
71
|
+
? BorderWidth
|
|
72
|
+
: V extends BorderRadiiValues[]
|
|
73
|
+
? BorderRadii
|
|
74
|
+
: string[]
|
|
75
|
+
> {
|
|
76
|
+
return function (
|
|
77
|
+
props: Record<string, unknown>,
|
|
78
|
+
propName: string,
|
|
79
|
+
componentName: string,
|
|
80
|
+
location: string
|
|
81
|
+
) {
|
|
82
|
+
const propValue = props[propName]
|
|
83
|
+
|
|
84
|
+
if (typeof propValue === 'undefined') {
|
|
85
|
+
return null
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (typeof propValue !== 'string') {
|
|
89
|
+
return new Error(
|
|
90
|
+
`Invalid ${location} \`${propName}\` of type \`${typeof propValue}\` supplied to \`${componentName}\`, expected ` +
|
|
91
|
+
`a string.`
|
|
92
|
+
)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const propValues = propValue.split(' ')
|
|
96
|
+
const valuesLength = propValues.length
|
|
97
|
+
if (valuesLength > 0 && valuesLength < 5) {
|
|
98
|
+
for (let i = 0; i < valuesLength; i++) {
|
|
99
|
+
const valueIndex = (validValues as string[]).indexOf(propValues[i])
|
|
100
|
+
if (valueIndex === -1) {
|
|
101
|
+
return new Error(
|
|
102
|
+
`Invalid ${location} \`${propName}\` \`${propValues[i]}\` supplied to \`${componentName}\`, expected ` +
|
|
103
|
+
`a one of \`${validValues.join(', ')}\`.`
|
|
104
|
+
)
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
} else {
|
|
108
|
+
return new Error(
|
|
109
|
+
`Invalid ${location} \`${propName}\` \`${propValue}\` supplied to \`${componentName}\`, expected ` +
|
|
110
|
+
`between one and four of the following valid values: \`${validValues.join(
|
|
111
|
+
', '
|
|
112
|
+
)}\`.`
|
|
113
|
+
)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return null
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export default ThemeablePropTypes
|
|
121
|
+
export { ThemeablePropTypes, shorthandPropType }
|
package/src/styleUtils/index.ts
CHANGED
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
25
|
export { ThemeablePropValues } from './ThemeablePropValues'
|
|
26
|
+
export { ThemeablePropTypes } from './ThemeablePropTypes'
|
|
26
27
|
export { makeThemeVars } from './makeThemeVars'
|
|
27
28
|
export { getShorthandPropValue } from './getShorthandPropValue'
|
|
28
29
|
export { mirrorShorthandCorners } from './mirrorShorthandCorners'
|
package/src/useTheme.ts
CHANGED
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
import { useTheme as useEmotionTheme } from '@emotion/react'
|
|
26
26
|
import canvas from '@instructure/ui-themes'
|
|
27
27
|
import { isEmpty } from '@instructure/ui-utils'
|
|
28
|
+
import { ThemeRegistry } from '@instructure/theme-registry'
|
|
28
29
|
|
|
29
30
|
import type { BaseThemeOrOverride } from './EmotionTypes'
|
|
30
31
|
|
|
@@ -33,13 +34,20 @@ import type { BaseThemeOrOverride } from './EmotionTypes'
|
|
|
33
34
|
* private: true
|
|
34
35
|
* ---
|
|
35
36
|
* A hook that will return the currently applied theme object from the nearest Context.
|
|
36
|
-
* If there is no
|
|
37
|
+
* If there is no Context, then it tries to get the current theme from the global ThemeRegistry.
|
|
38
|
+
* If there is no theme provided to the Context and ThemeRegistry it will return the default `canvas` theme.
|
|
37
39
|
* @returns The theme object
|
|
38
40
|
*/
|
|
39
41
|
const useTheme = () => {
|
|
40
42
|
// This reads the theme from Emotion's ThemeContext
|
|
41
43
|
let theme = useEmotionTheme() as BaseThemeOrOverride
|
|
44
|
+
|
|
42
45
|
if (isEmpty(theme)) {
|
|
46
|
+
const globalTheme = ThemeRegistry.getCurrentTheme()
|
|
47
|
+
|
|
48
|
+
if (globalTheme) {
|
|
49
|
+
return globalTheme
|
|
50
|
+
}
|
|
43
51
|
if (process.env.NODE_ENV !== 'production') {
|
|
44
52
|
console.warn(
|
|
45
53
|
`No theme provided for [InstUISettingsProvider], using default <canvas> theme.`
|
package/src/withStyle.tsx
CHANGED
|
@@ -116,7 +116,7 @@ const defaultValues = {
|
|
|
116
116
|
* [InstUISettingsProvider](#InstUISettingsProvider) component, and/or set
|
|
117
117
|
* explicitly via its `themeOverride` prop.
|
|
118
118
|
*
|
|
119
|
-
* InstUISettingsProvider provides a theme object (e.g. the [canvas theme](/#canvas)).
|
|
119
|
+
* InstUISettingsProvider provides a theme object with global theme variables (e.g. the [canvas theme](/#canvas)).
|
|
120
120
|
* These variables are mapped to the component's own variables in `theme.js` (see [theming](#theming-basics) for more info).
|
|
121
121
|
*
|
|
122
122
|
* With the `themeOverride` prop you can directly set/override the component theme variables declared in theme.js. It accepts an object or a function. The function has the component's theme and the currently active main theme as its parameter.
|
|
@@ -177,7 +177,6 @@ const withStyle = decorator(
|
|
|
177
177
|
generateComponentTheme?: GenerateComponentTheme
|
|
178
178
|
allowedProps?: string[]
|
|
179
179
|
originalType?: WithStyleComponent
|
|
180
|
-
defaultProps?: Partial<any>
|
|
181
180
|
} = forwardRef((props, ref) => {
|
|
182
181
|
const theme = useTheme()
|
|
183
182
|
|
|
@@ -252,7 +251,12 @@ const withStyle = decorator(
|
|
|
252
251
|
// more info: https://github.com/facebook/react/issues/13455
|
|
253
252
|
WithStyle.originalType = ComposedComponent.originalType || ComposedComponent
|
|
254
253
|
|
|
254
|
+
// we have to pass these on, because sometimes users
|
|
255
|
+
// access propTypes of the component in other components
|
|
256
|
+
// eslint-disable-next-line react/forbid-foreign-prop-types
|
|
257
|
+
WithStyle.propTypes = ComposedComponent.propTypes
|
|
255
258
|
WithStyle.defaultProps = ComposedComponent.defaultProps
|
|
259
|
+
|
|
256
260
|
// These static fields exist on InstUI components
|
|
257
261
|
WithStyle.allowedProps = ComposedComponent.allowedProps
|
|
258
262
|
|