Viewer
A viewer component for embedded PDFs, HTML documents and images inside Monster layouts.
import { Viewer } from "@schukai/monster/source/components/content/viewer.mjs";Introduction
The Monster Viewer embeds documents and media directly into the page. Use it for PDFs, HTML previews or images that should remain inside the surrounding Monster interface.
When to use Viewer
- Use it for inline previewing: Especially useful for document-centric admin and review flows.
- Use it when context around the content matters: Surrounding actions can stay visible while the content is viewed.
- Do not use it for interactive editing of remote apps: Embedded application runtimes may need an iframe-based approach instead.
With A PDF
The viewer is a simple control that allows you to display PDF documents, images, and HTML content seamlessly. Internally, it does not use a custom PDF viewer but instead relies on the browser's capabilities via the <object> tag. Browsers that do not support this functionality will therefore not work.
In this example, a PDF file is loaded via a URL and displayed in the control. The URL can also be passed directly to the control.
Javascript
import '@schukai/monster/source/components/content/viewer.mjs';
/**
* A string representing the relative URL path to the PDF file.
* This variable specifies the location of the "monster.pdf" file within
* the assets/examples directory.
*/
const pdfUrl = "/assets/examples/monster.pdf";
/**
* A boolean variable indicating the presence or absence of a scrollbar.
*
* When set to `true`, the scrollbar is enabled, allowing the user to scroll through
* a content area. When set to `false`, the scrollbar is disabled, restricting scrolling.
*
* This variable can be used to toggle the visibility or functionality of a scrollbar
* in a user interface or a specific component.
*/
const navigation = true, toolbar = true, scrollbar = false;
fetch(pdfUrl)
.then(response => {
if (!response.ok) {
throw new Error('Error');
}
return response.blob();
})
.then(
pdfData => {
document.querySelector("monster-viewer").setPDF(pdfData, navigation, toolbar, scrollbar);
})
.catch(e => console.log('Error!'));<script type="module">//import '@schukai/monster/source/components/content/viewer.mjs';
const pdfUrl = "/assets/examples/monster.pdf";
const viewerId = "uDu5f3";
const navigation = true, toolbar = true, scrollbar = false;
fetch(pdfUrl)
.then(response => {
if (!response.ok) {
throw new Error('Netzwerkantwort war nicht ok');
}
return response.blob();
})
.then(pdfData => {
//const pdfUrl = URL.createObjectURL(pdfData);
document.getElementById(viewerId).setPDF(pdfData, navigation, toolbar, scrollbar);
})
.catch(e => console.log('Error!'));</script>HTML
<monster-viewer>
</monster-viewer>Stylesheet
/** no additional stylesheet is defined **/With An Image
The viewer is a simple control that allows you to display PDF documents, images, and HTML content seamlessly.
In this example, a image file is loaded via a URL and displayed in the control. The URL can also be passed directly to the control.
Javascript
import '@schukai/monster/source/components/content/viewer.mjs';
const url = "/assets/examples/monster.png";
fetch(url)
.then(response => {
if (!response.ok) {
throw new Error('Error');
}
return response.blob();
})
.then(data => {
document.querySelector("monster-viewer").setImage(data)
})
.catch(e => console.log('Error!'));<script type="module">//import '@schukai/monster/source/components/content/viewer.mjs';
const pdfUrl = "/assets/examples/monster.png";
const viewerId = "uDu5f2";
const navigation = true, toolbar = true, scrollbar = false;
fetch(pdfUrl)
.then(response => {
if (!response.ok) {
throw new Error('Error');
}
return response.blob();
})
.then(pdfData => {
//const pdfUrl = URL.createObjectURL(pdfData);
document.getElementById(viewerId).setPDF(pdfData, navigation, toolbar, scrollbar);
})
.catch(e => console.log('Error!'));</script>HTML
<monster-viewer>
</monster-viewer>Stylesheet
/** no additional stylesheet is defined **/With HTML Content
The viewer is a simple control that allows you to display PDF documents, images, and HTML content seamlessly.
In this example, a html file is loaded via a URL and displayed in the control. The URL can also be passed directly to the control.
Javascript
import '@schukai/monster/source/components/content/viewer.mjs';
const url = "/assets/examples/monster.html";
fetch(url)
.then(response => {
if (!response.ok) {
throw new Error('Error');
}
return response.blob();
})
.then(data => {
document.querySelector("monster-viewer").setHTML(data);
})
.catch(e => console.log('Error!'));<script type="module">//import '@schukai/monster/source/components/content/viewer.mjs';
const pdfUrl = "/assets/examples/monster.html";
const viewerId = "uDu5f1";
const navigation = true, toolbar = true, scrollbar = false;
fetch(pdfUrl)
.then(response => {
if (!response.ok) {
throw new Error('Error');
}
return response.blob();
})
.then(pdfData => {
//const pdfUrl = URL.createObjectURL(pdfData);
document.getElementById(viewerId).setPDF(pdfData, navigation, toolbar, scrollbar);
})
.catch(e => console.log('Error!'));</script>HTML
<monster-viewer>
</monster-viewer>Stylesheet
/** no additional stylesheet is defined **/Component Design
This component is built using the Shadow DOM, which allows it to encapsulate its internal structure and styling. By using a shadow root, the component's internal elements are isolated from the rest of the webpage, ensuring that external styles or scripts cannot accidentally modify its internal layout or behavior.
Shadow DOM and Accessibility
Since the component is encapsulated within a shadow root, direct access to its internal elements is restricted. This means that developers cannot manipulate or style these elements from outside the component. The Shadow DOM helps maintain consistency in the design and behavior of the component by preventing external interference.
Customizing Through Exported Parts
While the Shadow DOM restricts direct access to the component's internal structure, customization is still possible through exported parts. Specific parts of the component are made accessible for styling by being explicitly marked for export. These exported parts can be targeted and customized using CSS, allowing you to modify the appearance of the component without compromising its encapsulation.
Available Part Attributes
viewer: This part represents the entire viewer area, where the content (e.g., PDF, HTML, or image) is displayed. Use this to style the layout or appearance of the viewer.
Below is an example of how to use CSS part attributes to customize different parts of the viewer.
monster-viewer::part(viewer) {
background-color: #ffffff;
border: 1px solid #ccc;
border-radius: 5px;
}
Explanation of the Example
monster-viewer::part(viewer): Styles the viewer area, giving it a white background, a border, and slightly rounded corners.
Accessibility
Accessibility is a key consideration in the design of this component. By following best practices for web accessibility, the component ensures that users of all abilities can interact with it effectively. This includes support for keyboard navigation, screen readers, and other assistive technologies to provide an inclusive user experience.
HTML Structure
<monster-viewer></monster-viewer>JavaScript Initialization
const element = document.createElement('monster-viewer');
document.body.appendChild(element);Exported
ViewerDerived from
CustomElementOptions
The Options listed in this section are defined directly within the class. This class is derived from several parent classes, including the CustomElement class. Therefore, it inherits Options from these parent classes. If you cannot find a specific Options in this list, we recommend consulting the documentation of the CustomElement.
- since
- deprecated
Properties and Attributes
The Properties and Attributes listed in this section are defined directly within the class. This class is derived from several parent classes, including the CustomElement class and ultimately from HTMLElement. Therefore, it inherits Properties and Attributes from these parent classes. If you cannot find a specific Properties and Attributes in this list, we recommend consulting the documentation of the CustomElement.
data-monster-options: Sets the configuration options for the collapse component when used as an HTML attribute.data-monster-option-[name]: Sets the value of the configuration option[name]for the collapse component when used as an HTML attribute.
Methods
The methods listed in this section are defined directly within the class. This class is derived from several parent classes, including the CustomElement class and ultimately from HTMLElement. Therefore, it inherits methods from these parent classes. If you cannot find a specific method in this list, we recommend consulting the documentation of the CustomElement.
Structural methods
setAudio(data)data{blob|string}: - The audio content. This can be a Blob, a URL, or a string.
- {void}: return value.
setContent(content,[mediaType="text/plain"])content{string|object}: - The content to be set or a declarative payload.[mediaType="text/plain"]{string}: - The media type of the content. Defaults to "text/plain" if not specified.
- {void}: method does not return a value.
{Error}Throws an error if shadowRoot is not defined.
encoding when data is a string. Use encoding: "url" for URLs and encoding: "base64" for base64 payloads.setDownload(data,)data: dataundefined: filename
setHTML(data)data{htmlelement|url|string|blob}: data
setImage(data)data{(blob|string)}: - The image data, which can be a Blob, a valid URL, or a string representation of the image.
- {void}: not return a value.
setMarkdown(data)data{string|blob}: data
customRenderers['text/markdown'].setMessage(emailData)emailData{object}: - The structured email data.
setPDF(data,[navigation=true],[toolbar=true],[scrollbar=false])data{blob|url|string|object}: The PDF data to be embedded. Can be provided as a Blob, URL, or base64 string,[navigation=true]{boolean}: Determines whether the navigation pane is displayed in the PDF viewer.[toolbar=true]{boolean}: Controls the visibility of the toolbar in the PDF viewer.[scrollbar=false]{boolean}: Configures the display of the scrollbar in the PDF viewer.
- {void}: method returns nothing but sets the embedded PDF as the content.
encoding when data is a string. Use encoding: "url" for URLs and encoding: "base64" for base64 payloads. or a declarative payload: { data, encoding, navigation, toolbar, scrollbar }.setPlainText(data)data{blob|htmlelement|string}: - The input data to be processed. It can be a Blob object, an HTMLElement,
- {void}: This method does not return any value. It processes the content and updates the relevant option
setVideo(data)data{blob|string}: - The video data to set. It can be a Blob, URL, or string.
- {void}: method does not return a value. It updates the viewer's state.
{Error}Throws an error if the provided data is not a Blob or URL.
Static methods
[instanceSymbol]()- {symbol}
instanceof operator.getCSSStyleSheet()- {CSSStyleSheet[]}
getTag()- {string}
Lifecycle methods
Lifecycle methods are called by the environment and are usually not intended to be called directly.
[assembleMethodSymbol]()- {Viewer}
disconnectedCallback()Events
This component does not fire any public events. It may fire events that are inherited from its parent classes.