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

@blue-hood/velement

v0.1.1

Published

A minimal DOM renderer for legacy browsers.

Downloads

2

Readme

CircleCI Maintainability Release

velement

A minimal DOM renderer for legacy browsers

Usage

With JavaScript:

import VirtualElement, { appendChildren, createElement } from 'velement';

class JSDiv extends VirtualElement {
  constructor(element) {
    super(element || 'div');
    this.element.innerHTML = 'VirtualDivElement';
  }
}

const container = document.createElement('div');

const htmlElement = createElement('div', null);
htmlElement.innerHTML = `HTMLDivElement`;
const virtualElement = createElement(JSDiv, {});

appendChildren(container, 'TextNode', htmlElement, virtualElement);

Then with TypeScript:

import VirtualElement, { appendChildren, createElement } from 'velement';

class TSDiv extends VirtualElement<HTMLDivElement> {
  public constructor(element: HTMLDivElement | null) {
    super(element || 'div');
    this.element.innerHTML = 'VirtualDivElement';
  }
}

declare module 'velement' {
  function createElement(type: typeof TSDiv, props: {}, ...children: Child[]): TSDiv;
}

const container = document.createElement('div');

const htmlElement = createElement('div', null);
htmlElement.innerHTML = `HTMLDivElement`;
const virtualElement = createElement(TSDiv, {});

appendChildren(container, 'TextNode', htmlElement, virtualElement);

Container element will be rendered like:

<div>
  TextNode
  <div>
    HTMLDivElement
  </div>
  <div>
    VirtualDivElement
  </div>
</div>

Install

Just run npm install @blue-hood/velement or yarn add @blue-hood/velement.

Source

src/index.ts

The transpiled code for ES3 target ES2015 Modules is located at dist/.

VirtualElement class

VirtualElements are wrapper elements of HTML elements. Typically, a minimum VirtualElement with HTMLDivElement is defined as:

import VirtualElement from 'velement';

class TSDiv extends VirtualElement<HTMLDivElement> {
  public constructor(element: HTMLDivElement | null) {
    super(element || 'div');
    this.element.innerHTML = 'VirtualDivElement';
  }
}

new TSDiv(null);

Then, the constructor can have properties. The inner HTML element can be accessed through this.element.

import VirtualElement from 'velement';

interface TextDivProps {
  text: string;
}

class TextDiv extends VirtualElement<HTMLDivElement> {
  public constructor(element: HTMLDivElement | null, props: TextDivProps) {
    super(element || 'div');

    this.element.innerHTML = props.text;
  }
}

new TextDiv(null, {
  text: 'VirtualDivElement. '
});

VirtualElement also can be rendered to existing element.

const div = document.createElement('div');
new TextDiv(div, {
  text: 'VirtualDivElement. '
});

Utility methods

createElement

interface Attributes {
  [name: string]: any;
}
type Child = HTMLElement | VirtualElement | string;

export function createElement<HElement extends HTMLElement>(
  type: keyof HTMLElementTagNameMap,
  props: Attributes | null,
  ...children: Child[]
): HElement;

export function createElement<VElement extends VirtualElement, Props>(
  type: { new (element: null, props: Props): VElement },
  props: Props,
  ...children: Child[]
): VElement;

Create HTML element or VirtualElement with properties. And render children to the inner element, this is equal to appendChildren function.

ex. HTMLDivElement

import { createElement } from 'velement';

createElement(
  'div',
  {
    style: `
    color: red;
  `
  },
  'HTMLDivElement. '
);

ex. VirtualElement

import VirtualElement, { createElement } from 'velement';

interface ColorDivProps {
  color: string;
}

class ColorDiv extends VirtualElement<HTMLDivElement> {
  public constructor(element: HTMLDivElement | null, props: ColorDivProps) {
    super(element || 'div');

    this.element.style.color = props.color;
  }
}

declare module 'velement' {
  function createElement(type: typeof ColorDiv, props: ColorDivProps, ...children: Child[]): ColorDiv;
}

createElement(
  ColorDiv,
  {
    color: 'red'
  },
  'VirtualDivElement. '
);

appendChildren

type Child = HTMLElement | VirtualElement | string;

function appendChildren(element: HTMLElement, ...children: Child[]): void;

Append children - HTML element, VirtualElement, text - to HTML element.

ex.

import VirtualElement, { appendChildren, createElement } from 'velement';

class TSDiv extends VirtualElement<HTMLDivElement> {
  public constructor(element: HTMLDivElement | null) {
    super(element || 'div');
    this.element.innerHTML = 'VirtualDivElement';
  }
}

declare module 'velement' {
  function createElement(type: typeof TSDiv, props: {}, ...children: Child[]): TSDiv;
}

const container = document.createElement('div');

const htmlElement = createElement('div', null);
htmlElement.innerHTML = `HTMLDivElement`;

appendChildren(container, createElement(TSDiv, {}), htmlElement, 'TextNode. ');