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: Copy source code locally through the
rspress ejectcommand and modify it directly to override.
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 override these classnames in the same way as CSS Variables.
For example:
theme/index.tsx: 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 content.
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.
Only use @rspress/core/theme-original when customizing themes
-
In the
docsdirectory, use@rspress/core/theme,@rspress/core/themepoints to yourtheme/index.tsx. -
In the
themedirectory, use@rspress/core/theme-original,@rspress/core/theme-originalalways points to Rspress built-in theme components.
Wrap: Add props/slots on re-exported components
Wrap means adding props to re-exported components. Here's an example of inserting some content before the nav bar title:
It's worth noting that the Layout component is designed with a series of props to support slot elements. You can use these props to extend the default theme layout:
Eject: Modify source code directly
Eject means copying the source code of a single Rspress built-in component locally and then modifying it directly. The steps are as follows:
-
Execute CLI
rspress eject [component], Rspress will eject the source code of the specified component to the localtheme/componentsdirectory, without ejecting dependencies. -
Update
theme/index.tsxre-export:
- Modify
theme/components/DocFooter.tsxas needed to meet your requirements.
Rspress components are split with fine granularity, you can see which components are suitable for eject in Built-in 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.
Redevelop a custom theme
If you're developing a custom theme from scratch, you need to understand the basic structure of the theme and the runtime API.
1. Basic structure
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 content.
In the theme/index.tsx file, you need to export a Layout component, which is the entry component of your theme:
2. Runtime API
usePageData
Get information about all data on the site, such as:
useLang
Get the current language information, such as:
Content
Get MDX component content, such as:
Route hook
react-router-dom is used inside Rspress to implement routing, so you can directly use the Hook of react-router-dom, for example:
3. Reusing search functionality
The default theme comes with built-in search functionality, which we can break down into two components:
- The search box, i.e., the entry point to invoke the search.
- The search panel that pops up after clicking on the search box.
Full reuse
If you want to fully reuse the search functionality, you can directly import the Search component, like so:
Reusing the search panel
If you only want to reuse the search panel and customize the search box part, then you need to import the SearchPanel component in your theme component, like so:
In this case, you need to maintain the focused state and setFocused method yourself, and pass them as props to the SearchPanel component for controlling the display and hiding of the search panel.
Reuse default full text search logic
If you want to reuse the default full text search logic, you can use the useFullTextSearch Hook, for example:
Here, initialized indicates whether the search is initialized, the search method is used to search for keywords, returning a Promise, the result of the Promise is the result of the default full text search.
It should be noted that the useFullTextSearch Hook will automatically load the search index during initialization, so you need to first determine the initialized status, ensure that initialization is complete, and then call the search method.
The type definition of the search method is as follows:
The limit represents the maximum number of search results, default is 7, which means by default it returns a maximum of seven article results.