Custom elements JSX-render
Small library for creating HTMLElements from JSX markup. It is very useful inside of custom elements.
Here is an example of simple SPA created with this library: https://github.com/DenRostokin/learning-custom-elements
Contents
How to install
The required packages are @babel/plugin-syntax-jsx
, @babel/plugin-transform-react-jsx
and custom-elements-jsx
:
$ npm install --save-dev @babel/plugin-syntax-jsx @babel/plugin-transform-react-jsx $ npm install --save custom-elements-jsx
or
$ yarn add --dev @babel/plugin-syntax-jsx @babel/plugin-transform-react-jsx $ yarn add custom-elements-jsx
First of all you need to config your .babelrc file like this:
// .babelrc
Usage
Lets create our custom element:
import jsx Component from "custom-elements-jsx"; { const children age = thisprops; return <div ="customElement-container"> <h3>`Hello, `</h3> <h4>`You are years old`</h4> </div> ; } // Check that the element hasn't already been registeredif !windowcustomElements windowcustomElements;
Remember that you must use dash in the custom elements names. https://developers.google.com/web/fundamentals/web-components/customelements
Now you can use this component inside of other custom elements or append it to the root element:
const element = <custom =>Den</custom-element>; const root = document; // the element is an instanse of the HTMLElementroot;
Of cause element with id="root" should exist in your html-file:
This is a React way when you have only one empty div-element and you render your app into it.
When you create a custom component first of all you go to the constructor of it. You should remember, that you don't have props inside of constructor method. If you want to do something before a first render you have to call special method, called componentDidCreate
. This method will be called when component will be added into DOM tree, but before the render function.
After this method you are going to the render method. You can return such values as: HTMLElement, array of HTMLElements like children, fragment created by createFragmentWithChildren(), null, undefind or false.
Also you have 3 additional methods of component lifecycle: componentDidMount
, componentDidUpdate
, componentWillUnmount
. Method componentDidMount
will be called after the render method. Method componentWillUnmount
will be called after removing custom element from a DOM tree.
You can update custom element content by using method update()
without parameters.render
method will be called inside of update
and then will be called componentDidUpdate
method.
Custom element inherited from a Component class has state
property and setState
function which set new state and call update
method to update element content.
import jsx Component from 'custom-elements-jsx' { thisstate = value: 1 } { this } { const value = thisstate return <div ="container"> <h3>value</h3> <button =>Increment!</button> </div> } }}
By default all props that you specify to custom element will be passed inside of it. If you want to set some prop as attribute you need to specify catch
prop:
const element = <custom ="hello" ="id" />;
You can specify multiple props in the catch
. You need to use array for this:
const element = <custom ="hello" ="button" = />;
Features
You can connect Redux to your custom element:
import jsx Component from "custom-elements-jsx";import bindActionCreators from "redux"; import store from "./store.js";import * as actions from "./actions.js"; const mapState = value: statevalue; const bindedActions = ; { thisunsubscribe = store; } { this; } { const value = ; return <button =>value</button>; }
You have to use store.getState()
inside of render
method to get new values from the store after updating.
Custom-elements-jsx allows you to use element <custom-fragment>
. This element works like React.Fragment:
import jsx Component from "custom-elements-jsx"; { return <custom> <h1>Title</h1> <div>Simple block</div> <p>Empty paragraph</p> </custom-fragment> ; } if !windowcustomElements windowcustomElements; documentbody;
After that you will get the markup like this:
Title Simple block Empty paragraph
Also you can use fuction createFragmentWithChildren(children, props)
. This function receives array of children (each child must be an instanse of the HTMLElement) and props object and adds props into each child. Then it creates fragment using document.createDocumentFragment()
function and fills the fragment by children with props.
; const div = document;const p = document; const children = div p; documentbody;
This function can be usefull if you need to add props to the children inside your custom element:
import jsx Component createFragmentWithChildren from "custom-elements-jsx"; { const name = thisprops; return <h1>name</h1>; } if !windowcustomElements windowcustomElements; { const name = "John"; return ; } if !windowcustomElements windowcustomElements; documentbody;
After that you will get the markup like this:
John John
How to test
To test this library you need to install Chrome browser if you still dont have because
karma` test runner needs it. Then you can run:
npm install && npm run test
or
yarn && yarn test
If you want to run tests in watch mode you need to use test:watch
except test
command.