Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/gvergnaud/vdom-tag

A template literals tag function to build a virtual dom tree
https://github.com/gvergnaud/vdom-tag

dom html template-tags virtual-dom

Last synced: about 2 months ago
JSON representation

A template literals tag function to build a virtual dom tree

Awesome Lists containing this project

README

        

# vdom-tag

Es6 Tag function to create a virtual-dom tree from a template literal.

Very similar to [hyperx](https://github.com/choojs/hyperx), but has a better
support for custom components and supports multiple root elements.

This package does not provide any way to render the tree, you have to combine it
with a virtual dom implementation to make it useful (e.g. virtual-dom, React, ...).

```
npm install vdom-tag
```

### Examples

```js
import h from 'virtual-dom/h'
import createElement from 'virtual-dom/create-element'
import createTag from 'vdom-tag'

const html = createTag(h)

const title = 'world'

const tree = html`


hello ${title}!

Connected users



    ${['Oh', 'My', 'God'].map(name => html`
  • console.log(name, 'clicked!')}>
    ${name}

  • `)}


`

document.body.appendChild(createElement(tree))
```

### Features

#### Multiple root elements

```js
const list = html`

  • No

  • need

  • for

  • React.Fragment

  • !!

  • `

    const app = html`


    ${list}

    `

    document.body.appendChild(createElement(app))
    ```

    #### Custom elements

    ```js
    const MyComponent = () => html`

    hey

    `
    const tree = html`

    <${MyComponent} />

    `
    ```

    #### With React

    ```js
    import React from 'react'
    import { render } from 'react-dom'
    import createTag from 'vdom-tag'
    const html = createTag(React.createElement)

    const App = ({ name = 'React' }) => html`

    Hello, ${name}!


    `

    render(, document.querySelector('#root'))
    ```