Skip to content

Instantly share code, notes, and snippets.

@saibotsivad
Created June 22, 2018 15:46
Show Gist options
  • Save saibotsivad/675ee4d02a71d23405baffa4b1fb8489 to your computer and use it in GitHub Desktop.
Save saibotsivad/675ee4d02a71d23405baffa4b1fb8489 to your computer and use it in GitHub Desktop.
render components to target elements you don't have control over

from a discord discussion with Rich Harris ❤️

import { SomeChart } from './components/SomeChart.html';
import { SomeOtherThing } from './components/SomeOtherThing.html';
const embeds = {
'some-chart': SomeChart,
'some-other-thing': SomeOtherThing
};
document.on('ready', () => {
[...document.querySelectorAll('.interactive')].forEach(target => {
const Component = embeds[target.dataset.embed];
new Component({ target });
});
});
<div class="interactive" data-embed="some-chart"></div>
<script src="path/to/bundle.js"></script>
<!-- later, in a separate embed (which ends up in the same big blob of HTML) -->
<div class="interactive" data-embed="some-other-thing"></div>
<script src="path/to/bundle.js"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment