Skip to content

Instantly share code, notes, and snippets.

@pvolyntsev
Last active March 16, 2017 16:38
Show Gist options
  • Save pvolyntsev/67acceebdbbdd55ab6a7f6e2369f099c to your computer and use it in GitHub Desktop.
Save pvolyntsev/67acceebdbbdd55ab6a7f6e2369f099c to your computer and use it in GitHub Desktop.
Vue + WordPress + wpColorPicker
Vue.component('colorPicker', {
props: ['value'],
template: '<input type="text">',
mounted: function () {
var vm = this;
$(this.$el)
.val(this.value)
// WordPress color picker
.wpColorPicker({
defaultColor: this.value,
change: function(event, ui) {
// emit change event on color change using mouse
vm.$emit('input', ui.color.toString());
}});
},
watch: {
value: function (value) {
// update value
$(this.$el).wpColorPicker('color', value);
}
},
destroyed: function () {
$(this.$el).off().wpColorPicker('destroy'); // (!) Not tested
}
});
<div id="demo"></div>
<script type="text/x-template" id="demo-template">
<color-picker v-model="color" placeholder="#000000"></color-picker>
</script>
var vm = new Vue({
el: '#demo',
template: '#demo-template',
data: {
color: '#ff0000' // this will be binded to wpColorPicker
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment