Skip to main content
Components are the foundation of Primo. You write components using Svelte, attach content fields to them, and they become blocks that editors can use to build pages. This guide covers everything you need to know about writing effective components.

Components vs Blocks

Understanding the distinction helps you build better: Components are pure Svelte code:
  • Reusable UI elements (Button, Card, Hero)
  • Accept props and handle logic
  • No content fields attached
  • Can be used anywhere in your Svelte code
Blocks are components with content fields:
  • Same Svelte component code
  • Plus editable content fields (text, images, links)
  • Used specifically in page types and pages
  • Editors can modify content, not code
When you create a block in Primo, you’re writing a Svelte component and then attaching fields to it. The component code stays the same; fields make it editable.

Component Structure

Every Primo component follows this structure:

Writing Your First Component

Let’s build a simple Button component step by step:
1

Identify the fields you need

Think about what data your component needs. For a button block:
  • text: The button text
  • href: The link destination
  • variant: The button style (primary, secondary, outline)
Fields will be automatically available in your component code.
Plan your fields first, then reference them in your template. You can set default values in the field configuration.
2

Add the markup

Write the HTML structure:
3

Style the component

Add scoped styles:

Best Practices

1. Handle Empty Values

Components should work gracefully even with empty fields:

2. Use Conditional Rendering

Only show elements when content exists:

3. Handle Arrays Safely

When working with repeater fields, always check if the array has items:

4. Use Scoped Styles

Primo components use scoped styles by default. Use :global() when you need to style child elements:

5. Make Components Responsive

Always consider mobile devices:

6. Create Component Variations

Instead of creating many similar blocks, use fields to create variations of a single component: Layout variations:
Color/theme variations:
Size variations:
One flexible block with variant options is easier to maintain than multiple similar blocks. Editors can adjust layouts without developers creating new components.

Working with Rich Content

Rich Text Fields

When using rich text fields, use {@html} to render HTML:
Only use {@html} with trusted content. Primo sanitizes rich text fields, but be cautious with user-generated content.

Markdown Content

If you’re using markdown fields, Primo processes them automatically. But you can also use a markdown processor for additional control:

Advanced Patterns

Reactive Statements

Use reactive statements ($:) for computed values based on fields:

Event Handling

Handle user interactions:

Conditional Classes

Use dynamic classes for variants:

Component Composition

Build complex components from simpler ones:
In Primo’s component editor, you write single-file components. For composition, you can create separate blocks and reference them, or write everything in one component file.

Performance Considerations

Lazy Loading Images

For image-heavy components:

Optimize Re-renders

Use reactive statements efficiently:

Testing Your Components

Use the Preview

Primo’s in-browser editor provides instant preview:
  1. Write your component code
  2. Add test content in fields
  3. See changes update in real-time
Test with empty fields, long text, special characters, and edge cases to ensure your component is robust.

Test Different Screen Sizes

Use browser dev tools in the preview to test:
  • Mobile (320px - 768px)
  • Tablet (768px - 1024px)
  • Desktop (1024px+)

Common Patterns

Card Component

Grid Layout

Accordion Component

Next Steps

Working with Fields

Learn how to attach content fields to your components.

Using the Component Library

Organize and reuse components across sites.

Field Types Reference

Complete reference of all available field types.

Your First Site

See components in action with a complete site example.
The best components are simple, focused, and reusable. Start with basic functionality, then add complexity only when needed.