-
Notifications
You must be signed in to change notification settings - Fork 1
/
react-shadow-dom.js
36 lines (34 loc) · 1.28 KB
/
react-shadow-dom.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*! React Shadow DOM v 0.0.3 | Copyright (c) 2016 Ivan (3axap4eHko) Zakharchenko*/
'use strict';
( function(global) {
var React;
var ReactDOM;
if (typeof require === 'function') {
React = require('react');
ReactDOM = require('react-dom')
} else {
React = global.React;
ReactDOM = global.ReactDOM;
}
function aggregate(data, attr) {
return (data[attr.name]=attr.value,data);
}
function attributesToObject(element) {
return Array.from(element.attributes).reduce(aggregate, {});
}
function register(name, componentClass, onRender) {
var component = Object.create(HTMLElement.prototype);
component.createdCallback = function() {
var shadow = this.createShadowRoot();
var props = attributesToObject(this);
var element = React.createElement(componentClass, props);
ReactDOM.render(element, shadow, onRender);
};
document.registerElement(name, { prototype: component });
return componentClass;
}
React.registerShadow = register;
React.createShadowClass = function (name, spec) {
return register(name, React.createClass(spec));
};
})(typeof global == 'object' ? global : typeof window == 'object' ? window : this);