1 | # jsx-dom
|
2 | [![License](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause)
|
3 | [![build status](https://travis-ci.org/glixlur/jsx-dom.svg?branch=master)](https://travis-ci.org/glixlur/jsx-dom)
|
4 | [![dependency status](https://david-dm.org/glixlur/jsx-dom/status.svg)](https://david-dm.org/glixlur/jsx-dom#info=dependencies)
|
5 | [![devDependency status](https://david-dm.org/glixlur/jsx-dom/dev-status.svg)](https://david-dm.org/glixlur/jsx-dom#info=devDependencies)
|
6 | [![npm version](https://badge.fury.io/js/jsx-dom.svg)](https://badge.fury.io/js/jsx-dom)
|
7 | ![big mood](https://img.shields.io/badge/kirito-eugeo-blue.svg)
|
8 |
|
9 | Use JSX for creating DOM elements.
|
10 |
|
11 | ## Installation
|
12 | ```bash
|
13 | npm install --save jsx-dom
|
14 | yarn install jsx-dom
|
15 | ```
|
16 |
|
17 | ## Usage
|
18 | **Note:** If you previously use `h` as pragma, there is nothing you need to change.
|
19 |
|
20 | ```jsx
|
21 | import * as React from 'jsx-dom';
|
22 |
|
23 | // DOM Elements.
|
24 | document.body.appendChild(
|
25 | <div id="greeting" class="alert">Hello World</div>
|
26 | );
|
27 |
|
28 | // Functional components
|
29 | // `defaultProps` and `props.children` are supported natively and work as you expected.
|
30 | function Hello(props) {
|
31 | return <div>Hello {props.firstName}, {props.lastName}!</div>;
|
32 | }
|
33 |
|
34 | document.body.appendChild(
|
35 | <Hello firstName="Johny" lastName="Appleseed" />
|
36 | );
|
37 |
|
38 | ```
|
39 |
|
40 | ## Syntax
|
41 | `jsx-dom` is based on the React JSX syntax with a few additions:
|
42 |
|
43 | ### Class
|
44 | 1. `class` is supported as an attribute as well as `className`.
|
45 | 2. `class` can take:
|
46 |
|
47 | * a string
|
48 | * an object with the format `{ [key: string]: boolean }`. Keys with a truthy value will be added to the classList
|
49 | * an array of values where falsy values (see below) are filtered out
|
50 | * an array of any combination of the above
|
51 |
|
52 | Note that `false`, `true`, `null`, `undefined` will be ignored per [React documentations](https://facebook.github.io/react/docs/jsx-in-depth.html#booleans-null-and-undefined-are-ignored), and everything else will be used. For example,
|
53 |
|
54 | ```jsx
|
55 | <div class="greeting" />
|
56 | <div class={[ condition && "class" ]} />
|
57 | <div class={{ hidden: isHidden, 'has-item': this.array.length > 0 }} />
|
58 | <div class={[ classArray1, classArray2, ['nested'] ]} />
|
59 | ```
|
60 |
|
61 | ### Style
|
62 | 1. `style` accepts both strings and objects.
|
63 |
|
64 | ```jsx
|
65 | <div style="background: transparent;" />
|
66 | <div style={{ background: 'transparent', fontFamily: 'serif' }} />
|
67 | ```
|
68 |
|
69 | ### Other Attributes
|
70 | 1. `dataset` accepts an object, where keys with a `null` or `undefined` value will be ignored.
|
71 | ```jsx
|
72 | <div dataset={{ user: "guest", isLoggedIn: false }} />
|
73 | ```
|
74 |
|
75 | 2. Attributes starts with `on` and has a function value will be treated as an event listener and attached to the node with `addEventListener`.
|
76 | ```jsx
|
77 | <div onClick={ e => e.preventDefault() } />
|
78 | ```
|
79 |
|
80 | 3. `innerHTML`, `innerText` and `textContent` are accepted.
|
81 |
|
82 | 4. `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](https://reactjs.org/docs/react-api.html#reactcreateref). This is useful when you have a nested node tree and need to access a node inside without creating an intermediary variable.
|
83 |
|
84 | ```jsx
|
85 | // Callback
|
86 | <input ref={ node => $(node).typehead({ hint: true }) } />
|
87 |
|
88 | // React.createRef
|
89 | import * as React from 'jsx-dom';
|
90 |
|
91 | const textbox = React.createRef();
|
92 | render(
|
93 | <div>
|
94 | <label>Username:</label>
|
95 | <input ref={ textbox } />
|
96 | </div>
|
97 | );
|
98 |
|
99 | window.onerror = () => {
|
100 | textbox.current.focus();
|
101 | };
|
102 | ```
|
103 |
|
104 | ### Functional components
|
105 | You can write functional components and receive passed `props` in the same way in React. Unlike
|
106 | React, `props.children` is guaranteed to be an array.
|
107 |
|
108 | ### SVG and Namespaces
|
109 | A custom build with a list of commonly used SVG tags is included.
|
110 |
|
111 | ```jsx
|
112 | // Use 'jsx-dom/svg';
|
113 | import * as React from 'jsx-dom/svg';
|
114 |
|
115 | document.body.appendChild(
|
116 | <div class="flag" style={{ display: 'flex' }}>
|
117 | <h1>Flag of Italy</h1>
|
118 | <svg width="150" height="100" viewBox="0 0 3 2" class="flag italy">
|
119 | <rect width="1" height="2" x="0" fill="#008d46" />
|
120 | <rect width="1" height="2" x="1" fill="#ffffff" />
|
121 | <rect width="1" height="2" x="2" fill="#d2232c" />
|
122 | </svg>
|
123 | </div>
|
124 | );
|
125 | ```
|
126 |
|
127 | Below is a list of SVG tags included.
|
128 | > 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
|
129 |
|
130 | 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`.
|
131 |
|
132 | ```jsx
|
133 | import * as React from 'jsx-dom';
|
134 |
|
135 | <a namespaceURI={ React.SVGNamespace }>I am an SVG element!</a>
|
136 | ```
|
137 |
|
138 | ## Goodies
|
139 | Two extra functions and one constant are provided by this package:
|
140 |
|
141 | 1. `preventDefault(event: Event): Event`
|
142 | 2. `stopPropagation(event: Event): Event`
|
143 | 3. `SVGNamespace` is the `namespaceURI` string for SVG Elements.
|
144 |
|
145 | ## Browser Support
|
146 | `jsx-dom` requires `Object.keys` and `Object.create` support. This means IE9 or later.
|
147 |
|
148 | ## Known Issues
|
149 |
|
150 | `<div />`, and other tags, are inferred as a general `JSX.Element` in TypeScript instead of
|
151 | `HTMLDivElement` (or the equivalents). This is a known bug and its fix depends on [TypeScript#21699](https://github.com/Microsoft/TypeScript/issues/21699).
|