Skip to content

Instantly share code, notes, and snippets.

@jessuni
Last active January 30, 2022 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jessuni/33bfd7eb5433e2979af288c5514fea8c to your computer and use it in GitHub Desktop.
Save jessuni/33bfd7eb5433e2979af288c5514fea8c to your computer and use it in GitHub Desktop.
contenteditable-vue-3-decoupling
<template>
<div v-once contenteditable="true" @input="update" v-text="modelValue"></div>
</template>
<script>
export default {
props: {
modelValue: {
type: String,
default: '',
},
},
emits: ['update:modelValue'],
watch: {
modelValue(v) {
// or use a debounce with certain timeout here to check if user stops typing
if (document.activeElement === this.$el) {
return
}
this.$el.textContent = v
},
},
methods: {
update(e) {
this.$emit('update:modelValue', e.target.textContent)
},
},
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment