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

@tournant/dropdown

v1.0.6

Published

Accessible dropdown menu

Downloads

1

Readme

@tournant/dropdown

A flexible, accessible dropdown menu.


NPM version GitHub license

💁‍ Read Before Use

This component implements a dropdown menu using the ARIA menu role. It’s a a dropdown menu.

«Fine,» you might think, «I need to implement a nested navigation and need a dropdown.» If so, this component is not the right choice. Technically speaking, a menu is no navigation. I highly encourage reading Menu & Menu Buttons of Heydon Pickering’s Inclusive Components blog or Adrian Roselli’s post Don’t Use ARIA Menu Roles for Site Nav.

Installation

You can install the component using NPM or Yarn.

npm install @tournant/dropdown --save

If you use Yarn:

yarn add @tournant/dropdown

Once the component is installed you need to import it wherever you want to use it.

import TournantDropdown from '@tournant/dropdown'

Don’t forget to add it to the registered components (been there, done that):

components: {
  TournantDropdown,
  // ... all the other amazing components
}

API

This is just a quick overview. For an in-depth guide how to use the component check the section Usage below.

Props

  • yPosition: Default: bottom. Must be top or bottom. Controls vertical positioning.
  • xPosition: Default: left. Must be left or right. Controls horizontal positioning.
  • ariaLabel: Optional. Add the aria-label attribute on a button. Can be used if it not viable to put text inside of the button. For other options see the article Accessible Icon Buttons from Sara Soueidan.

Slots

  • button-text: Text inside of the button. If you just add an icon, ensure to give the button an accessible name. Either via the ariaLabel prop or one of the other techniques explained by Sara Soueidan.
  • items: Items of the menu. All elements in here must have a role of menuitem and a tabindex="-1".

Styles

The component exposes the following CSS classes for its parts:

| Classname | Element | | ----------------------- | --------------------------------- | | t-ui-dropdown | Root | | t-ui-dropdown__toggle | The button to open/close the menu | | t-ui-dropdown__menu | div containing the menu buttons |

The component comes with some default styles, e.g. it is setting position: absolute on the menu container as well as z-index: 10.

Positioning

Depending on use the menu might have to appear in different places. This happens through the yPosition and xPosition props. Depending on the respective value, a modifier class is added. You can use these classes to apply more styling and adapt the positioning further.

The following table shows an overview of these:

| Position | Modifier Class | | -------- | -------------- | | left | -is-left | | right | -is-right | | bottom | -is-bottom | | top | -is-top |

Usage

Labeling the Toggle Button

Text can be added inside of the menu using the button-text slot:

<template v-slot:button-text>
	Options
</template>

If it isn’t possible to add a word inside of the button read the article Accessible Icon Buttons from Sara Soueidan. In it she describes multiple options to ensure that Icon Buttons are accessible. If you opt for using aria-label you can use the ariaLabel prop to add it to the element.

💁‍ Once the component is mounted, it will check some values to determine if the button has accessible text. It will warn you in the browser console if it couldn’t detect any.

Opening and Closing the Menu

The menu gets toggled between open and closed state by triggering a click event on the button. As it uses a button element, this can also happen via the ENTER or SPACE keys. Additionally, can be used.

To close the menu, the user can click outside of it, Tab↹ away or use the ESC key. While the menu button is focussed will close the menu, too.

Menu Items

Items inside of the menu can be added via the items slot.

<template v-slot:items>
	<button tabindex="-1" role="menuitem">Rename</button>
	<button tabindex="-1" role="menuitem">Delete</button>
</template>

Make sure to add role="menuitem" and tabindex="-1" to the items you add. The role helps assistive technology to make sense of the items and override any native role the elements might have. Since keyboard navigation inside of the menu happens with and the items need to be taken out of the focus order.

Querying for Menu Items

Once the menu opens the component uses querySelectorAll('[role=menuitem]:not([disabled])') to construct an Array of all enabled items in the menu.

Bugs & Enhancements

If you found a bug, please create a bug ticket.

For enhancements please refer to the contributing guidelines.