Creating a custom snippet
Pre-condition: You must create your snippet file in thesnippets directory.
Any page in the
snippets directory will be treated as a snippet and will not be rendered into a standalone page. If you want to create a standalone page from the snippet, import the snippet into another file and call it as a component.Default export
The simplest way to create a reusable snippet is using the default export pattern.- Step 1: Create snippet
- Step 2: Import snippet
Add content to your snippet file that you want to re-use across multiple locations. Optionally, you can add variables that can be filled in via props when you import the snippet.
snippets/my-snippet.mdx
Reusable variables
Export and reuse specific variables across multiple pages.- Step 1: Export variables
- Step 2: Import and use
Export a variable from your snippet file:
snippets/path/to/custom-variables.mdx
Reusable variables are perfect for maintaining consistent values like product names, version numbers, or common configuration values across your documentation.
Reusable components
Create more complex reusable components that accept multiple props.- Step 1: Create component
- Step 2: Import and render
Inside your snippet file, create a component that takes in props by exporting your component in the form of an arrow function.
snippets/custom-component.mdx
Use cases
Reusable snippets are ideal for various documentation scenarios:Common use cases
- API credentials setup - Reuse authentication instructions across API endpoints
- Prerequisites - Share common setup requirements across multiple guides
- Warning messages - Maintain consistent warning text throughout docs
- Product versions - Keep version numbers synchronized
- Code examples - Share boilerplate code across tutorials
- Contact information - Display consistent support details
Best practices
- Organization
- Content design
- Maintenance
- Keep snippets in the
/snippetsdirectory - Use descriptive file names
- Organize snippets into subdirectories by category
- Document what each snippet does
When you update a snippet, the changes automatically propagate to all pages that import it, ensuring consistency across your entire documentation.