Composite

Example

Live documentation examples with preview and synchronized source code.

The Example component renders documentation examples with live previews and synchronized source code from a single Astro file. This prevents drift between preview and code display.

Basic usage

In .astro files

import Example from '@alfadormx/blocks/composite/Example.astro'; import MyComponent from
'@alfadormx/blocks/primitives/MyComponent.astro'; import sourceCode from
'~/examples/my-category/MyComponent/example.astro?raw';
<Example source={sourceCode} name="basic">
  <MyComponent />
</Example>

In .mdx files

import Example from '@alfadormx/blocks/composite/Example.astro';
import MyComponent from '@alfadormx/blocks/primitives/MyComponent.astro';
import sourceCode from '~/examples/my-category/MyComponent/example.astro?raw';

<Example source={sourceCode} name="basic">
  <MyComponent />
</Example>

Source display rules

By default, Example shows the entire example file including imports. Imports use the package’s public paths (@alfadormx/blocks/...), the same a site writes.

For didactic examples that prioritize clarity over completeness, use sourceType="template-only" to show only the component template, hiding imports:

<Example source={sourceCode} sourceType="template-only">
  <MyComponent />
</Example>

Props reference

  • source?: string — Raw source string (from ?raw import, prioritized)
  • sourceFile?: string — Fallback path (Astro files only)
  • sourceType?: 'full' | 'template-only' — Display scope (default: 'full')
  • code?: boolean | false — Show CodeBlock (default: true)
  • defaultCodeBlockConfig?: LayerConfig<Omit<CodeBlockProps, 'code'>> — CodeBlock customization
  • name?: string — Section identifier for data-doc-section
  • class?: string — Wrapper class
  • container?: Omit<ContainerProps, 'children' | 'default'> — Container configuration
  • defaultContainerConfig?: LayerConfig<...> — Default Container config
  • exampleSlot?: { class?: string } — Preview wrapper config
  • defaultExampleSlotConfig?: LayerConfig<...> — Default preview wrapper config

Browser test scoping

Each example is wrapped with data-doc-section="<name>", enabling browser tests to isolate examples by section. Use this in Playwright specs:

const example = page.locator('[data-doc-section="basic"]');
await expect(example.locator('button')).toBeVisible();

Examples

Basic button

basic
---
import Button from '@alfadormx/blocks/primitives/Button.astro';
---

<Button>Click me</Button>

Colored buttons

colored
---
import Button from '@alfadormx/blocks/primitives/Button.astro';
---

<div class="flex gap-2">
  <Button intent="primary">Primary</Button>
  <Button intent="success">Success</Button>
</div>

Visual only (no code)

API Reference

PropTypeDefaultDescription
sourcestring—Raw source string (from `?raw` import, prioritized over sourceFile).
sourceFilestring—Fallback path to example file, resolved relative to component (Astro files only).
sourceType'full' | 'template-only''full'Display scope for the source code. - `full`: entire file including imports (default) - `template-only`: template only, imports removed
codeboolean | falsetrueWhether to show the CodeBlock with source code. Default: `true` (show code). Pass `false` to hide source for purely visual examples.
defaultCodeBlockConfigLayerConfig<Omit<CodeBlockProps, 'code'>>—CodeBlock configuration merged over internal defaults. Accepts any CodeBlockProps except the `code` prop (source is provided by Example).
namestring—Section identifier used in the `data-doc-section` attribute on the preview wrapper. Used by browser tests to scope to this example. If not provided, falls back to 'example'.
classstring—Classes added to the outer wrapper.
containerOmit<ContainerProps, 'children' | 'default'>—Container configuration for the Example wrapper. This field is set on every render; to style only one context, use a default…Config layer.
defaultContainerConfigLayerConfig<Omit<ContainerProps, 'children' | 'default'>>—Default Container config merged before the `container` instance. Use this to set baseline padding, border, or spacing for all Example uses.
exampleSlot{ /** Additional CSS classes on the preview wrapper. */ class?: string; }—Props for the preview content wrapper (`<div data-doc-section>`). This field is set on every render; to style only one context, use a default…Config layer.
defaultExampleSlotConfigLayerConfig<{ /** Additional CSS classes on the preview wrapper. */ class?: string; }>—Default example slot config merged before the `exampleSlot` instance. Use this to set baseline padding, border or border styling for all Example previews.