Skip to content

MDX Components

Many Astro sites contain components which are specifically designed for use in MDX content files. Here’s a simple example.

Highlight.astro
<span class="highlight"><slot /></span>
<style>
.highlight {
background: var(--color-highlight-bg);
padding: 0 8px 4px;
}
</style>

Which we might use like this.

my-post.mdx
This is some <Highlight>text I wanna highlight</Highlight>.

We can probably remember how to use a simple component like this, but using more complex components which accept multiple props can often mean opening a code editor and looking at the component file before we can use it.

Astro Editor helps with using these kinds of components by providing an interface for searching and inserting them into MDX content files. When editing an MDX file, Command + Slash Control + Slash Command + Slash will open a palette showing all the components inside your configured MDX directory. You filter the results by typing, as you’d expect.

The MDX component builder palette listing the available components, each with a framework badge

Each entry shows its framework as a coloured badge and a count of its props. If the component has a doc comment above it, a short description is pulled from it and shown under the name (and is searchable too).

Hitting Enter Enter Enter will open an interface showing the available props and their types along with a preview of what will be inserted.

The component builder's configuration step showing a component's props, their types, and a preview of the code to be inserted

Optional props can be toggled on or off and Command + Enter Control + Enter Command + Enter will insert the component into your document with your cursor in the first editable prop or slot. You can use Tab Tab Tab to move between them. When a prop only accepts a few set values, the first is pre-filled to get you started.

Optional props toggled on in the component builder, ready to be inserted

For components written in a framework (React, Vue, or Svelte), the configure step also lets you pick a client directiveclient:load, client:idle, client:visible, client:media, or client:only. This controls when (and whether) Astro hydrates the component in the browser, and gets added to the inserted tag. client:idle is recommended for most cases. Astro components don’t need one, so the option only appears for framework components.

Any component meant to be used as above. By default, Astro Editor looks in src/components/mdx/ and its subdirectories, but you can configure this in the preferences. The component builder automatically detects which props and slots are available by reading the component’s source code and works with well-formed components written in:

  • Astro (.astro files)
  • React (.tsx, .jsx files)
  • Vue (.vue files)
  • Svelte (.svelte files)