How the pieces fit
A long article needs the same few aids every time: a way to see its shape, a way to link to one part, a sense of how far there is to go, and somewhere for the asides. Each aid is a separate component. All of them work from the article's own markup, so the only thing they share is an element id and a sound heading order.
Scroll the demo. The bar at the top fills as you read the article, the contents list marks the section in view, each heading shows a link when you point at it, and the two footnotes are collected at the end.
<reading-progress data-for="guide"></reading-progress> <layout-sidebar data-layout-side="end"> <aside> <page-toc scope="#guide"></page-toc> </aside> <article id="guide"> <h1>Starting a sourdough culture</h1> <page-stats data-for="guide" data-show="reading-time,word-count"></page-stats> <heading-links> <h2>What you need</h2> <p>Wholemeal rye is the most forgiving flour to start with<foot-note>Rye carries more wild yeast on the grain.</foot-note>.</p> ... </heading-links> <foot-notes></foot-notes> </article></layout-sidebar>
Contents and heading links
<page-toc> builds its list from the headings inside the element that scope selects (main by default) at the levels in levels (h2,h3 by default). It lists only headings that have an id, because a contents entry has to link somewhere.
<heading-links> supplies those ids. For each heading inside it that has none, it makes one from the heading text, then adds the permalink. Use the two together and you never write a heading id by hand. If you want an address to stay the same when a heading is reworded, give that heading an id yourself and both components keep it.
- To leave a heading out of both, put
data-toc-ignore on it or on an element around it.
- The contents list updates when headings are added, removed or hidden. Call
refresh() on it if you change heading text in place.
- For a short page,
orientation="horizontal" turns the list into a single scrolling row to place above the article.
- To have a contents list that works before any script loads, write the list of links inside
<page-toc> yourself. The component then adds only the tracking.
<heading-links> <h2 id="first-week">The first seven days</h2> <h3>Days one to three</h3> <h3 data-toc-ignore>A note on jars</h3> </heading-links>
Progress and reading time
<reading-progress> with no attributes tracks the scroll of the whole document. Give it data-for with an article's id to measure that article alone, which is what you want when a page has a long header, comments or a footer. data-position="bottom" moves the bar to the bottom edge. The bar is decoration and is hidden from assistive technology, which already knows the scroll position.
<page-stats> counts the words in the element named by data-for and divides by 238 words a minute, or the rate in data-wpm. Chinese and Japanese text is timed by characters. Choose what to show with data-show: any of reading-time, word-count and last-modified. The date is the page's last-modified tag (see Page Metadata), never the file date the server reports.
Mark anything that should not count, such as a related-links block inside the article, with data-stats-ignore.
If your build already knows the numbers, write the list inside <page-stats> and leave off data-for. The component keeps your markup as it is. The <page-stats> page shows the markup.
Other ways to read
Two components change how the article is taken in. Both are optional and both leave the article markup alone.
<reader-view> wraps the article and adds a bar with text-size buttons and a switch between two modes. scroll is the article as usual. pages covers the window and lays the text out in columns that turn like pages, with Escape to leave. Text size and column count are remembered per page in the reader's browser.
<text-reader> reads the element named by for aloud, using the speech voices built into the browser, and marks each word as it is spoken. It reads paragraphs and list items by default. Set selectors to include headings or other elements. variant="icon" gives a single button that fits a toolbar. Where the browser has no speech support the component hides itself.
<page-tools> <print-page></print-page> <share-wc variant="icon"></share-wc> <text-reader variant="icon" for="guide" selectors="h2,h3,p,li"></text-reader></page-tools>
For a link that returns to the top, add data-back-to-top to an anchor. It stays hidden until the reader has scrolled about one screen down.