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

@grafikr/components

v2.0.0

Published

### What is Components?

Downloads

661

Readme

Components

What is Components?

Components is a microframework meant to mount code on top of a DOM element. Components encourage lazyloading of all modules by utilizating events to load the components. However, you are not locked into this behaivor.

Installation

$ yarn add @grafikr/components

Usage

First you have to create a new component.

import { Component } from '@grafikr/components';

export default Component((node, { app }) => {
  console.log(node); // The DOM element
  console.log(app); // The app instance
});

Then you have to register your component.

import { App } from '@grafikr/components';

const app = new App({
  // Async component (Recommended)
  'async-component': ['mouseenter', () => import('components/async-component')],

  // Synced component
  'sync-component': require('components/sync-component'),
});

app.mount();

Then you have to create the DOM elements.

<div data-component="my-component"></div>

Loaders

Creating custom loader

Sometimes default pointer events is not good enough to load your component. You may want to load it when it's visible the viewport, or when a certain event is emitted.

To create a custom loader you will first have to create the loader.

The first argument passed is of type { node: HTMLElement } & Context. The second argument is an async function which mounts the component.

import { Loader } from '@grafikr/components';

export default Loader(({ node }, load) => {
  // Add a event.
  // You can use `node` and `emitter` here.
  document.addEventListener('my-custom-event', load);

  // Return a function which disconnects the listeners.
  // This is useful when using multiple loaders.
  return () => {
    document.removeEventListener('my-custom-event', load);
  };
});

Then you have to register the loader for the component.

import CustomLoader from 'loaders/my-custom-loader';

const app = new App({
  'component': [
    CustomLoader,
    () => import('components/component'),
  ],
});

app.mount();

Using multiple loaders

It's very easy to add multiple loaders. You can combine multiple loaders by passing an array.

const app = new App({
  'component': [
    ['mouseenter', 'click'],
    () => import('components/component')
  ],

  'other-component': [
    [customLoader, anotherCustomLoader, 'mouseenter'],
    () => import('components/other-component'),
  ],
});

app.mount();

Context object

app

The app instance.

Component((node, { app }) => {
  ...
});

dispatchEvent

A wrapper around document.dispatchEvent. Is used to store past events received in useEventHistory.

Component((node, { dispatchEvent }) => {
  dispatchEvent('event-name', any)
});

useEventHistory

A function which takes in a function which gets called with all past events, up until the component is mounted.

Component((node, { useEventHistory }) => {
  useEventHistory((events) => {
    events.forEach(([event, payload]) => {
      // event? Name of the event
      // payload? The payload sent to the event
      
      ...  
    });
  })
});

onMounted

A function which takes in a function which get called when the component is initially mounted.

Component((node, { onMounted }) => {
  onMounted(() => {
    ...
  })
});

onTriggered

A function which takes in a function which get called every time is triggered by a loader.

Component((node, { onTriggered }) => {
  onTriggered(() => {
    ...
  })
});

Typed events for dispatchEvent and useEventHistory

To add types for dispatchEvent and useEventHistory, you can add a .d.ts file with the following code:

type CustomEventMap = {
  'typed-event': any;
};

type AppEvent = Array<
  { [K in keyof CustomEventMap]: [K, CustomEventMap[K]] }[keyof CustomEventMap]
>;

declare module '@grafikr/components' {
  export interface EventStore {
    dispatch<K extends keyof CustomEventMap>(type: K, payload: CustomEventMap[K]): void;
    history(fn: (events: AppEvent) => void, filter?: string | string[]): void;
  }
}

export {};