# Shadow DOM

[Sirv Media Viewer](/help/sirv-media-viewer/) supports the use of Shadow DOM, allowing hidden DOM trees to be attached to elements in the regular DOM tree.

Using an AI coding agent?

Install the [`sirv-media-viewer`](https://www.skills.sh/sirv/image-optimization-skill/sirv-media-viewer) skill, and your agent can add the viewer to your site. Ask in plain words, such as “Add a gallery with zoom and a [360 spin](/help/spin/getting-started/how-spin-files-are-created/) to the product page.” [See all 8 skills](/help/rest-api/sirv-skills-for-ai-agents/)

The shadow DOM tree starts with a shadow root, under which you can attach to any elements you wish, in the same way as the normal DOM.

## Usage

### 1\. Include the latest sirv.js

```html
<script src="https://scripts.sirv.com/sirvjs/v3/sirv.js"></script>
```

### 2\. Create your web components

To start/stop a Sirv Media Viewer instance within Shadow DOM, you should pass the context of a Shadow DOM in a second argument to the **Sirv.start()** and **Sirv.stop()** methods.

#### For open Shadow DOM

```html
<script type="text/javascript">
  customElements.define('sirv-component-open',
    class extends HTMLElement {
      constructor() {
        super();
        const template = document.getElementById('sirv-component');
        const templateContent = template.content;
        this.template = templateContent

        this.attachShadow({ mode: 'open' })
          .appendChild(templateContent.cloneNode(true));

        this.start();
      }

        start() {
          Sirv.start('.Sirv', this.shadowRoot);
        }

        stop() {
          Sirv.stop('.Sirv', this.shadowRoot);
        }

        nextSlide() {
          const slider = Sirv.getInstance('.Sirv', 'viewer', this.shadowRoot);
        slider.next();
      }
    }
  );
</script>
```

#### For closed Shadow Dom

```html
<script type="text/javascript">
  customElements.define('sirv-component-closed',
    class extends HTMLElement {
      constructor() {
        super();
        const template = document.getElementById('sirv-component');
        const templateContent = template.content;
        this.template = templateContent

        this._shadowRoot = this.attachShadow({ mode: 'closed' })
        this._shadowRoot.appendChild(templateContent.cloneNode(true));

        this.start();
      }

      start() {
        Sirv.start('.Sirv', this._shadowRoot);
      }

      stop() {
        Sirv.stop('.Sirv', this._shadowRoot);
      }

      nextSlide() {
        const slider = Sirv.getInstance('.Sirv', 'viewer', this._shadowRoot);
        slider.next();
      }
    }
  );
</script>
```

### 3\. Create your template component

```html
<template id="sirv-component">
  <div class="Sirv">
    <div data-src="https://demo.sirv.com/demo/chacos/chacos.spin"></div>
    <div data-type="zoom" data-src="https://demo.sirv.com/demo/chacos/still-1.jpg"></div>
    <div data-type="zoom" data-src="https://demo.sirv.com/demo/chacos/still-2.jpg"></div>
    <div data-type="zoom" data-src="https://demo.sirv.com/demo/chacos/still-3.jpg"></div>
  </div>
</template>
```

### 4\. Use your components

#### Sirv Media Viewer inside an “open” Shadow DOM

```html
<sirv-component-open></sirv-component-open>
```

#### Sirv Media Viewer inside a “closed” Shadow DOM

```html
<sirv-component-closed></sirv-component-closed>
```

In both modes, the viewer works the same as it does on a normal page.
