> ## Documentation Index
> Fetch the complete documentation index at: https://docs.beyondwords.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Playlist

> Render the player's content list and let users jump between items.

> [tab] Preview

> [tab] Code
>
> ```js Playlist.js theme={null}
> import { newEvent } from "@/lib/newEvent";
> import { nowPlayingMarkerSvg } from "@/registry/html/icons";
> import { BwPlayerConsumer } from "@/registry/html/PlayerProvider";
>
> export class BwPlaylist extends BwPlayerConsumer {
>   _list = null;
>   _listener = null;
>   _content = null;
>   _contentIndex = null;
>
>   constructor() {
>     super();
>     this._list = document.createElement("ul");
>     this._list.style.listStyle = "none";
>     this._list.style.margin = "0";
>     this._list.style.padding = "0";
>   }
>
>   #render() {
>     const content = this._player?.content ?? [];
>     const contentIndex = this._player?.contentIndex;
>
>     if (content === this._content && contentIndex === this._contentIndex) return;
>     this._content = content;
>     this._contentIndex = contentIndex;
>
>     this._list.replaceChildren(
>       ...content.map((item, index) => {
>         const btn = document.createElement("button");
>         btn.setAttribute("aria-current", contentIndex === index);
>         btn.setAttribute("aria-label", `Play ${item.title}`);
>         btn.style.display = "flex";
>         btn.style.alignItems = "center";
>         btn.style.gap = "0.5rem";
>         btn.style.width = "100%";
>         btn.addEventListener("click", () => {
>           if (!this._player) return;
>           this._player.onEvent(
>             newEvent({
>               type: "PressedPlaylistItem",
>               description: "A playlist item was pressed.",
>               index,
>             }),
>           );
>         });
>
>         const marker = document.createElement("span");
>         marker.style.display = "inline-flex";
>         marker.style.width = "1rem";
>         marker.style.justifyContent = "center";
>         if (contentIndex === index) {
>           marker.innerHTML = nowPlayingMarkerSvg(16);
>         } else {
>           marker.textContent = index + 1;
>         }
>
>         const title = document.createElement("span");
>         title.textContent = item.title;
>
>         btn.append(marker, title);
>
>         const li = document.createElement("li");
>         li.appendChild(btn);
>         return li;
>       }),
>     );
>   }
>
>   connectedCallback() {
>     super.connectedCallback();
>     this._listener = this._player?.addEventListener("<any>", () => this.#render());
>     this.#render();
>     this.appendChild(this._list);
>   }
>
>   disconnectedCallback() {
>     this._player?.removeEventListener("<any>", this._listener);
>     this._listener = null;
>     super.disconnectedCallback();
>   }
> }
>
> customElements.define("bw-playlist", BwPlaylist);
> ```

## Installation

```bash npm theme={null}
npx @beyondwords/cli components add playlist --html
```

```bash pnpm theme={null}
pnpm dlx @beyondwords/cli components add playlist --html
```

```bash yarn theme={null}
yarn dlx @beyondwords/cli components add playlist --html
```

```bash bun theme={null}
bunx --bun @beyondwords/cli components add playlist --html
```

## Usage

Import the modules (each calls `customElements.define`), then use the elements. Nest the playlist inside a `<bw-player-provider>`.

```html theme={null}
<bw-player-provider
  data-project-id="9504"
  data-playlist-id="153102"
  data-show-user-interface="false"
>
  <bw-playlist></bw-playlist>
</bw-player-provider>

<script type="module">
  import "@/registry/html/PlayerProvider";
  import "@/registry/html/Playlist";
</script>
```

`<bw-playlist>` finds its nearest
[`<bw-player-provider>`](https://docs.beyondwords.io/components/html/player-provider) ancestor and maps over
its `content`. It renders a button per item and marks the active one
(`contentIndex`) with a now-playing icon. Pressing an item emits a `PressedPlaylistItem`
event (built with [`newEvent`](https://docs.beyondwords.io/components/html/new-event)) carrying its `index`.

> [!NOTE]
>
> The event is emitted rather than setting `player.contentIndex` directly. This
> lets the player handle ads and outros correctly when switching tracks.

> [!WARNING]
>
> These components expect the BeyondWords player to be available as a global
> `BeyondWords` (the player UMD script). Load it before the provider connects.

## Reference

| | |
| - | - |
| **Attributes** | None |
| **Player state read** | `content`, `contentIndex` |
| **Events emitted** | `PressedPlaylistItem` (carries the item's `index`) |
| **Requires** | a [`<bw-player-provider>`](https://docs.beyondwords.io/components/html/player-provider) ancestor and the player UMD global loaded |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.