No Preview

Sorry, but you either have no stories or none are selected somehow.

If the problem persists, check the browser console, or the terminal you've run Storybook from.

The component failed to render properly, likely due to a configuration issue in Storybook. Here are some common causes and how you can address them:

  1. Missing Context/Providers: You can use decorators to supply specific contexts or providers, which are sometimes necessary for components to render correctly. For detailed instructions on using decorators, please visit the Decorators documentation.
  2. Misconfigured Webpack or Vite: Verify that Storybook picks up all necessary settings for loaders, plugins, and other relevant parameters. You can find step-by-step guides for configuring Webpack or Vite with Storybook.
  3. Missing Environment Variables: Your Storybook may require specific environment variables to function as intended. You can set up custom environment variables as outlined in the Environment Variables documentation.

For a more specialized visualization of checkbox and radio elements.

There are various methods to integrate this component into your project.

We advise opting for the "Standard HTML" approach for when you need full access to the input field and using the "Web Component" method in every other case.

Standard HTML

The card-control standard HTML will be renamed in a future version.

<div class="checkbox-button-card"> <input id="CardControl_1" name="checkbox-button-card-control_1" class="form-check-input" type="checkbox" /> <label class="form-check-label" for="CardControl_1"> <span>Label</span> </label> </div>
NameDescriptionDefaultControl
GroupValidation
boolean
-
General
Type
string
-
Label
The main label of the input
string
-
Description
A short additional description
string
-
Icon
string
-
States
Checked
When set to true, places the component in the checked state.
boolean
-
Disabled
When set to true, disables the component's functionality and places it in a disabled state.There are accessibility concerns with the disabled state.
Please read our disabled state accessibility guide.
boolean
-
Validation
Controls the display of the component's validation state.
string
-
NameDescriptionDefaultControl
Background-Color
The background color of a surrounding wrapper element.
string
-
General
Type
string
-
States
Checked
When set to true, places the component in the checked state.
boolean
-
Disabled
When set to true, disables the component's functionality and places it in a disabled state.There are accessibility concerns with the disabled state.
Please read our disabled state accessibility guide.
boolean
-
Validation
Controls the display of the component's validation state.
string
-

Checkbox cards can be grouped together. Use a fieldset/legend combination to label the group. If there is an error, link the legend with the error message through the aria-describedby attribute on the legend, pointing to the id of the error message.

Legend
NameDescriptionDefaultControl
General
Type
string
-
States
Validation
Controls the display of the component's validation state.
string
-

Firefox currently does not support

the new CSS :has pseudo-selector

. As a fallback, the following states have to be mirrored on the top level element in the form of classes (see below for a snippet):

  • checked
  • disabled
  • is-invalid

Check caniuse :has() to check if you still need the fallback.

This snippet adds a global event listener to mirror the focused and checked states to the parent of the input. This fallback has to be applied as long as Firefox does not support the :has selector.

document.addEventListener('input', e => { if (!(e.target instanceof Element) || e.target.nodeName !== 'input') return; const parent = e.target.parentElement; if ( parent?.classList.contains('checkbox-button-card') || parent?.classList.contains('radio-button-card') ) { parent.classList.toggle('checked', (e.target as HTMLInputElement).checked); } });

Make sure the @swisspost/design-system-styles package is already present in your project or follow the installation guidelines.

To import all Design System styles:

@use '@swisspost/design-system-styles/index.scss';

To import only the styles required for this component:

@use '@swisspost/design-system-styles/basics.scss'; @use '@swisspost/design-system-styles/components/choice-control-card.scss';
Webcomponent

The card-control webcomponent is deprecated and will be removed in the next major version.

<post-card-control label="Label" type="checkbox"> </post-card-control>
NameDescriptionDefaultControl
props
label*
Defines the text in the control-label.
string
description
Defines the description in the control-label.
string
null
type*
Defines the type attribute of the control.
"checkbox""radio"
name

Defines the name attribute of the control. This is a required property, when the control should participate in a native form. If not specified, a native form will never contain this controls value.This is a required property, when the control is used with type radio.

string
null
value
Defines the value attribute of the control. This is a required property, when the control is used with type radio.
string
null
checked
Defines the checked attribute of the control. If true, the control is selected at its value will be included in the forms' data.
boolean
false
disabled
Defines the disabled attribute of the control. If true, the user can not interact with the control and the controls value will not be included in the forms' data.
boolean
false
validity

Defines the validation validity of the control. To reset validity to an undefined state, simply remove the attribute from the control.

nullboolean
null
icon

Defines the icon name inside the card. If not set the icon will not show up.

string
null
slots
default
Content to place into the default slot.

Markup accepted: block content.

Even if it is generally possible, we do not recommend using interactive elements in this slot because the background of the card control is clickable.
This can lead to confusion when the hit box of nested interactive controls is not clearly separated from the background, is invalid HTML and click events bubbling up to the card control will unexpectedly toggle it if they're not captured.
More info: https://accessibilityinsights.io/info-examples/web/nested-interactive/

other
-
icon
To insert a custom icon into the named icon slot.

Markup accepted: inline content.

It is only meant for img or svg elements and overrides the icon property.

other
-
events
postInput

An event emitted whenever the components checked state is toggled. The event payload (emitted under event.detail.state) is a boolean: true if the component is checked, false if it is unchecked.

{ state: boolean; value: string; }
--
postChange

An event emitted whenever the components checked state is toggled. The event payload (emitted under event.detail.state) is a boolean: true if the component is checked, false if it is unchecked. If the component is used with type radio, it will only emit this event, when the checked state is changing to true.

{ state: boolean; value: string; }
--
methods
reset

A public method to reset the controls checked and validity state. The validity state is set to null, so it's neither valid nor invalid.

reset() => Promise<void>
--

The <post-card-control> element is part of the @swisspost/design-system-components package. For more information, read the getting started with components guide.

NameDescriptionDefaultControl
Background-Color
The background color of a surrounding wrapper element.
string
-
props
type*
Defines the type attribute of the control.
"checkbox""radio"
checked
Defines the checked attribute of the control. If true, the control is selected at its value will be included in the forms' data.
boolean
false
disabled
Defines the disabled attribute of the control. If true, the user can not interact with the control and the controls value will not be included in the forms' data.
boolean
false
validity

Defines the validation validity of the control. To reset validity to an undefined state, simply remove the attribute from the control.

nullboolean
null

You can use our built-in icons by just adding the icon property with the name of the desired icon.
If this is not enough, you can also use the named icon slot and add your very own custom icon.

Make sure you remove all the width and height attributes from the img or svg tag. Otherwise we can not ensure, our styles will work properly.

If you need to add other content to the component, you can use the default slot to do so.

Even if it is generally possible, we do not recommend using interactive elements in this slot because the background of the card control is clickable.

This can lead to confusion when the hit box of nested interactive controls is not clearly separated from the background, is invalid HTML and click events bubbling up to the card control will unexpectedly toggle it if they're not captured. More info: https://accessibilityinsights.io/info-examples/web/nested-interactive/

  • List item
  • List item
  • List item

You can use the component directly in your forms, the control value will be available in the FormData of the surrounding <form> element, just as you are used to from native input elements.

Update the control and submit or reset the form to see how its FormData value changes.

Legend
Legend

FormData

Submit or reset the form to see how the FormData will look like.

{}
NameDescriptionDefaultControl
Checkbox
value
Set the value of the checkbox card.
string
null
disabled
Set the disabled state of the checkbox card.
boolean
false
validity

Defines the validation validity of the control. To reset validity to an undefined state, simply remove the attribute from the control.

nullboolean
null
disabled fieldset
Set the disabled attribute of the fieldset around the checkbox card.
boolean
-
Radio
value
Set the value prefix of the radio cards.
string
-
disabled
Set the disabled state of the second radio card.
string
-
disabled fieldset
Set the disabled attribute of the fieldset around the radio cards.
boolean
-
validity
Defines the validation validity of the control. To reset validity to an undefiend state, simply remove the attribute from the control.
nullboolean
-

Change the width of a <card-control> component, by putting it (for example) in a grid. If you like to stretch all <card-control> components within a row to the same height, simply add the class .h-100 to them.

NameDescriptionDefaultControl
General
Columns
Controls the amount of elements per row shown in the grid.
number
-
Full Height
Stretch elements in one row to the maximum height.
boolean
-

As you can create radio button groups with native <radio> elements, you can do the same with our <post-card-control> component as well.
Just add the same name attribute value to multiple <post-card-control type="radio"> components.

Legend

The <post-card-control> offers a reset method to reset it to the initial state (validity and checked state). The method can be called directly on the element itself.

const cardControl = document.querySelector('post-card-control') as HTMLPostCardControlElement; cardControl.reset();