Basic Syntax
Use a code fence with thepreview kind:
```preview:name template=type
name: Unique identifier for the previewtemplate: Eitherhtml(default) orreact
Templates
HTML Template (template=html)
Raw HTML rendered directly in an iframe.
- No compilation step
- Supports inline
<style>and<script>tags - Full HTML/CSS/JS capabilities
React Template (template=react)
React components compiled via SWC at runtime.
- Compiles JSX to JavaScript
- React and ReactDOM are available globally
- Use
React.createElementor JSX syntax - Rendered into
<div id="root"></div>
Container Queries
Preview content should use container queries instead of media queries for responsive behavior, since iframe width varies with split layout.HTML Container Queries
- Wrap content in a
container-type: inline-sizeparent - Use
@container (min-width: ...)instead of@media - Cards stack vertically in narrow iframes, switch to horizontal when wide
Interactive Examples
HTML with JavaScript
React with State
Styling
Inline Styles
Style Tag
Regions
Previews don’t have standard region support, but you can define custom regions based on your content:Real Example
Best Practices
HTML vs React
Use HTML when:- Demonstrating static layouts
- Teaching CSS concepts
- No state management needed
- Simpler is better
- Demonstrating interactive components
- Teaching React patterns
- State management required
- Complex interactivity
Responsive Design
- Always use container queries, not media queries
- Wrap content in
container-type: inline-sizeparent - Test in split mode (narrower iframes)
- Use flexible layouts (flexbox, grid)
Accessibility
- Include focus styles for keyboard navigation
- Use semantic HTML (
<button>,<nav>, etc.) - Add ARIA labels when needed
- Ensure sufficient color contrast
Performance
- Keep previews simple (avoid heavy libraries)
- React compilation happens at runtime (adds latency)
- For complex demos, prefer HTML template
- Avoid infinite loops in React components
Limitations
- React template requires
React.createElementsyntax (JSX is compiled) - No external libraries (React/ReactDOM are pre-loaded)
- Iframe is sandboxed (
sandbox="allow-scripts") - No network requests from preview code
- Height is auto-calculated from content