Dahlia
输入关键词搜索文档。

自定义

Overriding Components

Replace Dahlia shell components when configuration is not enough.

Use component overrides when a project needs to replace part of the Dahlia shell: markup, placement, or behavior. Prefer normal theme configuration and CSS tokens first. For example, use pageActions, footer.sections, accent colors, gray scale, radius, and CSS variables before replacing a component.

Overrides are configured in astro.config.ts through the components object passed to dahlia({...}). Paths are resolved from the project root.

astro.config.ts
import { defineConfig } from 'astro/config';
import dahlia from '@prosefly/astro-theme-dahlia';
export default defineConfig({
integrations: [
dahlia({
components: {
PageAside: './src/components/dahlia/PageAside.astro',
},
}),
],
});

How It Works

The shell renders named override slots. Each slot has a default implementation, and many defaults delegate to public theme components exported from @prosefly/astro-theme-dahlia/components.

txt
layout -> override slot -> default implementation -> public theme component

Use dahlia({ components }) with override slot names such as PageAside or PageActions. Import public theme components only when your local override wants to reuse package behavior. See Theme Components for the public export list.

Override Points

Only these names are valid keys in the components object passed to dahlia({...}).

NameWhere It RendersProps
AssistantGlobal assistant widget area near the end of <body>.none
SiteBrandMain header brand link.none
HeaderNavbarDesktop header navbar and mobile menu navbar.currentPath?: string, mobile?: boolean
HeaderSocialIconsDesktop header social area and mobile menu social area.mobile?: boolean
FooterLinksFooter link grid.none
SearchDialogMain header search trigger and dialog.none
PageHeaderDocs article title area.title, description?, sectionTitle?, pageActions, pageUrl, markdownUrl, currentLocale
PageActionsAction control inside PageHeader.actions, title, pageUrl, markdownUrl, currentLocale?
PageAsideRight sidebar content and mobile page tools.headings, editUrl, title, pageUrl, markdownUrl, currentSlug, currentLocale, tableOfContents?
PageMetaLast updated and contributors below docs content.contributors, lastUpdated, title, currentLocale
PageNavigationPrevious and next links below docs content.navigation, currentLocale?

Importing a public component does not make it an override point. The configured key must be one of the slot names above.

Minimal Override

Create a local Astro component for the slot. This example keeps the default right sidebar and adds an EthicalAds placement below it:

src/components/dahlia/PageAside.astro
---
import { PageAside } from '@prosefly/astro-theme-dahlia/components';
---
<PageAside {...Astro.props} />
<aside class="mt-6 border-t border-(--dahlia-border-subtle) pt-5">
<script
async
src="https://media.ethicalads.io/media/client/ethicalads.min.js"
></script>
<div
class="flat"
data-ea-publisher="your-publisher-id"
data-ea-type="text"
></div>
</aside>

Register it:

astro.config.ts
dahlia({
components: {
PageAside: './src/components/dahlia/PageAside.astro',
},
})

Dahlia now renders this file anywhere the shell asks for PageAside.

Reusing Existing Behavior

An override can still reuse Dahlia behavior. Import from @prosefly/astro-theme-dahlia/components when you want the package implementation, and forward Astro.props if the component receives props.

Use a virtual module when one override needs to call another configured override slot. This preserves the user’s replacement instead of forcing the package implementation.

src/components/dahlia/PageHeader.astro
---
import PageActions from 'virtual:prosefly/dahlia/components/PageActions';
---
<header>
<h1>{Astro.props.title}</h1>
<PageActions
actions={Astro.props.pageActions}
currentLocale={Astro.props.currentLocale}
markdownUrl={Astro.props.markdownUrl}
pageUrl={Astro.props.pageUrl}
title={Astro.props.title}
/>
</header>

Translations

Dahlia provides the resolved UI translator on Astro.locals.t. Use it when visible shell text should follow the current locale. See Internationalization for the translator API.

Debugging

If an override does not appear:

  • Confirm the key matches one of the supported override point names exactly.
  • Confirm the file path is relative to the project root.
  • Restart the dev server after changing astro.config.ts.
  • Check that the override file has a default Astro component export.
  • Forward Astro.props when reusing a public theme component.

最后更新于 2026年8月24日

贡献者