Vanilla Breeze v0.7.4

Vanilla Breeze v0.7.4 released

0.7.4 gives the Ambient pack weather. A page names one feed URL on its own site, and the pack sets data-weather from it, uses its sunrise and sunset for dawn and dusk, and never shows a stale observation. Helpers for producing the feed from Open-Meteo are included. Ambient p…

Read the release notes

pdf-viewer

<pdf-viewer> shows a PDF document in the page with a toolbar for page navigation, zoom, search, thumbnails, download and print. Its toolbar, sidebar and borders read Vanilla Breeze design tokens, so they follow the active theme.

At a glance

Package@profpowell/pdf-viewer 0.2.0. The README marks it as alpha: the API may change before 1.0.
Elements<pdf-viewer>
Renders inShadow DOM
DependenciesPDF.js, the PDF renderer from Mozilla. It is bundled into the published file, so there is nothing else to load.
Sourcegithub.com/ProfPowell/pdf-viewer
Documentationprofpowell.github.io/pdf-viewer
LicenceMIT
In Vanilla BreezeNot bundled. Load it yourself.

Install

Load the component from a CDN with one script tag. The file is about 2.4 MB because it contains PDF.js.

Or install it from npm and import it in your own script.

Demo

A five-page sample document with the thumbnail sidebar open. Change the site theme to see the toolbar and sidebar change with it.

Usage

Point src at a PDF file. The viewer is 600 pixels tall unless you set a height in CSS.

Open on a given page, fit the page to the width of the viewer, and show the thumbnail sidebar and the search bar.

Load a file the visitor picks, and listen for the event that reports the page count.

Reference

Attributes

AttributeValuesDefaultPurpose
srcURLNoneThe PDF document to load.
pageNumber, starting at 11The page shown.
zoomA number such as 1.5, or fit-width, fit-page, autofit-widthThe zoom level, as a number or a fit mode.
modelight, darkFollows the page, then the system settingForces the light or dark color scheme.
viewsingle, continuoussingleShows one page at a time, or all pages in one scrolling column.
show-toolbarBoolean. Set show-toolbar="false" to hide.ShownShows the toolbar.
show-thumbnailsBooleanOffOpens the thumbnail sidebar.
show-searchBooleanOffOpens the search bar.
no-downloadBooleanOffHides the download button.
no-printBooleanOffHides the print button.
titleTextNoneThe title shown in the toolbar. It is also the file name used for a download.
lazyBooleanOffWaits to load the document until the viewer scrolls into view.

Methods and properties

The properties src, page, zoom, mode and view mirror the attributes of the same name and can be set from script.

MethodPurpose
nextPage(), prevPage(), goToPage(n)Move between pages.
getPageCount(), getCurrentPage()Return the number of pages and the current page number.
zoomIn(), zoomOut(), setZoom(level)Change the zoom. setZoom() takes a number or 'fit-width', 'fit-page', 'auto'.
setData(source)Loads a PDF from an ArrayBuffer, a Uint8Array or a File. Returns a promise.
openFile()Opens the browser's file chooser so the visitor can pick a PDF.
download(), print()Download or print the current document. Both return a promise.
search(query), clearSearch()Search the document text, and clear the search.
toggleFullscreen(), toggleThumbnails(), toggleSearch()Turn fullscreen, the thumbnail sidebar and the search bar on or off.

Events

EventDetailFires when
pdf-loaded{ url, pageCount, title }The document has loaded.
pdf-load-error{ url, error }The document failed to load.
page-change{ page, previousPage, pageCount }The current page changes.
zoom-change{ zoom, previousZoom }The zoom level changes.
view-change{ view }The view switches between single and continuous.
file-opened{ name, size }The visitor opens a local file.

CSS custom properties

Set these on the element to change one viewer. Each one wins over the Vanilla Breeze token listed beside it.

PropertyControlsFalls back to
--pdf-viewer-bgViewer background--color-surface
--pdf-viewer-toolbar-bgToolbar background--color-surface-raised
--pdf-viewer-sidebar-bgThumbnail sidebar background--color-surface-sunken
--pdf-viewer-content-bgThe area behind the pageBuilt-in gray
--pdf-viewer-border-colorBorders--color-border
--pdf-viewer-text-color, --pdf-viewer-text-mutedText and secondary text--color-text, --color-text-muted
--pdf-viewer-accent-colorAccent color for active controls--color-interactive
--pdf-viewer-hover-bgButton hover backgroundBuilt-in value
--pdf-viewer-font-family, --pdf-viewer-font-sizeTypeface and base text size--font-sans; 14px
--pdf-viewer-shadowShadow under the page--shadow-m in light mode, --shadow-l in dark mode
--pdf-viewer-sidebar-width, --pdf-viewer-toolbar-heightSidebar width and toolbar height200px; 40px
--pdf-viewer-highlight, --pdf-viewer-highlight-activeSearch matches, and the current matchBuilt-in yellow and orange

The component also exposes four shadow parts for ::part() selectors: toolbar, content, sidebar and search.

See the pdf-viewer documentation for the full detail.

With Vanilla Breeze

The component reads Vanilla Breeze tokens directly. Custom properties inherit into the shadow DOM, so no setup is needed.

TokenDrives
--color-surfaceViewer background
--color-surface-raisedToolbar background
--color-surface-sunkenThumbnail sidebar background
--color-border, --border-width-thinBorder color and width
--color-text, --color-text-mutedText and secondary text
--color-interactiveAccent color
--color-errorThe load error message
--font-sansTypeface
--font-size-2xs, --font-size-xs, --font-size-lText sizes in the toolbar and overlays
--size-3xs, --size-2xs, --size-xs, --size-s, --size-mPadding and gaps
--radius-xs, --radius-s, --radius-mCorner radius of buttons, inputs and the viewer frame
--shadow-s, --shadow-m, --shadow-lShadows

The fallback order for each value is: the --pdf-viewer-* property if you set it, then the Vanilla Breeze token, then a built-in default. Without Vanilla Breeze the built-in defaults apply.

Three of the names the component looks for are not Vanilla Breeze tokens: --color-surface-hover, --font-size-sm and --font-size-base. Button hover backgrounds and most text sizes therefore use the built-in defaults (13px and 14px text) on a Vanilla Breeze page. Set --pdf-viewer-hover-bg and --pdf-viewer-font-size to change them.

The page of the PDF stays white in light and dark mode. This is deliberate and matches how a PDF prints.

For light and dark mode the component watches data-mode, data-theme and a dark class on the page and updates when they change. Set the mode attribute to force one.

Accessibility

The README has no accessibility notes. The points below come from the code of version 0.2.0.

KeyAction
Right arrow, Down arrow, Page DownNext page
Left arrow, Up arrow, Page UpPrevious page
Home, EndFirst page, last page
+, -Zoom in, zoom out
0Fit to width
Ctrl or Cmd + FOpen or close the search bar
EscapeClose the search bar, or leave fullscreen
↑