Socket
Socket
Sign inDemoInstall

@vonage/element-f

Package Overview
Dependencies
Maintainers
14
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@vonage/element-f

A functional shim to custom element definition


Version published
Weekly downloads
2
Maintainers
14
Weekly downloads
 
Created
Source

element-f

A functional shim to custom element definition.

Installation

npm i @vonage/element-f

Basics

In order to define a custom-element, you only need one definition function:

import elementF from "@voange/element-f";

const MyElement = elementF(function(){
    // --- Your logic goes here --
});

To tap into lifecycle events, this function can use the "life" event emitter:

const MyElement = elementF(function(life){
    life.on('connected', ()=> console.log(`I'm Alive!`));
});

The following events are thrown:

  • connected - Fired upon connectedCallback
  • disconnected - Fired upon disconnectedCallback
  • attribute - Fired when an observed attribute changes.

To observe attributes, just add their list to elementF call:

const MyElement = elementF(function(life){
    life.on('attribute', ({ name, oldValue, newValue })=> {
        // name can be "one" or "two"
    });
}, ["one", "two"]);

Usage

To define a custom element using the standard class notation:

class MyButton extends HTMLElement {
    
    constructor(){
      super();
      console.log(`I'm alive!`);
    }

    static get observedAttributes(){
        return ['disabled'];
    }
    
    attributeChangedCallback(name, oldValue, newValue) {
      this.classList.toggle('disabled', newValue); 
    }

    connectedCallback() {
      this.innerHTML = "<b>I'm an x-foo-with-markup!</b>";
    }
}

Defining the same element using element-f would look like this:

const MyButton = elementF(function(life){
  
  life.on('connected', ()=> { 
    this.innerHTML = "<b>I'm an x-foo-with-markup!</b>"; 
  });
  
  life.on('attribute', ({ name, newValue, oldValue })=> {
    this.classList.toggle('disabled', newValue); 
  });
  
  console.log(`I'm alive!`);

}, ['disabled']);

Keywords

FAQs

Package last updated on 13 Dec 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc