JSFiddle - React, Tailwind, and code Playground

by Jason Miller

HTML

<script src="https://npmcdn.com/preact@beta"></script>

CSS

* { color:#888; }

Babel + JSX

let { h, Component, render } = preact; /** @jsx h */

// demo
setTimeout( () => {
	class Clock extends Component {
		state = { time:new Date() };
		componentDidMount() {
			this.timer = setInterval(() => this.setState({ time:new Date() }), 1000);
		}
		componentWillUnmount() {
			clearInterval(this.timer);
		}
		render({ }, { time }) {
			return <span class="time">{time.toTimeString()}</span>
		}
	}
	render((
		<div style="border:1px solid red">
			<h4>Outside frame</h4>
			<Clock />
      <Framed>
				<div style="border:1px solid blue">
					<h4>Inside frame</h4>
					<Clock />
				</div>
      </Framed>
		</div>
  ), document.body);
});


/** Renders its children inside an iframe.
 *  @example
 *    <Framed>
 *			This is all rendered into
 *			<strong>an iframe!</strong>.
 *    </Framed>
 */
class Framed extends Component {
	componentDidUpdate() {
		this.renderChildren();
	}
	componentDidMount() {
		this.renderChildren();
	}
  renderChildren() {
		let win = this.frame && this.frame.contentWindow,
			body = win && win.document && win.document.body;
		if (!body) return this.setState({});
		this.rendered = render((
			<div style="position:static;overflow:visible;">
				{ this.props.children }
			</div>
		), body, this.rendered);
	}
  
  render({ children, ...props }) {
  	return <iframe ref={c => this.frame=c} frameBorder="0" {...props} />;
  }
}