Scroll Plugin
The Scroll Plugin is a foundational plugin responsible for page layout, virtualization, and navigation. It provides the essential <Scroller />
component, which intelligently renders only the visible pages to ensure high performance, and the useScroll
composable for building page navigation controls.
Installation
This plugin depends on the Viewport
plugin. You must install both packages.
npm install @embedpdf/plugin-scroll @embedpdf/plugin-viewport
Registration
Import ScrollPluginPackage
and its Viewport
dependency, and add them to the plugins
array.
import { createPluginRegistration } from '@embedpdf/core'
// ... other imports
import { ViewportPluginPackage } from '@embedpdf/plugin-viewport/svelte'
import { ScrollPluginPackage, ScrollStrategy } from '@embedpdf/plugin-scroll/svelte'
const plugins = [
// ... other essential plugins
createPluginRegistration(LoaderPluginPackage, { /* ... */ }),
createPluginRegistration(RenderPluginPackage),
// Register dependencies first
createPluginRegistration(ViewportPluginPackage),
// Register and configure the scroll plugin
createPluginRegistration(ScrollPluginPackage, {
strategy: ScrollStrategy.Vertical, // or Horizontal
initialPage: 1,
}),
]
Usage
The plugin provides the <Scroller />
component for rendering pages and the useScroll
composable for navigation.
The <Scroller />
and Snippets
The <Scroller />
component is the heart of the layout system. It must be placed inside a <Viewport />
.
Crucially, the <Scroller />
does not render pages itself. Instead, it calculates which pages should be visible and uses your provided snippet to render each one. This gives you complete control over the content of each page placeholder.
<script lang="ts">
import { RenderLayer } from '@embedpdf/plugin-render/svelte';
import { Scroller, type RenderPageProps } from '@embedpdf/plugin-scroll/svelte';
import { Viewport } from '@embedpdf/plugin-viewport/svelte';
</script>
{#snippet RenderPageSnippet(page: RenderPageProps)}
<div style:width={`${page.width}px`} style:height={`${page.height}px`} style:position="relative">
<RenderLayer pageIndex={page.pageIndex} scale={page.scale} />
</div>
{/snippet}
<Viewport>
<Scroller {RenderPageSnippet} />
</Viewport>
Page Navigation
The useScroll
composable provides everything you need to build a page navigation UI. It returns an object with a provides
property for action methods and a state
property with reactive data like the current page number.
<script lang="ts">
import { useScroll } from '@embedpdf/plugin-scroll/svelte';
const scroll = useScroll();
</script>
{#if scroll.provides}
<div>
<span>Page {scroll.state.currentPage} of {scroll.state.totalPages}</span>
<button onclick={() => scroll.provides?.scrollToNextPage()}>Next</button>
</div>
{/if}
Live Example
This example features a complete page navigation component that allows jumping to a specific page and moving forward or backward.
API Reference
Configuration (ScrollPluginConfig
)
Option | Type | Description |
---|---|---|
strategy | ScrollStrategy | Sets the scroll direction. Can be ScrollStrategy.Vertical or ScrollStrategy.Horizontal . Default: ScrollStrategy.Vertical |
pageGap | number | The space in pixels between pages or spreads. Default: 10 |
initialPage | number | The 1-based page number to scroll to when a document first loads. |
bufferSize | number | The number of pages to render outside the visible area for a smoother scrolling experience. Default: 2 |
Component: <Scroller />
The virtualized container that lays out pages.
Prop | Type | Description |
---|---|---|
RenderPageSnippet | Snippet<RenderPageProps> | (Required) A snippet that defines how to render each page. It receives a RenderPageProps object with layout data like pageIndex , width , height , and scale . |
Composable: useScroll()
A convenience composable for building page navigation UIs.
Returns
An object with the following properties:
Property | Type | Description |
---|---|---|
provides | ScrollCapability | null | An object with methods to control scrolling and navigation. null if the plugin is not ready. |
state | object | A reactive object containing the current scroll state. |
state
Properties
Property | Type | Description |
---|---|---|
currentPage | number | The current page number that is most visible in the viewport. |
totalPages | number | The total number of pages in the document. |
provides
Methods (ScrollCapability
)
Method | Description |
---|---|
scrollToPage(options) | Scrolls to a specific page. The options object takes a pageNumber and optional behavior: 'smooth' . |
scrollToNextPage() | Scrolls to the next page or spread. |
scrollToPreviousPage() | Scrolls to the previous page or spread. |
Need Help?
Join our community for support, discussions, and to contribute to EmbedPDF's development.