Skip to content

Instantly share code, notes, and snippets.

@pl12133
Last active September 6, 2018 14:44
Show Gist options
  • Select an option

  • Save pl12133/dc6e91f5ea903067c1475ec8bba413dd to your computer and use it in GitHub Desktop.

Select an option

Save pl12133/dc6e91f5ea903067c1475ec8bba413dd to your computer and use it in GitHub Desktop.
import { h, Component } from 'preact';
function withScript(src) {
return (Child) => class ScriptLoader extends Component {
static script;
componentWillMount() {
if (typeof ScriptLoader.script === 'undefined') {
ScriptLoader.script = document.createElement('script');
ScriptLoader.script.src = src;
ScriptLoader.script.addEventListener('load', () => this.setState({ loaded: true }));
document.body.appendChild(ScriptLoader.script);
}
}
render(props, { loaded }) {
return loaded && <Child {...props} />
}
}
}
/**
withScript('http://n12v.com/focus-transition/flying-focus.js');
withScript('http://nv.github.io/focus-snail/standalone/focus-snail.js');
withScript('http://nv.github.io/focus-hug/standalone/focus-hug.js');
withScript('http://nv.github.io/focus-zoom/focus-zoom.js');
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment