Last active
January 30, 2022 09:38
-
-
Save jessuni/33bfd7eb5433e2979af288c5514fea8c to your computer and use it in GitHub Desktop.
contenteditable-vue-3-decoupling
This file contains hidden or 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
<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