Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proof of concept: Shadow DOM support #1877

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions examples/shadow-dom/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/** @jsx React.DOM */

function createCustomElementClass(tagName) {
if (typeof window !== 'undefined') {
document.registerElement(tagName, {
prototype: Object.create(HTMLElement.prototype)
});
}
return React.DOM.createDOMComponentClass(false, tagName);
}

var ShadowRoot = React.createClass({
getDefaultProps: function() {
return {component: React.DOM.div};
},

render: function() {
return this.props.component();
},

componentDidMount: function() {
var shadowRoot = window.honk = this.getDOMNode().createShadowRoot();
shadowRoot.innerHTML = '<span></span>';
this._shadowRoot = shadowRoot.children[0];
this.rerender();
},

componentDidUpdate: function() {
this.rerender();
},

rerender: function() {
React.renderComponent(<span>{this.props.children}</span>, this._shadowRoot);
}
});

var WebComponent = React.createClass({
render: function() {
return this.transferPropsTo(
<ShadowRoot component={createCustomElementClass(this.props.tagName)}>
{this.props.children}
</ShadowRoot>
);
}
});

var PaperButton = React.createClass({
render: function() {
return (
<WebComponent tagName="paper-button">
<style>{css}</style>
<h3>Hello world</h3>
</WebComponent>
);
}
});

var Hello = React.createClass({
render: function() {
return (
<div>
<PaperButton />
</div>
);
}
});

React.renderComponent(<Hello />, document.body);
34 changes: 34 additions & 0 deletions examples/shadow-dom/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv='Content-type' content='text/html; charset=utf-8'>
<title>Basic Example with External JSX</title>
<link rel="stylesheet" href="../shared/css/base.css" />
</head>

<body>
<h1>Basic Example with External JSX</h1>
<div id="container">
<p>
If you can see this, React is not working right. This is probably because you&apos;re viewing
this on your file system instead of a web server. Try running
<pre>
python -m SimpleHTTPServer
</pre>
and going to <a href="http://localhost:8000/">http://localhost:8000/</a>.
</p>
</div>
<h4>Example Details</h4>
<p>This is written with JSX in a separate file and transformed in the browser.</p>
<p>
Learn more about React at
<a href="http://facebook.github.io/react" target="_blank">facebook.github.io/react</a>.
</p>
<script src="../shared/thirdparty/es5-shim.min.js"></script>
<script src="../shared/thirdparty/es5-sham.min.js"></script>
<script src="../shared/thirdparty/console-polyfill.js"></script>
<script src="../../build/react.js"></script>
<script src="../../build/JSXTransformer.js"></script>
<script type="text/jsx" src="example.js"></script>
</body>
</html>
1 change: 1 addition & 0 deletions src/browser/ReactDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ var injection = {
}
};

ReactDOM.createDOMComponentClass = createDOMComponentClass;
ReactDOM.injection = injection;

module.exports = ReactDOM;
9 changes: 6 additions & 3 deletions src/browser/ui/ReactDOMComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,12 @@ function assertValidProps(props) {
function putListener(id, registrationName, listener, transaction) {
var container = ReactMount.findReactContainerForID(id);
if (container) {
var doc = container.nodeType === ELEMENT_NODE_TYPE ?
container.ownerDocument :
container;
var doc;
if (container.parentNode.hasOwnProperty('olderShadowRoot') || container.nodeType !== ELEMENT_NODE_TYPE) {
doc = container;
} else {
doc = container.ownerDocument;
}
listenTo(registrationName, doc);
}
transaction.getPutListenerQueue().enqueuePutListener(
Expand Down
2 changes: 0 additions & 2 deletions vendor/browser-transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ var docblock = require('jstransform/src/docblock');
var transform = require('jstransform').transform;
var visitors = require('./fbtransform/visitors');

var runScripts;
var loadScripts;
var headEl;
var dummyAnchor;
var inlineScriptCount = 0;
Expand Down