Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit ca5d511

Browse files
authored
Merge pull request #19 from ryansolid/autodetect
Autodetect
2 parents 589467a + 74af33b commit ca5d511

File tree

65 files changed

+605
-1236
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+605
-1236
lines changed

README.md

+28-25
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,24 @@ This plugin treats all lowercase tags as html elements and mixed cased tags as C
1515

1616
In general JSX Attribute Expressions are treated as properties by default, with exception of hyphenated(-) ones that will always be set as attributes on the DOM element. Plain string attributes(Non expression, no {}) will be treated as attributes.
1717

18-
<b>For dynamic expressions that should be wrapped in a computation for partial re-render use inner parenthesis in the expression ```{( )}```.</b>
18+
This library uses a heuristic whether to dynamic wrap expressions based on if they contain function calls or property access. Simple literals and variable expressions won't be wrapped.
1919

2020
## Example
2121

2222
```jsx
23-
const view = ({ item }) =>
24-
<tr class={( item.id === selected ? 'danger' : '' )}>
25-
<td class="col-md-1">{ item.id }</td>
23+
const view = ({ item }) => {
24+
const itemId = item.id;
25+
<tr class={ itemId === selected() ? 'danger' : '' }>
26+
<td class="col-md-1">{ itemId }</td>
2627
<td class="col-md-4">
27-
<a onclick={e => select(item, e)}>{( item.label )}</a>
28+
<a onclick={e => select(item, e)}>{ item.label }</a>
2829
</td>
2930
<td class="col-md-1"><a onclick={e => del(item, e)}>
3031
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
3132
</a></td>
3233
<td class="col-md-6"></td>
3334
</tr>
35+
}
3436
```
3537
Compiles to:
3638

@@ -41,21 +43,23 @@ import { wrap as _$wrap } from "dom";
4143
const _tmpl$ = document.createElement("template");
4244
_tmpl$.innerHTML = `<tr><td class="col-md-1"></td><td class="col-md-4"><a></a></td><td class="col-md-1"><a><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></a></td><td class="col-md-6"></td></tr>`;
4345

44-
const view = ({ item }) =>
45-
function () {
46+
const view = ({ item }) => {
47+
const itemId = item.id;
48+
return function() {
4649
const _el$ = _tmpl$.content.firstChild.cloneNode(true),
4750
_el$2 = _el$.firstChild,
4851
_el$3 = _el$2.nextSibling,
4952
_el$4 = _el$3.firstChild,
5053
_el$5 = _el$3.nextSibling,
5154
_el$6 = _el$5.firstChild;
52-
_$wrap(() => _el$.className = item.id === selected ? 'danger' : '');
53-
_$insert(_el$2, item.id);
55+
_$wrap(() => _el$.className = itemId === selected() ? 'danger' : '');
56+
_$insert(_el$2, itemId);
5457
_el$4.onclick = e => select(item, e);
5558
_$insert(_el$4, () => item.label);
5659
_el$6.onclick = e => del(item, e);
5760
return _el$;
5861
}();
62+
}
5963
```
6064
The use of cloneNode improves repeat insert performance and precompilation reduces the number of references to the minimal traversal path. This is a basic example which doesn't leverage event delegation or any of the more advanced features described below.
6165

@@ -78,9 +82,6 @@ Boolean to indicate whether to enable automatic event delegation on camelCase.
7882
### contextToCustomElements
7983
Boolean indicates whether to set current render context on Custom Elements and slots. Useful for seemless Context API with Web Components.
8084

81-
### alwaysWrap
82-
Removes the need for `{( )}` syntax. All expressions will be dynamic. This can lead to severe performance degradation. Use at your own risk.
83-
8485
### alwaysCreateComponents
8586
Always use createComponent method instead of just calling the function. Needed to support class components.
8687

@@ -112,7 +113,7 @@ These will be treated as event handlers expecting a function. All lowercase are
112113

113114
```jsx
114115
<ul>
115-
{( list().map(item => <li model={item.id} onClick={handler} />) )}
116+
{ list().map(item => <li model={item.id} onClick={handler} />) }
116117
</ul>
117118
```
118119
This delegation solution works with Web Components and the Shadow DOM as well if the events are composed. That limits the list to custom events and most UA UI events like onClick, onKeyUp, onKeyDown, onDblClick, onInput, onMouseDown, onMouseUp, etc..
@@ -126,7 +127,7 @@ Important:
126127

127128
This takes an object and assigns all the keys as classes which are truthy.
128129
```jsx
129-
<div classList={({ selected: isSelected(), editing: isEditing() })} />
130+
<div classList={{ selected: isSelected(), editing: isEditing() }} />
130131
```
131132
### events
132133

@@ -137,38 +138,40 @@ Generic event method for Level 3 "addEventListener" events. Experimental.
137138

138139
### ... (spreads)
139140

140-
Spreads let you pass multiple props at once. If you wish dynamic updating remember to wrap with a parenthesis:
141+
Spreads let you pass multiple props at once:
141142

142143
```jsx
143-
<div {...static} {...(dynamic)} />
144+
<div {...static} />
144145
```
145146

146147
Keep in mind given the independent nature of binding updates there is no guarantee of order using spreads at this time. It's under consideration.
147148

148149
## Components
149150

150-
Components are just Capital Cased tags. The same rules around dynamic wrapping apply. Instead of wrapping with computation dynamic props will just be getter accessors. * Remember property access triggers so don't destructure outside of computations.
151+
Components are just Capital Cased tags. Instead of wrapping with computation dynamic props will just be getter accessors. * Remember property access triggers so don't destructure outside of computations unless you intend the content to be static.
151152

152153
```jsx
153-
const MyComp = props => (
154-
<>
155-
<div>{( props.param )}</div>
156-
<div>{ props.static }</div>
154+
const MyComp = props => {
155+
const staticProp = props.other;
156+
return <>
157+
<div>{ props.param }</div>
158+
<div>{ staticProp }</div>
157159
</>
158-
);
160+
};
159161

160-
<MyComp param={( dynamic() )} other={ static } />
162+
<MyComp param={ dynamic() } other={ static } />
161163
```
162164

163-
Components may have children. This is available as props.children. It may be a node, a function, or a string, or an array of the aforementioned. Non-expression children like DOM nodes are set to evaluate lazily (upon access by default). For single expressions you must use {( )} to have the same behaviour.
165+
Components may have children. This is available as props.children. It may be a node, a function, or a string, or an array of the aforementioned. Non-expression children like DOM nodes are set to evaluate lazily (upon access by default).
164166

165167
## Fragments
166168

167169
This plugin also supports JSX Fragments with `<></>` notation. These will be compiled to arrays. The fragment syntax provides the convenience of being able to use the template syntax to wrap expressions.
168170

169171
## SVG
170172

171-
There is basic SVG support with this library. Most element/attributes should work but no support for namespaces yet.
173+
There is basic SVG support with this library. Most element attributes should work but no support for namespaces yet.
174+
172175
## Acknowledgements
173176

174177
The concept of using JSX to DOM instead of html strings and context based binding usually found in these libraries was inspired greatly by [Surplus](https://github.com/adamhaile/surplus).

package-lock.json

+49-49
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "babel-plugin-jsx-dom-expressions",
33
"description": "A JSX to DOM plugin that wraps expressions for fine grained change detection",
4-
"version": "0.12.2",
4+
"version": "0.13.0",
55
"author": "Ryan Carniato",
66
"license": "MIT",
77
"repository": {
@@ -18,12 +18,12 @@
1818
"prepublishOnly": "npm run build"
1919
},
2020
"devDependencies": {
21-
"@babel/core": "7.6.2",
21+
"@babel/core": "7.6.4",
2222
"babel-plugin-tester": "^7.0.1",
2323
"coveralls": "3.0.6",
24-
"dom-expressions": "~0.12.0",
24+
"dom-expressions": "0.13.0-beta.0",
2525
"jest": "^24.9.0",
26-
"rollup": "^1.22.0",
26+
"rollup": "^1.24.0",
2727
"rollup-plugin-node-resolve": "^5.2.0"
2828
},
2929
"dependencies": {

0 commit comments

Comments
 (0)