Skip to main content
Webhaven

Main navigation

  • Home
  • Who it's for
    • For Experts
    • For Agencies
    • For Starters
  • ROI calculator
  • Docs
  • Get launch notified
Just launched the Webhaven demo website. Check it out! 🚀

Breadcrumb

  1. Home
  2. Webhaven documentation
  3. Theming
  4. An example component

Documentation

  • Documentation
    • Welcome
  • Get started
    • What is Webhaven?
    • Installation
  • Configuration
    • Drupal Canvas
    • Site settings
  • Theming
    • Introduction
    • Theme structure
    • Create a custom theme
    • Single Directory Components (SDC)
    • Component structure
    • An example component
  • Handy resources
    • Roadmap
    • What are Drupal recipes?
    • What is a Drupal site template?
    • What is Drupal Canvas?
    • Drupal Canvas vs Layout Builder vs Paragraphs
    • What is DDEV?
    • Useful DDEV commands
    • What is Drush?
    • Useful Drush commands

An example component

Now that we have learned all about the structure a Webhaven component let's dive into an actual example.

For this example we'll dive deeper into a pirate-boat component.

Component structure

/pirate-boat
|
|- pirate-boat.twig
|- pirate-boat.component.yml
|- pirate-boat.scss

pirate-boat.twig

{% if name %}
{% set attributes = create_attribute() %}
<div{{ attributes.addClass('pirate-boat', class|default('')) }}>
  <h3 class="pirate-boat__name">{{ name }}</h3>
  {% if crew %}
    <p class="pirate-boat__crew">Crew: {{ crew }}</p>
  {% endif %}
  {% if description %}
    <p class="pirate-boat__description">{{ description }}</p>
  {% endif %}
</div>
{% endif %}

The pirate-boat.twig file renders the DOM structure for the component. Only the name prop is required, crew and description are optional and only rendered when provided.

pirate-boat.component.yml

$schema: https://git.drupalcode.org/project/drupal/-/raw/HEAD/core/assets/schemas/v1/metadata.schema.json
name: Pirate Boat
group: Cards
props:
  type: object
  required:
    - name
  properties:
    name:
      type: string
      title: Boat name
      examples:
        - The Black Pearl
    crew:
      type: integer
      title: Crew size
      examples:
        - 42
    description:
      type: string
      title: Description
      examples:
        - A fearsome vessel sailing the seven seas.
    class:
      type: string
      title: Additional CSS class

We define our component configuration here. The name prop is required, everything else is optional.

A component can have props and/or slots. If you want to learn more on props & slots we recommend reading the Drupal SDC documentation.

pirate-boat.scss

.pirate-boat {
  display: flex;
  flex-direction: column;
  gap: var(--spacing-2);

  &__name {
    font-size: var(--font-2xl);
  }

  &__crew {
    color: var(--color-muted);
  }
}

This example uses --color-muted, which is defined in your theme's variables file at src/scss/base/_variables.scss.

All other files are automatically generated when you run npm run watch or npm run build. In this case that means pirate-boat.css and pirate-boat.css.map are generated automatically.

You've seen how it works. Now see what it's worth.

See how much time and money Webhaven saves on every Drupal project.
Calculate my savings
Webhaven
Ship your best Drupal work, faster.

Links

  • Who it's for
  • Get launch notified
  • Docs
  • Free tools
  • Blog
  • Get in touch

Follow on

  • drupal profile
  • linkedin profile
  • x_twitter profile
© 2026 Webhaven.
All rights reserved.

Legal

  • Privacy policy
  • Terms of service
  • Accessibility
  • Login
Built with