|
| 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). |
0 commit comments