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?rawimport, 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 customizationname?: string— Section identifier fordata-doc-sectionclass?: string— Wrapper classcontainer?: Omit<ContainerProps, 'children' | 'default'>— Container configurationdefaultContainerConfig?: LayerConfig<...>— Default Container configexampleSlot?: { class?: string }— Preview wrapper configdefaultExampleSlotConfig?: 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
---
import Button from '@alfadormx/blocks/primitives/Button.astro';
---
<Button>Click me</Button>Colored buttons
---
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
| Prop | Type | Default | Description |
|---|---|---|---|
| source | string | — | Raw source string (from `?raw` import, prioritized over sourceFile). |
| sourceFile | string | — | 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 |
| code | boolean | false | true | Whether to show the CodeBlock with source code. Default: `true` (show code). Pass `false` to hide source for purely visual examples. |
| defaultCodeBlockConfig | LayerConfig<Omit<CodeBlockProps, 'code'>> | — | CodeBlock configuration merged over internal defaults. Accepts any CodeBlockProps except the `code` prop (source is provided by Example). |
| name | string | — | 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'. |
| class | string | — | Classes added to the outer wrapper. |
| container | Omit<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. |
| defaultContainerConfig | LayerConfig<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. |
| defaultExampleSlotConfig | LayerConfig<{ /** 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. |