Skip to content

Commit a98af82

Browse files
committed
docs(many): fix default theme variables sections on component doc pages
1 parent a04f8e7 commit a98af82

27 files changed

Lines changed: 78 additions & 27 deletions

File tree

packages/__docs__/buildScripts/build-docs.mts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,16 +421,34 @@ function parseThemes() {
421421
resource: { ...canvasHighContrast, resolvedComponents: resolveComponents(legacyCanvasHighContrast) }
422422
}
423423
parsed['canvas'] = {
424-
resource: { ...legacyCanvas, resolvedColors: resolveNewThemeColors(legacyCanvas), description: canvas.description }
424+
resource: {
425+
...legacyCanvas,
426+
resolvedColors: resolveNewThemeColors(legacyCanvas),
427+
resolvedComponents: resolveComponents(legacyCanvas),
428+
description: canvas.description
429+
}
425430
}
426431
parsed['canvas-high-contrast'] = {
427-
resource: { ...legacyCanvasHighContrast, resolvedColors: resolveNewThemeColors(legacyCanvasHighContrast), description: canvasHighContrast.description }
432+
resource: {
433+
...legacyCanvasHighContrast,
434+
resolvedColors: resolveNewThemeColors(legacyCanvasHighContrast),
435+
resolvedComponents: resolveComponents(legacyCanvasHighContrast),
436+
description: canvasHighContrast.description
437+
}
428438
}
429439
parsed[light.key] = {
430-
resource: { ...light, resolvedColors: resolveNewThemeColors(light.newTheme as typeof legacyCanvas) }
440+
resource: {
441+
...light,
442+
resolvedColors: resolveNewThemeColors(light.newTheme as typeof legacyCanvas),
443+
resolvedComponents: resolveComponents(light.newTheme as typeof legacyCanvas)
444+
}
431445
}
432446
parsed[dark.key] = {
433-
resource: { ...dark, resolvedColors: resolveNewThemeColors(dark.newTheme as typeof legacyCanvas) }
447+
resource: {
448+
...dark,
449+
resolvedColors: resolveNewThemeColors(dark.newTheme as typeof legacyCanvas),
450+
resolvedComponents: resolveComponents(dark.newTheme as typeof legacyCanvas)
451+
}
434452
}
435453
const canvasSemantics = legacyCanvas.semantics(legacyCanvas.primitives)
436454
parsed['shared-tokens'] = { resource: legacyCanvas.sharedTokens(canvasSemantics) }

packages/__docs__/src/Document/index.tsx

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { View } from '@instructure/ui-view'
2929
import { Tabs } from '@instructure/ui-tabs'
3030
import type { TabsProps } from '@instructure/ui-tabs'
3131
import type { NewBaseTheme } from '@instructure/ui-themes'
32+
import type { ComponentTheme as ComponentThemeData } from '@instructure/shared-types'
3233
import { SourceCodeEditor } from '@instructure/ui-source-code-editor'
3334
import { withStyleForDocs as withStyleNew } from '../withStyleForDocs'
3435

@@ -78,47 +79,48 @@ class Document extends Component<DocumentProps, DocumentState> {
7879

7980
fetchGenerateComponentTheme = async () => {
8081
const { doc, themeVariables } = this.props
81-
let generateTheme
82-
// Doc IDs use dot notation (e.g. "Menu.Item") but theme component keys
83-
// use PascalCase without dots (e.g. "MenuItem").
84-
// New-theme entries are in themeVariables.newTheme.components.
8582
const selectedId = this.state.selectedDetailsTabId
8683
type ComponentKey = keyof NewBaseTheme['components']
84+
// When a child tab is selected, work from the child's doc/component;
85+
// otherwise from the main doc's.
8786
const childDoc =
8887
selectedId !== doc.id
8988
? doc?.children?.find((value) => value.id === selectedId)
9089
: null
91-
// in case of some components, we need to display the theme variables of other components based on themeId (like displaying the theme variables of Options in Drillsdown.Group)
90+
const currentComponentInstance =
91+
selectedId === doc.id
92+
? doc?.componentInstance
93+
: childDoc?.componentInstance
94+
// Resolve the theme registry key. Default: doc id with dots stripped
95+
// (e.g. "Menu.Item" → "MenuItem"). Two ways to override the default:
96+
// - `themeId:` in YAML frontmatter (read from childDoc.themeId) — used
97+
// when a doc page wants to show another component's tokens
98+
// (e.g. Drilldown.Group → 'Options').
99+
// - `static themeId` on the component class — used when a component
100+
// borrows another's tokens via `useTokensFrom` at runtime
101+
// (e.g. Button → 'BaseButton', ColorMixer.Slider → 'Slider').
92102
const themeKey: ComponentKey = (childDoc?.themeId ||
103+
currentComponentInstance?.themeId ||
93104
selectedId?.replace(/\./g, '')) as ComponentKey
94105
const isLegacyTheme = this.context?.componentVersion == 'v11_6'
95-
// new theme
106+
// New theme: tokens are pre-resolved into plain objects at build time
107+
// (the build emits JSON, so generator functions can't be carried through).
96108
if (!isLegacyTheme) {
97-
// resolvedComponents contains pre-computed plain objects (built at build time)
98109
const resolvedComponents = (themeVariables as Record<string, unknown>)
99110
?.resolvedComponents as Record<string, unknown> | undefined
100111
const newThemeEntry = resolvedComponents?.[themeKey as string]
101-
const componentInstance =
102-
selectedId === doc.id
103-
? doc?.componentInstance
104-
: childDoc?.componentInstance
105112
if (
106113
newThemeEntry &&
107-
typeof componentInstance?.generateComponentTheme !== 'function'
114+
typeof currentComponentInstance?.generateComponentTheme !== 'function'
108115
) {
109-
// new theme - use pre-computed theme object directly
110116
this.setState({
111117
componentTheme: newThemeEntry as DocumentState['componentTheme']
112118
})
113119
return
114120
}
115121
}
116-
// old theme - use generateComponentTheme function
117-
if (selectedId === doc.id) {
118-
generateTheme = doc?.componentInstance?.generateComponentTheme
119-
} else {
120-
generateTheme = childDoc?.componentInstance?.generateComponentTheme
121-
}
122+
// Legacy theme: call the component's generateComponentTheme directly.
123+
const generateTheme = currentComponentInstance?.generateComponentTheme
122124
if (typeof generateTheme === 'function' && themeVariables) {
123125
this.setState({ componentTheme: generateTheme(themeVariables) })
124126
} else {
@@ -179,9 +181,7 @@ class Document extends Component<DocumentProps, DocumentState> {
179181
</code>
180182
</View>
181183
) : null}
182-
<ComponentTheme
183-
componentTheme={componentTheme as any /* TODO-theme-types check */}
184-
/>
184+
<ComponentTheme componentTheme={componentTheme as ComponentThemeData} />
185185

186186
<View margin="x-large 0 0" display="block">
187187
<Heading

packages/__docs__/src/Document/props.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,16 @@ type DocumentProps = DocumentOwnProps & WithStyleProps<null, DocumentStyle>
4646

4747
type DocumentStyle = ComponentStyle<'githubCornerOctoArm' | 'githubCorner'>
4848

49+
type ResolvedNewComponentTheme = ReturnType<
50+
NewBaseTheme['components'][keyof NewBaseTheme['components']]
51+
>
52+
4953
type DocumentState = {
5054
selectedDetailsTabId: string | undefined
5155
pageRef: HTMLDivElement | null
5256
componentTheme:
5357
| ThemeVariables[keyof ThemeVariables]
54-
| NewBaseTheme['components'][keyof NewBaseTheme['components']]
58+
| ResolvedNewComponentTheme
5559
| undefined
5660
}
5761

packages/ui-buttons/src/Button/v2/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ category: components
4141
@withStyleNew(null, 'BaseButton')
4242
class Button extends Component<ButtonProps> {
4343
static readonly componentId = 'Button'
44+
// Button v2 uses BaseButton's tokens; tell Document where to look for theme variables
45+
static readonly themeId = 'BaseButton'
4446

4547
static allowedProps = allowedProps
4648
static defaultProps = {

packages/ui-buttons/src/CloseButton/v2/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ category: components
4444
@withStyleNew(generateStyle, 'BaseButton')
4545
class CloseButton extends Component<CloseButtonProps> {
4646
static readonly componentId = 'CloseButton'
47+
// Uses BaseButton's tokens; tell Document where to look for theme variables
48+
static readonly themeId = 'BaseButton'
4749

4850
static allowedProps = allowedProps
4951
static defaultProps = {

packages/ui-buttons/src/CondensedButton/v2/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ category: components
4141
@withStyleNew(null, 'BaseButton')
4242
class CondensedButton extends Component<CondensedButtonProps> {
4343
static readonly componentId = 'CondensedButton'
44+
// Uses BaseButton's tokens; tell Document where to look for theme variables
45+
static readonly themeId = 'BaseButton'
4446

4547
static allowedProps = allowedProps
4648
static defaultProps = {

packages/ui-buttons/src/IconButton/v2/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ category: components
4444
@withStyleNew(null, 'BaseButton')
4545
class IconButton extends Component<IconButtonProps> {
4646
static readonly componentId = 'IconButton'
47+
// Uses BaseButton's tokens; tell Document where to look for theme variables
48+
static readonly themeId = 'BaseButton'
4749

4850
static allowedProps = allowedProps
4951
static defaultProps = {

packages/ui-checkbox/src/Checkbox/v2/CheckboxFacade/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ parent: Checkbox
4545
@withStyleNew(generateStyle, 'Checkbox')
4646
class CheckboxFacade extends Component<CheckboxFacadeProps> {
4747
static readonly componentId = 'CheckboxFacade'
48+
static readonly themeId = 'Checkbox'
4849

4950
static allowedProps = allowedProps
5051
static defaultProps = {

packages/ui-checkbox/src/Checkbox/v2/ToggleFacade/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ parent: Checkbox
4545
@withStyleNew(generateStyle, 'Toggle')
4646
class ToggleFacade extends Component<ToggleFacadeProps> {
4747
static readonly componentId = 'ToggleFacade'
48+
static readonly themeId = 'Toggle'
4849

4950
static allowedProps = allowedProps
5051
static defaultProps = {

packages/ui-color-picker/src/ColorMixer/v2/ColorPalette/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ private: true
5050
class ColorPalette extends Component<ColorPaletteProps, ColorPaletteState> {
5151
static allowedProps = allowedProps
5252
static readonly componentId = 'ColorMixer.Palette'
53+
static readonly themeId = 'Palette'
5354

5455
constructor(props: ColorPaletteProps) {
5556
super(props)

0 commit comments

Comments
 (0)