Customize Theme
Custom theme by yourself
You Should Know
This theme is designed to “NPM Package” for the following reasons:
- The code written in package may be frequently changed. If users change them at will, it will significantly increase the cost of subsequent updates and merging (various conflicts for code)
- Roll back and switch between different versions easily, to maintain user stability
- Reduce system coupling & increase reuse ability
- The NPM Package mode can be used as a “Astro plugin” to append additional operations that enhance user experience during the build phase
- Support some built-in commands like new, check, info, etc.
- Use as component library for other Astro projects (yes, this project can be broken down into the UI component library): Package details ↗.
Before you customize it, you should check site.config.ts to make sure there is no option that satisfy your need.
Swizzling
This theme chose an elegant method called ‘Swizzling’, which is inspired by the design from Docusaurus ↗.
- Use an IDE to quickly view the source code corresponding to a certain component (In VSCode, it is to click the component with Ctrl).
- Copy to
src/components/<your prefer directory>. - After modification, change the corresponding reference to your own file path.
Let’s make an example to customize the Footer component:
-
Search the
Footerin your project code:src/layout/BaseLayout.astro
astro--- import { Footer, Header, ThemeProvider } from 'astro-pure/components/basic' import type { SiteMeta } from 'astro-pure/types' // ... --- -
Find it in theme source code:
node_modules/astro-theme-pure/components/basic/index.ts
tsexport { default as Footer } from './Footer.astro' export { default as Header } from './Header.astro' export { default as ThemeProvider } from './ThemeProvider.astro'Then you get the
Footercomponent source code atnode_modules/astro-theme-pure/components/basic/Footer.astro. -
Copy the
Footer.astrofile to your project:
bashcp node_modules/astro-theme-pure/components/basic/Footer.astro src/components/custom/Footer.astro -
Solve the sub-dependencies problem:
src/components/custom/Footer.astro
astro--- import config from 'virtual:config' import { Icon } from '../user' import { Icon } from 'astro-pure/user' // ... --- -
Change the reference in
BaseLayout.astro:src/layout/BaseLayout.astro
astro--- import { Footer, Header, ThemeProvider } from 'astro-pure/components/basic' // [!code --] import { Header, ThemeProvider } from 'astro-pure/components/basic' // [!code ++] import { Footer } from '@/components/custom/Footer.astro' // [!code ++] // ... ---
Then you’ve done a “localization” of the component.
Package Mode
If you want to make some breaking changes or just make a test, this method may follow “what you see is what you get”.
- Making sure you have the original theme code (
./packages/pure). If yo’ve deleted it, download it from the Releases ↗. - Link it to your project using your package manager (Example: Bun ↗, NPM ↗, PNPM ↗).
- Then change the theme code as it is a part of your code!