# Collaborative Editor

This block combines [`AvatarStack`](https://vitehub.dev/docs/ui/avatar-stack), [`ConnectionStatus`](https://vitehub.dev/docs/ui/connection-status), [`SyncStatus`](https://vitehub.dev/docs/ui/sync-status), and the [caret styles](https://vitehub.dev/docs/ui/carets) around a document. Ben types, and Cleo has a phrase selected. Save to see the save state. Drop the connection to see the header wait, then show Reconnecting. The room is fake. No editor runs and no requests leave the page.

_Interactive preview available on the rendered documentation page._


## Connect Realtime

[Realtime](https://vitehub.dev/docs/realtime) runs the room. `useRealtimeTiptap()` returns all the header needs. Its `extensions` draw the carets with `RealtimeCaret`.

| From `useRealtimeTiptap()` | Into |
| --- | --- |
| `people` | `AvatarStack :people` |
| `status` | `ConnectionStatus :status` |
| `synced`, `history.pending`, and the result of `history.checkpoint()` | `SyncStatus :status`, derived below |
| `extensions` | The editor. Carets are on unless you pass `carets: false` |

```vue [DocumentEditor.vue]
<script setup lang="ts">
import { EditorContent, useEditor } from "@tiptap/vue-3";
import type { SyncStatusValue } from "vite-hub/ui";
import { useRealtimeTiptap } from "vite-hub/realtime/vue";
import { computed, ref } from "vue";

const props = defineProps<{ path: string }>();
const realtime = useRealtimeTiptap("docs", props.path);
const { people, status } = realtime;
const editor = useEditor({ extensions: realtime.extensions.value });

const failed = ref(false);
const sync = computed<SyncStatusValue>(() => {
  if (!realtime.synced.value) return "loading";
  if (realtime.history.pending.value) return "synchronizing";
  return failed.value ? "error" : "synchronized";
});

// A checkpoint saves the room to the Workspace and rejects when the save fails.
async function save() {
  try {
    await realtime.history.checkpoint();
    failed.value = false;
  } catch {
    failed.value = true;
  }
}
</script>

<template>
  <header>
    <SyncStatus :status="sync" />
    <ConnectionStatus :status="status" />
    <AvatarStack :people="people" size="sm" variant="outline" />
  </header>
  <EditorContent :editor="editor" />
  <button type="button" @click="save">Save</button>
</template>
```

Key the editor component by `path`, so another document joins another room.

## Change the carets

Pass `carets` options to change the carets. `selectionRender` returns the attributes for a remote selection. `render` builds the caret element. Options you leave out keep the default.

```ts fragment
import { useEditor } from "@tiptap/vue-3";
import { useRealtimeTiptap } from "vite-hub/realtime/vue";

const realtime = useRealtimeTiptap("docs", path, {
  carets: {
    selectionRender: user => ({
      class: "ProseMirror-yjs-selection",
      style: `background-color: color-mix(in oklab, ${user.color} 18%, transparent)`,
    }),
  },
});
const editor = useEditor({ extensions: realtime.extensions.value });
```

## What to keep

- `people` comes from the room's awareness (shared presence state). With `auth: true`, the server sets each name and image from the session. Do not let the client choose them.
- `AvatarStack` shows a person with two tabs as one face.
- Presence colors are `#RRGGBB`. Avatar rings, carets, and selections all use them, so each person has one color.

## Components used

- [Avatar stack](https://vitehub.dev/docs/ui/avatar-stack)
- [Connection status](https://vitehub.dev/docs/ui/connection-status)
- [Sync status](https://vitehub.dev/docs/ui/sync-status)
- [Carets](https://vitehub.dev/docs/ui/carets)

<!-- vitehub-docs:section-links -->
## In this section

- [Start](https://vitehub.dev/raw/docs/ui.md)
- [AI components](https://vitehub.dev/raw/docs/ui/ai-components.md)
- Blocks
  - [Chat App](https://vitehub.dev/raw/docs/ui/blocks/chat-app.md)
  - [Code Review](https://vitehub.dev/raw/docs/ui/blocks/code-review.md)
  - [Collaborative Editor](https://vitehub.dev/raw/docs/ui/blocks/collaborative-editor.md) (this page)
- [Parts](https://vitehub.dev/raw/docs/ui/message.md)
- [Chat](https://vitehub.dev/raw/docs/ui/chat.md)
- [Collaboration](https://vitehub.dev/raw/docs/ui/avatar-stack.md)
- [Agent work](https://vitehub.dev/raw/docs/ui/tool-list.md)
- [Utilities](https://vitehub.dev/raw/docs/ui/attachments.md)

Next: [Trace](https://vitehub.dev/raw/docs/ui/trace.md)

All pages: [llms.txt](https://vitehub.dev/llms.txt)
