Veil UI
Components

Rich Text

A lightweight contenteditable editor with basic formatting controls.

Installation

$ npx veil-ui@latest add rich-text

Usage

import { RichText } from "@/components/ui/rich-text";

<RichText
  onChange={(html) => setValue(html)}
  defaultValue="<p>Hello <strong>world</strong>.</p>"
/>;

Examples

Basic

Disabled

This is a deliberately small editor: bold, italic and underline on a contenteditable surface. For collaborative documents or complex embeds, bring a dedicated editor such as Tiptap or Lexical.

API reference

PropTypeDefaultDescription
defaultValuestring—Initial HTML; sanitized against a tag and attribute allowlist.
onChange(html: string) => void—Called with the editor HTML after every input.
placeholderstring"Write something…"Hint shown while the editor is empty.
disabledbooleanfalseMakes the editor read-only and disables the toolbar.

Accessibility

  • The editable area is a labelled, multiline textbox; the toolbar has a name and icon buttons carry labels.
  • Toolbar buttons keep the text selection because mousedown is prevented.
  • The initial HTML is parsed in an inert document and filtered, so scripts, event handlers and javascript: URLs never reach the editor.

Vue and plain HTML

This component also ships for Vue (Reka UI) and plain HTML. Install it with npx veil-ui add rich-text --framework vue or switch the framework in components.json.

<script setup lang="ts">
import { Bold, Italic, Underline } from "lucide-vue-next";
import { computed, onMounted, ref } from "vue";

import { cn } from "@/lib/utils";

const props = withDefaults(
  defineProps<{
    defaultValue?: string;
    placeholder?: string;
    disabled?: boolean;
    class?: string;
  }>(),
  { defaultValue: "", placeholder: "Write something…", disabled: false },
);

const emit = defineEmits<{ change: [html: string]; "update:modelValue": [html: string] }>();

const commands = [
  { command: "bold", label: "Bold", Icon: Bold },
  { command: "italic", label: "Italic", Icon: Italic },
  { command: "underline", label: "Underline", Icon: Underline },
] as const;

const allowedTags = new Set([
  "P", "BR", "DIV", "SPAN", "STRONG", "B", "EM", "I", "U", "S",
  "UL", "OL", "LI", "A", "BLOCKQUOTE", "CODE", "PRE", "H1", "H2",
  "H3", "H4", "H5", "H6",
]);
const blockedTags = new Set(["SCRIPT", "STYLE", "IFRAME", "OBJECT", "EMBED"]);
const allowedAttributes = new Set(["href", "target", "rel"]);

const editor = ref<HTMLElement | null>(null);
const empty = ref(
  props.defaultValue.replace(/<[^>]*>/g, "").trim().length === 0,
);

const showPlaceholder = computed(() => empty.value);

function sanitizeHtml(html: string) {
  const doc = new DOMParser().parseFromString(html, "text/html");

  for (const element of Array.from(doc.body.querySelectorAll("*"))) {
    if (blockedTags.has(element.tagName)) {
      element.remove();
      continue;
    }
    if (!allowedTags.has(element.tagName)) {
      element.replaceWith(...Array.from(element.childNodes));
      continue;
    }
    for (const attribute of Array.from(element.attributes)) {
      if (!allowedAttributes.has(attribute.name)) {
        element.removeAttribute(attribute.name);
      }
    }
    if (
      element.tagName === "A" &&
      /^\s*javascript:/i.test(element.getAttribute("href") ?? "")
    ) {
      element.removeAttribute("href");
    }
  }

  return doc.body.innerHTML;
}

function syncValue() {
  const element = editor.value;
  if (!element) return;
  empty.value = (element.textContent ?? "").trim().length === 0;
  const html = element.innerHTML;
  emit("change", html);
  emit("update:modelValue", html);
}

function applyCommand(command: "bold" | "italic" | "underline") {
  if (props.disabled) return;
  editor.value?.focus();
  document.execCommand(command);
  syncValue();
}

onMounted(() => {
  if (editor.value) editor.value.innerHTML = sanitizeHtml(props.defaultValue);
});
</script>

<template>
  <div
    data-slot="rich-text"
    :class="
      cn(
        'overflow-hidden rounded-md border bg-background focus-within:ring-2 focus-within:ring-ring',
        disabled && 'opacity-60',
        props.class,
      )
    "
  >
    <div
      role="toolbar"
      aria-label="Text formatting"
      class="flex items-center gap-1 border-b p-1"
    >
      <button
        v-for="item in commands"
        :key="item.command"
        type="button"
        :aria-label="item.label"
        :disabled="disabled"
        class="rounded-sm p-1.5 text-muted-foreground transition-colors hover:bg-accent hover:text-accent-foreground disabled:pointer-events-none disabled:opacity-50"
        @mousedown.prevent
        @click="applyCommand(item.command)"
      >
        <component :is="item.Icon" class="size-4" aria-hidden="true" />
      </button>
    </div>
    <div class="relative">
      <span
        v-if="showPlaceholder"
        aria-hidden="true"
        class="pointer-events-none absolute left-3 top-2 text-sm text-muted-foreground"
      >
        {{ placeholder }}
      </span>
      <div
        ref="editor"
        role="textbox"
        aria-label="Rich text editor"
        aria-multiline="true"
        :contenteditable="!disabled"
        class="min-h-24 px-3 py-2 text-sm leading-relaxed outline-none"
        @input="syncValue"
      />
    </div>
  </div>
</template>

On this page