Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for DocumentFragment in TypeScript #28

Merged
merged 4 commits into from
Jan 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,10 @@ function h(tagName, attrs) {
}

exports.h = h;

// Improve TypeScript support for DocumentFragment
// https://github.com/Microsoft/TypeScript/issues/20469
exports.React = {
fregante marked this conversation as resolved.
Show resolved Hide resolved
createElement: h,
Fragment: DocumentFragment
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@
"rules": {
"react/prop-types": 0,
"react/no-unknown-property": 0,
"react/no-danger": 0
"react/no-danger": 0,
"import/first": 0
},
"settings": {
"react": {
Expand Down
19 changes: 16 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ $ npm install --save dom-chef
## Usage

Make sure to use a JSX transpiler, set JSX [`pragma`](https://babeljs.io/docs/en/next/babel-plugin-transform-react-jsx.html#pragma)
to `h` and optinally the [`pragmaFrag`](https://babeljs.io/docs/en/next/babel-plugin-transform-react-jsx.html#pragmafrag)
to `h` and optionally the [`pragmaFrag`](https://babeljs.io/docs/en/next/babel-plugin-transform-react-jsx.html#pragmafrag)
to `DocumentFragment` [if you need fragment support](https://reactjs.org/blog/2017/11/28/react-v16.2.0-fragment-support.html).

```js
Expand All @@ -40,8 +40,8 @@ const plugins = [
[
'@babel/plugin-transform-react-jsx',
{
pragma: 'h',
pragmaFrag: 'DocumentFragment',
pragma: 'h',
pragmaFrag: 'DocumentFragment',
}
]
];
Expand All @@ -65,6 +65,19 @@ const el = (
document.body.appendChild(el);
```

### Alternative usage

You can avoid configuring your JSX compiler by just letting it default to `React` and exporting the `React` object:

```js
const {React} = require('dom-chef');
```

This has the advantage of enabling `Fragment` support with the TypeScript compiler, if you're using it compile JSX without Babel. Related issue: https://github.com/Microsoft/TypeScript/issues/20469

```
TS17016: JSX fragment is not supported when using --jsxFactory
```
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this to possibly reach people who google that and to make the exact error clear.


## Recipes

Expand Down
4 changes: 3 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import browserEnv from 'browser-env';
import {spy} from 'sinon';
import test from 'ava';
import {h} from '.';

// This order and `require` are necessary because the
// `DocumentFragment` global is used/exported right away
browserEnv();
const {h} = require('.');

test('render childless element', t => {
const el = <br/>;
Expand Down