# Details modal

> Opening a modal on a clicked category, with its values, content of your own in plain HTML or from JavaScript, or your own modal instead.

Source: https://eunomia-ds.com/charts/details/

A chart can open a modal on a category once it's clicked, or activated with Enter or Space once the plot is focused: headed by the category's label, it lists the value of every series, then whatever you add below. It works the same on every chart. This page covers opening it, adding content to it in plain HTML or from JavaScript, and replacing it with your own.

## Opening it

`details` turns the modal on: every category becomes clickable, the pointer turns into a hand, and the tooltip ends with the hint "Click to see the details". The modal is an `eun-modal`, which has to be registered too.

```html
<script type="module">
  import "@eunomia/charts/bar-chart.js";
  import "@eunomia/elements/modal.js";
</script>

<eun-bar-chart label="Orders per day and channel" integer stacked details></eun-bar-chart>
```

The modal closes with its close button, a click on the backdrop or Escape.

## What it lists

| Chart | Lists |
| --- | --- |
| Bar, line, combo | Every series with its swatch and its value, `display: "none"` included |
| Stacked bars | The same, then their total |
| Pie, doughnut | Every series with the slice's value and its share |
| Bubble | Every series' value for the clicked bubble |

Values are written as in the tooltip, with `format` or `valueFormatter`.

## Content of your own, in HTML

### The same for every category

Anything placed in the `details` slot is shown under the values, such as a note or a link to a report.

```html
<eun-bar-chart label="Orders per day" integer details>
  <p slot="details">Orders are counted once paid, refunds excluded.</p>
</eun-bar-chart>
```

### Filled for the clicked category

A `<template slot="details">` is filled for the clicked category each time the modal opens, with the same attributes as the [tooltip's template](https://eunomia-ds.com/charts/tooltip/#in-html-a-template): `data-field` writes the category, its `key`, a total or a series' value, `data-each` repeats an element per series, and `data-bind-<attr>` writes an attribute, `{category}`, `{key}`, `{tick}` and `{index}` replaced (URL-encoded in an `href` or a `src`). Here, a link to the orders of the clicked day:

```html
<eun-bar-chart label="Orders per day" integer details>
  <template slot="details">
    <p>
      <a part="details-link" data-bind-href="/orders/?day={key}">
        See the orders of <span data-field="category"></span> →
      </a>
    </p>
  </template>
</eun-bar-chart>
```

`data-bind-*` never writes an event handler (`on…`) or a `style`, and drops a link that would start with `javascript:`. Like the tooltip's, the content is rendered inside the chart: style it inline, or through a `part` and `::part()`.

## Content from JavaScript

`eunchartselect` fires before the modal opens, with the `category`, its `index` and the `values` of every series: fill the `details` slot from it, such as with the records behind the values.

```html
<eun-bar-chart label="Orders per day" integer details>
  <ul slot="details" id="orders"></ul>
</eun-bar-chart>

<script type="module">
  const chart = document.querySelector("eun-bar-chart");
  chart.addEventListener("eunchartselect", async (event) => {
    const orders = await fetchOrders(event.category.key);
    document
      .querySelector("#orders")
      .replaceChildren(...orders.map(renderOrder));
  });
</script>
```

## Your own modal

`eunchartselect` is cancelable: `preventDefault()` keeps the details modal closed, to open your own view instead (a drawer, a page, a filter), while `details` keeps the hint and the pointer.

```js
chart.addEventListener("eunchartselect", (event) => {
  event.preventDefault();
  openOrdersDrawer(event.category.key);
});
```

Without any modal at all, `selectable` makes the categories clickable and fires `eunchartselect`, with no hint unless `select-hint` writes one.

```html
<eun-bar-chart selectable select-hint="Click to filter the orders"></eun-bar-chart>
```

## Custom appearance

The modal is the `details` part: it takes the `eun-modal` custom properties, such as `--modal-width` (560px at most by default), and any other rule.

```css
.orders-chart::part(details) {
  --modal-width: 720px;
  --modal-border-radius: 16px;
}
```

`select-hint` rewrites the tooltip's hint.

```html
<eun-bar-chart details select-hint="Click to list the orders"></eun-bar-chart>
```

## Keyboard and accessibility

- Once the plot is focused, the arrow keys move from one category to the next, and Enter or Space opens the details, as a click does.
- The modal is an `eun-modal`: the focus moves into it, and Escape closes it.
- Its values are written in full, as in the data table read by screen readers.
