Skip to content

Shadow DOM

By , Customer Success DirectorUpdated

Open this page in a new chat

Short answer

Sirv Media Viewer works inside Shadow DOM, both open and closed. Include the latest sirv.js, then pass the shadow root as the second argument to Sirv.start() and Sirv.stop() inside your web component, so the viewer starts within it. This page shows the code for a template component and for using it in each mode.

After reading this you will know

  • How to start Sirv Media Viewer inside open or closed Shadow DOM
  • How to build and use the web components

Sirv Media Viewer supports the use of Shadow DOM, allowing hidden DOM trees to be attached to elements in the regular DOM tree.

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.

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

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.

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

Sirv Media Viewer inside an “open” Shadow DOM

Section titled “Sirv Media Viewer inside an “open” Shadow DOM”
<sirv-component-open></sirv-component-open>

Sirv Media Viewer inside a “closed” Shadow DOM

Section titled “Sirv Media Viewer inside a “closed” Shadow DOM”
<sirv-component-closed></sirv-component-closed>

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

Back to top