# Card

Component of the SBB design system Lyne.

- Package: `@sbb-esta/lyne-elements` (Angular: `@sbb-esta/lyne-angular`)
- Structure: main element with parts; use parts only within the element they belong to
- Categories: Container
- Status: Design guidelines complete

## Links

- Page on digital.sbb.ch: https://digital.sbb.ch/en/design-system/lyne/components/card/
- Storybook: https://lyne-elements.app.sbb.ch/?path=/docs/elements-card--docs
- Angular (`@sbb-esta/lyne-angular`): https://lyne-angular.app.sbb.ch/angular/components/card/overview (API: https://lyne-angular.app.sbb.ch/angular/components/card/api)
- Figma: https://www.figma.com/design/mWknI2rC5DJmOgRO61WKai/Lyne-Components?node-id=7491-18
- GitHub: https://github.com/sbb-design-systems/lyne-components/tree/v5.12.0/src/elements/card

## Elements

- `sbb-card`: It displays content related to a single subject.
  - `sbb-card-badge`, only within `sbb-card`: It displays content within a badge.
  - `sbb-card-button` (also as `sbb-card-link`), only within `sbb-card`: It turns the `sbb-card` into a button element.

## Overview

### What does the component do?
A card is a flexible container element. It combines content and actions on a single topic in one block.

### When should the component be used?
* To present related information in a compact way.
* To give a page visual hierarchy and structure.
* To display content, for example text, together with an action.

### Rules.
* The content of a card belongs to the same topic and is worded concisely.
* A card usually has one action. In this case, the entire card is the click target (card link or card button).
* If a card has several interactive elements, the click targets are placed on these elements.
* The text of a card link and card button is meaningful. It is only read by screen readers and search engines.
* A card badge shows additional information such as prices or discounts and needs an `aria-label` with a complete sentence.

## Implementation (web components)

The `<sbb-card>` component is a generic content container; its task is to present content related to a single subject.

```html
<sbb-card>Card content</sbb-card>
```

It is possible to use the component together with the `<sbb-card-badge>`
and the `<sbb-card-button>`/`<sbb-card-link>`.

#### With `<sbb-card-badge>`

The `<sbb-card-badge>` component can be used to display a badge in the upper right corner.
It can display information like prices or discounts.

To achieve the correct spacing between elements inside the card badge, we recommend to use `span`-elements.
All content parts are presented with a predefined gap in between.

Note: Additionally to using it in a card, the card badge can also be used
in the [sbb-selection-expansion-panel](https://lyne-elements.app.sbb.ch/?path=/docs/elements-selection-expansion-panel--docs) component.

```html
<sbb-card color="white">
  <sbb-card-badge aria-label="Super saver sales ticket price starts at CHF 19.99">
    <span>%</span>
    <span>from CHF</span>
    <span>19.99</span>
  </sbb-card-badge>
  Card content
</sbb-card>
```

#### With `<sbb-card-button>`/`<sbb-card-link>`

To add an action to a card, add a `<sbb-card-button>` or a `<sbb-card-link>` to the card.
With these components the card area becomes clickable.

Also consult the accessibility section down below on what to consider
for these elements, as their content is only visible to screen readers.

```html
<sbb-card>
  <sbb-card-link href="https://www.sbb.ch">Check all the wonderful trips available.</sbb-card-link>
  Buy trips
</sbb-card>

<sbb-card>
  <sbb-card-button type="submit" form="buy" value="trip">Buy this trip.</sbb-card-button>
</sbb-card>
```

The `<sbb-card-link>` is internally rendered as a link,
accepting its associated properties (`href`, `target`, `rel` and `download`).

The `<sbb-card-button>` is equivalent to a native `button`,
accepting its associated properties (`type`, `name`, `value` and `form`).

### Style

Generally, the padding of the `<sbb-card>` can be set directly on the host. However, it's possible to choose among
different preset spacing classes.

```html
<sbb-card class="sbb-card-spacing-3x-xxs">Card content</sbb-card>
<sbb-card class="sbb-card-spacing-xxxs-xxs">Card content</sbb-card>
<sbb-card class="sbb-card-spacing-xxxs-s">Card content</sbb-card>
<sbb-card class="sbb-card-spacing-4x-xxs">Card content</sbb-card>
<sbb-card class="sbb-card-spacing-xxs">Card content</sbb-card>
<sbb-card class="sbb-card-spacing-s">Card content</sbb-card>
<sbb-card class="sbb-card-spacing-l">Card content</sbb-card>
```

The component has four different values to choose from for the `color` property; default is `white`.

```html
<sbb-card color="milk">Card content</sbb-card>
<sbb-card color="transparent-bordered">Card content</sbb-card>
<sbb-card color="transparent-bordered-dashed">Card content</sbb-card>
```

### Accessibility

Normally, a `<sbb-card>` should be a single action, however, it's possible to place other interactive elements
in the card content. Interactive content will automatically be detected and made accessible to click / focus.
In cases where there should be only a visual button or link inside the card content without a different action, the
static component should be used (e.g. `<sbb-button-static></sbb-button-static>`).

For the `<sbb-card-badge>` it is recommended to place an `aria-label` on
`<sbb-card-badge>` to describe the displayed information in a full sentence,
as in the example above.

For the `<sbb-card-link>` and the `<sbb-card-button>` it is **important**
that a descriptive message is being added as content
as it is used for search engines and screen-reader users.

```html
<sbb-card-link href="https://www.sbb.ch">Buy a half-fare ticket now</sbb-card-link>
```

```html
<sbb-card-button>Buy a half-fare ticket now</sbb-card-button>
```

#### Windows High Contrast Notes

In high contrast mode, all the content of a link or a button receives a specific color which overrides every other color.

However, as the content of the card is not directly inside the button or link,
this does not happen when the slotted content has a specific color set.
To improve coloring, it's needed to manually define styles for Window high contrast mode (setting `LinkText` or `ButtonText`).

## API (web components)

Full API tables (attributes, events, slots, CSS properties): https://digital.sbb.ch/en/design-system/lyne/components/card/api.md
