Layout for Astro
Astro document parts for the page shell, head, body, and main content.
Use the Layout parts in a document shell or a template layout. The framed examples show how those parts arrange a page with its own document structure.
Installation
bunx --bun shadcn@latest add @gnostic/layoutnpx shadcn@latest add @gnostic/layoutpnpm dlx shadcn@latest add @gnostic/layoutComplete the Installation guide first. Then copy the component files below.
No extra packages are required.
Copy the files
src/components/ui/layout/index.ts
export { default as Layout } from './layout.astro'
export { default as LayoutBody } from './layout-body.astro'
export { default as LayoutHead } from './layout-head.astro'
export { default as LayoutMain } from './layout-main.astro'
export { default as Seo } from './seo.astro'
export type { SeoProps } from './types.ts'
src/components/ui/layout/layout-body.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
type Props = HTMLAttributes<"body">
const { class: className, ...props } = Astro.props
---
<body
class={cn(
"bg-background text-foreground overscroll-none antialiased",
className,
)}
data-slot="layout-body"
{...props}
>
<slot />
</body>
<style is:global>
body {
font-family: var(--font-sans);
}
</style>
src/components/ui/layout/layout-head.astro
---
import Seo from "./seo.astro"
import type { SeoProps } from "./types.ts"
type Props = SeoProps
const {
name,
title,
description,
image,
noindex,
nofollow,
canonical,
graph,
...props
} = Astro.props
---
<head data-slot="layout-head" {...props}>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="generator" content={Astro.generator} />
<Seo
name={name}
title={title}
description={description}
image={image}
noindex={noindex}
nofollow={nofollow}
canonical={canonical}
graph={graph}
/>
<slot />
</head>
src/components/ui/layout/layout-main.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
type Props = HTMLAttributes<"main">
const { class: className, ...props } = Astro.props
---
<main
data-slot="layout-main"
class={cn(
"@container/main flex flex-col in-data-[slot=sidebar-inset]:rounded-[inherit] not-in-data-[slot=sidebar-inset]:w-full",
className,
)}
{...props}
>
<slot />
</main>
src/components/ui/layout/layout.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
type Props = HTMLAttributes<"html">
const { class: className, lang, ...props } = Astro.props
---
<html
class={cn(
"bg-background text-foreground has-data-[variant=inset]:bg-sidebar overscroll-none scroll-smooth",
className,
)}
data-slot="layout"
lang={Astro.currentLocale || "en"}
{...props}
>
<slot />
</html>
<style is:global>
:root {
--container: 1280px;
color-scheme: light;
}
.dark {
color-scheme: dark;
}
@view-transition {
navigation: auto;
}
</style>
Usage
---
import {
Layout,
LayoutBody,
LayoutHead,
LayoutMain,
} from "@/components/ui/layout"
---
<Layout lang="en">
<LayoutHead
name="gnostic ui"
title="Page title"
description="Page description"
/>
<LayoutBody>
<LayoutMain>
<slot />
</LayoutMain>
</LayoutBody>
</Layout>
Responsive layout
LayoutMain uses @container/main to create a responsive layout.
Notes
Layoutowns the root<html>element and global visual baseline.LayoutHeadcentralizes metadata, canonical URLs, and social tags.Seosupplies the same metadata inside an existing<head>. Import it from@/components/ui/layout/seo.astro.LayoutBodyandLayoutMainprovide the default body and page-content wrappers.LayoutMainis a neutral structural wrapper for page content.
API Reference
LayoutHead and Seo share SeoProps from layout/types.ts.
SeoProps
name?string
The name of the site.
stringtitle?string
The title of the page. Used for the `<title>` tag.
stringname ?? ""description?string
The description of the page.
stringimage?{ src?: ImageMetadata | string; alt?: string }
The image of the page. Used for the `<meta property="og:image" content="...">` tag.
{ src?: ImageMetadata | string; alt?: string }noindex?boolean
Whether to add the `noindex` attribute to the `<meta name="robots" content="...">` tag.
booleannofollow?boolean
Whether to add the `nofollow` attribute to the `<meta name="robots" content="...">` tag.
booleancanonical?string
The canonical URL of the page. Used for the `<link rel="canonical" href="...">` tag.
stringgraph?Record<string, unknown> | null
The graph data for the page. Used for the `<script type="application/ld+json">` tag.
Record<string, unknown> | nullRead the installed source for the complete prop types.
When to use Layout
Use Layout parts in the site template that owns the route. Set the document language and provide page metadata in the head. Keep reusable blocks inside the main content rather than making them own the document.
Keyboard and behavior
A page needs one main landmark and a logical heading order. Add a skip link when repeated navigation comes before the main content. These shell parts do not infer titles or canonical URLs from arbitrary content.
Theme and layout
The layout imports shared styles so installed components use the same tokens. Theme and page background choices belong at this level. Avoid a second html or body element inside a reusable block.