svelte-example-yjs

PreviousNext

Real-time collaboration using Yjs CRDT library.

Docs
prosekitblock

Preview

Loading preview…
registry/src/svelte/examples/yjs/editor-component.svelte
<script lang="ts">
import 'prosekit/basic/style.css'
import 'prosekit/basic/typography.css'
import 'prosekit/extensions/yjs/style.css'

import { createEditor } from 'prosekit/core'
import { ProseKit } from 'prosekit/svelte'
import type { Awareness } from 'y-protocols/awareness'
import type * as Y from 'yjs'

import { Toolbar } from '../../ui/toolbar'

import { defineExtension } from './extension'

interface Props {
  doc: Y.Doc
  awareness: Awareness
}

const props: Props = $props()

const extension = defineExtension(props.doc, props.awareness)
const editor = createEditor({ extension })
</script>

<ProseKit {editor}>
  <div class="box-border h-full w-full min-h-36 overflow-y-hidden overflow-x-hidden rounded-md border border-solid border-gray-200 dark:border-gray-700 shadow-sm flex flex-col bg-white dark:bg-gray-950 text-black dark:text-white">
    <Toolbar />
    <div class="relative w-full flex-1 box-border overflow-y-auto">
      <div {@attach editor.mount} class="ProseMirror box-border min-h-full px-[max(4rem,calc(50%-20rem))] py-8 outline-hidden outline-0 [&_span[data-mention=user]]:text-blue-500 [&_span[data-mention=tag]]:text-violet-500"></div>
    </div>
  </div>
</ProseKit>

Installation

npx shadcn@latest add @prosekit/svelte-example-yjs

Usage

import { SvelteExampleYjs } from "@/components/svelte-example-yjs"
<SvelteExampleYjs />