Package Exports
- device-navigation
- device-navigation/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (device-navigation) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
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-navigationUsage
- 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> `; 
- Construct a - NavControllerinstance, passing in an element which is parent of all navigable elements:- 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() { return new NavController(document.querySelector('main')!); } 
- Call - NavControllermethods 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.