Code Monkey home page Code Monkey logo

react-custom-element-builder's Introduction

CircleCI

react-custom-element-builder

This helper function creates a custom element from a React class.

Installation

To install:

yarn add react-custom-element-builder

Simple use

Here is the simplest possible use:

import createCustomElement from 'react-custom-element-builder';

window.customElements.define('my-element', createCustomElement(MyReactComponent));

Attributes

You have to specify all the attributes you want to watch. Attributes are automatically coerced to props for the React component.

window.customElements.define('my-element', createCustomElement(MyReactComponent, {
  attributes: {
    name: 'Megan',
    value: undefined,
  },
}));

If the browser doesn't specify an attribute for name then the value will be megan.

Properties

You can map properties to the React component using the properties option:

window.customElements.define('my-element', createCustomElement(MyReactComponent, {
  properties: {
    images: {
      default: [],
    },
    href: {},
  },
}));

This would put images and href properties on the custom element that is then accessible via JS in the client.

Assuming that you have HTML like this:

<my-element id="foo">
</my-element>

You could have Javascript in the client like so:

document.getElementById('foo').images = [...];

And the React component would be re-rendered with the new property value.

Methods

Shocker, some HTML elements have methods on them, like play on a video element. It's not really the React way, but, if you want to do something like that, you can use the methods option.

window.customElements.define('my-video', createCustomElement(MyVideoComponent, {
  methods: {
    play() {
      this.setAttribute('playing', true);
    },
  },
}));

The point of making custom elements is for these elements to behave as you would think a native HTML element would behave. So if a native HTML element would have some methods you should add them.

Stateful components

For stateful components a reference to the react component is available on the _reference property. You can access that externally or within a method like so:

window.customElements.define('my-video', createCustomElement(MyVideoComponent, {
  methods: {
    play() {
      this._reference.play();
    },
  },
}));

Emitting events

Events will not pass from your React component up to the host page without adding the composed flag. Here is an example:

this.myRef.dispatchEvent(new CustomEvent('my-click', {
  composed: true,
}));

react-custom-element-builder's People

Contributors

jherr avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.