项目作者: alfa-laboratory

项目描述 :
Use your React components as WebComponents
高级语言: JavaScript
项目地址: git://github.com/alfa-laboratory/regap.git
创建时间: 2017-04-15T11:25:21Z
项目社区:https://github.com/alfa-laboratory/regap

开源协议:

下载


Regap

Use your React components as WebComponents.

Motivation

React is a very popular frontend framework. A lot of companies builds own UI libraries based on React.
Regap helps you to provide your React UI library as WebComponents. For example:

  • It helps your designer to build prototypes just using HTML/CSS.
  • It helps your 3rd party teams without special skills to build simple landing pages and etc.
  • It helps your to share knoweldge to all your teams about how your UI library looks and feels.

Base concepts

Attributes reflection

Regap provides feature to reflect WebComponents attributes to React components props.

Usage example

  1. // react-component.js
  2. import React from 'react';
  3. export default class ReactComponent extends React.Component {
  4. static propTypes = {
  5. someCounter: React.PropTypes.number.isRequired
  6. };
  7. render() {
  8. return (
  9. <div>Some counter: {this.props.someCounter}</div>
  10. );
  11. }
  12. }
  13. // regap-component.js
  14. import regap from 'regap';
  15. import ReactComponent from './react-component';
  16. regap('x-example', ReactComponent, {
  17. // Place your attributes description to `attrs` object. Use property name as attribute name and provide
  18. // `name` key to describe React component's prop name. Than provide `type` key to describe `prop` type.
  19. attrs: {
  20. 'some-counter': {
  21. name: 'someComponent',
  22. type: regap.types.NumberType
  23. }
  24. }
  25. });
  26. // Render WebComponent to body
  27. document.body.innerHTML = '<x-example some-counter="5"></x-example>';
  28. // Get WecComponent's instance
  29. let xExample = document.body.querySelector('x-example');
  30. xExample.setAttribute('some-counter', 10); // Rerender component throw `setAttribute` call.
  31. xExample['some-counter'] = 20; // Rerender component throw property assign.

Predefined types

  • regap.types.ArrayType
  • regap.types.BooleanType
  • regap.types.FunctionType
  • regap.types.NumberType
  • regap.types.ObjectType
  • regap.types.StringType

Callbacks binding

Regap provides feature to bind WebComponents callbacks to React components callbacks props.

Usage example

  1. // react-component.js
  2. import React from 'react';
  3. export default class ReactComponent extends React.Component {
  4. static propTypes = {
  5. onClick: React.PropType.func.isRequired
  6. };
  7. handleClick() {
  8. this.props.onClick('first value', 'second value');
  9. }
  10. render() {
  11. return (
  12. <div onClick={ this.handleClick } ></div>
  13. );
  14. }
  15. }
  16. // regap-component.js
  17. import regap from 'regap';
  18. import ReactComponent from './react-component';
  19. regap('x-example', ReactComponent, {
  20. // Place your callbacks description to `callbacks` object. Use property name as callback name and provide
  21. // `name` key to describe React component's prop name.
  22. callbacks: {
  23. click: {
  24. onFunctionName: 'onClick'
  25. }
  26. }
  27. });
  28. // Render WebComponent to body
  29. document.body.innerHTML = '<x-example></x-example>';
  30. // Get WecComponent's instance
  31. let xExample = document.body.querySelector('x-example');
  32. // Subscribe to React component's event.
  33. xExample.addEventListener('click', (event) => {
  34. console.log(event.detail[0]); // 'first value'
  35. console.log(event.detail[1]); // 'second value'
  36. });

Public methods binding

Regap provides feature to bind WebComponents public methods to React components public methods.

Usage example

  1. // react-component.js
  2. import React from 'react';
  3. export default class ReactComponent extends React.Component {
  4. /**
  5. * @public
  6. */
  7. focus() {
  8. this.input.focus();
  9. }
  10. render() {
  11. return (
  12. <input ref={(input) => { this.input = input; }} />
  13. );
  14. }
  15. }
  16. // regap-component.js
  17. import regap from 'regap';
  18. import ReactComponent from './react-component';
  19. regap('x-example', ReactComponent, {
  20. // Place your public methods description to `methods` object. Use property name as public method name and
  21. // provide `name` key to describe React component's public method name.
  22. methods: {
  23. focus: {
  24. name: 'focus'
  25. }
  26. }
  27. });
  28. // Render WebComponent to body
  29. document.body.innerHTML = '<x-example></x-example>';
  30. // Get WecComponent's instance
  31. let xExample = document.body.querySelector('x-example');
  32. // Call React component's public method.
  33. xExample.focus();

Slots

Regap provides feature to reflect WebComponents slots to React components props.

  1. // react-component.js
  2. import React from 'react';
  3. export default class ReactComponent extends React.Component {
  4. static propTypes = {
  5. someOtherChildren: React.PropTypes.node
  6. };
  7. render() {
  8. return (
  9. <div>
  10. {this.props.children}
  11. <span>{this.props.someOtherChildren}</span>
  12. </div>
  13. );
  14. }
  15. }
  16. // regap-component.js
  17. import regap from 'regap';
  18. import ReactComponent from './react-component';
  19. regap('x-example', ReactComponent, {
  20. // Place your slots description to `slots` object. Use property name as slot name and
  21. // provide `name` key to describe React component's prop name.
  22. //
  23. // Regap provides slot `children` by default.
  24. slots: {
  25. 'some-other-children': {
  26. name: 'someOtherChildren'
  27. }
  28. }
  29. });
  30. // Render WebComponent to body
  31. document.body.innerHTML =
  32. '<x-example>' +
  33. 'Some children content' +
  34. '<span slot="some-other-children">' +
  35. 'Some other children content' +
  36. '</span>'
  37. '</x-example>';

API Reference

  1. /**
  2. * Attribute description.
  3. *
  4. * @typedef {Object} AttrOption
  5. * @property {String} name React component prop name
  6. * @property {AttrType} type Attribute type
  7. */
  8. /**
  9. * Callback description.
  10. *
  11. * @typedef {Object} CallbackOption
  12. * @property {String} onFunctionName React component callback prop name
  13. */
  14. /**
  15. * Public method description.
  16. *
  17. * @typedef {Object} MethodOption
  18. * @property {String} name React component public method name
  19. */
  20. /**
  21. * Slot description.
  22. *
  23. * @typedef {Object} SlotOption
  24. * @property {String} name React prop name
  25. */
  26. /**
  27. * Regap component description.
  28. *
  29. * @typedef {Object} RegapComponentOptions
  30. * @property {Object.<AttrOption>} attrs
  31. * @property {Object.<CallbackOption>} callbacks
  32. * @property {Object.<SlotOption>} slots
  33. * @property {Object.<PublicMethodOption>} methods
  34. * @property {Function} onBeforeCreated
  35. * @property {Function} onAfterCreated
  36. * @property {Function} onBeforeAttached
  37. * @property {Function} onAfterAttached
  38. * @property {Function} onBeforeAttributeChanged
  39. * @property {Function} onAfterAttributeChanged
  40. * @property {Function} onBeforeDetached
  41. * @property {Function} onAfterDetached
  42. * @property {Object} ctor
  43. */
  44. /**
  45. * Register WebComponent based on React component.
  46. *
  47. * @param {String} tagName Tag name
  48. * @param {Function} reactComponentCtor React component constructor
  49. * @param {RegapComponentOptions} [options] Some options
  50. * @returns {Function} WebComponent constructor.
  51. */
  52. regap(tagName, reactComponentCtor, options = {});
  53. /**
  54. * Exported Regap HTMLElement.
  55. *
  56. * @type {Object}
  57. */
  58. regap.HTMLElement;
  59. /**
  60. * Exported mixins collection.
  61. *
  62. * @type {Array.<Object>}
  63. */
  64. regap.mixins;
  65. /**
  66. * Exported predefined types collection.
  67. *
  68. * @type {Object.<AttrType>}
  69. */
  70. regap.types;

License

  1. The MIT License (MIT)
  2. Copyright (c) 2017 Alfa Laboratory
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.