# media-volume-indicator

Display temporary visual feedback for keyboard and gesture volume actions

## Import

```ts
import '@videojs/html/ui/volume-indicator';
import '@videojs/html/ui/volume-indicator-fill';
import '@videojs/html/ui/volume-indicator-value';
```

## Anatomy

```html
<media-volume-indicator>
  <media-volume-indicator-fill>
    <media-volume-indicator-value></media-volume-indicator-value>
  </media-volume-indicator-fill>
</media-volume-indicator>
```

## Behavior

`<media-volume-indicator>` displays feedback for `toggleMuted` and `volumeStep` actions emitted by a [`<media-hotkey>`](https://videojs.org/docs/framework/html/reference/components/hotkey) or [`<media-gesture>`](https://videojs.org/docs/framework/html/reference/components/gesture) in the same player `<media-container>`. It does not react to mute buttons, volume sliders, direct player-store changes, or volume changes on their own.

The input event arrives before its action is resolved. The indicator uses the media snapshot from that moment to predict the next mute state and volume. `toggleMuted` predicts the opposite mute state. `volumeStep` adds its `value` to the snapshot volume and clamps the result from 0 to 1. A step whose clamped result is above 0 also predicts that mute will clear.

The root exposes the predicted level:

| `data-level` | Predicted state |
| --- | --- |
| `"off"` | Muted or volume is 0 |
| `"low"` | Unmuted volume is greater than 0 and at most 0.5 |
| `"high"` | Unmuted volume is greater than 0.5 |

`<media-volume-indicator-value>` displays the rounded percentage from 0% through 100%. `<media-volume-indicator-fill>` receives the same percentage through `--media-volume-fill`. Nest the value inside the fill so the progress treatment and text form one visual unit.

The indicator closes after `closeDelay`, which defaults to 800 milliseconds. Repeated handled actions update the current value and restart that close timer without replaying the entry transition. Each update uses the latest media snapshot; the component does not independently accumulate volume steps.

The element remains in the document with `hidden` until the next handled action.

When a nonzero `volumeStep` cannot move past an already-clamped edge, `data-min` or `data-max` is present for 300 milliseconds. Hitting the same edge again briefly clears the attribute, then restores it on the next task so a CSS boundary animation can restart. Merely reaching 0% or 100% does not trigger the boundary attribute until another step tries to move farther.

## Styling

| Attribute | Values | Description |
| --- | --- | --- |
| `data-open` | Present / absent | Present while the indicator is open |
| `data-level` | `"off"`, `"low"`, or `"high"` | Predicted volume level |
| `data-min` | Present / absent | Present briefly after a blocked downward step at minimum volume |
| `data-max` | Present / absent | Present briefly after a blocked upward step at maximum volume |
| `data-starting-style` | Present / absent | Present during the open transition |
| `data-ending-style` | Present / absent | Present during the close transition |

`--media-volume-fill` is set on `<media-volume-indicator-fill>`, not `<media-volume-indicator>`. Read it from a fill selector when sizing a progress treatment.

```css
media-volume-indicator-fill::before {
  width: var(--media-volume-fill, 0%);
}

media-volume-indicator[data-starting-style],
media-volume-indicator[data-ending-style] {
  opacity: 0;
}
```

## Accessibility

`<media-volume-indicator>` is visual feedback and does not create a live region. Keep volume and mute available through keyboard-operable controls, and pair the player with [`<media-status-announcer>`](https://videojs.org/docs/framework/html/reference/components/status-announcer) when state changes should be announced to screen readers. Do not make `<media-volume-indicator-value>` a live region.

## Examples

### Basic Usage

Focus the player, then press M to mute or unmute. Use Arrow Up and Arrow Down to adjust volume by 5%.

**index.html**

```html
<video-player>
  <media-container class="html-volume-indicator-basic" tabindex="0">
    <video src="https://stream.mux.com/BV3YZtogl89mg9VcNBhhnHm02Y34zI1nlMuMQfAbl3dM/highest.mp4" autoplay muted playsinline loop></video>
    <p class="html-volume-indicator-basic__instructions">Focus the player · M: mute · ↑/↓: volume ±5%</p>
    <media-volume-indicator class="html-volume-indicator-basic__indicator" aria-hidden="true">
      <media-volume-indicator-fill class="html-volume-indicator-basic__fill">
        <media-volume-indicator-value class="html-volume-indicator-basic__value"></media-volume-indicator-value>
      </media-volume-indicator-fill>
    </media-volume-indicator>
    <media-hotkey keys="m" action="toggleMuted"></media-hotkey>
    <media-hotkey keys="ArrowUp" action="volumeStep" value="0.05"></media-hotkey>
    <media-hotkey keys="ArrowDown" action="volumeStep" value="-0.05"></media-hotkey>
  </media-container>
</video-player>
```

**index.css**

```css
.html-volume-indicator-basic {
  position: relative;
}

.html-volume-indicator-basic:focus-visible {
  outline: 3px solid #60a5fa;
  outline-offset: 2px;
}

.html-volume-indicator-basic video {
  display: block;
  width: 100%;
}

.html-volume-indicator-basic__instructions {
  position: absolute;
  top: 10px;
  left: 50%;
  padding: 6px 10px;
  margin: 0;
  color: white;
  text-align: center;
  background: rgb(0 0 0 / 70%);
  border-radius: 4px;
  translate: -50%;
}

.html-volume-indicator-basic__indicator {
  position: absolute;
  top: 50%;
  left: 50%;
  display: grid;
  min-width: 220px;
  padding: 18px 20px 34px;
  color: white;
  pointer-events: none;
  background: rgb(0 0 0 / 72%);
  border-radius: 12px;
  place-items: center;
  translate: -50% -50%;
  transition:
    opacity 160ms ease-in-out,
    scale 160ms ease-in-out;
}

.html-volume-indicator-basic__indicator::before {
  margin-bottom: 12px;
  font-size: 28px;
  line-height: 1;
}

.html-volume-indicator-basic__indicator[data-level="off"]::before {
  content: "🔇";
}

.html-volume-indicator-basic__indicator[data-level="low"]::before {
  content: "🔉";
}

.html-volume-indicator-basic__indicator[data-level="high"]::before {
  content: "🔊";
}

.html-volume-indicator-basic__fill {
  position: relative;
  width: 180px;
  height: 10px;
  background: rgb(255 255 255 / 25%);
  border-radius: 9999px;
}

.html-volume-indicator-basic__fill::before {
  position: absolute;
  inset-block: 0;
  left: 0;
  width: var(--media-volume-fill, 0%);
  content: "";
  background: currentColor;
  border-radius: inherit;
  transition: width 160ms linear;
}

.html-volume-indicator-basic__value {
  position: absolute;
  top: calc(100% + 6px);
  left: 50%;
  font-variant-numeric: tabular-nums;
  translate: -50%;
}

.html-volume-indicator-basic__indicator[data-starting-style],
.html-volume-indicator-basic__indicator[data-ending-style] {
  opacity: 0;
  scale: 0.85;
}

@media (prefers-reduced-motion: no-preference) {
  .html-volume-indicator-basic__indicator[data-min],
  .html-volume-indicator-basic__indicator[data-max] {
    animation: html-volume-indicator-basic-shake 300ms linear;
  }
}

@keyframes html-volume-indicator-basic-shake {
  20% {
    translate: calc(-50% - 8px) -50%;
  }

  40% {
    translate: calc(-50% + 6px) -50%;
  }

  60% {
    translate: calc(-50% - 4px) -50%;
  }

  80% {
    translate: calc(-50% + 2px) -50%;
  }
}
```

**index.ts**

```ts
import '@videojs/html/video/player';
import '@videojs/html/ui/container';
import '@videojs/html/ui/hotkey';
import '@videojs/html/ui/volume-indicator';
import '@videojs/html/ui/volume-indicator-fill';
import '@videojs/html/ui/volume-indicator-value';

const initializedVideos = new WeakSet<HTMLVideoElement>();

function initializeDemos(): void {
  document.querySelectorAll<HTMLVideoElement>('.html-volume-indicator-basic video').forEach((video) => {
    if (initializedVideos.has(video)) return;

    initializedVideos.add(video);
    video.volume = 0.5;
  });
}

initializeDemos();
document.addEventListener('astro:page-load', initializeDemos);
```

## API Reference

### media-volume-indicator

#### Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `closeDelay` (attribute `close-delay`) | `number` | — | Delay in milliseconds before the indicator closes. |

#### State

State is reflected as data attributes for CSS styling.

| Property | Type | Description |
| --- | --- | --- |
| `transitionStarting` | `boolean` | Whether the open transition is in progress. |
| `transitionEnding` | `boolean` | Whether the close transition is in progress. |
| `open` | `boolean` | Whether the indicator is open. |
| `generation` | `number` | Increments each time a volume input action updates the indicator. |
| `level` | `'off' \| 'low' \| 'high' \| null` | Predicted volume level after the input action. |
| `value` | `string \| null` | Predicted volume formatted as a percentage. |
| `fill` | `string \| null` | Predicted volume percentage used by the Fill part. |
| `min` | `boolean` | Whether a downward step tried to move past minimum volume. |
| `max` | `boolean` | Whether an upward step tried to move past maximum volume. |

#### Data attributes

| Attribute | Type | Description |
| --- | --- | --- |
| `data-open` | — | Present while the indicator is open. |
| `data-level` | `'off' \| 'low' \| 'high' \| null` | Predicted volume level as `"off"`, `"low"`, or `"high"`. |
| `data-min` | — | Present briefly when a downward step cannot lower the volume further. |
| `data-max` | — | Present briefly when an upward step cannot raise the volume further. |
| `data-starting-style` | — | Present during the open transition. |
| `data-ending-style` | — | Present during the close transition. |

#### CSS custom properties

| Variable | Description |
| --- | --- |
| `--media-volume-fill` | Current predicted volume percentage, set on the Fill part. |

### media-volume-indicator-fill

### media-volume-indicator-value

---

HTML documentation: https://videojs.org/docs/framework/html/llms.txt
All documentation: https://videojs.org/llms.txt
