> ## 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.

# Feature Image

> Displays the feature image of the current content item.

> [tab] Preview

> [tab] Code
>
> ```js FeatureImage.js theme={null}
> import { BwPlayerConsumer } from "@/registry/html/PlayerProvider";
>
> export class BwFeatureImage extends BwPlayerConsumer {
>   _img = null;
>   _listener = null;
>
>   constructor() {
>     super();
>
>     this._img = document.createElement("img");
>     this._img.style.maxWidth = "100%";
>     this._img.style.objectFit = "contain";
>   }
>
>   #render() {
>     const item = this._player?.content?.[this._player.contentIndex];
>     if (!item?.imageUrl) {
>       this._img.remove();
>       return;
>     }
>     if (this._img.getAttribute("src") !== item.imageUrl) this._img.setAttribute("src", item.imageUrl);
>     this._img.setAttribute("alt", item.title ?? "");
>     if (!this._img.isConnected) this.appendChild(this._img);
>   }
>
>   connectedCallback() {
>     super.connectedCallback();
>     this._listener = this._player?.addEventListener("<any>", () => this.#render());
>     this.#render();
>   }
>
>   disconnectedCallback() {
>     this._player?.removeEventListener("<any>", this._listener);
>     this._listener = null;
>     super.disconnectedCallback();
>   }
> }
>
> customElements.define("bw-feature-image", BwFeatureImage);
> ```

## Installation

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

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

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

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

## Usage

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

```html theme={null}
<bw-player-provider
  data-project-id="9504"
  data-content-id="ba00ef51-03ac-4a85-8b98-e45c957e8ef8"
  data-show-user-interface="false"
>
  <bw-feature-image></bw-feature-image>
</bw-player-provider>

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

`<bw-feature-image>` finds its nearest
[`<bw-player-provider>`](https://docs.beyondwords.io/components/html/player-provider) ancestor, reads `content`
and `contentIndex` from it, and renders the current item's `imageUrl` as an
`<img>` (using its `title` as the `alt` text), updating on every player event.
It renders nothing when the item has no image, so it's safe to drop in
unconditionally.

> [!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** | None (display only) |
| **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.