1.0.35 • Published 3 years ago

rwt-sitenav v1.0.35

Weekly downloads
54
License
MIT
Repository
github
Last release
3 years ago
Open Source DOM Component

Sitenav

Website pull-out navigation

Motivation

Sometimes you need to provide your website visitors with quick access to a large quantity of pages without cluttering the main reading area of the page.

The rwt-sitenav DOM component does this by keeping the menu off the left-hand side of the page until the user needs it. The menu is activated, sliding over the page content, when the user clicks on the hamburger menu button. Activation may also be initiated through the component's toggleMenu method or through its event interface.

The component has these features:

  • The hamburger menu button is subtly displayed along the left-hand margin, appearing brighter on hover.
  • The first few times that a visitor interacts with your website, the menu button pulses to draw attention to itself.
  • The menu scrolls vertically, accommodating any number of menu items.
  • Menu items may be kept separate from the DOM component, allowing the webmaster to change its contents in a single centralized place. Alternatively, menu items may be slotted directly between the component's opening and closing tags.
  • The menu item corresponding to the current page is highlighted and scrolled into view when the page is first loaded.
  • The menu has an event interface for showing and hiding itself.
  • The menu emits a custom event to close sibling menus and dialog boxes.
  • A keyboard listener is provided to allow a shortcut key to open/close the menu.

In the wild

To see an example of this component in use, visit the BLUEFIDDLE website and press F1 "Menu". 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-sitenav 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

Using the DOM component

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

  • Add a script tag to load the component's rwt-sitenav.js file:
<script src='/node_modules/rwt-sitenav/rwt-sitenav.js' type=module></script>             
  • Add the component tag somewhere on the page.

      * For scripting purposes, apply an `id` attribute.
      * Apply a `sourceref` attribute with a reference to an HTML file containing the
         menu's text and any CSS it needs.
      * Optionally, apply a `shortcut` attribute with something like `F9`, `F10`, etc. for
         hotkey access.
      * For WAI-ARIA accessibility apply a `role=navigation` attribute.
      * For simple menus, the `sourceref` may be omitted and the menu hyperlinks may be
         slotted into the DOM component. Simply place the hyperlinks directly between the

    <rwt-sitenav> and </rwt-sitenav> tags.

<rwt-sitenav id=sitenav sourceref='/menu.html' shortcut=F9 role=navigation></rwt-sitenav>

Menu template

The content and format of the menu is not prescribed. You may add hyperlinks with formatting that matches your website. Still, one important guideline to keep in mind is that the text of each menu item should be short so that it doesn't wrap into multiple lines. If necessary, the width of the panel can be adjusted using the component's --width CSS variable.

The easiest way to start building your menu is to follow this pattern, which uses list items li for individual pages, and headings h2 for grouping similar pages.

<h2><a href='/products.html' >Products</a></h2>
<ul>
    <li><a href='/products/grappling-hooks.html'>Grappling hooks</a></li>
    <li><a href='/products/ropes.html'>Ropes</a></li>
    <li><a href='/products/carabiners.html'>Carabiners</a></li>
</ul>
<h2><a href='/services.html' >Services</a></h2>
<ul>
    <li><a href='/services/training.html'>Training</a></li>
    <li><a href='/services/certification.html'>Certification</a></li>
</ul>

Self identification

The menu item corresponding to the current page can be highlighted when it identifies itself to the menu. This is accomplished by adding a meta tag to the page that contains the short-form URL of the page itself. For example, if the page's full URL is https://example.com:443/services.html the shortform URL would be /services.html.

The short-form URL may be added to any of three special-purpose meta tags, like this:

<meta name=sitenav:level1 content='/services.html' />
<meta name=sitenav:level2 content='/services/certification.html' />
<meta name=sitenav:level3 content='/services/certification/instructor.html' />

The three items correspond to the artificial hierarchical depth of the menu items. When the page is first loaded, the DOM component uses the content attribute of these meta tags as search terms, looking for the menu item with a matching anchor href. When found, that menu item is highlighted and scrolled into view.

The search for a corresponding href is conducted starting with the deepest level, going from sitenav:level3 to sitenav:level2 to sitenav:level1.

Continuing with the previous example, if the current page is https://example.com:443/services.html , then this menu item will be highlighted:

<h2><a href='/services.html' >Services</a></h2>

Customization

Menu size and position

When visible, the menu is absolutely positioned along the left-hand side of the viewport. Its position and size may be overridden using CSS by defining new values for the top, bottom, and width variables.

rwt-sitenav {
    --top: 0;
    --bottom: 0;
    --width: 15.5rem;
    --z-index: 1;
}

Menu color scheme

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

rwt-sitenav {
    --background: var(--nav-black);
    --level1-color: var(--title-blue);
    --level2-color: var(--pure-white);
    --accent-color: var(--yellow);
    --accent-background: var(--pure-black);
}

Life-cycle events

The component issues life-cycle events.

Event controllers

The menu can be controlled with its event interface.


Reference

License

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

1.0.35

3 years ago

1.0.34

3 years ago

1.0.33

4 years ago

1.0.32

4 years ago

1.0.31

4 years ago

1.0.30

4 years ago

1.0.29

4 years ago

1.0.28

4 years ago

1.0.27

4 years ago

1.0.26

4 years ago

1.0.25

4 years ago

1.0.24

4 years ago

1.0.23

4 years ago

1.0.20

4 years ago