Premium DOM Component
Favorites
Site-specific favorites dialog by Read Write Tools Dec 10, 2019
Abstract |
---|
The rwt-favorites DOM component uses the browser's local-storage facility to hold a website-specific list of favorite pages. Visitors can add or remove pages from the list using a popup dialog box. |
Motivation
When browsing for information, it's common to glance over page after page of "not quite what I'm looking for" stuff. When you finally discover the gem you're looking for, you naturally don't want to lose it.
Saving that page to your browser's list of favorites is a common way to keep track of important URLs. Unfortunately, the browser's list of favorites tends to get so cluttered over time that it's no longer useful.
This rwt-favorites DOM component is a way to give a similar functionality to your visitors, but on a local website basis. Users can save pages for later in-depth reading, or for follow-up work, or for ready reference. It is most useful on large websites.
In the wild
To see an example of this component in use, visit the BLUEPHRASE website and press F8 "Favorites". 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-favorites DOM component works in any browser that supports modern W3C standards. Templates are written using BLUE PHRASE 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-favorites
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
npm install rwt-favorites
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-favorites DOM component into it using the command:
git clone https://github.com/readwritetools/rwt-favorites.git
Using the DOM component
After installation, you need to add four things to your HTML page to make use of it.
- Add a
script
tag to load the component'srwt-favorites.js
file:
<script src='/node_modules/rwt-favorites/rwt-favorites.js' type=module></script>
-
Add the component tag somewhere on the page.
- For scripting purposes, apply an
id
attribute. - Optionally, apply a
sourceref
attribute with a reference to a JSON file containing initial favorites. - And for WAI-ARIA accessibility apply a
role=search
attribute.
- For scripting purposes, apply an
<rwt-favorites id=favorites sourceref='/initial-favorites.json' role=search></rwt-favorites>
- Add a button for the visitor to click to show the dialog:
<a id=favorite-button title='Favorites (F7)'>★</a>
- Add a listener to respond to the click event:
<script type=module>
document.getElementById('favorite-button').addEventListener('click', (e) => {
document.getElementById('favorites').toggleDialog(e);
});
</script>
Customization
Preset favorites
The JSON file referenced by the sourceref
attribute is used to initialize the
local-storage area with "favorites" that you think are appropriate for
first-time visitors. It is only used the first time a visitor encounters this
component on any page in your website. It is ignored from then on.
The format for this JSON file follows this pattern:
[
{
"filePath": "/page1.html",
"title": "Page 1",
"description": "The most important feature of this website",
"star": true
},
{
"filePath": "...",
"title": "...",
"description": "...",
"star": true
}
]
Dialog size and position
The dialog is absolutely positioned towards the bottom right of the viewport.
Its size may be overridden using CSS by defining new values for --width
and --height
.
rwt-favorites {
--width: 70vw;
--height: 50vh;
--bottom: 1rem;
--right: 1rem;
}
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-favorites {
--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);
}
Internals
The visitor's favorites are stored in local-storage under the key favorite-data
.
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 thecomponent-loaded
event is received. Call this asynchronously withawait
.
Reference
Documentation | READ WRITE HUB | |
Source code | github | |
Component catalog | DOM COMPONENTS | |
Package installation | npm | |
Publication venue | READ WRITE STACK |
License
The rwt-favorites DOM component is not freeware. After evaluating it and before using it in a public-facing website, eBook, mobile app, or desktop application, you must obtain a license from Read Write Tools .
Favorites Software License Agreement
- This Software License Agreement ("Agreement") is a legal contract between you and Read Write Tools ("RWT"). The "Materials" subject to this Agreement include the "Favorites" software and associated documentation.
- By using these Materials, you agree to abide by the terms and conditions of this Agreement.
- The Materials are protected by United States copyright law, and international treaties on intellectual property rights. The Materials are licensed, not sold to you, and can only be used in accordance with the terms of this Agreement. RWT is and remains the owner of all titles, rights and interests in the Materials, and RWT reserves all rights not specifically granted under this Agreement.
- Subject to the terms of this Agreement, RWT hereby grants to you a limited, non-exclusive license to use the Materials subject to the following conditions:
- You may not distribute, publish, sub-license, sell, rent, or lease the Materials.
- You may not decompile or reverse engineer any source code included in the software.
- You may not modify or extend any source code included in the software.
- Your license to use the software is limited to the purpose for which it was originally intended, and does not include permission to extract, link to, or use parts on a separate basis.
- An eBook published under a single title and author.
- A mobile app for distribution under a single app name.
- A desktop application published under a single application name.
- A website published under a single domain name. For this purpose, and by way of example, the domain names "alpha.example.com" and "beta.example.com" are considered to be separate websites.
- A load-balanced collection of web servers, used to provide access to a single website under a single domain name.
Activation
To activate your license, copy the rwt-registration-keys.js
file to the root
directory of your website, providing the customer-number
and access-key
sent to
your email address, and replacing example.com
with your website's hostname.
Follow this example:
export default [{ "product-key": "rwt-favorites", "registration": "example.com", "customer-number": "CN-xxx-yyyyy", "access-key": "AK-xxx-yyyyy" }]