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

@maximux13/svelte-roving-ux

v1.1.0

Published

A svelte library to implement roving UX, with support to keyboard navigation and focus management and more.

Downloads

2

Readme

Svelte Roving UX

Introduction

This is a Svelte implementation of the roving tabindex UX pattern. It is a simple way to manage focus in a list of items, such as a menu or a list of cards. additionally it allows you to provide a custom keyboard shortcut to interact with the selected item.

Demo

You can find a demo here.

Installation

pnpm install --save @maximux13/svelte-roving-ux

Usage

To use this library in your Svelte project, first import it into your component:

import roving from '@maximux13/svelte-roving-ux';

Then, add the use:roving directive to the parent element of the items you want to make focusable:

<div use:roving>
  <button>Item 1</button>
  <button>Item 2</button>
  <button>Item 3</button>
</div>

This will make all direct child elements focusable. If you want to make only some of the child elements focusable or use other element to control the focus, you can use the target option:

<div use:roving={{ target: 'button:not(.disabled)' }}>
  <button>Item 1</button>
  <button>Item 1</button>
  <button class="disabled">Item 2</button>
</div>

You can also use the startIndex option to set the index of the item that should be selected initially:

<div use:roving={{ startIndex: 1 }}>
  <button>Item 1</button>
  <button>Item 2</button>
  <button>Item 3</button>
</div>

If you want to execute a callback function when an item is selected, you can use the callback option:

<script>
  function callback(item, index, previous, next) {
    console.log(item, index, previous, next);
  }
</script>

<div use:roving={{ callback }}>
  <button>Item 1</button>
  <button>Item 2</button>
  <button>Item 3</button>
</div>

The callback function receives the following parameters:

  • item: The selected item
  • index: The index of the selected item
  • previous: The index of the previously selected item
  • next: The index of the next item that will be selected

If you want to use a custom keyboard shortcut to interact with the selected item, you can use the keybindings option:

<script>
  function callback(item, index) {
    console.log(item, index);
  }

  const keybindings = {
    'ctrl+x': callback,
    'alt+y': callback
  };
</script>

<div use:roving={{ keybindings }}>
  <button>Item 1</button>
  <button>Item 2</button>
  <button>Item 3</button>
</div>

The keybindings object has the hotkey as key and the callback function as value. The callback function receives the following parameters:

  • item: The selected item
  • index: The index of the selected item

Supported modifiers are:

  • Alt (or Option) (Mac only) => alt
  • Control (or Ctrl) => ctrl
  • Meta (or Command) (Mac only) => meta
  • Shift => shift

Supported keys are:

  • A-Z
  • 0-9
  • F1-F12
  • ArrowUp => up
  • ArrowDown => down
  • ArrowLeft => left
  • ArrowRight => right
  • Enter => enter
  • Space => space
  • Backspace => delete
  • Escape => escape | esc
  • Meta => meta | cmd | mod

If you consider that a keybinding is missing, please open an issue or submit a pull request.

Options

| Option | Type | Default | Description | | ----------- | -------- | ------- | ------------------------------------------------------------------------------------------------------------------- | | target | string | null | CSS selector for child elements that should be focusable. If null, all child elements are focusable | | startIndex | number | 0 | Index of the item that should be selected initially | | callback | function | null | Callback function that is called when an item is selected. The function receives the item, index, previous and next | | keybindings | object | {} | Object with keybindings. The key is the hotkey that is pressed and the value is the callback function |

Contributing

We welcome contributions to this project! To get started, please follow these steps:

  1. Fork this repository to your own account.
  2. Clone your forked repository to your local machine.
  3. Create a new branch for your changes.
  4. Make your changes and commit them with a descriptive message.
  5. Push your changes to your forked repository.
  6. Submit a pull request to the main repository.

Please ensure that your code adheres to our coding standards and passes our automated tests before submitting a pull request. We also recommend that you open an issue to discuss your proposed changes before starting work on a pull request.

Thank you for your contributions to this project!

Acknowledgements

This library was inspired by the following libraries:

We would like to thank the authors of these libraries for their contributions to the open source community.

License

This project is licensed under the MIT License.