Resources

Vue Component for Inline Text Editing: Enhancing Web Interfaces

Vue Component for Inline Text Editing: Enhancing Web Interfaces

For plain text, switch between a rendered element and a textarea. Reach for a WYSIWYG library only when users need formatting. And v-model does not work on contenteditable elements, which is why so many first attempts at inline editing in Vue break.

Textarea toggle or contenteditable

A toggle shows a paragraph, swaps in a textarea on click and swaps back on save. A contenteditable element lets people type directly into the paragraph, which feels better and costs more code.

For anything that is not rich text, pick the toggle.

Plain text or rich text

Product names, titles and short descriptions are plain text. Blog posts and comments with bold and links are rich text, and that is where an editor such as Froala earns its place, since building bold, lists and link dialogs yourself is a project of its own. Froala’s own comparison of HTML and WYSIWYG editors makes the case for the second group, though it is written by a vendor of one.

Why not put v-model on a contenteditable element?

v-model is built for native form elements such as input, textarea and select. A contenteditable div has no value property, so you listen for the input event, read the text yourself and write it back.

Writing it back is the catch. If the component re-renders the element’s content from state on every keystroke, the cursor jumps to the start of the text, and users notice within a second.

Display mode versus edit mode, in code

The Vue components model fits this problem well: one component owns its own edit state, so a page can hold ten of them without any shared variable. The component below takes the text as a prop, keeps a draft while editing and emits the new value on save.

<script setup>
import { ref } from 'vue'

const props = defineProps({ modelValue: String })
const emit = defineEmits(['update:modelValue'])

const editing = ref(false)
const draft = ref('')

function start() {
  draft.value = props.modelValue
  editing.value = true
}

function save() {
  emit('update:modelValue', draft.value)
  editing.value = false
}

function cancel() {
  editing.value = false
}
</script>

<template>
  <p v-if="!editing" @click="start">{{ modelValue }}</p>
  <div v-else>
    <textarea v-model="draft"></textarea>
    <button @click="save">Save</button>
    <button @click="cancel">Cancel</button>
  </div>
</template>

Diagram of a Vue inline editor moving from display mode to edit mode and back through a save step with sanitizing

Use it as <InlineText v-model=”title” /> and the parent keeps the saved value.

Working on a draft instead of the real value is what makes Cancel trivial, and it also means a failed save never leaves the page showing text the server does not have. If the save request fails, keep the editor open with the draft intact and show the error next to it. Closing the editor and silently dropping the text is the version users complain about.

Obviously, you also want the paragraph to be keyboard reachable. Click handlers on a paragraph are invisible to people who tab through the page, so give it a button role or add an explicit edit button.

Inline editors tend to break in a few predictable places.

  • Losing unsaved text when the parent re-renders
  • Saving on blur, which fires when someone clicks Cancel
  • Rendering rich text back with v-html

The third is the dangerous one. Curly-brace interpolation escapes HTML, so the code above is safe as written. Rich text output from an editor is HTML, and rendering it with v-html means any script a user can type will run for the next visitor. Sanitize it on the server before saving and again before showing it, and read up on how cross-site scripting threats arise.

For Froala, install the package with npm install vue-froala-wysiwyg –save. Its documentation page for Vue 2 registers the plugin with Vue.use and uses the template line below, and it points to a separate SDK for Vue 3, so check that page instead of copying Vue 2 snippets into a Vue 3 project. Froala is a commercial editor, and the sample on that page runs on a trial license key that is not for production.

<froala :tag="'textarea'" :config="config" v-model="model"></froala>

With a library in the mix, the snags are different.

  • Browsers can render the same editor output slightly differently
  • Custom plugins and advanced formatting need more integration work than the basic toolbar

Short answers

Does Vue update the page while someone types?

Yes, the bound data is reactive.

Can any WYSIWYG library work inside Vue?

Mostly, by wrapping it in a component and syncing content through props and events.

Do I need Vue 3 for this?

The toggle pattern works in both, but the script setup syntax above is Vue 3. In Vue 2 the same logic goes into data and methods.

Bogdan Sandu

Stay sharp. Ship better code.

Every week: one curated article, one tool worth knowing, one tip you can use tomorrow. No noise, no padding.