In design systems, a variant allow grouping multiple component properties into a predefined set. The variant can then be used as a shortcut to render a component.
In SDC, front-end developers could previously define a variant as a prop, but this approach did not support custom titles or descriptions to convey the variant’s purpose.
This change allows component authors to specify a title and description that differ from the machine name, enabling more meaningful labels and detailed explanations of each variant’s purpose.
Now, you can use variants as a property at the root of your component declaration:
name: Card
variants:
primary:
title: Primary
description: ...
secondary:
title: Secondary
description: ...
props: {}
slots: {}
In your component twig template, you can use this as a regular prop:
{% if variant is not empty %}
{% set attributes = attributes.addClass('card-' ~ variant) %}
{% endif %}
When rendering a component from another twig template, you can specify the variant with:
{% include "mytheme:card" with {
variant: 'primary',
attributes: create_attribute({'class': ['banner__title']}),
label: content.field_title,
} only %}
Using the render arrays API (e.g. from a controller), you can use:
$build['content'] = [
'#type' => 'component',
'#component' => 'mytheme:card',
'#variant' => 'secondary',
'#props' => [
'label' => $this->t('My custom card'),
],
];
Note that the concept of variants introduced in this change is not related to the broader use of "variants" in design systems and tools like Storybook, where the term typically refers to a combination of prop values and is commonly used for demonstrating components.