1.0.0 • Published 6 years ago

@ramlmn/view v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
6 years ago

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