Last active
May 12, 2017 14:31
-
-
Save developit/297df6840052fcd32ced1335df96def6 to your computer and use it in GitHub Desktop.
Just bundle embed.js via webpack with target:umd and the name "embed" (or pick one)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="/path/to/embed.umd.js"></script> | |
<div id="foo"></div> | |
<script> | |
var parent = document.getElementById('foo') | |
embed.renderWidget( | |
'foo', // widget name | |
{ a: 'b' }, // props | |
parent // render into this | |
); | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { h, render } from 'preact'; | |
// could use webpack require context here, but we'll be verbose: | |
import Foo from './components/foo'; | |
import Bar from 'component-from-some-other-repo'; | |
const COMPONENTS = { | |
Foo, | |
Bar | |
}; | |
// case-insensitive lookup of a named component ("widget") | |
export function getWidget(name) { | |
for (let i in COMPONENTS) { | |
if (COMPONENTS.hasOwnProperty(i) && i.toLowerCase()===name.toLowerCase()) { | |
return COMPONENTS[i]; | |
} | |
} | |
} | |
export function renderWidget(name, props, parent) { | |
let Widget = getWidget(name); | |
render(<Widget {...props} />, parent, parent.firstChild); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment