Open Source DOM Component

Reading Summary

Display a visitor's reading history

by Read Write Tools
Abstract
The rwt-reading-summary DOM component is a popup dialog box showing a visitor's reading history, as captured via the rwt-reading-points DOM component.

Motivation

As the user moves from page to page within a website, the rwt-reading-points component tracks which pages have been visited, what percentage has been read, and how much time was spent reading each page. This information is kept in the user's local storage.

The rwt-reading-summary DOM component displays the information in summary form.

In the wild

To see an example of this component in use, visit the READ WRITE SERVE website and press F4 "Reading Summary". To understand what's going on under the hood, use the browser's inspector to view the HTML source code and network activity, and follow along as you read this documentation.

Installation

Prerequisites

The rwt-reading-summary DOM component works in any browser that supports modern W3C standards. Templates are written using BLUEPHRASE notation, which can be compiled into HTML using the free Read Write View desktop app. It has no other prerequisites. Distribution and installation are done with either NPM or via Github.

Download

Download using NPM

OPTION 1: Familiar with Node.js and the package.json file?
Great. Install the component with this command:

npm install rwt-reading-summary

OPTION 2: No prior experience using NPM?
Just follow these general steps:

  • Install Node.js/NPM on your development computer.
  • Create a package.json file in the root of your web project using the command:
  • npm init
  • Download and install the DOM component using the command:
  • npm install rwt-reading-summary

Important note: This DOM component uses Node.js and NPM and package.json as a convenient distribution and installation mechanism. The DOM component itself does not need them.

Download using Github

If you prefer using Github directly, simply follow these steps:

  • Create a node_modules directory in the root of your web project.
  • Clone the rwt-reading-summary DOM component into it using the command:
  • git clone https://github.com/readwritetools/rwt-reading-summary.git

Using the DOM component

After installation, you need to add several things to your HTML page to make use of it.

  • Add a script tag to load the component's rwt-reading-summary.js file:
  • <script src='/node_modules/rwt-reading-summary/rwt-reading-summary.js' type=module></script>             
  • Add the component tag somewhere on the page.
    • For scripting purposes, apply an id attribute.
    • Optionally, apply a shortcut attribute with something like F1, F2, etc. for hotkey access.
    • And for WAI-ARIA accessibility apply a role=contentinfo attribute.
    <rwt-reading-summary id=reading-summary role=contentinfo shortcut=F4></rwt-reading-summary>
  • Add a button to allow the visitor to show the dialog. Here the button contains the number '0' as its text. This will be replaced with the user's experience points when the rwt-reading-summary-data event is received.
  • <a id='reading-summary-button' aria-haspopup='true' aria-controls='reading-summary'>0</a>
  • Add a listener to respond to the click event:
  • <script type=module>
    document.getElementById('reading-summary-button').addEventListener('click', (e) => {
    document.getElementById('reading-summary').toggleDialog(e);
    });
    </script>
  • Add a listener to capture the rwt-reading-summary-data event and show the experience points on the summary button:
  • <script type=module>
    document.addEventListener('rwt-reading-summary-data', (e) => {
    var el = document.getElementById('reading-summary-button');
    el.innerHTML = e.detail.pointsObtained;
    el.title = `Reading Summary: ${e.detail.pagesRead} pages / ${e.detail.readingTime} / ${e.detail.pointsObtained} points / (${e.detail.shortcutKey}) for details`;
    });
    </script>

Customization

Dialog size and position

The dialog is absolutely positioned towards the bottom left of the viewport. Its size may be overridden using CSS by defining new values for the size and position variables.

rwt-reading-summary {
--width: 70vw;
--height: 50vh;
--bottom: 1rem;
--left: 1rem;
--caption-bar-height: 1.5rem;
--message-height: 1.5rem;
}

Dialog color scheme

The default color palette for the dialog uses a dark mode theme. You can use CSS to override the variables' defaults:

rwt-reading-summary {
--color: var(--white);
--accent-color1: var(--yellow);
--accent-color2: var(--js-blue);
--background: var(--black);
--accent-background1: var(--medium-black);
--accent-background2: var(--pure-black);
--accent-background3: var(--nav-black);
--accent-background4: var(--black);
}

Life-cycle events

The component issues life-cycle events.

component-loaded
Sent when the component is fully loaded and ready to be used. As a convenience you can use the waitOnLoading() method which returns a promise that resolves when the component-loaded event is received. Call this asynchronously with await.

Event controllers

The dialog box can be controlled with its event interface.

toggle-reading-summary
The component listens on DOM document for toggle-reading-summary messages. Upon receipt it will show or hide the dialog box.
keydown
The component listens on DOM document for keydown messages. If the user presses the configured shortcut key (F1, F2, etc) it will show/hide the dialog box. The Esc key closes the dialog box.
collapse-popup
The component listens on DOM document for collapse-popup messages, which are sent by sibling dialog boxes. Upon receipt it will close itself.
click
The component listens on DOM document for click messages. When the user clicks anywhere outside the dialog box, it closes itself.

Reference

DOM components logo Documentation READ WRITE HUB
git logo Source code github
DOM components logo Component catalog DOM COMPONENTS
npm logo Package installation npm
Read Write Stack logo Publication venue READ WRITE STACK

License

The rwt-reading-summary DOM component is licensed under the MIT License.

MIT License

Copyright © 2023 Read Write Tools.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Reading Summary — Display a visitor's reading history

🔗 🔎