Skip to content

Instantly share code, notes, and snippets.

@tbranyen
Created January 1, 2017 21:25
Show Gist options
  • Save tbranyen/ac44ad52c8c352a671c3efca7517d0fe to your computer and use it in GitHub Desktop.
Save tbranyen/ac44ad52c8c352a671c3efca7517d0fe to your computer and use it in GitHub Desktop.
diffHTML Web Component
import { html } from 'diffhtml';
import { WebComponent } from 'diffhtml-components';
class CocktailEditor extends WebComponent(['recipe']) {
render() {
return html`${this.state.markup}`;
}
constructor() {
super();
this.fetchRecipe(this.props.recipe);
}
componentWillReceiveProps(nextProps) {
this.fetchRecipe(nextProps.recipe);
}
fetchRecipe(recipe) {
return CocktailEditor.importRecipe(recipe).then(ev => {
this.setState({ markup: html(ev.target.response) });
});
}
static importRecipe(href) {
return new Promise((onload, onerror) => {
const xhr = Object.assign(new XMLHttpRequest(), { onload, onerror });
xhr.open('GET', href);
xhr.send();
});
}
}
customElements.define('cocktail-editor', CocktailEditor);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment