Scroll Plugin
The Scroll Plugin handles page layout, virtualization, and navigation logic. It provides the <Scroller /> component, which efficiently renders only the visible pages dynamically based on the current viewport position, and the useScroll composable for programmatic navigation.
This plugin works in tandem with the Viewport Plugin and supports multi-document layouts, managing independent scroll states and strategies (vertical/horizontal) for each open document.
Installation
This plugin depends on the Viewport plugin.
npm install @embedpdf/plugin-scroll @embedpdf/plugin-viewportRegistration
Import ScrollPluginPackage and add it to your plugin configuration.
import { createPluginRegistration } from '@embedpdf/core'
// ... other imports
import { ViewportPluginPackage } from '@embedpdf/plugin-viewport/vue'
import { ScrollPluginPackage, ScrollStrategy } from '@embedpdf/plugin-scroll/vue'
const plugins = [
// ... other essential plugins
createPluginRegistration(DocumentManagerPluginPackage, { /* ... */ }),
createPluginRegistration(RenderPluginPackage),
// Register dependencies first
createPluginRegistration(ViewportPluginPackage),
// Register and configure the scroll plugin
createPluginRegistration(ScrollPluginPackage, {
defaultStrategy: ScrollStrategy.Vertical, // or Horizontal
defaultPageGap: 10,
}),
]Usage
The <Scroller /> Component
The <Scroller /> component is the heart of the layout system. It must be placed inside a <Viewport />.
You must provide the documentId to link the scroller to the correct document state. The scoped slot is called for each visible page, receiving layout information, allowing you to render the page content (e.g., using <RenderLayer />).
<template>
<Viewport :document-id="activeDocumentId">
<Scroller :document-id="activeDocumentId">
<template #default="{ page }">
<div :style="{ width: `${page.width}px`, height: `${page.height}px`, position: 'relative' }">
<RenderLayer :document-id="activeDocumentId" :page-index="page.pageIndex" />
</div>
</template>
</Scroller>
</Viewport>
</template>Page Navigation
To build navigation controls (like “Next Page” or “Go to Page 5”), use the useScroll composable. This composable requires a getter for the documentId of the target document.
<script setup lang="ts">
import { useScroll } from '@embedpdf/plugin-scroll/vue';
const props = defineProps<{ documentId: string }>();
const { provides: scroll, state } = useScroll(() => props.documentId);
</script>
<template>
<div>
<span>Page {{ state.currentPage }} of {{ state.totalPages }}</span>
<button @click="scroll?.scrollToNextPage()">Next</button>
</div>
</template>Live Examples
Page Navigation
This example features a complete page navigation component that allows jumping to a specific page and moving forward or backward.
Scroll to Page on Load
To scroll to a specific page when a document loads, use the onLayoutReady event from the scroll capability. This event fires once the document layout is fully initialized and ready for navigation.
API Reference
Configuration (ScrollPluginConfig)
| Option | Type | Description |
|---|---|---|
defaultStrategy | ScrollStrategy | The default scroll direction (Vertical or Horizontal). Default: Vertical |
defaultPageGap | number | Pixel gap between pages. Default: 10 |
defaultBufferSize | number | Number of pages to render outside the visible viewport. Default: 2 |
Component: <Scroller />
The virtualized container that lays out pages.
| Prop | Type | Description |
|---|---|---|
documentId | string | (Required) The ID of the document to render. |
Default Scoped Slot Props
| Property | Type | Description |
|---|---|---|
page | object | An object containing all layout data for the page, including pageIndex, width, height, scale, and rotation. |
Composable: useScroll(documentId)
Returns the scroll state and capability methods for a specific document.
Parameters
| Parameter | Type | Description |
|---|---|---|
documentId | () => string | A getter function that returns the document ID to track. |
Returns
| Property | Type | Description |
|---|---|---|
state | { currentPage: number, totalPages: number } | Reactive state object containing the current page info. |
provides | Ref<ScrollScope | null> | A ref object with methods to control scrolling and navigation. |
provides Methods (ScrollScope)
| Method | Description |
|---|---|
scrollToPage(options) | Scrolls to a specific page. options: { pageNumber, behavior?, center? }. |
scrollToNextPage() | Scrolls to the next page or spread. |
scrollToPreviousPage() | Scrolls to the previous page or spread. |
setScrollStrategy(s) | Changes the scroll strategy (e.g. to ScrollStrategy.Horizontal) for this document. |
Events (ScrollCapability)
Access events via useScrollCapability().
| Event | Payload | Description |
|---|---|---|
onLayoutReady | { documentId, isInitial } | Fires when a document’s layout is ready for interaction. isInitial is true only on the first layout after document load, false on subsequent layouts (e.g., when switching tabs). |
onPageChange | { documentId, pageNumber, totalPages } | Fires when the current page changes. |
onScroll | { documentId, metrics } | Fires on scroll with detailed viewport metrics. |
Need Help?
Join our community for support, discussions, and to contribute to EmbedPDF's development.