Created
January 1, 2017 21:25
-
-
Save tbranyen/ac44ad52c8c352a671c3efca7517d0fe to your computer and use it in GitHub Desktop.
diffHTML Web Component
This file contains 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 { 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