# Elevation

Elevation says which layer is on top. Five levels, each a
[`box-shadow`](https://developer.mozilla.org/en-US/docs/Web/CSS/box-shadow) paired with a surface color:

| Level | Role token | Used by |
| --- | --- | --- |
| `--dui-elevation-0` | `--dui-elevation-flat` | The page and content on it |
| `--dui-elevation-1` | `--dui-elevation-raised` | Cards |
| `--dui-elevation-2` | `--dui-elevation-overlay` | Toasts |
| `--dui-elevation-3` | `--dui-elevation-popover` | Popovers, menus, tooltips, nav dropdowns |
| `--dui-elevation-4` | `--dui-elevation-dialog` | Dialogs and drawers |
| `--dui-elevation-1` | `--dui-elevation-panel` | The app shell's main content panel on the frame, on `--dui-elevation-panel-surface` (the page surface) |

Each has a `-surface` twin (`--dui-elevation-2-surface`, `--dui-elevation-overlay-surface`).
**Components use the role tokens only**, never the numbers, so a theme can move a role to another level.

- **A ring plus soft layers.** Every level from 1 up is a 1px ring in the hairline color
  (`--dui-elevation-ring`) plus one or two layers with a negative spread, at 12% alpha or less in
  light: level 1 is `0 1px 2px -1px`; level 2 `0 2px 4px -2px` and `0 4px 12px -4px`; level 3
  `0 4px 8px -4px` and `0 12px 24px -8px`; level 4 `0 8px 16px -8px` and `0 24px 48px -12px`. The ring is
  part of the role, so **an elevated component draws no border of its own**. It keeps a transparent
  border for forced colors (below). Radix Themes builds its shadows the same way
  ([shadows](https://www.radix-ui.com/themes/docs/theme/shadows)), for comparison.
- **Light scheme: the shadow does the work.** Every level keeps `--dui-color-surface`. Shadows grow
  with the level.
- **Dark scheme: lighter surfaces.** Shadows barely show on a dark page, so each level mixes
  `--dui-elevation-surface-step` more of the text color into the surface with
  [`color-mix()`](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/color-mix)
  ([CSS Color 5](https://www.w3.org/TR/css-color-5/#color-mix)), picked per scheme by
  [`light-dark()`](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/light-dark).
  Levels 2 to 4 add a 1px top inset highlight (`--dui-elevation-highlight`) in dark only, and the ring
  reads slightly brighter because the dark hairline is the light text color at low alpha.
  Text, muted text, links, control borders and the focus ring meet
  [4.5:1](https://www.w3.org/WAI/WCAG22/Understanding/contrast-minimum.html) and
  [3:1](https://www.w3.org/WAI/WCAG22/Understanding/non-text-contrast.html) on every level, in light and
  dark, in the default and forest themes. `tests/tokens.spec.js` checks it.
- **Theme knobs.** `--dui-color-shadow` (shadow color), `--dui-elevation-strength` (0% to 100% of it) and
  `--dui-elevation-surface-step` (dark-scheme step per level). The levels are re-derived on every theme
  scope, like the other dependent tokens.
- **Forced colors: borders, not shadows.** In [forced colors mode](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/forced-colors)
  `box-shadow` computes to `none` ([CSS Color Adjustment](https://www.w3.org/TR/css-color-adjust-1/#forced-colors-properties)),
  so every elevated component keeps a transparent border, which forced colors paints in a system
  color (`CanvasText`) there.
- **Floating panels.** `--dui-elevation-panel` and `--dui-elevation-panel-surface`, with
  `--dui-radius-surface` corners and `--dui-space-panel-inset` from the frame edge, are for the app
  shell's main content panel. The app bar and the sidebars sit flat on `--dui-color-surface-frame`
  around it, with no hairlines. **Our choice:** one inset content panel on a quiet frame, as
  [Linear](https://linear.app/blog/how-we-redesigned-the-linear-ui) draws its app, because a single
  edge separates the chrome from the content with less noise than floating every sidebar.
- **Our choice:** toasts sit at the overlay level, below popovers and dialogs, because they are
  non-modal and must not cover an open menu or dialog.
- **Our choice:** the dark-scheme step mixes in the text color rather than white, so a theme's tint
  carries into its raised surfaces.

`--dui-shadow-raised` is deprecated: it now points at `--dui-elevation-1` and is removed in the next
release. [`examples/elevation.html`](https://domestic-ui.odonckers.dev/principles/elevation.md) shows every level in light and dark side by side.

## Live example

### Levels in light and dark

```html
<div class="grid">
  <section class="panel stack" data-color-scheme="light" aria-labelledby="light-title">
    <h3 id="light-title">Light</h3>
    <ol class="stack" aria-label="Elevation levels, light">
      <li class="level" style="--level-shadow: var(--dui-elevation-0); --level-surface: var(--dui-elevation-0-surface)">
        <strong>Level 0: flat</strong>
        <span class="level-note">The page itself and content on it. <a href="#levels-title">Link</a></span>
      </li>
      <li class="level" style="--level-shadow: var(--dui-elevation-1); --level-surface: var(--dui-elevation-1-surface)">
        <strong>Level 1: raised</strong>
        <span class="level-note">Cards. <a href="#levels-title">Link</a></span>
      </li>
      <li class="level" style="--level-shadow: var(--dui-elevation-2); --level-surface: var(--dui-elevation-2-surface)">
        <strong>Level 2: overlay</strong>
        <span class="level-note">Toasts and other non-modal layers. <a href="#levels-title">Link</a></span>
      </li>
      <li class="level" style="--level-shadow: var(--dui-elevation-3); --level-surface: var(--dui-elevation-3-surface)">
        <strong>Level 3: popover</strong>
        <span class="level-note">Popovers, menus, tooltips, nav dropdowns. <a href="#levels-title">Link</a></span>
      </li>
      <li class="level" style="--level-shadow: var(--dui-elevation-4); --level-surface: var(--dui-elevation-4-surface)">
        <strong>Level 4: dialog</strong>
        <span class="level-note">Dialogs and drawers. <a href="#levels-title">Link</a></span>
      </li>
      <li class="level" style="--level-shadow: var(--dui-elevation-panel); --level-surface: var(--dui-elevation-panel-surface)">
        <strong>Role: panel</strong>
        <span class="level-note">Floating sidebars and side panels: the level 2 shadow on the raised surface. <a href="#levels-title">Link</a></span>
      </li>
    </ol>
  </section>
  <section class="panel stack" data-color-scheme="dark" aria-labelledby="dark-title">
    <h3 id="dark-title">Dark</h3>
    <ol class="stack" aria-label="Elevation levels, dark">
      <li class="level" style="--level-shadow: var(--dui-elevation-0); --level-surface: var(--dui-elevation-0-surface)">
        <strong>Level 0: flat</strong>
        <span class="level-note">The page itself and content on it. <a href="#levels-title">Link</a></span>
      </li>
      <li class="level" style="--level-shadow: var(--dui-elevation-1); --level-surface: var(--dui-elevation-1-surface)">
        <strong>Level 1: raised</strong>
        <span class="level-note">Cards. <a href="#levels-title">Link</a></span>
      </li>
      <li class="level" style="--level-shadow: var(--dui-elevation-2); --level-surface: var(--dui-elevation-2-surface)">
        <strong>Level 2: overlay</strong>
        <span class="level-note">Toasts and other non-modal layers. <a href="#levels-title">Link</a></span>
      </li>
      <li class="level" style="--level-shadow: var(--dui-elevation-3); --level-surface: var(--dui-elevation-3-surface)">
        <strong>Level 3: popover</strong>
        <span class="level-note">Popovers, menus, tooltips, nav dropdowns. <a href="#levels-title">Link</a></span>
      </li>
      <li class="level" style="--level-shadow: var(--dui-elevation-4); --level-surface: var(--dui-elevation-4-surface)">
        <strong>Level 4: dialog</strong>
        <span class="level-note">Dialogs and drawers. <a href="#levels-title">Link</a></span>
      </li>
      <li class="level" style="--level-shadow: var(--dui-elevation-panel); --level-surface: var(--dui-elevation-panel-surface)">
        <strong>Role: panel</strong>
        <span class="level-note">Floating sidebars and side panels: the level 2 shadow on the raised surface. <a href="#levels-title">Link</a></span>
      </li>
    </ol>
  </section>
</div>
```

### Level swatch CSS

Each `.level` swatch draws one elevation level from two custom properties: `--level-shadow` (its `box-shadow`) and `--level-surface` (its background).

In the `<head>` of the live example above.

```html
<style>
  .level {
    display: flex;
    flex-direction: column;
    gap: var(--dui-space-3xs);
    padding: var(--dui-space-m);
    background-color: var(--level-surface);
    color: var(--dui-color-text);
    border: var(--dui-border-width) solid transparent;
    border-radius: var(--dui-radius-surface);
    box-shadow: var(--level-shadow);
  }

  .level-note {
    color: var(--dui-color-text-muted);
  }

  @media (forced-colors: active) {
    .level {
      border-color: canvastext;
    }
  }
</style>
```

### Components by role

```html
<div class="stack">
  <article data-card>
    <h3>Raised: a card</h3>
    <p>Level 1. <a href="#roles-title">A link on the card</a>.</p>
  </article>
  <div class="cluster">
    <button type="button" popovertarget="role-popover">Popover</button>
    <div id="role-popover" popover data-popover>
      <p>Level 3, the popover level.</p>
    </div>
    <dui-menu>
      <button type="button" popovertarget="role-menu">Menu</button>
      <div id="role-menu" popover data-popover>
        <button type="button">Rename</button>
        <button type="button">Archive</button>
      </div>
    </dui-menu>
    <button type="button" commandfor="role-dialog" command="show-modal">Dialog</button>
    <button type="button" data-toast="info">Toast</button>
  </div>
  <dialog id="role-dialog" data-dialog aria-labelledby="role-dialog-title">
    <header>
      <h2 id="role-dialog-title">Level 4: dialog</h2>
    </header>
    <p>The highest level, with the lightest surface in the dark scheme.</p>
    <form method="dialog">
      <footer>
        <button type="submit" autofocus>Close</button>
      </footer>
    </form>
  </dialog>
  <dui-toaster role="status"></dui-toaster>
</div>
```
