Skip to content

Commit 2a9488d

Browse files
committed
WIP(many): deprecate globalThis and allow local themes
1 parent 3180a02 commit 2a9488d

File tree

7 files changed

+29
-9
lines changed

7 files changed

+29
-9
lines changed

packages/emotion/src/getTheme.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,17 @@ import type {
3737
* ---
3838
* private: true
3939
* ---
40-
* Gives back the theme object for the the provider.
40+
* Gives back the theme object for the provider.
4141
*
4242
* If a valid InstUI theme is given, it just returns the theme.
4343
*
4444
* If an override object is given, it returns the ancestor theme and
4545
* the overrides merged together.
4646
*
47-
* @param {object} themeOrOverride - A full theme or an override object
48-
* @returns {function} A function that returns with the theme object for the [ThemeProvider](https://emotion.sh/docs/theming#themeprovider-reactcomponenttype)
47+
* @param themeOrOverride - A full theme or an override object
48+
* @returns A function that returns with the theme object for the [ThemeProvider](https://emotion.sh/docs/theming#themeprovider-reactcomponenttype)
49+
* This function is called by Emotion on theme provider creation, where
50+
* `ancestorTheme` is a theme object from an ancestor `ThemeProvider`
4951
*/
5052
const getTheme =
5153
(themeOrOverride: ThemeOrOverride) =>
@@ -71,7 +73,6 @@ const getTheme =
7173

7274
// we need to clone the ancestor theme not to override it
7375
let currentTheme
74-
7576
if (Object.keys(ancestorTheme).length === 0) {
7677
const globalTheme = ThemeRegistry.getCurrentTheme()
7778

packages/emotion/src/useTheme.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ import type { ThemeOrOverride } from './EmotionTypes'
3636
* A hook that will return the currently applied theme object from the nearest Context.
3737
* If there is no Context, then it tries to get the current theme from the global ThemeRegistry.
3838
* If there is no theme provided to the Context and ThemeRegistry it will return the default `canvas` theme.
39-
* @returns {object} the theme object
39+
* @returns The theme object
4040
*/
4141
const useTheme = () => {
42+
// This reads the theme from Emotion's ThemeContext
4243
let theme = useEmotionTheme() as ThemeOrOverride
4344

4445
if (isEmpty(theme)) {

packages/theme-registry/src/ThemeRegistry.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* ---
2727
* category: utilities/themes
2828
* ---
29+
* DEPRECATED. This will be deleted, please use InstUISettingsProvider instead.
2930
* A global theme registry used for registering theme objects, setting globally available themes
3031
* and receiving the currently used theme.
3132
* @module ThemeRegistry
@@ -52,7 +53,9 @@ type Registry<T extends RegisteredTheme> = {
5253
}
5354

5455
type RegisteredTheme<T extends BaseTheme = BaseTheme> = T & {
56+
// DEPRECATED. Use InstUISettingsProvider instead
5557
use(arg?: { overrides: DeepPartial<BaseThemeVariables> }): void
58+
// DEPRECATED. Read its variables dirrectly from the theme object.
5659
variables: BaseThemeVariables
5760
}
5861

@@ -225,7 +228,8 @@ function makeTheme<T extends BaseTheme>(theme: T): RegisteredTheme<T> {
225228
* Registers the passed theme into the ThemeRegistry.
226229
* @param {BaseTheme} theme - the theme object to register into the ThemeRegistry
227230
* @returns {RegisteredTheme} If the given theme is already in the ThemeRegistry then simply return that theme.
228-
* Otherwise returns the theme with a wrapper around it, so it can be `.use()`-ed to activate the given theme from the registry.
231+
* Otherwise, returns the theme with a wrapper around it, so it can be `.use()`-ed to activate the given theme from the registry.
232+
* This function also adds a `variables` prop for backwards compatibility (deprecated).
229233
* @module registerTheme
230234
*/
231235
function registerTheme<T extends BaseTheme>(theme: T): RegisteredTheme<T> {

packages/ui-themes/src/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ import type {
3232
UI
3333
} from '@instructure/shared-types'
3434

35-
import canvasHighContrast from './themes/canvasHighContrast'
36-
import canvas from './themes/canvas'
35+
import canvasHighContrast, {
36+
canvasHighContrastThemeLocal
37+
} from './themes/canvasHighContrast'
38+
import canvas, { canvasThemeLocal } from './themes/canvas'
3739

3840
import {
3941
primitives,
@@ -61,7 +63,9 @@ type ThemeSpecificStyle<ComponentTheme> = {
6163

6264
export {
6365
canvas,
66+
canvasThemeLocal,
6467
canvasHighContrast,
68+
canvasHighContrastThemeLocal,
6569
primitives,
6670
additionalPrimitives,
6771
dataVisualization

packages/ui-themes/src/themes/canvas/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,5 @@ const __theme: CanvasTheme = {
6969
const theme = ThemeRegistry.registerTheme(__theme)
7070

7171
export default theme
72+
// theme without the use() function and `variables` prop
73+
export { __theme as canvasThemeLocal }

packages/ui-themes/src/themes/canvasHighContrast/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ const __theme: CanvasHighContrastTheme = {
3939
...sharedThemeTokens,
4040
colors
4141
}
42-
const theme = ThemeRegistry.registerTheme(__theme)
4342

43+
const theme = ThemeRegistry.registerTheme(__theme)
4444
export default theme
45+
// theme without the use() function and `variables` prop
46+
export { __theme as canvasHighContrastThemeLocal }

packages/ui-utils/src/isBaseTheme.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ const baseThemeProps: BaseThemeVariableKeys = [
3939
'typography'
4040
]
4141

42+
/**
43+
* Checks if the given param is an object with all the keys needed for an
44+
* Instructure theme.
45+
* @param theme Anything. This function will throw an error if it's not a theme
46+
* object.
47+
*/
4248
const isBaseTheme = (theme: any): theme is BaseTheme => {
4349
if (Array.isArray(theme) || typeof theme === 'function') {
4450
throw new Error()

0 commit comments

Comments
 (0)