Try It

Click or tap anywhere on the page: below the faint reading line advances, above it goes back. The current paragraph is highlighted.

## The reading line The faint blue line a third of the way down is the reading line. Each tap scrolls so that the end of the current paragraph sits on it. Tap anywhere below the line to move to the next paragraph. The paragraph you land on is highlighted so your eye can find its place again. Tap above the line to step back a paragraph. ## Formatting Paragraphs are separated by a blank line. A single line break is kept as a line break inside the same paragraph. Markdown is styled but its syntax stays visible, so **strong**, *emphasis*, ~~strike~~, `code` and [links](https://example.com) all keep their markers. Each list item is its own stop: - First item - Second item, which is long enough to wrap so you can see the text hang under itself rather than under the marker - [x] A finished task > Quotes keep their marker too. ``` Fenced code blocks keep their whitespace. ``` ## Long blocks When a block is too tall to sit above the reading line, it is aligned to the top of the view instead, and taps page through it before moving on. This paragraph is deliberately long so you can see that happen in the small frame on this page. Keep tapping below the line and the view moves down by most of a screen at a time until the end of the paragraph is visible; only then does the next tap move on to the following block. Tapping above the line pages back up through it the same way, and once you are back at its top the next tap returns to the previous block. On a phone held in portrait this is what keeps a long paragraph readable without it shooting past the reading line in one go. ## The end That's all there is to it. Paste your own text below to try it with something longer.

Usage

Put the text inside the element and give it a height — the reader fills its container.

<div style="height: 100dvh">
  <text-reader># A heading

First paragraph, separated from the next by a blank line.

Second paragraph.</text-reader>
</div>

<script src="/text-reader/text-reader.wc.js"></script>

// or as a module import
import '/text-reader/text-reader.wc.js';

Changing the Text

The text is read once, when the element connects. To show different text, replace the element. When inserting it as markup, escape the text first.

const reader = document.createElement('text-reader');
reader.textContent = text;
container.replaceChildren(reader);

API

Methods and behaviour.

// Step through paragraphs programmatically
reader.scrollToNextBlock();          // smooth by default
reader.scrollToNextBlock('instant');
reader.scrollToPreviousBlock();

// Recalculate the top/bottom padding that lets the first and last
// paragraphs reach the reading line. Runs on window resize; call it
// yourself if the reader's container changes size some other way.
reader.updatePadding();

// Behaviour notes
// - Clicks are listened for on the whole document, not just the
//   reader, so a tap anywhere advances or goes back. The listener
//   is removed when the element is disconnected.
// - Markdown is styled with its syntax left visible: headings,
//   strong, em, strike, inline code, quotes, lists, task lists,
//   fenced code and links (http/https/mailto only).
// - Each list item is its own stop; blocks too tall to sit above
//   the reading line are top-aligned and paged through.
// - Links don't open on a tap (a tap steps the reader); use the
//   context menu (right click / long press) to open them.
// - Follows prefers-color-scheme for light and dark colours.
// - Text selection is disabled inside the reader.