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

web-directive

v0.1.1

Published

A library to implement directive functions for native HTML without any framework, which is inspired by Vue.js.

Downloads

3

Readme

Web Directive

A library to implement directive functions for native HTML without any framework, which is inspired by Vue.js.

See DEMO

<button w-copy="Text to copy">
  ...
</button>

<script>
  wd.register('copy', {
    mounted(el, { value }) {
      el.addEventListener('click', () => {
        navigator.clipboard.writeText(value);
      });
    }
  });
</script>

Installation

NPM or Yarn

npm i web-directive

# OR

yarn add web-directive

UnPkg

<script src="https://www.unpkg.com/web-directive/dist/web-directive.umd.min.js"></script>

<script type="module" src="https://www.unpkg.com/web-directive/dist/web-directive.es.min.js"></script>

Getting Started

ES Module

import WebDirective from 'web-directive';

const wd = new WebDirective();
wd.listen();

export default wd;

Browser

<script src="path/to/web-directive/dist/web-directive.umd.js"></script>

<script>
  const wd = new WebDirective();
  wd.listen();
</script>

Listen to smaller scope.

const element = document.querySelector('#foo');

wd.listen(element);

Stop listening

wd.disconnect();

Register Directives

After register a directive (for example: foo), you can add w-foo directive to any HTML element and the directive will instantly work.

This is very useful that if you want to add some cross-platform custom logic to existing Vue/React/Angular template without writing code for every frameworks.

wd.register('foo', {
  // Reguired
  // When element attach to DOM or attribute attach to element
  mounted(el, bindings) {
    // Do any thing you want
    const { value } = bindings;
    
    el._foo = new Foo(value);
  },
  
  // Optional
  // When element detach from DOM or attribute dettach from element
  unmounted(el, bindings) {
    el._foo.stop();
    delete el._foo;
  },
  
  // Optional
  // When values changed
  updated(el, bindings) {
    const { value } = bindings;
    el._foo.setOptions(value);
  }
});

Now, add w-foo to HTML, it will run the mounted() hook:

const ele = document.querySelector('...');

ele.setAttribute('w-foo', '{ "options": ...}');

The bindings interface:

interface WebDirectiveBinding<T extends Element = HTMLElement> {
  directive: string;
  node: T;
  value: any;
  oldValue: any;
  mutation?: MutationRecord;
  dir: WebDirectiveHandler<T>;
}

Use JSON as value

wd.register('foo', {
  mounted(el, { value }) {
    const options = JSON.parse(value || '{}');
  },
});

Custom Prefix

const wd = new WebDirective({
  prefix: 'x-'
});

Todo

  • Support modifier and arguments like: w-foo.bar:yoo