Skip to content

Commit 140e729

Browse files
delucissarah11918HiDeoo
authored
Support component customisation (#709)
Co-authored-by: Sarah Rainsberger <[email protected]> Co-authored-by: HiDeoo <[email protected]>
1 parent 903a579 commit 140e729

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1683
-527
lines changed

.changeset/plenty-donkeys-lay.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
'@astrojs/starlight': minor
3+
---
4+
5+
Add support for overriding Starlight’s built-in components
6+
7+
⚠️ **BREAKING CHANGE** — The page footer is now included on pages with `template: splash` in their frontmatter. Previously, this was not the case. If you are using `template: splash` and want to continue to hide footer elements, disable them in your frontmatter:
8+
9+
```md
10+
---
11+
title: Landing page
12+
template: splash
13+
# Disable unwanted footer elements as needed
14+
editUrl: false
15+
lastUpdated: false
16+
prev: false
17+
next: false
18+
---
19+
```
20+
21+
⚠️ **BREAKING CHANGE** — This change involved refactoring the structure of some of Starlight’s built-in components slightly. If you were previously overriding these using other techniques, you may need to adjust your code.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
---
2+
title: Overriding Components
3+
description: Learn how to override Starlight’s built-in components to add custom elements to your documentation site’s UI.
4+
sidebar:
5+
badge: New
6+
---
7+
8+
Starlight’s default UI and configuration options are designed to be flexible and work for a range of content. Much of Starlight's default appearance can be customized with [CSS](/guides/css-and-tailwind/) and [configuration options](/guides/customization/).
9+
10+
When you need more than what’s possible out of the box, Starlight supports building your own custom components to extend or override (completely replace) its default components.
11+
12+
## When to override
13+
14+
Overriding Starlight’s default components can be useful when:
15+
16+
- You want to change how a part of Starlight’s UI looks in a way not possible with [custom CSS](/css-and-tailwind/).
17+
- You want to change how a part of Starlight’s UI behaves.
18+
- You want to add some additional UI alongside Starlight’s existing UI.
19+
20+
## How to override
21+
22+
1. Choose the Starlight component you want to override.
23+
You can find a full list of components in the [Overrides Reference](/reference/overrides/).
24+
25+
This example will override Starlight’s [`SocialIcons`](/reference/overrides/#socialicons) component in the page nav bar.
26+
27+
2. Create an Astro component to replace the Starlight component with.
28+
This example renders a contact link.
29+
30+
```astro
31+
---
32+
// src/components/EmailLink.astro
33+
import type { Props } from '@astrojs/starlight/props';
34+
---
35+
36+
<a href="mailto:[email protected]">E-mail Me</a>
37+
```
38+
39+
3. Tell Starlight to use your custom component in the [`components`](/reference/configuration/#components) configuration option in `astro.config.mjs`:
40+
41+
```js {9-12}
42+
// astro.config.mjs
43+
import { defineConfig } from 'astro/config';
44+
import starlight from '@astrojs/starlight';
45+
46+
export default defineConfig({
47+
integrations: [
48+
starlight({
49+
title: 'My Docs with Overrides',
50+
components: {
51+
// Override the default `SocialLinks` component.
52+
SocialIcons: './src/components/EmailLink.astro',
53+
},
54+
}),
55+
],
56+
});
57+
```
58+
59+
## Reuse a built-in component
60+
61+
You can build with Starlight’s default UI components just as you would with your own: importing and rendering them in your own custom components. This allows you to keep all of Starlight's basic UI within your design, while adding extra UI alongside them.
62+
63+
The example below shows a custom component that renders an e-mail link along with the default `SocialLinks` component:
64+
65+
```astro {4,8}
66+
---
67+
// src/components/EmailLink.astro
68+
import type { Props } from '@astrojs/starlight/props';
69+
import Default from '@astrojs/starlight/SocialIcons.astro';
70+
---
71+
72+
<a href="mailto:[email protected]">E-mail Me</a>
73+
<Default {...Astro.props}><slot /></Default>
74+
```
75+
76+
When rendering a built-in component inside a custom component:
77+
78+
- Spread `Astro.props` into it. This makes sure that it receives all the data it needs to render.
79+
- Add a [`<slot />`](https://docs.astro.build/en/core-concepts/astro-components/#slots) inside the default component. This makes sure that if the component is passed any child elements, Astro knows where to render them.
80+
81+
## Use page data
82+
83+
When overriding a Starlight component, your custom implementation receives a standard `Astro.props` object containing all the data for the current page.
84+
This allows you to use these values to control how your component template renders.
85+
86+
For example, you can read the page’s frontmatter values as `Astro.props.entry.data`. In the following example, a replacement [`PageTitle`](/reference/overrides/#pagetitle) component uses this to display the current page’s title:
87+
88+
```astro {5} "{title}"
89+
---
90+
// src/components/Title.astro
91+
import type { Props } from '@astrojs/starlight/props';
92+
93+
const { title } = Astro.props.entry.data;
94+
---
95+
96+
<h1 id="_top">{title}</h1>
97+
98+
<style>
99+
h1 {
100+
font-family: 'Comic Sans';
101+
}
102+
</style>
103+
```
104+
105+
Learn more about all the available props in the [Overrides Reference](/reference/overrides/#prop-types).
106+
107+
### Only override on specific pages
108+
109+
Component overrides apply to all pages. However, you can conditionally render using values from `Astro.props` to determine when to show your custom UI, when to show Starlight’s default UI, or even when to show something entirely different.
110+
111+
In the following example, a component overriding Starlight's [`Footer`](/reference/overrides/#footer-1) displays "Built with Starlight 🌟" on the homepage only, and otherwise shows the default footer on all other pages:
112+
113+
```astro
114+
---
115+
// src/components/ConditionalFooter.astro
116+
import type { Props } from '@astrojs/starlight/props';
117+
import Default from '@astrojs/starlight/Footer.astro';
118+
119+
const isHomepage = Astro.props.slug === '';
120+
---
121+
122+
{
123+
isHomepage ? (
124+
<footer>Built with Starlight 🌟</footer>
125+
) : (
126+
<Default {...Astro.props}>
127+
<slot />
128+
</Default>
129+
)
130+
}
131+
```
132+
133+
Learn more about conditional rendering in [Astro’s Template Syntax guide](https://docs.astro.build/en/core-concepts/astro-syntax/#dynamic-html).

docs/src/content/docs/guides/sidebar.mdx

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
---
22
title: Sidebar Navigation
33
description: Learn how to set up and customize your Starlight site’s sidebar navigation links.
4-
sidebar:
5-
badge: New
64
---
75

86
import FileTree from '../../../components/file-tree.astro';

docs/src/content/docs/index.mdx

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ head:
55
content: Starlight 🌟 Build documentation sites with Astro
66
description: Starlight helps you build beautiful, high-performance documentation websites with Astro.
77
template: splash
8+
editUrl: false
9+
lastUpdated: false
810
banner:
911
content: |
1012
<span class="ph-banner">

docs/src/content/docs/reference/configuration.md

+16
Original file line numberDiff line numberDiff line change
@@ -436,3 +436,19 @@ Sets the delimiter between page title and site title in the page’s `<title>` t
436436

437437
By default, every page has a `<title>` of `Page Title | Site Title`.
438438
For example, this page is titled “Configuration Reference” and this site is titled “Starlight”, so the `<title>` for this page is “Configuration Reference | Starlight”.
439+
440+
### `components`
441+
442+
**type:** `Record<string, string>`
443+
444+
Provide the paths to components to override Starlight’s default implementations.
445+
446+
```js
447+
starlight({
448+
components: {
449+
SocialLinks: './src/components/MySocialLinks.astro',
450+
},
451+
});
452+
```
453+
454+
See the [Overrides Reference](/reference/overrides/) for details of all the components that you can override.

0 commit comments

Comments
 (0)