Generator Functional Components. A wrapper for creating Web components through JS Generator function
npm install @web-companions/gfc> Generator Functional Components. A wrapper for creating Web components with JS Generator function
- ๐ Fast: Built with standards based Custom Elements and JS Generator.
- ๐ช Typed: Written in TypeScript.
- โ๏ธ JSX: Can be used with JSX or without it (babel-plugin-transform-jsx-to-tt).
- ๐ชข Flexible: Fit to different template libraries.
- ๐งน No dependencies
---
---
```
npm install @web-companions/gfc --save
---
For the best DX I suggest using this library with babel-plugin-transform-jsx-to-tt and lit-html or another library to rendering templates to DOM.
index.jsx
`tsx
import { EG, p } from '@web-companions/gfc';
import { render } from 'lit-html';
/**
* Counter element
*/
export const counterElement = EG({
props: {
msg: p.req
},
})(function* (props) {
let count = 0;
while (true) {
props = yield render(
<>
type="button"
onclick={() => {
count++;
this.next();
}}
>
{props?.msg}
{count}
>,
this
);
}
});
// define a new custom Counter element with tag 'demo-counter-element'
const CounterElement = counterElement('demo-counter-element');
/**
* ROOT element
*/
EG({
props: {
header: p.req
},
})(function* (props) {
while (true) {
props = yield render(
style={css
margin: 10px;
}
>
{props.header}
More examples are here.
---
#### Node view
- This type of view could be created only inside an Element view.
- If some of Node view will be rendered by a condition we should create them with prepared ref objects. Otherwise, some of Nodes can start to use the same inner state.