Skip to content

Commit

Permalink
feat: passthru Component and render from preact
Browse files Browse the repository at this point in the history
Now instead of:

```js
const { Component } = require('preact');
const h = require('phy');
```

you can say:

```js
const { h, Component } = require('phy');
```

(but the old syntax also works)
  • Loading branch information
dbushong committed Oct 10, 2018
1 parent 106ed18 commit 31a2298
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ module.exports = function SomeComponent() {
}
```

or, if you need access to other things from Preact, `Component` and `render`
are passed thru for your convenience:

```js
const { h, Component } = require('phy');

class SomeComponent extends Component {
render() {
return h('#foo', h('span.bar', 'whee!'));
}
}
module.exports = SomeComponent;
```

## Optional Tag Helpers

At the cost of a modestly larger import and slight function call overhead,
Expand Down
8 changes: 7 additions & 1 deletion lib/phy.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

'use strict';

const preact = require('preact');

// simplified from lodash
const objectCtorString = Object.toString();
function isPlainObject(obj) {
Expand Down Expand Up @@ -85,4 +87,8 @@ function h(createElement, selector, attrs) {
return createElement.apply(null, [tag, attrs].concat(kids));
}

module.exports = h.bind(null, require('preact').createElement);
module.exports = exports = h.bind(null, preact.createElement);

exports.h = exports;
exports.Component = preact.Component;
exports.render = preact.render;
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions test/phy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@ const assert = require('assertive');
const h = require('../');
const render = require('preact-render-to-string');

const { h: h2, Component } = require('../');

function Comp(props) {
const attrs = Object.assign({}, props);
delete attrs.children;
return h('span', attrs, props.children);
}

class Comp2 extends Component {
render() {
return h2('span', 'hooray');
}
}

const tests = [
[
'tag, id, multiple classes',
Expand Down Expand Up @@ -77,6 +85,7 @@ const tests = [
h(Comp, { alt: 'meow' }, h('b')),
'<span alt="meow"><b></b></span>',
],
['Component passthru', h2(Comp2), '<span>hooray</span>'],
];

describe('phy', () => {
Expand Down

0 comments on commit 31a2298

Please sign in to comment.