npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nav-panel

v3.0.1

Published

A collapsible navigation panel for web pages

Downloads

24

Readme

Navigation Panel

A collapsible navigation panel for web pages.

How it works

The mechanism for showing and hiding the panel works similarly to Bootstrap's Collapse component. When showing, the panel is made visible with the display CSS property, then it transitions to the target width or height. When hiding, it transitions to width or height 0, then it is hidden with the display property.

Configurability

Speaking of transitions, there is a special transition mode: the fullscreen mode. When enabled, the panel will transition to and from 100%, otherwise it will calculate the original width or height of the panel and transition to and from that value.

Transition orientation can also be configured through the verticalTransition. The value of true makes it use the height for the transition, while false makes it use the width.

To have your panel close whenever the browser window is resized, enable the option closeOnResize.

Accessibility

This plugin toggles aria-expanded, provides keyboard navigation with the arrow keys, performs focus monitoring to close the panel when it is no longer in focus, plus other keyboard and pointer interactions. The code snippets here include the necessary ARIA attributes. Currently, only anchor elements <a> are supported as focusable panel items.

Wanna see it in action? I use it on my portfolio page and on a demo project on smaller viewport sizes.

Installation and Usage

Install the npm package using your preferred package manager.

npm install nav-panel
yarn add nav-panel

Then import the module.

const NavigationPanel = require('nav-panel');

Now create an instance of NavigationPanel and configure it. The first argument is the selector for the toggle button that we will create.

var navPanel = new NavigationPanel('.np-toggle', {
    fullscreen: false,
    verticalTransition: true,
    closeOnResize: true
});

Creating the elements

We're going to need some CSS for the next steps. They enable the collapsible behavior and the transitioning effect.

@import "~nav-panel/css/nav-panel.css";

Create the toggle button.

<button class="np-toggle" data-target="#nav-menu" aria-label="" aria-expanded="false" aria-controls="nav-menu" aria-haspopup="true">
  Menu
</button>

Make sure data-target points to the collapsible panel we'll create next. To meet accessibility requirements, you should also set aria-label and aria-controls accordingly. We're using the nav-menu id attribute for this example.

Now create the collapsible element (the panel itself).

<nav id="nav-menu" class="np-collapsible">
  <a href="#">Home</a>
  <a href="#">About</a>
  <a href="#">Contact</a>
</nav>

That's it. Give it a try!

CSS

This plugin does not apply any decorative CSS. Styling these elements is up to you. You'll probably want to manage z-index, set position: fixed on the panel and give it a background-color, among other things.

  • Feel free to hide the panel and its toggle button with display: none on specific viewport breakpoints, if that's what you need. The script will ignore events when they're hidden.
  • You can change from which edge of the screen the panel will appear to come from by using positioning properties such as right: 0 and bottom: 0.
  • You can make the panel contents scrollable with overflow-y: auto.

Synthetic events

To allow you to hook into the plugin's functionality, synthetic events are fired from the panel element on certain conditions.

| Event type | Fired when | | --------------- | --------------------------------------------- | | np-show | The panel is expanded (waits for transition) | | np-hide | The panel is collapsed (waits for transition) |

One common use for this is to alternate between 'open' and 'close' icons on the toggle button.

document.querySelector('#nav-menu').addEventListener('np-show', function(event) {
  // Do something
});

Controlling the panel from external scripts

You may call show(), hide(), toggle() and isTransitioning() on any instance of NavigationPanel.