Customizing base component styles
In the Experience Builder theme module, you can customize base component styles by creating and exporting style objects in path to theme/style.ts.
Base components refer to those that can be directly imported from jimu-ui. You can see all base components here.
Customizing styles for the Button component:
// style.ts
import type { ThemeComponentStyleOptions } from 'jimu-theme'
export const Button: ThemeComponentStyleOptions['Button'] = {
root: ({ theme }) => {
return {
// Your custom styles here
}
}
}Key points:
- The exported object name must match the component name, the object key is fixed to "root".
- Use
Themeto get the proper type definitions for the component style object.Component Style Options - The style value is a function that returns an Emotion-compatible style object:
theme: the runtime theme object, containing theme variables.
Dynamically changing Button styles based on variant and color:
// style.ts
import type { ThemeComponentStyleOptions } from 'jimu-theme'
export const Button: ThemeComponentStyleOptions['Button'] = {
root: ({ styleState, theme }) => {
const styles = (styleState.variant === 'contained' && styleState.color === 'primary') ? {
backgroundColor: theme.sys.color.secondary.main,
color: theme.sys.color.secondary.text,
'&:hover': {
backgroundColor: theme.sys.color.secondary.dark,
color: theme.sys.color.secondary.text
}
} : {}
return styleState.tag === 'a' ? { '&[role="button"]': styles } : styles
}
}Key points:
style: the component's runtime state, which equals the props of the component. You can refer to the props of each component underState Components/jimu-ui/index.
Global styles
To apply global styles across the entire app, use the special Css component:
// style.ts
import type { ThemeComponentStyleOptions } from 'jimu-theme'
export const CssBaseline: ThemeComponentStyleOptions['CssBaseline'] = {
root: () => ({
html: {
fontSize: '80%'
}
})
}Additionally, Css’s style includes a root property that represents the app’s root URL. This allows you to load fonts or assets using absolute paths:
// style.ts
export const CssBaseline = {
root: ({ styleState }) => {
const rootUrl = styleState.rootUrl || ''
return `
@font-face {
font-family: 'Family name';
src: url('${rootUrl}themes/<theme name>/assets/<font file>') format('truetype');
font-weight: 400;
font-style: normal;
}
`
}
}Limitations on further customization
Typically, a custom theme can still be edited through the Experience Builder Theme Settings panel.
However, once component styles are overridden via style.ts (or style.scss), the corresponding components will no longer respond to Theme Settings adjustments.
If you are certain that your custom styles do not conflict with Theme Settings, you can explicitly allow theme editing by setting theme to true in the manifest file:
"themeCustomizable": trueThis will enable Theme Settings modifications even for themes with advanced custom styles.