The Astro component patterns that make a theme easy to edit are the boring ones: typed Props with defaults, passing extra attributes through with ...rest, merging classes with class:list, using named slots instead of piles of props, and keeping copy, links and colors in config files rather than inside components. Each one means a change happens in one obvious place.
This guide shows each pattern with code you can drop into an Astro 7 project, and explains why it matters when someone else (or you, six months later) has to change the site.
Key Takeaways
- Declare
interface Propsin every component and give optional props sensible defaults.- Extend
HTMLAttributes<'tag'>and spread...restso callers can addid,aria-*ordata-*without editing the component.- Use
class:listto merge a caller’sclasswith the component’s own classes.- Prefer named slots over string props for anything that might contain markup.
- Keep site name, navigation and social links in one
src/config/file, and colors in CSS custom properties.
What makes an Astro theme easy to edit
When people customize a theme, they usually want to change one of four things: text, links, colors, or which sections appear on a page. A theme is easy to edit when each of those has exactly one home.
The test is simple. Ask “where do I change the phone number?” or “how do I remove the testimonials section?” If the answer is “one file, one line,” the component patterns are working. If the answer involves searching 40 files, they aren’t.
The patterns below are the ones that consistently pass that test. None of them require a framework or a library. They all use features built into .astro components, which the Astro components guide introduces from the start.
Pattern 1: typed props with defaults
Every component should declare what it accepts. TypeScript checks callers, and your editor autocompletes props:
---
// src/components/ui/Button.astro
interface Props {
href?: string;
variant?: 'primary' | 'secondary' | 'ghost';
size?: 'sm' | 'md' | 'lg';
}
const { href, variant = 'primary', size = 'md' } = Astro.props;
const Tag = href ? 'a' : 'button';
---
<Tag href={href} class={`btn btn--${variant} btn--${size}`} type={href ? undefined : 'button'}>
<slot />
</Tag>
Defaults in the destructuring line mean most usages are short (<Button href="/contact/">Talk to us</Button>), and the union types document every allowed value. Someone editing the theme can open the file and see the whole contract at the top.
Avoid boolean-flag explosions like isPrimary, isLarge, isOutline. A single variant union is easier to read and can’t end up in impossible combinations.
Pattern 2: pass attributes through with HTMLAttributes
A component that wraps an element should accept that element’s normal attributes. Astro ships the types for this in astro/types:
---
// src/components/ui/Card.astro
import type { HTMLAttributes } from 'astro/types';
interface Props extends HTMLAttributes<'article'> {
title: string;
}
const { title, class: className, ...rest } = Astro.props;
---
<article class:list={['card', className]} {...rest}>
<h3 class="card__title">{title}</h3>
<slot />
</article>
Now a caller can write <Card title="Pricing" id="pricing" data-track="card" aria-labelledby="x"> without the component knowing about any of those attributes. The ...rest spread forwards them.
Two details matter here. class is a reserved word in JavaScript, so destructure it as class: className. And spread rest after any attributes you want callers to be able to override, or before ones you want to lock.
Pattern 3: merge classes with class:list
class:list takes strings, arrays, objects and falsy values, and outputs a clean, de-duplicated class string:
---
// src/components/ui/Badge.astro
import type { HTMLAttributes } from 'astro/types';
interface Props extends HTMLAttributes<'span'> {
tone?: 'neutral' | 'success' | 'warning';
pill?: boolean;
}
const { tone = 'neutral', pill = false, class: className, ...rest } = Astro.props;
---
<span
class:list={[
'badge',
`badge--${tone}`,
{ 'badge--pill': pill },
className,
]}
{...rest}
>
<slot />
</span>
The caller’s class is appended last, so utility classes like mt-4 from Tailwind CSS land on the element without fighting the component. This is the single most useful habit for theme components, because nearly every customization request starts with “add a little space here.” The class:list tip covers the edge cases.
Pattern 4: named slots instead of string props
String props are fine for plain text. As soon as content might contain a link, an icon or emphasis, use a slot:
---
// src/components/sections/Hero.astro
interface Props {
align?: 'left' | 'center';
}
const { align = 'left' } = Astro.props;
const hasActions = Astro.slots.has('actions');
---
<section class:list={['hero', `hero--${align}`]}>
<p class="hero__eyebrow"><slot name="eyebrow" /></p>
<h1 class="hero__title"><slot name="title">Welcome</slot></h1>
<div class="hero__body"><slot /></div>
{hasActions && (
<div class="hero__actions">
<slot name="actions" />
</div>
)}
</section>
---
// src/pages/index.astro
import Hero from '../components/sections/Hero.astro';
import Button from '../components/ui/Button.astro';
---
<Hero align="center">
<span slot="eyebrow">New in October 2026</span>
<Fragment slot="title">Ship a <em>fast</em> site this week</Fragment>
<p>Everything renders to static HTML.</p>
<Fragment slot="actions">
<Button href="/contact/">Get in touch</Button>
<Button href="/work/" variant="ghost">See our work</Button>
</Fragment>
</Hero>
Three things are happening. The title slot has fallback content (“Welcome”) if nothing is passed. Astro.slots.has('actions') skips the wrapper div entirely when there are no actions, so you don’t get empty spacing. And <Fragment slot="..."> passes several elements into one slot without an extra wrapper. The named slots guide goes deeper.
Pattern 5: polymorphic components with an as prop
Headings are the classic case. A section title may need to be an h2 on one page and an h3 on another, but look the same:
---
// src/components/ui/Heading.astro
import type { Polymorphic } from 'astro/types';
type Props = Polymorphic<{ as: 'h1' | 'h2' | 'h3' | 'h4'; size?: 'lg' | 'md' }>;
const { as: Tag = 'h2', size = 'md', class: className, ...rest } = Astro.props;
---
<Tag class:list={['heading', `heading--${size}`, className]} {...rest}>
<slot />
</Tag>
Polymorphic from astro/types types the attributes for whichever tag as resolves to. Visual style and document outline stay independent, which keeps both design and accessibility easy to fix. Any variable that starts with a capital letter can be used as a dynamic tag in Astro templates.
Pattern 6: one config file for site-wide content
Business details shouldn’t live inside a footer component. Put them in a typed module:
// src/config/site.ts
export const site = {
name: 'Northwind Studio',
description: 'Brand and web design for independent businesses.',
email: '[email protected]',
phone: '+1 555 0100',
nav: [
{ label: 'Work', href: '/work/' },
{ label: 'Services', href: '/services/' },
{ label: 'About', href: '/about/' },
{ label: 'Contact', href: '/contact/' },
],
social: [
{ label: 'GitHub', href: 'https://github.com/northwind' },
{ label: 'LinkedIn', href: 'https://www.linkedin.com/company/northwind' },
],
} as const;
---
// src/components/layout/Footer.astro
import { site } from '../../config/site';
---
<footer>
<p>{site.name}</p>
<a href={`mailto:${site.email}`}>{site.email}</a>
<ul>
{site.social.map((item) => <li><a href={item.href}>{item.label}</a></li>)}
</ul>
</footer>
The header, footer, contact page and SEO component all import the same object. Changing the email address is a one-line edit. The site config tip shows a fuller version with SEO defaults.
Pattern 7: design tokens as CSS custom properties
Colors, fonts and spacing belong in one stylesheet, referenced everywhere else by name:
/* src/styles/tokens.css */
:root {
--color-bg: #ffffff;
--color-text: #111827;
--color-accent: #2563eb;
--font-sans: 'Inter', system-ui, sans-serif;
--radius: 0.5rem;
--space-section: clamp(3rem, 8vw, 6rem);
}
[data-theme='dark'] {
--color-bg: #0b1020;
--color-text: #e5e7eb;
}
---
// src/components/ui/Button.astro (style block)
---
<style>
.btn--primary {
background: var(--color-accent);
color: var(--color-bg);
border-radius: var(--radius);
}
</style>
Rebranding becomes a matter of changing a handful of values. If the theme uses Tailwind CSS, map these variables into the Tailwind config so utilities and component styles share one source. The styling docs cover where tokens and global styles live in an Astro project.
Organizing components so people can find them
A flat src/components/ folder with 80 files is hard to navigate. A shallow split by role helps:
src/components/
ui/ Button, Badge, Card, Heading, Icon
sections/ Hero, Features, Pricing, Testimonials, CTA
layout/ Header, Footer, Nav, SkipLink
src/config/ site.ts
src/styles/ tokens.css, global.css
src/layouts/ BaseLayout.astro, PostLayout.astro
Pages then read like an outline: a list of sections, each with its content passed in. Removing the testimonials from the home page means deleting one line. Adding a section means importing one component.
A few more habits round this out:
- Keep each section component self-contained, with its own
<style>block. - Avoid prop drilling more than two levels. If data is site-wide, import it from config.
- Put sample data that editors replace (team members, pricing tiers) in
src/data/or a content collection, not hard-coded in markup. - Leave a short comment at the top of complex components saying what they render.
Look for these patterns when you evaluate Astro themes, because they are what make it realistic to customize an Astro theme without reading every file first. The official Astro components docs cover the underlying syntax.
FAQ
How do I pass a class to an Astro component?
Destructure it as const { class: className } = Astro.props, because class is a reserved word, then merge it with class:list={['my-component', className]}. Without this, a class passed by the caller is silently ignored.
How do I type props in an Astro component?
Declare an interface Props (or type Props) in the component frontmatter. Astro picks it up automatically for Astro.props, and your editor will flag missing or wrong props where the component is used. Extend HTMLAttributes<'tag'> from astro/types to accept the native attributes of the wrapped element.
What is the difference between props and slots in Astro?
Props pass values such as strings, numbers and objects. Slots pass rendered markup, including other components. Use a prop for a plain label or a variant name, and a slot for anything that might contain links, formatting or child components.
How do I check if a slot has content in Astro?
Call Astro.slots.has('name') in the frontmatter or template. It returns true when the caller passed content for that slot, so you can skip wrapper elements and avoid empty spacing.
Should Astro theme components use a UI framework?
Usually not. Layout, cards, heroes and navigation work well as plain .astro components with small scripts for toggles. Add a framework island only where a component needs real client-side state.