Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions docs/contributing/freezing-themes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
---
title: Freezing themes
category: Contributing
order: 9
---

# Freezing themes

## Why freeze?

Component themes in the new theming system are generated from `@instructure/instructure-design-tokens`. That package has its own semantic version, so a major bump can rename, reshape, or drop tokens a component relies on.

If a component's theme variables change in a way that breaks the component, we freeze the component's current theme structure. The component keeps resolving its tokens from that frozen snapshot, so it renders the same after the breaking token change. This is the theming counterpart of the [Multi-Version System](#multi-version-system): the old component version stays intact, and the frozen theme is what keeps it intact.

## How to freeze

When design tokens release a new major version, snapshot the previous one. For a 4.0.0 release, the previous major is 3, so add:

```sh
---
type: code
---
packages/ui-themes/src/themes/frozenThemes/designTokensV3/
├── index.ts
├── primitives.ts
├── semantics.ts
├── sharedTokens.ts
└── components/
├── index.ts
└── <component>/
├── type.ts
├── canvas.ts
├── canvasHighContrast.ts
├── dark.ts
└── light.ts
```

The folder is a partial theme. Include only what broke:

- the themes of the components that broke in 4.0.0, one folder per component with a file per theme key (`canvas`, `canvas-high-contrast`, `dark`, `light`)
- primitives, semantics, and shared tokens, but only if those broke too

Then export the snapshot and its component types from `frozenThemes/index.ts` and from the `ui-themes` entry point, and register the component value types in the `FrozenComponentValues` union in `packages/ui-themes/src/index.ts`. That union is what makes `themeOverride.components` accept the frozen token shape.

See `packages/ui-themes/src/themes/frozenThemes/designTokensV1` for a worked example.

## What's next?

A frozen component opts in by passing the snapshot to `withStyleNew` or `useStyleNew` and typing its theme against the snapshot's component types.

```js
---
type: code
---
// class component
import { frozenThemesDesignTokensV1 } from '@instructure/ui-themes'

@withStyleNew(generateStyle, null, frozenThemesDesignTokensV1)
class Alert extends Component<AlertProps, AlertState> { /* ... */ }

// function component
const styles = useStyleNew({
generateStyle,
componentId: 'Pill',
themeOverride: props.themeOverride,
frozenTheme: frozenThemesDesignTokensV1
})
```

`props.ts` and `styles.ts` switch from `NewComponentTypes` to the frozen types:

```js
---
type: code
---
import type { DesignTokensV1ComponentTypes } from '@instructure/ui-themes'

const generateStyle = (
componentTheme: ReturnType<DesignTokensV1ComponentTypes['Alert']>,
props: AlertProps,
sharedTokens: SharedTokens
): AlertStyle => { /* ... */ }
```
2 changes: 1 addition & 1 deletion packages/emotion/src/useStyleNew.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const useStyleNew = <
// if a new theme has been added to the lib since this component version has been frozen, it can't be used with this
// theme, so we throw an error. Solution: upgrade to the latest version, it will support it
if (frozenTheme && !frozenTheme[themeKey]) {
console.error(
throw new Error(
`The version of ${componentId} you are using does not support the currently applied "${themeKey}" theme. ` +
`Please upgrade to the latest version of ${componentId}.`
)
Expand Down
2 changes: 1 addition & 1 deletion packages/emotion/src/withStyleNew.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ const withStyleNew = decorator(
// if a new theme has been added to the lib since this component version has been frozen, it can't be used with this
// theme, so we throw an error. Solution: upgrade to the latest version, it will support it
if (frozenTheme && !frozenTheme[themeKey]) {
console.error(
throw new Error(
`The version of ${displayName} you are using does not support the currently applied "${themeKey}" theme. ` +
`Please upgrade to the latest version of ${displayName}.`
)
Expand Down
3 changes: 2 additions & 1 deletion packages/ui-alerts/src/Alert/v2/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,15 @@ import generateStyle from './styles.js'

import { allowedProps } from './props.js'
import type { AlertProps, AlertState } from './props'
import { frozenThemesDesignTokensV1 } from '@instructure/ui-themes'

/**
---
category: components
---
**/
@withDeterministicId()
@withStyleNew(generateStyle)
@withStyleNew(generateStyle, null, frozenThemesDesignTokensV1)
class Alert extends Component<AlertProps, AlertState> {
static displayName = 'Alert'
static readonly componentId = 'Alert'
Expand Down
7 changes: 5 additions & 2 deletions packages/ui-alerts/src/Alert/v2/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ import type {
WithStyleProps,
ComponentStyle
} from '@instructure/emotion'
import type { NewComponentTypes } from '@instructure/ui-themes'
import type { Renderable } from '@instructure/shared-types'
import type { WithDeterministicIdProps } from '@instructure/ui-react-utils'
import type { DesignTokensV1ComponentTypes } from '@instructure/ui-themes'

type AlertOwnProps = {
/**
Expand Down Expand Up @@ -122,7 +122,10 @@ type PropKeys = keyof AlertOwnProps
type AllowedPropKeys = Readonly<Array<PropKeys>>

type AlertProps = AlertOwnProps &
WithStyleProps<ReturnType<NewComponentTypes['Alert']>, AlertStyle> &
WithStyleProps<
ReturnType<DesignTokensV1ComponentTypes['Alert']>,
AlertStyle
> &
WithDeterministicIdProps

type AlertStyle = ComponentStyle<
Expand Down
5 changes: 3 additions & 2 deletions packages/ui-alerts/src/Alert/v2/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
* SOFTWARE.
*/
import { boxShadowObjectsToCSSString } from '@instructure/ui-themes'
import type { NewComponentTypes, SharedTokens } from '@instructure/ui-themes'
import type { SharedTokens } from '@instructure/ui-themes'
import type { AlertProps, AlertStyle } from './props'
import type { DesignTokensV1ComponentTypes } from '@instructure/ui-themes'

/**
* ---
Expand All @@ -36,7 +37,7 @@ import type { AlertProps, AlertStyle } from './props'
* @return {Object} The final style object, which will be used in the component
*/
const generateStyle = (
componentTheme: ReturnType<NewComponentTypes['Alert']>,
componentTheme: ReturnType<DesignTokensV1ComponentTypes['Alert']>,
props: AlertProps,
sharedTokens: SharedTokens
): AlertStyle => {
Expand Down
3 changes: 2 additions & 1 deletion packages/ui-pill/src/Pill/v2/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ import generateStyle from './styles.js'

import type { PillProps, PillState } from './props'
import { allowedProps } from './props.js'
import { frozenThemesDesignTokensV1 } from '@instructure/ui-themes'

/**
---
category: components
---
**/

@withStyleNew(generateStyle)
@withStyleNew(generateStyle, null, frozenThemesDesignTokensV1)
class Pill extends Component<PillProps, PillState> {
static displayName = 'Pill'
static readonly componentId = 'Pill'
Expand Down
4 changes: 2 additions & 2 deletions packages/ui-pill/src/Pill/v2/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ import type {
WithStyleProps,
ComponentStyle
} from '@instructure/emotion'
import type { NewComponentTypes } from '@instructure/ui-themes'
import type {
AsElementType,
OtherHTMLAttributes
} from '@instructure/shared-types'
import type { DesignTokensV1ComponentTypes } from '@instructure/ui-themes'

type PillOwnProps = {
as?: AsElementType
Expand Down Expand Up @@ -63,7 +63,7 @@ type PropKeys = keyof PillOwnProps
type AllowedPropKeys = Readonly<Array<PropKeys>>

type PillProps = PillOwnProps &
WithStyleProps<ReturnType<NewComponentTypes['Pill']>, PillStyle> &
WithStyleProps<ReturnType<DesignTokensV1ComponentTypes['Pill']>, PillStyle> &
OtherHTMLAttributes<PillOwnProps>

type PillStyle = ComponentStyle<
Expand Down
4 changes: 2 additions & 2 deletions packages/ui-pill/src/Pill/v2/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
* SOFTWARE.
*/

import type { NewComponentTypes } from '@instructure/ui-themes'
import type { PillProps, PillStyle } from './props'
import type { DesignTokensV1ComponentTypes } from '@instructure/ui-themes'

/**
* ---
Expand All @@ -35,7 +35,7 @@ import type { PillProps, PillStyle } from './props'
* @return {Object} The final style object, which will be used in the component
*/
const generateStyle = (
componentTheme: ReturnType<NewComponentTypes['Pill']>,
componentTheme: ReturnType<DesignTokensV1ComponentTypes['Pill']>,
props: PillProps
): PillStyle => {
const { color } = props
Expand Down
2 changes: 1 addition & 1 deletion packages/ui-scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"dependencies": {
"@babel/cli": "^7.27.2",
"@instructure/command-utils": "workspace:*",
"@instructure/instructure-design-tokens": "github:instructure/instructure-design-tokens#v1.5.0",
"@instructure/instructure-design-tokens": "github:instructure/instructure-design-tokens#v1.9.0",
"dprint": "^0.57.1",
"http-server": "^14.1.1",
"inquirer": "^14.2.1",
Expand Down
15 changes: 11 additions & 4 deletions packages/ui-select/src/Select/__tests__/Select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ import { render } from 'vitest-browser-react'
import { page, userEvent } from 'vitest/browser'
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
import type { MockInstance } from 'vitest'

import { color2hex } from '@instructure/ui-color-utils'
import canvas from '@instructure/ui-themes'

import { Select } from '@instructure/ui-select/latest'
import {
CheckInstUIIcon,
Expand Down Expand Up @@ -1327,6 +1331,9 @@ describe('<Select />', () => {
})

it('should render dynamically colored icons before option', async () => {
const iconTheme = canvas.newTheme.components.Icon(
canvas.newTheme.semantics(canvas.newTheme.primitives)
)
const renderBeforeLabel = (props: any) => {
return (
<CheckInstUIIcon
Expand Down Expand Up @@ -1356,12 +1363,12 @@ describe('<Select />', () => {
'ul[role="listbox"] li [role="presentation"] svg'
)
// the icons colour themselves via `stroke`, not `fill`
const iconStyles = Array.from(icons).map(
(icon) => window.getComputedStyle(icon).stroke
const iconStyles = Array.from(icons).map((icon) =>
color2hex(window.getComputedStyle(icon).stroke)
)

expect(iconStyles[0]).toBe('rgb(207, 74, 0)')
expect(iconStyles[1]).toBe('rgb(43, 122, 188)')
expect(iconStyles[0]).toBe(iconTheme.warningColor)
expect(iconStyles[1]).toBe(iconTheme.infoColor)
})

it('should set maxHeight according to the visibleOptionsCount prop', async () => {
Expand Down
34 changes: 31 additions & 3 deletions packages/ui-themes/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ import type {
Light
} from './themes/newThemeTokens'

import { frozenThemesDesignTokensV1 } from './themes/frozenThemes'
import type { DesignTokensV1ComponentTypes } from './themes/frozenThemes'

type ThemeMap = {
canvas: CanvasTheme
'canvas-high-contrast': CanvasHighContrastTheme
Expand All @@ -72,9 +75,32 @@ type ThemeMap = {

type ThemeKeys = keyof ThemeMap

// converts `AA: (semantics: any) => AA` to `AA: AA`
type KeysOfUnion<T> = T extends unknown ? keyof T : never

type ExclusiveUnion<T, All = T> = T extends unknown
? T & { [K in Exclude<KeysOfUnion<All>, keyof T>]?: never }
: never

type FrozenComponentValuesV1 = {
Alert: ReturnType<DesignTokensV1ComponentTypes['Alert']>
Pill: ReturnType<DesignTokensV1ComponentTypes['Pill']>
}

/**
* Every frozen token-shape map. Register a new frozen version by adding it to
* this union — nothing below needs to change.
*/
type FrozenComponentValues = FrozenComponentValuesV1

type FrozenValueFor<
K extends PropertyKey,
Maps = FrozenComponentValues
> = Maps extends Record<K, unknown> ? Maps[K] : never

type NewComponentsAsValue = {
[K in keyof NewComponentTypes]: ReturnType<NewComponentTypes[K]>
[K in keyof NewComponentTypes]: K extends KeysOfUnion<FrozenComponentValues>
? ExclusiveUnion<ReturnType<NewComponentTypes[K]> | FrozenValueFor<K>>
: ReturnType<NewComponentTypes[K]>
}

type NewThemeOverrideObject = {
Expand Down Expand Up @@ -105,7 +131,8 @@ export {
primitives,
additionalPrimitives,
dataVisualization,
boxShadowObjectsToCSSString
boxShadowObjectsToCSSString,
frozenThemesDesignTokensV1
}
export default canvas
export type {
Expand All @@ -128,6 +155,7 @@ export type {
Dark,
Light,
NewComponentTypes,
DesignTokensV1ComponentTypes,
NewBaseTheme,
NewThemeOverrideObject,
TokenBoxshadowValueInst,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 - present Instructure, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

import type { SemanticsCanvas as Semantics } from '../../semantics'

const alert = (semantic: Semantics) => ({
background: semantic.color.background.elevatedSurface.base,
borderRadius: semantic.borderRadius.lg,
borderStyle: 'solid',
borderWidth: semantic.borderWidth.md,
closeButtonMarginRight: '0.5rem',
closeButtonMarginTop: '0.5rem',
color: semantic.color.institutional.brandFontColorDark,
contentFontFamily: semantic.fontFamily.base,
contentFontSize: semantic.fontSize.textBase,
contentFontWeight: semantic.fontWeight.body.base,
contentLineHeight: semantic.lineHeight.standalone.base,
dangerBorderColor: semantic.color.stroke.error,
dangerIconBackground: semantic.color.background.error,
iconColor: semantic.color.icon.onColor,
infoBorderColor: semantic.color.stroke.info,
infoIconBackground: semantic.color.background.info,
successBorderColor: semantic.color.stroke.success,
successIconBackground: semantic.color.background.success,
warningBorderColor: semantic.color.stroke.warning,
warningIconBackground: semantic.color.background.warning,
contentPaddingVertical: semantic.spacing.spaceMd,
contentPaddingHorizontal: semantic.spacing.spaceXl
})
export default alert
Loading
Loading