Viewport Plugin
The Viewport Plugin is a foundational element for any viewer. It provides the <Viewport /> component, which acts as the scrollable “window” through which users see the PDF pages.
This plugin is designed for multi-document layouts. It manages metrics, scroll state, and dimensions independently for every open document, allowing you to build split-screen views, tabbed interfaces, or multi-window applications easily.
Installation
The plugin is available as a separate NPM package.
npm install @embedpdf/plugin-viewportRegistration
Import ViewportPluginPackage and add it to the plugins array.
import { createPluginRegistration } from '@embedpdf/core'
// ... other imports
import { ViewportPluginPackage } from '@embedpdf/plugin-viewport/vue'
const plugins = [
// ... other essential plugins
createPluginRegistration(DocumentManagerPluginPackage, { /* ... */ }),
createPluginRegistration(ScrollPluginPackage),
createPluginRegistration(RenderPluginPackage),
// Register and configure the viewport plugin
createPluginRegistration(ViewportPluginPackage, {
viewportGap: 20, // Adds 20px of padding inside the viewport
}),
]Usage
The plugin provides the <Viewport /> component for the UI and composables for programmatic control.
The <Viewport /> Component
The <Viewport /> component is the main scrollable container. You must provide a documentId to link the scroll container to the specific document state managed by the engine.
<template>
<Viewport
:document-id="activeDocumentId"
class="h-full w-full bg-gray-100"
>
<Scroller :document-id="activeDocumentId">
<!-- page content -->
</Scroller>
</Viewport>
</template>Programmatic Scrolling
To scroll a specific document programmatically, use useViewportCapability to get the API, then use .forDocument(id) to target the specific viewport.
<script setup lang="ts">
import { computed } from 'vue';
import { useViewportCapability } from '@embedpdf/plugin-viewport/vue';
const props = defineProps<{ documentId: string }>();
const { provides: viewportCapability } = useViewportCapability();
// Get the API scoped to this specific document
const viewport = computed(() => viewportCapability.value?.forDocument(props.documentId));
const scrollToTop = () => {
viewport.value?.scrollTo({ x: 0, y: 0, behavior: 'smooth' });
};
</script>
<template>
<button @click="scrollToTop">Top</button>
</template>Monitoring Scroll Activity
To react to scrolling events (e.g., to show a “Back to Top” button or hide a floating toolbar), use the useViewportScrollActivity composable. This composable returns the reactive state for a specific document.
<script setup lang="ts">
import { useViewportScrollActivity } from '@embedpdf/plugin-viewport/vue';
const props = defineProps<{ documentId: string }>();
// Pass a getter that returns the documentId to track
const scrollActivity = useViewportScrollActivity(() => props.documentId);
</script>
<template>
<span v-if="scrollActivity.isScrolling">Scrolling...</span>
<span v-else>Idle</span>
</template>Live Example
This example demonstrates a complete setup. It loads a document, passes the documentId to the components, and creates a toolbar that controls scrolling for that specific document.
API Reference
Configuration (ViewportPluginConfig)
| Option | Type | Description |
|---|---|---|
viewportGap | number | The padding (in pixels) to apply around the content inside the viewport. Default: 10 |
scrollEndDelay | number | The time in milliseconds after the last scroll event before the isScrolling state is set to false. Default: 300 |
Component: <Viewport />
A div-based component that acts as the scrollable container. It accepts all standard div attributes like style and class.
| Prop | Type | Description |
|---|---|---|
documentId | string | (Required) The unique ID of the document this viewport is displaying. |
Composable: useViewportCapability()
Connects your component to the viewport’s functions.
Returns
| Property | Type | Description |
|---|---|---|
provides | Ref<ViewportCapability | null> | A ref object with methods to interact with the viewport system. |
ViewportCapability Methods
| Method | Description |
|---|---|
forDocument(id) | Returns a ViewportScope object to control a specific document’s viewport. |
getViewportGap() | Returns the configured viewport gap. |
isViewportMounted(id) | Returns true if the viewport for the given document ID is currently mounted in the DOM. |
ViewportScope Methods (Returned by forDocument)
| Method | Description |
|---|---|
scrollTo(position) | Scrolls the viewport. position: { x, y, behavior?, center? }. |
getMetrics() | Returns current dimensions (width, height, scrollTop, scrollWidth, etc.). |
isScrolling() | Returns true if the user is currently scrolling. |
getBoundingRect() | Returns the DOMRect of the viewport container. |
Composable: useViewportScrollActivity(documentId)
A composable for tracking the scroll state of a specific document.
Parameters
| Parameter | Type | Description |
|---|---|---|
documentId | () => string | A getter function that returns the document ID to track. |
Returns (ScrollActivity)
| Property | Type | Description |
|---|---|---|
isScrolling | boolean | true if the user is currently scrolling or a smooth scroll animation is in progress. |
isSmoothScrolling | boolean | true only when a smooth scroll animation initiated by scrollTo is in progress. |
Need Help?
Join our community for support, discussions, and to contribute to EmbedPDF's development.