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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@webfeet/reflect-vanilla

v0.1.0

Published

Decorators implementing HTML attribute reflection rules for vanilla custom elements

Readme

@webfeet/reflect-vanilla

This package exports decorators encapsulating the @webfeet/reflect package to cut verbosity by at least 50%.

There are two sets of reflection decorators: direct and cached.

It actually comes with 2 distinct implementations:

Direct decorators

Direct decorators (exposed at @webfeet/reflect-vanilla) implement auto-accessor properties to directly read and write to HTML attributes, the way reflection is specified in HTML.

class MyElement extends HTMLElement {
  @reflectXxx({ ...options }) accessor attr;
}

[!IMPORTANT] Those decorators can only be applied to auto-accessor properties, and entirely implement the property's accessors, such that other decorators applied after them won't have any effect on the getter and setter (they could still add initializers though).

Cached decorators

Cached decorators (exposed at @webfeet/reflect-vanilla/cached.js) make either auto-accessor properties or property setters to write to HTML attributes, and integrate with attributeChangeCallback to process the attribute value only once when it changes, and cache the value in a private field.

@reflectXxx({ ...options }) accessor attr;

or (to allow for custom getters, or custom processing in setters)

// The value will be of the appropriate type,
// and initialized to the appropriate default value at construction time.
#attr;
get attr() {
  return this.#attr;
}
@reflectXxx({ ...options })
set attr(value) {
  // At the time this is called, the HTML attribute has already been set,
  // and parsed back to the appropriate type.
  // This setter will also be called when the attribute changes in any way.
  this.#attr = value;
}

To integrate with attributeChangedCallback, the decorators store metadata on the element's class. The functions getObservedAttributes(elementClass) and reflectAttributeToProperty(element, attributeName, oldValue, newValue) can be called to implement the static observedAttributes property and attributeChangedCallback respectively.

class MyElement extends HTMLElement {
  static get observedAttributes() {
    return getObservedAttributes(this);
  }

  attributeChangedCallback(name, oldValue, newValue) {
    reflectAttributeToProperty(this, name, oldValue, newValue);
  }

  @reflectXxx({ ...options }) accessor attr;
}

The BaseElement class can be used as base class in place of HTMLElement to inherit default implementations of these.

class MyElement extends BaseElement {
  @reflectXxx({ ...options }) accessor attr;
}

Those getObservedAttributes, reflectAttributeToProperty, and BaseElement are directly re-exported from @webfeet/vanilla-core for convenience.

API

This package exports decorator factories with the same name as the reflectors exported by @webfeet/reflect. All decorator factories take options as properties of an object passed to the factory.

All decorator factories have an optional attribute option giving the name of the HTML attribute that the property reflects. When not given, the lowercased name of the property is used.

Decorator factories also have the same options as the @webfeet/reflect function they wrap.

The reflectURL decorator, due to the specificities of reflecting a USVString attribute representing a URL, can only be applied to auto-accessor properties, whichever the flavor (direct or cached).

The reflectElementReference and reflectElementReferences only exist as cached decorators, and can only be applied to auto-accessor properties. They have an optional type option corresponding to the second argument of the same-named @webfeet/reflect function they wrap.