Custom theme
-
For CSS, Rspress provides CSS variables and BEM classnames for customization.
-
For JS / React, Rspress implements a runtime interface based on ESM re-exports, supporting modification or replacement of built-in components to implement your own home page, sidebar, search components, etc.
On top of this, there are two modes:
- wrap: Wrap and enhance Rspress built-in components through props / slots.
- eject: Directly override the entire component. You can use the
rspress ejectcommand to copy the source code locally and modify it directly.
The following will introduce them in order according to the degree of theme customization.
CSS variables
Rspress exposes some commonly used CSS variables. Compared to rewriting Rspress built-in React components, overriding CSS variables for customization is simpler and easier to maintain. You can view these CSS variables on the UI - CSS Variables page, and override them through:
BEM classname
Rspress built-in components all use BEM naming convention. You can use these classnames to override styles, in the same way as CSS Variables.
For example:
Override built-in components using ESM re-exports
By default, you need to create a theme directory under the project root directory, and then create an index.ts or index.tsx file under the theme directory, which is used to export the theme components.
You can write the theme/index.tsx file using built-in components from @rspress/core/theme-original:
By ESM re-export to override built-in components, all Rspress internal references to built-in components will preferentially use your re-exported version.
@rspress/core/theme-original is used to avoid circular references, only use it when customizing themes.
-
In the
docsdirectory, use@rspress/core/theme, which points to yourtheme/index.tsx. -
In the
themedirectory, use@rspress/core/theme-original, which always points to Rspress built-in theme components.
Wrap: Pass props/slots
Wrap means adding props to re-exported components. Here's an example of inserting some content before the navbar title:
The Layout component is designed with a series of slot props specifically for wrapping. You can use these props to extend the default theme layout:
Eject: Directly override the entire component
Eject means directly overriding a single Rspress built-in component, using your own version entirely. For this, Rspress provides the rspress eject [component] command to copy the source code of built-in components locally and modify them directly. The steps are as follows:
-
Execute CLI, Rspress will eject the source code of the specified component to the local
theme/componentsdirectory, without ejecting dependencies. -
Update
theme/index.tsxre-export:
- Modify
theme/components/DocFooter.tsxas needed to meet your requirements.
To make it easier for users to eject, Rspress components are split with fine granularity. You can see which components are suitable for eject in Layout Components.
Eject will increase maintenance costs, because when Rspress is updated in the future, these ejected components will not automatically receive updates, and you need to manually compare and merge changes.
Please check if wrap can meet your needs first. Only consider eject when wrap cannot meet your needs.