jsx-dom
JSX to document.createElement.
Last updated 2 years ago by alexlur .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install jsx-dom 
SYNC missed versions from official npm registry.

jsx-dom

License build status dependency status devDependency status npm version

Use JSX for creating DOM elements.

Installation

npm install --save jsx-dom
yarn install jsx-dom

Usage

Note: Using HyperScript? h pragma is also supported.

import * as React from "jsx-dom"

// DOM Elements.
document.body.appendChild(
  <div id="greeting" class="alert">
    Hello World
  </div>
)

// Functional components
// `defaultProps` and `props.children` are supported natively and work as you expect.
function Hello(props) {
  return (
    <div>
      Hello {props.firstName}, {props.lastName}!
    </div>
  )
}
Hello.defaultProps = {
  firstName: "John"
}

document.body.appendChild(<Hello firstName="Johnny" lastName="Appleseed" />)

Syntax

jsx-dom is based on the React JSX syntax with a few additions:

Class

  1. class is supported as an attribute as well as className.

  2. class can take:

    • a string
    • an object with the format { [key: string]: boolean }. Keys with a truthy value will be added to the classList
    • an array of values where falsy values (see below) are filtered out
    • an array of any combination of the above

Note that false, true, null, undefined will be ignored per React documentations, and everything else will be used. For example,

<div class="greeting" />
<div class={[ condition && "class" ]} />
<div class={{ hidden: isHidden, 'has-item': this.array.length > 0 }} />
<div class={[ classArray1, classArray2, ['nested'] ]} />

Style

  1. style accepts both strings and objects.
<div style="background: transparent;" />
<div style={{ background: "transparent", fontFamily: "serif" }} />

Children

Passing children as an explicit attribute, when there is no other JSX child nodes, is also supported.

<div children={["Total: ", 20]} />

Other Attributes

  1. dataset accepts an object, where keys with a null or undefined value will be ignored.
<div dataset={{ user: "guest", isLoggedIn: false }} />
  1. Attributes starts with on and has a function value will be treated as an event listener and attached to the node by setting the property directly (e.g. node.onclick = ...).
<div onClick={e => e.preventDefault()} />
  1. innerHTML, innerText and textContent are accepted.

  2. ref accepts either 1) a callback (node: Element) => void that allows access to the node after being created, or 2) a React style ref object. This is useful when you have a nested node tree and need to access a node inside without creating an intermediary variable.

// Callback
<input ref={node => $(node).typehead({ hint: true })} />

// React.createRef
import React, { createRef } from "jsx-dom"

const textbox = React.createRef()
render(
  <div>
    <label>Username:</label>
    <input ref={textbox} />
  </div>
)

window.onerror = () => {
  textbox.current.focus()
}

Functional components

You can write functional components and receive passed props in the same way in React. Unlike React, props.children is guaranteed to be an array.

SVG and Namespaces

A custom build with a list of commonly used SVG tags is included.

// Use 'jsx-dom/svg';
import React from "jsx-dom/svg"

document.body.appendChild(
  <div class="flag" style={{ display: "flex" }}>
    <h1>Flag of Italy</h1>
    <svg width="150" height="100" viewBox="0 0 3 2" class="flag italy">
      <rect width="1" height="2" x="0" fill="#008d46" />
      <rect width="1" height="2" x="1" fill="#ffffff" />
      <rect width="1" height="2" x="2" fill="#d2232c" />
    </svg>
  </div>
)

Below is a list of SVG tags included.

svg, animate, circle, clipPath, defs, desc, ellipse, feBlend, feColorMatrix, feComponentTransfer, feComposite, feConvolveMatrix, feDiffuseLighting, feDisplacementMap, feDistantLight, feFlood, feFuncA, feFuncB, feFuncG, feFuncR, feGaussianBlur, feImage, feMerge, feMergeNode, feMorphology, feOffset, fePointLight, feSpecularLighting, feSpotLight, feTile, feTurbulence, filter, foreignObject, g, image, line, linearGradient, marker, mask, metadata, path, pattern, polygon, polyline, radialGradient, rect, stop, switch, symbol, text, textPath, tspan, use, view

If you need to create an SVG element that is not in the list, or you want to specify a custom namespace, use the attribute namespaceURI.

import React, { SVGNamespace } from "jsx-dom"
<a namespaceURI={ SVGNamespace }>I am an SVG element!</a>

Goodies

Three extra functions and one constant are provided by this package:

  1. preventDefault(event: Event): Event
  2. stopPropagation(event: Event): Event
  3. createFactory(component: string | (props) => JSX.Element)
  4. SVGNamespace is the namespaceURI string for SVG Elements.
  5. className function.
  6. import { HTML } from "jsx-dom" contains short type aliases for HTML elements

Browser Support

There is no support for Internet Explorer, although it will very likely work if you bring your own polyfill.

Known Issues

<div />, and other tags, are inferred as a general JSX.Element in TypeScript instead of HTMLDivElement (or the equivalents). This is a known bug and its fix depends on TypeScript#21699.

Current Tags

  • 6.4.10                                ...           latest (6 days ago)

38 Versions

  • 6.4.10                                ...           6 days ago
  • 6.4.9                                ...           9 days ago
  • 6.4.8                                ...           9 days ago
  • 6.4.7                                ...           25 days ago
  • 6.4.6                                ...           25 days ago
  • 6.4.5                                ...           25 days ago
  • 6.4.4                                ...           5 months ago
  • 6.4.0                                ...           6 months ago
  • 6.3.1                                ...           a year ago
  • 6.3.0                                ...           a year ago
  • 6.2.1                                ...           a year ago
  • 6.1.1                                ...           2 years ago
  • 6.1.0                                ...           2 years ago
  • 5.2.0                                ...           2 years ago
  • 5.1.6                                ...           2 years ago
  • 5.1.5                                ...           2 years ago
  • 5.1.4                                ...           2 years ago
  • 5.1.3                                ...           3 years ago
  • 5.1.2                                ...           3 years ago
  • 5.1.1                                ...           3 years ago
  • 5.1.0                                ...           3 years ago
  • 5.0.2                                ...           3 years ago
  • 5.0.1                                ...           3 years ago
  • 5.0.0                                ...           3 years ago
  • 4.4.1                                ...           3 years ago
  • 4.4.0                                ...           3 years ago
  • 4.3.0                                ...           3 years ago
  • 4.2.0                                ...           3 years ago
  • 4.1.0                                ...           3 years ago
  • 4.0.5                                ...           3 years ago
  • 4.0.4                                ...           3 years ago
  • 4.0.3                                ...           3 years ago
  • 4.0.2                                ...           3 years ago
  • 4.0.1                                ...           3 years ago
  • 3.0.0                                ...           3 years ago
  • 2.0.2                                ...           3 years ago
  • 2.0.1                                ...           3 years ago
  • 2.0.0                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 2
This Month 115
Last Day 0
Last Week 60
Last Month 55
Dependencies (1)
Dev Dependencies (15)

Copyright 2014 - 2016 © taobao.org |