# Popover

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/popover/
- Storybook: https://lyne-elements.app.sbb.ch/?path=/docs/elements-popover--docs
- Angular (`@sbb-esta/lyne-angular`): https://lyne-angular.app.sbb.ch/angular/components/popover/overview (API: https://lyne-angular.app.sbb.ch/angular/components/popover/api)
- Figma: https://www.figma.com/design/mWknI2rC5DJmOgRO61WKai/Lyne-Components?node-id=7491-47
- GitHub: https://github.com/sbb-design-systems/lyne-components/tree/v5.12.0/src/elements/popover

## Elements

- `sbb-popover`: It displays contextual information within a popover.
  - `sbb-popover-close-button`, only within `sbb-popover`: Popover close button, intended to be placed inside sbb-popover.

## Related components

- Similar: [Tooltip](https://digital.sbb.ch/en/design-system/lyne/components/tooltip/index.md)

## Overview

### What does the component do?
A popover is a small [overlay](https://digital.sbb.ch/en/design-system/lyne/components/overlay/) that shows additional information or interactive content for an element. It opens on click or, optionally, on hover over the element.

### When should the component be used?
* To explain icons or buttons.
* To explain terms or help users make decisions without leaving the page.
* For plain, short text without interaction, use the [tooltip](https://digital.sbb.ch/en/design-system/lyne/components/tooltip/) instead.

### Rules.
* The content is short and works on all screen sizes.
* Only one popover is open at a time.
* A popover contains a title (heading level 2), which is visually hidden if necessary.
* For click-triggered popovers, place a close button at the start of the content. Do not use a close button for hover-triggered popovers.

## Implementation (web components)

The `<sbb-popover>` component can be useful for displaying contextual and additional information on mouse hover or click of a trigger element.

The component could be used:

- to describe icons and buttons;
- when more information is useful to help a user make decisions;
- when an element needs more context or explanation;
- when defining a term or inline item.

The component must be connected with the trigger element using the `trigger` property,
which accepts the id of the element, or directly its reference;

```html
<sbb-mini-button icon-name="circle-information-small" id="popover-trigger"></sbb-mini-button>

<sbb-popover trigger="popover-trigger">
  <sbb-popover-close-button></sbb-popover-close-button>
  <sbb-title level="2" visual-level="6" style="margin-block-start: 0">Popover Title.</sbb-title>
  <p>Popover content.</p>
</sbb-popover>
```

### Configuration

The open and close delays can be configured via global configuration. This values will be used as default, unless explicitly set on the element.

```ts
import { mergeConfig } from '@sbb-esta/lyne-elements/core.js';

mergeConfig({
  popover: {
    openDelay: 0, // ms before the popover opens
    closeDelay: 0, // ms before the popover closes
  },
});
```

### Interactions

The `<sbb-popover>` can be dismissed by clicking on an interactive element within its content,
by clicking on the close button or by performing another action on the page.

You can also indicate that an element within the popover content should close the `<sbb-popover>` when clicked
by marking it with the `sbb-popover-close` attribute.
To display a close button, place the `<sbb-popover-close-button>` inside the popover.
For accessibility reasons, the close button should be the first focusable element in the popover,
so it is recommended to place it at the beginning of the content.

```html
<sbb-popover>
  <sbb-popover-close-button></sbb-popover-close-button>
  <sbb-title level="2" visual-level="6" style="margin-block-start: 0">Popover Title.</sbb-title>
  <p>Popover content. <sbb-link sbb-popover-close>Link</sbb-link></p>
</sbb-popover>
```

You can also indicate that the `<sbb-popover>` should be shown on hover with the property `hoverTrigger`
and set a custom delay for the open and close animations (defaults to 0).
When using `hoverTrigger`, it is the consumer's responsibility to omit the `<sbb-popover-close-button>`,
as a close button is not appropriate in hover-triggered popovers.

If hover is not supported by the current device, the component will be triggered on click/tap as default.
The `<sbb-popover>` will automatically disappear after the hiding delay
if neither the trigger element nor the popover are on hover or if another action is performed on the page.

```html
<sbb-popover hover-trigger open-delay="500" close-delay="750">
  <sbb-title level="2" visual-level="6" style="margin-block-start: 0">Popover Title.</sbb-title>
  <p id="popover-content">Popover content. <sbb-link id="popover-link">Link</sbb-link></p>
</sbb-popover>
```

### Style

The `<sbb-popover>` automatically calculates where it should place itself, based on available space. Default is below and center.

The component has a negative variant which can be set using the `negative` property.
The `<sbb-popover-close-button>` component, if present, is automatically rendered in the `negative` variant,
while consumers need to set it manually on other components, or adjust styling accordingly.

### Accessibility

As the popover opens, the focus will automatically be set to the first focusable item within the component.
If the close button is not hidden, it's the first element and therefore gets focused (unless manually specified, see below).

Overlays should always contain a heading level 2 title. It can be visually hidden if necessary.

#### Controlling initial focus

The first element with the attribute `sbb-focus-initial` will receive focus on opening.
If the attribute is not used, the first focusable element receives focus (recommended).

```html
<sbb-popover>
  <sbb-link href="#">Link</sbb-link>
  <sbb-link sbb-focus-initial href="#">Link 2</sbb-link>
</sbb-popover>
```

## API (web components)

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