# virtual-widget

> Create a virtual-dom widget

Latest version **1.3.0** (published 2016-03-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install virtual-widget
pnpm add virtual-widget
yarn add virtual-widget
bun add virtual-widget
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.0 |
| Published | 2016-03-12 |
| First published | 2015-10-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 22 |
| Maintainers | yoshuawuyts |
| Keywords | virtual-dom, mercury, widget |

## Links

- npm: https://www.npmjs.com/package/virtual-widget
- Repository: https://github.com/yoshuawuyts/virtual-widget
- Homepage: https://github.com/yoshuawuyts/virtual-widget#readme
- Issues: https://github.com/yoshuawuyts/virtual-widget/issues
- npm.io page: https://npm.io/package/virtual-widget

## Dependencies (1)

- [noop2](https://npm.io/package/noop2.md) ^2.0.0

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 1.3.0 (latest) — 2016-03-12
- 1.2.2 — 2016-03-10
- 1.2.1 — 2016-03-10
- 1.2.0 — 2015-12-29
- 1.1.2 — 2015-10-06
- 1.1.1 — 2015-10-06
- 1.1.0 — 2015-10-06
- 1.0.0 — 2015-10-04

## README

# virtual-widget

[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][codecov-image]][codecov-url]
[![Downloads][downloads-image]][downloads-url]
[![js-standard-style][standard-image]][standard-url]

Create a virtual-dom widget.

## Installation
```sh
$ npm install virtual-widget
```

## Usage
```js
// map.js
const virtualWidget = require('virtual-widget')

module.exports = virtualWidget({
  init: function (state) {
    this.state = state
    var elem = document.createElement('div')
    this.map = GoogleMap(elem)
    this.map.setPosition(this.state.position)
    return elem
  },
  update: function (prev, el) {
    this.map = this.map || prev.map
    this.map.setPosition(this.state.position)
  },
  destroy: function (el) {
    // clear position
    this.state.position.set({ x: 0, y: 0 })
  }
})
```
```js
// view.js
const map = require('./map')

module.exports = view

function view (h, state) {
  return h('section.foo', [
    map(state)
  ])
}
```

## API
### render = virtualWidget(hooks)
Create a new `virtual-widget` using hooks. The following hooks are available:
- __init__: run when the element is first created. Define the instantiation
  logic here.
- __update__: run on a re-render. Gives a chance to update state.
- __destroy__: run before the widget is unmounted. It is passed the HTMLElement
  associated with the widget that will be removed. Generally used to clean up
  data and remove hooks.
- __render__: return a string of HTML for server side rendering. It's
  recommended to use [vdom-to-html][1] for server-side rendering.

## See Also
- [virtual-dom/docs/widgets](https://github.com/Raynos/mercury/blob/master/docs/widgets.md)
- [mercury/docs/how-to-do-custom-rendering](https://github.com/Raynos/mercury/blob/master/docs/faq.md#how-do-i-do-custom-rendering)
- [vdom-to-html][1]

## License
[MIT](https://tldrlegal.com/license/mit-license)

[npm-image]: https://img.shields.io/npm/v/virtual-widget.svg?style=flat-square
[npm-url]: https://npmjs.org/package/virtual-widget
[travis-image]: https://img.shields.io/travis/yoshuawuyts/virtual-widget/master.svg?style=flat-square
[travis-url]: https://travis-ci.org/yoshuawuyts/virtual-widget
[codecov-image]: https://img.shields.io/codecov/c/github/yoshuawuyts/virtual-widget/master.svg?style=flat-square
[codecov-url]: https://codecov.io/github/yoshuawuyts/virtual-widget
[downloads-image]: http://img.shields.io/npm/dm/virtual-widget.svg?style=flat-square
[downloads-url]: https://npmjs.org/package/virtual-widget
[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
[standard-url]: https://github.com/feross/standard
[1]: https://github.com/nthtran/vdom-to-html

---
_Source: https://npm.io/package/virtual-widget · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
