Skip to content
This repository has been archived by the owner on Jul 1, 2020. It is now read-only.

Latest commit

 

History

History
44 lines (35 loc) · 901 Bytes

readme.md

File metadata and controls

44 lines (35 loc) · 901 Bytes

View

Handle global events in views with ease

If there are multiple views in your applicaiton handling global events then triggering these events only for the active views is hard, or it is as simple as this library.

Insallation

npm i --save @ramlmn/view

Usage

In your component.js file

// CJS style
const {viewIn, viewOut, shouldHandle} = require('@ramlmn/view');

// ESM style
import {viewIn, viewOut, shouldHandle} from '@ramlmn/view';

// actual component code
class Modal extends HTMLElement {
   connectedCallback() {
     // a global event listener
     document.addEventListner('keydown', event => {
       if (shouldHandle(this.viewId)) {
         // do something
       }
     });
   }

   show() {
     // guard for repeated `viewIn()` calls
     this.viewId = viewIn();
   }

   hide() {
     viewOut(this.viewId);
   }
 }

License

MIT