# reactive-elements

> use a React.js component as web component

Latest version **1.0.0-alpha.1** (published 2019-08-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install reactive-elements
pnpm add reactive-elements
yarn add reactive-elements
bun add reactive-elements
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0-alpha.1 |
| Published | 2019-08-21 |
| First published | 2014-12-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 78.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 695 |
| Author | Denis Radin aka PixelsCommander |
| Maintainers | jackfranklin, lgi, pixelscommander |
| Keywords | react, web-components, custom elements, reactjs |

## Links

- npm: https://www.npmjs.com/package/reactive-elements
- Repository: https://github.com/reactive-elements/reactive-elements
- Issues: https://github.com/reactive-elements/reactive-elements/issues
- npm.io page: https://npm.io/package/reactive-elements

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.0.0-alpha.1 (latest) — 2019-08-21
- 1.0.0-alpha.0 (next) — 2018-12-19
- 0.10.0 — 2018-07-25
- 0.9.0 — 2018-05-10
- 0.8.0 — 2018-05-02
- 0.7.0 — 2018-02-27
- 0.6.8 — 2017-09-15
- 0.6.7 — 2016-10-11
- 0.6.5 — 2016-07-11
- 0.6.4 — 2016-04-21
- 0.6.3 — 2015-11-04
- 0.6.2 — 2015-11-04
- 0.6.1 — 2015-11-04
- 0.6.0 — 2015-10-27
- 0.5.9 — 2015-10-27
- … 14 more at https://npm.io/package/reactive-elements/versions

## README

<a href="http://pixelscommander.com/polygon/reactive-elements/example/#.U0LMA62Sy7o">
    <img alt="Reactive Elements" src="http://pixelscommander.com/polygon/reactive-elements/assets/logo-reactive-elements-small.png"/>
</a>

# Note! The docs here are for the v1.0.0 alpha. This is _not_ ready for production use yet.

You should use this README, which refers to 0.10.0, the latest stable version on npm: https://github.com/PixelsCommander/ReactiveElements/blob/7cce3d7b472989878ac1433cec0e8168fd4136aa/README.md



# Convert React.js components into Web Components

```sh
npm install reactive-elements
yarn add reactive-elements
```

## How to use?

### Directly in a browser

**Placing component in a pure HTML**

```html
<body>
	<my-react-component items="{window.someArray}"></my-react-component>
</body>
```

**React class definition**

```js
/* @jsx React.DOM */
MyComponent = React.createClass({
  render: function() {
    console.log(this.props.items); // passed as HTML tag`s argument
    console.log(this.props.children); // original tag children
    return (
      <ul>
        <li>React content</li>
      </ul>
    );
  },
});

ReactiveElements('my-react-component', MyComponent);
```

### With Bundler

```js
import React, { Component } from 'react';
import ReactiveElements from 'reactive-elements';

class Welcome extends Component {
  render() {
    return <h1>Hello, {this.props.name}</h1>;
  }
}

ReactiveElements('welcome-component', Welcome);
```

## Nesting

Original children of a custom element is injected to component as
`this.props.children`.

```html
<my-react-component>Hello world</my-react-component>
```

In this case `this.props.children` is equal to "Hello world".

Container node of the element is passed as `this.props.container`. Both
props.container and props.children have type of `documentFragment`.

## Boolean attribute transforms (added in version 0.7.0)

An attribute that has the value `"true"` or `"false"` will be converted into the
boolean `true` or `false` when given to the React component:

```html
<my-react-component is-logged-in="true">Hello world</my-react-component>
```

Here, `this.props.isLoggedIn === true` within the React component.

If you don't want this behaviour you can disable it with a special attribute:

```html
<my-react-component is-logged-in="true" reactive-elements-no-boolean-transform>Hello world</my-react-component>
```

## Exposing components methods on custom element

If you want to expose React component methods on custom element - assign them to
component as following:

```js
componentDidMount: function() {
    this.props.container.setTextContent = this.setTextContent.bind(this);
}
```

## Handling attributes change

You may add `attributeChanged` callback to component in order to handle / modify
/ filter incoming values.

```js
attributeChanged: function(attributeName, oldValue, newValue) {
    console.log('Attribute ' + attributeName + ' was changed from ' + oldValue + ' to ' + newValue);
    this.props[attributeName] = parseInt(newValue);
}
```

## Communicate via DOM events

You may trigger DOM event from React component by using following snippet:

```js
var event = new CustomEvent('change', {
  bubbles: true,
});
React.findDOMNode(this).dispatchEvent(event);
```

Subscribing to DOM events is similar:

```js
React.findDOMNode(this).addEventListener('change', function(e){...});
```

## Options

You can also specify options to the `ReactiveElements` call, e.g.

```js
ReactiveElements('welcome-component', Welcome, options);
```

### `options.useShadowDom` _(default `false`)_

By default, your React element is rendered directly into the web-component root. However, by setting this option - your React element will instead be rendered in a [Shadow DOM](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_shadow_DOM) inside the web-component instead.

## Dependencies

- [React.js](https://github.com/facebook/react)
- [React DOM](https://github.com/facebook/react)
- Custom elements support or
  [polyfill](https://github.com/WebComponents/webcomponentsjs)
- Support or [polyfills](https://github.com/zloirock/core-js) for:
  - `regexp.match`
  - `regexp.replace`
  - `object.define-setter`
  - `object.define-getter`
  - `object.define-property`
  - `function.name`
  - `web.dom.iterable`
  - `array.iterator`
  - `object.keys`
  - `object.set-prototype-of`
  - `reflect.construct`
  - `function.bind`

## License

MIT: http://mit-license.org/

Copyright 2014 Denis Radin aka [PixelsCommander](http://pixelscommander.com)

Inspired by Christopher Chedeau`s
[react-xtags](http://github.com/vjeux/react-xtags/)

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