Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@blue-hood/velement

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@blue-hood/velement

A minimal DOM renderer for legacy browsers.

  • 0.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

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. ');

Keywords

FAQs

Package last updated on 25 Sep 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc