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

Handle global events in views with ease

License

Notifications You must be signed in to change notification settings

ramlmn/view

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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