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

epub-reader

<epub-reader> opens an EPUB book (the common e-book file format) and shows it with a contents sidebar, a toolbar, search, bookmarks and highlights. It reads Vanilla Breeze design tokens for its own controls and for the text of the book, so both follow the active theme.

At a glance

Package@profpowell/epub-reader 0.1.0
Elements<epub-reader>
Renders inLight DOM. Each chapter of the book is shown in a sandboxed <iframe>, so the book's own styles stay separate from the page.
DependenciesNone
Sourcegithub.com/ProfPowell/epub-reader
Documentationprofpowell.github.io/epub-reader
LicenceMIT
In Vanilla BreezeNot bundled. Load it yourself.

Install

Load the component from a CDN with one script tag. The package has no build step, so the file is served from its src/ folder and imports its sibling modules from there.

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

The component needs a browser with DecompressionStream, which it uses to unpack the book: Chrome 80, Firefox 113, Safari 16.4 or later.

Demo

The reader loads a short sample book from the component's own site. Change the site theme, then move to another chapter, to see the controls and the book text change with it.

Usage

Point src at an EPUB file. A file on another site must allow cross-origin requests.

The reader fills the height of its parent and is never shorter than 20rem. Give it a height in CSS when the parent has none.

Open a file the visitor picks, and listen for the event that reports the book's details.

Reference

Two terms appear below. The spine is the ordered list of chapter files in an EPUB. A spine index is a position in that list, starting at 0.

Attributes

AttributeValuesDefaultPurpose
srcURLNoneThe EPUB file to load. Changing it loads the new book.
startNumber0The spine index to open first.
hide-tocBooleanOffStarts with the contents sidebar hidden.
allow-scriptsBooleanOffLets scripts inside the book run, for interactive books such as quizzes. A script in the book can then reach the host page, so turn it on only for books you trust.

Methods and properties

MemberPurpose
open(source)Loads a book from a URL, a File, a Blob or an ArrayBuffer. It replaces any open book.
close()Unloads the book and frees the memory it used.
next(), prev()Move to the next or previous chapter.
goToIndex(index, fragment?), goToPath(path)Jump to a spine index, or to a file path inside the book such as chapter2.xhtml#sec1.
search(query, options?)Searches the whole book. Resolves with a list of matches and the text around each one.
find(open)Opens or closes the find bar for the current chapter.
typography, resetTypography()Reading settings applied to the book text: typeface, text size, line height, paragraph spacing and justification. Assign an object with the settings you want to change.
bookmarks, toggleBookmark(label?), removeBookmark(id), goToBookmark(id)Read, add, remove and open bookmarks for the current book.
highlights, removeHighlight(id), goToHighlight(id)Read, remove and open highlights for the current book.
getLibrary(), openFromLibrary(id), removeFromLibrary(id), clearLibrary()Work with the library, the list of books the reader has stored in the browser.
getStorageEstimate()Resolves with how much browser storage is in use, or null where the browser cannot say.

The reader saves reading settings in localStorage. It saves opened books, reading positions, bookmarks and highlights in IndexedDB, the browser's built-in database. Nothing is sent to a server.

Events

All events bubble and cross shadow DOM boundaries.

EventDetailFires when
epub-loaded{ metadata, spineLength, toc }A book has loaded.
epub-navigate{ index, path, title }The reader moves to another chapter.
epub-error{ error }A book failed to load.
epub-position-restored{ spineIndex, scrollFraction, bookId }The reader returns to the place saved for this book.
epub-typography-change{ typography }A reading setting changes.
epub-bookmarks-change{ bookmarks }A bookmark is added or removed.
epub-highlights-change{ highlights }A highlight is added or removed.
epub-library-change{ reason, id }A book is added to or removed from the library, or the library is cleared.

The component has no CSS custom properties of its own. See the epub-reader documentation for the full detail.

With Vanilla Breeze

The reader renders in light DOM and its styles use Vanilla Breeze tokens directly. For the text of the book it reads four tokens from the element and applies them inside the chapter each time a chapter loads.

TokenDrives
--color-backgroundReader background, and the page background of the book
--color-textReader text, and the text of the book
--color-interactiveActive buttons, the current contents entry, and links in the book
--color-border, --border-width-thinBorders in the reader, and rules in the book
--color-surface, --color-surface-raisedToolbar, sidebar and panel backgrounds, and button backgrounds
--color-text-mutedSecondary text and button labels
--font-monoMonospace text in the panels
--font-size-2xs, --font-size-xs, --font-size-s, --font-size-m, --font-weight-semiboldText sizes and weight in the toolbar and panels
--size-3xs, --size-2xs, --size-s, --size-mPadding and gaps
--radius-s, --radius-m, --radius-fullCorner radius of buttons and panels
--shadow-lShadow under the floating panels

There is no component-level property to set first. Each value is the Vanilla Breeze token, then a built-in default. Without Vanilla Breeze the built-in defaults apply.

Two of the names the component looks for are not Vanilla Breeze tokens: --color-danger (error messages) and --color-interactive-text (text on active buttons). Those use the built-in defaults unless you define them.

To change one reader, set the tokens on the element. They apply to that reader and to the book inside it.

Accessibility

The README names keyboard navigation as a feature. The detail below comes from the code of version 0.1.0.

KeyAction
Right arrow, Page Down, SpaceNext chapter, or next page in paginated layout
Left arrow, Page UpPrevious chapter, or previous page in paginated layout
BAdd or remove a bookmark at the current place
Ctrl or Cmd + FOpen the find bar for the current chapter
EscapeClose the find bar
↑