You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This article will tell you how to develop custom theme.
3
+
1. For CSS, Rspress provides [CSS variables](#css-variables) and [BEM classnames](#bem-classname) for customization.
4
4
5
-
## Extensions based on the default theme
5
+
2. 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.
6
6
7
-
In most cases, you don't want to develop a theme from scratch, but want to extend it based on the default theme. At this time, you can refer to the following methods for theme development.
7
+
On top of this, there are two modes:
8
+
-[**wrap**](#wrap): **Wrap** and enhance Rspress built-in components through props / slots.
9
+
-[**eject**](#eject): **Copy** source code locally through the [`rspress eject`](../../api/commands.mdx#rspress-eject) command and modify it directly to override.
8
10
9
-
:::tip
10
-
If you want to develop a custom theme from scratch, you can go to [Redevelop a custom theme](/ui/custom-theme#redevelop-a-custom-theme).
11
-
:::
11
+
The following will introduce them in order according to the degree of theme customization.
12
12
13
-
### 1. Basic structure
13
+
## CSS variables \{#css-variables}
14
+
15
+
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](../../ui/vars.mdx) page, and override them through:
16
+
17
+
import { Tabs, Tab } from'@theme';
18
+
19
+
<Tabs>
20
+
<Tablabel="theme/index.tsx">
21
+
22
+
```tsx
23
+
import'./index.css';
24
+
export*from'@rspress/core/theme-original';
25
+
```
26
+
27
+
</Tab>
28
+
<Tablabel="theme/index.css">
29
+
```css
30
+
/* Copy CSS variable code here for style overrides */
31
+
```
32
+
</Tab>
33
+
</Tabs>
34
+
35
+
## BEM classname \{#bem-classname}
36
+
37
+
Rspress built-in components all use BEM naming convention. You can override these classnames in the same way as [CSS Variables](#css-variables).
## `theme/index.tsx`: Override built-in components using ESM re-exports \{#reexport}
14
66
15
67
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.
16
68
17
69
```txt
18
-
└── theme
19
-
└── index.tsx
70
+
├── docs
71
+
├── theme
72
+
│ └── index.tsx
73
+
└── rspress.config.ts
20
74
```
21
75
22
-
You can write the `theme/index.tsx` file as follows:
76
+
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.
88
+
89
+
:::tip Note
90
+
91
+
Only use `@rspress/core/theme-original` when customizing themes
On the one hand, you need to export a theme configuration object through `export`, on the other hand, you need to export all named exported content through `export * from '@rspress/core/theme-original'` so as to ensure your theme works fine.
127
+
</Tab>
128
+
129
+
<Tablabel="i18n.json">
35
130
36
-
### 2. Use slot
131
+
```json
132
+
{
133
+
"some content": {
134
+
"zh": "一些内容",
135
+
"en": "some content"
136
+
}
137
+
}
138
+
```
37
139
38
-
It is worth noting that the `Layout` component has designed a series of props to support slot elements. You can use these props to extend the layout of the default theme. For example, change the above `Layout` component to the following form:
140
+
</Tab>
141
+
142
+
</Tabs>
143
+
144
+
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:
This is a custom paragraph added after every H1 heading.
208
+
</OriginalP>
209
+
</>
210
+
);
211
+
},
212
+
}}
90
213
/>
91
214
);
92
215
@@ -95,54 +218,33 @@ export { Layout };
95
218
export*from'@rspress/core/theme-original';
96
219
```
97
220
98
-
### 3. Custom home page and 404 page
221
+
</details>
99
222
100
-
In addition to the slot method, if you want to extend the default theme components, you can also customize the Home page components and 404 page components,
101
-
as well as other Rspress [built-in components](https://github.com/web-infra-dev/rspress/tree/main/packages/theme-default/src/components)
223
+
## Eject: Modify source code directly \{#eject}
102
224
103
-
```tsx title="theme/index.tsx"
104
-
import {
105
-
SearchasBasicSearch,
106
-
LayoutasBasicLayout,
107
-
} from'@rspress/core/theme-original';
225
+
Eject means copying the source code of a single Rspress built-in component locally and then modifying it directly. The steps are as follows:
108
226
109
-
// Custom Home Page
110
-
const HomeLayout = () => <div>Home</div>;
111
-
// Custom 404 page
112
-
const NotFoundLayout = () => <div>404</div>;
113
-
// Use slot
114
-
const Layout = () => (
115
-
<BasicLayout
116
-
beforeNavTitle={<div>some content</div>}
117
-
HomeLayout={HomeLayout}
118
-
NotFoundLayout={NotFoundLayout}
119
-
/>
120
-
);
227
+
1. Execute CLI [`rspress eject [component]`](../../api/commands.mdx#rspress-eject), Rspress will eject the source code of the specified component to the local `theme/components` directory, without ejecting dependencies.
Of course, you may need to use page data during the development process, you can get it through [`Runtime API`](/ui/hooks/).
237
+
3. Modify `theme/components/DocFooter.tsx` as needed to meet your requirements.
134
238
135
-
### 4. Custom icon
239
+
Rspress components are split with fine granularity, you can see which components are suitable for eject in [Built-in Components](/ui/components/).
136
240
137
-
If you want to modify the icons used in the default theme component individually, just put the icons with the same name in the theme/assets directory.
241
+
:::warning Do you really need eject?
138
242
139
-
```txt
140
-
└── theme
141
-
└── assets
142
-
└── logo.svg
143
-
```
243
+
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.
144
244
145
-
You can view all the icons used in the default theme [here](https://github.com/web-infra-dev/rspress/tree/main/packages/theme-default/src/assets).
245
+
Please check if wrap can meet your needs first. Only consider eject when wrap cannot meet your needs.
0 commit comments