3.1.1 • Published 3 months ago

device-navigation v3.1.1

Weekly downloads
-
License
(MIT or CC0 1.0)
Repository
github
Last release
3 months ago

device-navigation

Allows navigation between HTML elements in one or two dimensions with non-mouse devices and unifies it with the mouse navigation experience.

Note that this is specifically built for usage within lit or element-vir HTML templates.

Install

npm i device-navigation

Usage

  1. Mark each element that should be navigable with the nav() directive:

    import {html} from 'element-vir';
    import {nav} from 'device-navigation';
    
    const myTemplate = html`
        <div ${nav()}></div>
    `;
  2. Construct a NavController instance, passing in an element which is parent of all navigable elements:

    import {assert} from '@augment-vir/assert';
    import {html} from 'element-vir';
    import {nav, NavController} from 'device-navigation';
    
    const myTemplate = html`
        <main>
            <div ${nav()}></div>
            <div ${nav()}></div>
            <div ${nav()}></div>
        </main>
    `;
    
    function setNavController() {
        const main = document.querySelector('main');
        assert.isDefined(main);
        return new NavController(main);
    }
  3. Call NavController methods from within listeners:

    import {html} from 'element-vir';
    import {nav, NavController, NavDirection} from 'device-navigation';
    
    const myTemplate = html`
        <main>
            <div ${nav()}></div>
            <div ${nav()}></div>
            <div ${nav()}></div>
        </main>
    `;
    
    function setupListeners(navController: NavController) {
        window.addEventListener('keydown', (event) => {
            if (event.code === 'ArrowUp') {
                navController.navigate({
                    allowWrapping: true,
                    direction: NavDirection.Up,
                });
            } else if (event.code === 'ArrowDown') {
                navController.navigate({
                    allowWrapping: true,
                    direction: NavDirection.Down,
                });
            } else if (event.code === 'Enter') {
                navController.enterInto();
            }
            // etc.
        });
    }

To see a full example, see the demo element.

3.1.1

3 months ago

3.1.0

3 months ago

3.0.3

6 months ago

3.0.2

6 months ago

3.0.1

8 months ago

3.0.0

9 months ago

1.0.1

12 months ago

2.0.1

12 months ago

2.0.0

12 months ago

1.0.0

1 year ago

0.0.2

1 year ago

0.0.1

2 years ago

0.0.0

2 years ago