Skip to content
Esc
↑↓navigate↵open⌘Jpreview
On this page

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/layout
npx shadcn@latest add @gnostic/layout
pnpm dlx shadcn@latest add @gnostic/layout

Complete 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

  • Layout owns the root <html> element and global visual baseline.
  • LayoutHead centralizes metadata, canonical URLs, and social tags.
  • Seo supplies the same metadata inside an existing <head>. Import it from @/components/ui/layout/seo.astro.
  • LayoutBody and LayoutMain provide the default body and page-content wrappers.
  • LayoutMain is a neutral structural wrapper for page content.

API Reference

LayoutHead and Seo share SeoProps from layout/types.ts.

SeoProps

PropType
name?string

The name of the site.

Typestring
title?string

The title of the page. Used for the `<title>` tag.

Typestring
Defaultname ?? ""
description?string

The description of the page.

Typestring
image?{ src?: ImageMetadata | string; alt?: string }

The image of the page. Used for the `<meta property="og:image" content="...">` tag.

Type{ src?: ImageMetadata | string; alt?: string }
noindex?boolean

Whether to add the `noindex` attribute to the `<meta name="robots" content="...">` tag.

Typeboolean
nofollow?boolean

Whether to add the `nofollow` attribute to the `<meta name="robots" content="...">` tag.

Typeboolean
canonical?string

The canonical URL of the page. Used for the `<link rel="canonical" href="...">` tag.

Typestring
graph?Record<string, unknown> | null

The graph data for the page. Used for the `<script type="application/ld+json">` tag.

TypeRecord<string, unknown> | null

Read 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.