.astro file extension.
If you know HTML, you already know enough to write your first Astro component.
<meta> tags that make SEO easy to work with. Components can be reusable UI elements, like a header or a profile card. Astro components can even contain an entire page layout or, when located in the special src/pages/ folder, be an entire page itself.
The most important thing to know about Astro components is that they don’t render on the client. They render to HTML either at build-time or on-demand. You can include JavaScript code inside of your component frontmatter, and all of it will be stripped from the final page sent to your users’ browsers. The result is a faster site, with zero JavaScript footprint added by default.
When your Astro component does need client-side interactivity, you can add standard HTML <script> tags or UI Framework components as “client islands”.
Component Structure
An Astro component is made up of two main parts: the Component Script and the Component Template. Each part performs a different job, but together they provide a framework that is both easy to use and expressive enough to handle whatever you might want to build.src/components/EmptyComponent.astro
The Component Script
Astro uses a code fence (---) to identify the component script in your Astro component. If you’ve ever written Markdown before, you may already be familiar with a similar concept called frontmatter. Astro’s idea of a component script was directly inspired by this concept.
You can use the component script to write any JavaScript code that you need to render your template. This can include:
- Importing other Astro components
- Importing other framework components, like React
- Importing data, like a JSON file
- Fetching content from an API or database
- Creating variables that you will reference in your template
src/components/MyComponent.astro
The Astro component script is TypeScript, which allows you to add additional syntax to JavaScript for editor tooling and error checking.
The Component Template
The component template is below the code fence and determines the HTML output of your component. If you write plain HTML here, your component will render that HTML in any Astro page it is imported and used. However, Astro’s component template syntax also supports JavaScript expressions, Astro<style> and <script> tags, imported components, and special Astro directives. Data and values defined in the component script can be used in the component template to produce dynamically-created HTML.
src/components/MyFavoritePokemon.astro
Component Props
An Astro component can define and accept props. These props then become available to the component template for rendering HTML. Props are available on theAstro.props global in your frontmatter script.
Here is an example of a component that receives a greeting prop and a name prop. Notice that the props to be received are destructured from the global Astro.props object.
src/components/GreetingHeadline.astro
src/components/GreetingCard.astro
Props type interface. Astro will automatically pick up the Props interface in your frontmatter and give type warnings/errors. These props can also be given default values when destructured from Astro.props.
src/components/GreetingHeadline.astro
src/components/GreetingHeadline.astro
Slots
The<slot /> element is a placeholder for external HTML content, allowing you to inject (or “slot”) child elements from other files into your component template.
By default, all child elements passed to a component will be rendered in its <slot />.
Unlike props, which are attributes passed to an Astro component available for use throughout your component with
Astro.props, slots render child HTML elements where they are written.src/components/Wrapper.astro
src/pages/fred.astro
<SomeLayoutComponent></SomeLayoutComponent> tags and sent to the component to render inside of common page elements defined there.
Named Slots
An Astro component can also have named slots. This allows you to pass only HTML elements with the corresponding slot name into a slot’s location. Slots are named using thename attribute:
src/components/Wrapper.astro
slot attribute on any child element to specify the name of the slot. All other child elements of the component will be injected into the default (unnamed) <slot />.
src/pages/fred.astro
Fallback Content for Slots
Slots can also render fallback content. When there are no matching children passed to a slot, a<slot /> element will render its own placeholder children.
src/components/Wrapper.astro
Component-based Design
Components are designed to be reusable and composable. You can use components inside of other components to build more and more advanced UI. For example, aButton component could be used to create a ButtonGroup component:
src/components/ButtonGroup.astro
HTML Components
Astro supports importing and using.html files as components or placing these files within the src/pages/ subdirectory as pages. You may want to use HTML components if you’re reusing code from an existing site built without a framework, or if you want to ensure that your component has no dynamic features.
HTML components must contain only valid HTML, and therefore lack key Astro component features:
- They don’t support frontmatter, server-side imports, or dynamic expressions
- Any
<script>tags are left unbundled, treated as if they had anis:inlinedirective - They can only reference assets that are in the
public/folder
A
<slot /> element inside an HTML component will work as it would in an Astro component. In order to use the HTML Web Component Slot element instead, add is:inline to your <slot> element.