CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-inferno

An extremely fast, React-like JavaScript library for building modern user interfaces

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

vnode-creation.mddocs/

VNode Creation

Virtual node creation functions for building virtual DOM trees with performance optimizations and type safety.

Capabilities

Create VNode

Creates virtual DOM nodes for HTML elements and other DOM nodes.

/**
 * Creates virtual DOM nodes for HTML elements and other DOM nodes
 * @param flags - VNodeFlags indicating the type and behavior of the VNode
 * @param type - HTML tag name or element type
 * @param className - CSS class name for the element
 * @param children - Child nodes of the element
 * @param childFlags - ChildFlags indicating the type of children
 * @param props - Properties and attributes for the element
 * @param key - Unique key for reconciliation
 * @param ref - Reference callback or object
 * @returns VNode representing the virtual element
 */
function createVNode<P>(
  flags: VNodeFlags,
  type: string,
  className?: string | null,
  children?: InfernoNode,
  childFlags?: ChildFlags,
  props?: Readonly<P> | null,
  key?: string | number | null,
  ref?: Ref | Refs<P> | null
): VNode;

Usage Examples:

import { createVNode, VNodeFlags, ChildFlags } from "inferno";

// Simple div element
const div = createVNode(VNodeFlags.HtmlElement, 'div', null, 'Hello World');

// Element with props and className
const input = createVNode(
  VNodeFlags.InputElement,
  'input',
  'form-control',
  null,
  ChildFlags.HasInvalidChildren,
  { type: 'text', placeholder: 'Enter text' }
);

// Element with children
const list = createVNode(
  VNodeFlags.HtmlElement,
  'ul',
  'list',
  [
    createVNode(VNodeFlags.HtmlElement, 'li', null, 'Item 1'),
    createVNode(VNodeFlags.HtmlElement, 'li', null, 'Item 2')
  ],
  ChildFlags.HasKeyedChildren
);

Create Component VNode

Creates VNodes for React-like components (class or functional).

/**
 * Creates VNodes for React-like components (class or functional)
 * @param flags - VNodeFlags indicating the component type
 * @param type - Component constructor, function, or ForwardRef
 * @param props - Properties passed to the component
 * @param key - Unique key for reconciliation
 * @param ref - Reference callback or object
 * @returns VNode representing the component
 */
function createComponentVNode<P>(
  flags: VNodeFlags,
  type: Function | ComponentType<P> | Component<P, unknown> | ForwardRef<P, unknown>,
  props?: Readonly<P> | null,
  key?: null | string | number,
  ref?: Ref | Refs<P> | null
): VNode;

Usage Examples:

import { createComponentVNode, VNodeFlags, Component } from "inferno";

// Class component
class MyComponent extends Component {
  render() {
    return createVNode(VNodeFlags.HtmlElement, 'div', null, this.props.message);
  }
}

const classVNode = createComponentVNode(
  VNodeFlags.ComponentClass,
  MyComponent,
  { message: 'Hello from class component' }
);

// Functional component
function FunctionalComponent(props) {
  return createVNode(VNodeFlags.HtmlElement, 'span', null, props.text);
}

const funcVNode = createComponentVNode(
  VNodeFlags.ComponentFunction,
  FunctionalComponent,
  { text: 'Hello from function' }
);

Create Text VNode

Creates VNodes for text content.

/**
 * Creates VNodes for text content
 * @param text - Text content (string, number, boolean, null, or undefined)
 * @param key - Unique key for reconciliation
 * @returns VNode representing the text node
 */
function createTextVNode(
  text?: string | boolean | null | number,
  key?: string | number | null
): VNode;

Usage Examples:

import { createTextVNode } from "inferno";

// Simple text
const textNode = createTextVNode('Hello World');

// Number as text
const numberNode = createTextVNode(42);

// Text with key
const keyedText = createTextVNode('Item', 'text-1');

// Empty text (null/undefined becomes empty string)
const emptyText = createTextVNode(null);

Create Fragment

Creates fragment VNodes for grouping multiple elements without a wrapper.

/**
 * Creates fragment VNodes for grouping multiple elements without a wrapper
 * @param children - Array of child nodes or single child
 * @param childFlags - ChildFlags indicating the type of children
 * @param key - Unique key for reconciliation
 * @returns VNode representing the fragment
 */
function createFragment(
  children: any,
  childFlags: ChildFlags,
  key?: string | number | null
): VNode;

Usage Examples:

import { createFragment, createVNode, ChildFlags, VNodeFlags } from "inferno";

// Fragment with multiple children
const fragment = createFragment([
  createVNode(VNodeFlags.HtmlElement, 'h1', null, 'Title'),
  createVNode(VNodeFlags.HtmlElement, 'p', null, 'Content')
], ChildFlags.HasNonKeyedChildren);

// Fragment with keyed children
const keyedFragment = createFragment([
  createVNode(VNodeFlags.HtmlElement, 'div', null, 'First', ChildFlags.HasInvalidChildren, null, 'div-1'),
  createVNode(VNodeFlags.HtmlElement, 'div', null, 'Second', ChildFlags.HasInvalidChildren, null, 'div-2')
], ChildFlags.HasKeyedChildren);

Create Portal

Creates portal VNodes for rendering content outside the normal component tree.

/**
 * Creates portal VNodes for rendering content outside the normal component tree
 * @param children - Content to render in the portal
 * @param container - DOM element where the portal content should be rendered
 * @returns VNode representing the portal
 */
function createPortal(children: any, container: ParentDOM): VNode;

Usage Examples:

import { createPortal, createVNode, VNodeFlags } from "inferno";

// Portal to render modal outside component tree
const modal = createVNode(VNodeFlags.HtmlElement, 'div', 'modal', 'Modal content');
const portalVNode = createPortal(modal, document.body);

// Portal to render tooltip in specific container
const tooltip = createVNode(VNodeFlags.HtmlElement, 'div', 'tooltip', 'Tooltip text');
const tooltipPortal = createPortal(tooltip, document.getElementById('tooltip-container'));

Direct Clone

Creates a direct clone of an existing VNode for reuse.

/**
 * Creates a direct clone of an existing VNode for reuse
 * @param vNodeToClone - VNode to clone
 * @returns New VNode instance with same properties
 */
function directClone(vNodeToClone: VNode): VNode;

Usage Examples:

import { directClone, createVNode, VNodeFlags } from "inferno";

// Original VNode
const original = createVNode(VNodeFlags.HtmlElement, 'div', 'container', 'Content');

// Clone for reuse
const cloned = directClone(original);

// Safe to use both in different parts of the tree
render(original, container1);
render(cloned, container2);

Get Element VNode Flags

Determines the appropriate VNode flags for a given element type string.

/**
 * Determines the appropriate VNode flags for a given element type string
 * @param type - Element type string (e.g., 'div', 'input', 'svg')
 * @returns VNodeFlags value corresponding to the element type
 */
function getFlagsForElementVnode(type: string): VNodeFlags;

Usage Examples:

import { getFlagsForElementVnode, VNodeFlags } from "inferno";

// Get flags for different element types
const divFlags = getFlagsForElementVnode('div');        // VNodeFlags.HtmlElement
const inputFlags = getFlagsForElementVnode('input');    // VNodeFlags.InputElement
const svgFlags = getFlagsForElementVnode('svg');        // VNodeFlags.SvgElement
const selectFlags = getFlagsForElementVnode('select');  // VNodeFlags.SelectElement
const textareaFlags = getFlagsForElementVnode('textarea'); // VNodeFlags.TextareaElement

// Use with createVNode for optimized element creation
const vnode = createVNode(
  getFlagsForElementVnode('input'),
  'input',
  null,
  null,
  ChildFlags.HasNonKeyedChildren,
  { type: 'text', value: 'Hello' }
);

Normalize Props

Normalizes a VNode's properties by moving certain props to the appropriate VNode fields.

/**
 * Normalizes a VNode's properties by moving certain props to the appropriate VNode fields
 * @param vNode - VNode to normalize
 * @returns The same VNode with normalized properties
 */
function normalizeProps(vNode: VNode): VNode;

Usage Examples:

import { normalizeProps, createVNode, VNodeFlags, ChildFlags } from "inferno";

// Create VNode with props that need normalization
const vnode = createVNode(
  VNodeFlags.HtmlElement,
  'div',
  null,
  null,
  ChildFlags.HasNonKeyedChildren,
  {
    className: 'my-class',
    children: 'Hello World'
  }
);

// Normalize props to move className and children to correct VNode fields
normalizeProps(vnode);
// vnode.className is now 'my-class'
// vnode.children is now 'Hello World'
// props.className and props.children are removed

VNode Properties

interface VNode {
  children: InfernoNode;
  childFlags: ChildFlags;
  dom: Element | null;
  className: string | null | undefined;
  flags: VNodeFlags;
  isValidated?: boolean;
  key: Key;
  props: any;
  ref: any;
  type: any;
}

type Key = string | number | undefined | null;

VNode Flags

VNodeFlags are used to optimize VNode handling and indicate the type of VNode (imported from 'inferno-vnode-flags'):

  • VNodeFlags.HtmlElement: Standard HTML elements (div, span, p, etc.)
  • VNodeFlags.InputElement: Input elements with special handling
  • VNodeFlags.SelectElement: Select elements with special handling
  • VNodeFlags.TextareaElement: Textarea elements with special handling
  • VNodeFlags.SvgElement: SVG elements
  • VNodeFlags.ComponentClass: Class-based components
  • VNodeFlags.ComponentFunction: Functional components
  • VNodeFlags.ForwardRefComponent: Forward ref components
  • VNodeFlags.Fragment: Fragment nodes
  • VNodeFlags.Portal: Portal nodes
  • VNodeFlags.Text: Text nodes

Child Flags

ChildFlags optimize child handling and indicate the type of children (imported from 'inferno-vnode-flags'):

  • ChildFlags.HasInvalidChildren: No children or invalid children
  • ChildFlags.HasVNodeChildren: Single VNode child
  • ChildFlags.HasTextChildren: Text-only children
  • ChildFlags.HasKeyedChildren: Array of children with keys
  • ChildFlags.HasNonKeyedChildren: Array of children without keys
  • ChildFlags.UnknownChildren: Children need normalization

Performance Tips

  1. Use appropriate flags: Providing correct VNodeFlags and ChildFlags improves performance
  2. Provide keys: Use keys for arrays of elements to enable efficient reconciliation
  3. Avoid unnecessary cloning: Only clone VNodes when they need to be reused
  4. Reuse VNodes: Clone and reuse VNodes instead of recreating identical ones

docs

component-system.md

core-rendering.md

event-handling.md

fragments-utilities.md

index.md

refs.md

vnode-creation.md

tile.json