npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

p-elements-core

v1.2.15

Published

P Elements Core V1

Downloads

1,212

Readme

P-Element

CustomElement base class

Static properties

projectorMode "append", "merge" or "replace"

The projector mode when using templateFromString function

style string

The style is adopted to the rootNode. if this is set there is no need for calling templateFromString. A root node is created and replaced with the result of the render function.

example

class MyElement extends CustomElement {
  static style = `
  :host {
    color: red;
  }`;

  render = () :VNode {
    return <div>Hello world</div>;
  }
}

isFormAssociated boolean

If true, elementInternals are created using attachInternals in the constructor.

example

class MyElement extends CustomElement {

  static isFormAssociated = true;

  @Property({type: "string", attribute: "value", reflect: true })
  value: string = "Peter" ;

  updated(property: string, oldValue: any, newValue: any) {
    if (property === "value"){
      this.internals.setFormValue(this.value);
    }
  }

  render = () :VNode {
    return <div>Hello {this.value}.</div>;
  }
}

Decorators

@CustomElementConfig

Use this decorator to define the custom element

example

@CustomElementConfig({
  tagName: "p-foo",
})
class PFooElement extends CustomElement {
  ...
}

If you want to extend a build in component you need to specify the buildin.

example

@CustomElementConfig({
    tagName: "super-a",
    options: {
      extends: "a",
    },
  })
class SuperAnchorElement extends HTMLAnchorElement {
  ...
}

@Property

Use the @Property decorator to define reactive properties.

example

@CustomElementConfig({
  tagName: "p-foo",
})
class PFooElement extends CustomElement {
  ...

  @Property({type: "string", attribute: "nick-name", reflect: true})
  nickName: string;

  attributeChangedCallback(name: string, oldValue: string, newValue: string): void {
    super.attributeChangedCallback(name, oldValue, newValue);
    ...
  }

The type option could be "string", "number", "boolean" or "object".

If attribute option is set the property will be set on attribute change. If you implement a attributeChangeCallback you need to call the super.attributeChangedCallback.

If reflect is true the attribute is set on property change.

Use converter option to convert attribute to property

const stringArrayConverter: AttributeConverter<string[]> = {
  fromAttribute: (value) => {
    if (!value) {
      return null;
    }
    return value.split(",").map((v) => v.trim());
  },
  toAttribute: (value) => {
    if (!value.join) {
      return null;
    }
    return value.join(",");
  },
};

class MyElement extend CustomElement {
  @Property({attribute: "items", reflect: true, converter: stringArrayConverter})
  items: string[] = ["foo", "bar"];
}

@Query decorator

Use the @Query decorator to define a property that returns an element from the shadow dom.

example

class PFooElement extends CustomElement {
  ...
  @Query("#MyInput")
  nameInput: HTMLInputElement;

  render = () => {
    return <div>
      <label for="MyInput"></label>
      <input type="text" id="MyInput" />
    </div>;
  }
}

@PropertyRenderOnSet @RenderOnSet

Properties decorated with @PropertyRenderOnSet or @RenderOnSet call renderNow when setting a value.

example

class PFooElement extends CustomElement {
  ...
  @PropertyRenderOnSet
  public foo: string = "foo";
}

@Bind

(deprecated, use arrow function myFn = () => console.log('myFn'))

Functions decorated with @Bind will be replaced with the result of bind(this) on the function.

Hooks

init

The init is the first function called after initialising of the custom element

shouldUpdate

Before a property is set to a new value

updated

After a property value is set to a new value

attributeChangedCallback

Call super.attributeChangedCallback() first.