Examples

Three editors, with their code

Each one is live: type, upload a photo, paste a link. The source beside it is the file that runs, so you can copy it into your app and swap the fake adapters for your own.

social-composer.tsx

A social composer

Write a post, add photos or paste a link, and post it to the feed below. The Post button waits for uploads to finish.

  • preset="social"
  • hasPendingUploads before posting
  • sanitizeDocument, then PostCanvasView in the feed
Loading the editor
'use client';

import {
  PostCanvas,
  getDocStats,
  hasPendingUploads,
  type JSONContent,
  type PostCanvasHandle,
  type PostCanvasTheme,
} from 'post-canvas';
import { PostCanvasView, sanitizeDocument } from 'post-canvas/render';
import 'post-canvas/styles.css';
import { useRef, useState } from 'react';
// The site's fake adapters: swap in your own upload, mentions, hashtags and link previews.
import { createFakeAdapters } from '@/lib/fake-adapters';

const adapters = createFakeAdapters();

interface Post {
  id: number;
  content: JSONContent;
}

/** A social composer: write, add photos or paste a link, then post to the feed below. */
export function SocialComposer({ theme = 'auto' }: { theme?: PostCanvasTheme }) {
  const editor = useRef<PostCanvasHandle>(null);
  const [draft, setDraft] = useState<JSONContent | null>(null);
  const [posts, setPosts] = useState<Post[]>([]);

  const stats = draft ? getDocStats(draft) : null;
  const hasContent = !!stats && (stats.words > 0 || stats.mediaBlocks > 0);
  const uploading = !!draft && hasPendingUploads(draft);

  const post = () => {
    if (!draft || !hasContent || uploading) return;
    // Your server does this step: sanitize what the client sent, then store it.
    const { doc } = sanitizeDocument(draft);
    setPosts((list) => [{ id: Date.now(), content: doc }, ...list]);
    editor.current?.clear();
  };

  return (
    <div className="flex flex-col gap-4">
      <PostCanvas
        ref={editor}
        preset="social"
        adapters={adapters}
        theme={theme}
        placeholder="What's new? Add photos, or paste a link."
        onChange={({ json }) => setDraft(json)}
        slots={{
          footer: (
            <div className="flex items-center justify-end gap-3 px-4 pb-4">
              {uploading && <span className="text-sm opacity-70">Waiting for uploads</span>}
              <button
                type="button"
                onClick={post}
                disabled={!hasContent || uploading}
                className="h-11 rounded-xl bg-[#2f5fd0] px-5 font-semibold text-white disabled:opacity-40"
              >
                Post
              </button>
            </div>
          ),
        }}
      />
      {posts.map((item) => (
        <article key={item.id} className="rounded-2xl border border-current/15 p-4">
          <p className="mb-2 text-sm font-semibold">You · just now</p>
          <PostCanvasView content={item.content} theme={theme} />
        </article>
      ))}
    </div>
  );
}

Read more in Saving and loading.

article-editor.tsx

An article editor

A title, a cover and the full set of blocks. Publish shows the article as a reader sees it, cover and all.

  • preset="article"
  • Controlled fields with onFieldsChange
  • A word count from getDocStats
Loading the editor
'use client';

import {
  PostCanvas,
  getDocStats,
  hasPendingUploads,
  type JSONContent,
  type PostCanvasTheme,
  type PostFields,
} from 'post-canvas';
import { PostCanvasView, sanitizeDocument } from 'post-canvas/render';
import 'post-canvas/styles.css';
import { useState } from 'react';
// The site's fake adapters: swap in your own upload, mentions, hashtags and link previews.
import { createFakeAdapters } from '@/lib/fake-adapters';

const adapters = createFakeAdapters();

interface Article {
  fields: PostFields;
  content: JSONContent;
}

/** An article editor with a title and a cover. Publish shows the article as a reader would see it. */
export function ArticleEditor({ theme = 'auto', initial }: { theme?: PostCanvasTheme; initial: Article }) {
  const [fields, setFields] = useState<PostFields>(initial.fields);
  const [content, setContent] = useState<JSONContent>(initial.content);
  const [published, setPublished] = useState<Article | null>(null);

  const words = getDocStats(content).words;
  const ready = !!fields.title?.trim() && words > 0 && !hasPendingUploads(content);

  return (
    <div className="flex flex-col gap-4">
      <PostCanvas
        preset="article"
        adapters={adapters}
        theme={theme}
        defaultContent={initial.content}
        fields={fields}
        onFieldsChange={setFields}
        onChange={({ json }) => setContent(json)}
      />
      <div className="flex items-center justify-end gap-3">
        <span className="text-sm opacity-70">{ready ? `${words} words` : 'Add a title to publish'}</span>
        <button
          type="button"
          disabled={!ready}
          onClick={() => setPublished({ fields, content: sanitizeDocument(content).doc })}
          className="h-11 rounded-xl bg-[#2f5fd0] px-5 font-semibold text-white disabled:opacity-40"
        >
          Publish
        </button>
      </div>
      {published && (
        <article className="overflow-hidden rounded-2xl border border-current/15">
          {published.fields.cover && (
            // A plain img keeps the example framework-free; next/image works too.
            // eslint-disable-next-line @next/next/no-img-element
            <img
              src={published.fields.cover.src}
              srcSet={published.fields.cover.srcset ?? undefined}
              sizes="(min-width: 1024px) 40rem, 100vw"
              width={published.fields.cover.width}
              height={published.fields.cover.height}
              alt={published.fields.cover.alt}
              className="aspect-[2/1] w-full object-cover"
            />
          )}
          <div className="p-5">
            <h2 className="mb-3 text-2xl font-bold">{published.fields.title}</h2>
            <PostCanvasView content={published.content} theme={theme} />
          </div>
        </article>
      )}
    </div>
  );
}

Read more in Presets.

comment-thread.tsx

A comment thread

A small box with no toolbar. Enter posts, Shift and Enter adds a line, and @ mentions people from your own list.

  • preset="comment" with submitOnEnter
  • onSubmit, then clear()
  • Each comment renders read-only
Loading the editor
'use client';

import { PostCanvas, getDocStats, type JSONContent, type PostCanvasHandle, type PostCanvasTheme } from 'post-canvas';
import { PostCanvasView, sanitizeDocument } from 'post-canvas/render';
import 'post-canvas/styles.css';
import { useRef, useState } from 'react';
// The site's fake adapters: swap in your own mention search.
import { createFakeAdapters } from '@/lib/fake-adapters';

const adapters = createFakeAdapters();

export interface Comment {
  id: string;
  author: string;
  content: JSONContent;
}

/** A comment thread: Enter posts, Shift and Enter adds a line, @ mentions people. */
export function CommentThread({ theme = 'auto', initial }: { theme?: PostCanvasTheme; initial: Comment[] }) {
  const editor = useRef<PostCanvasHandle>(null);
  const [comments, setComments] = useState(initial);

  return (
    <div className="flex flex-col gap-4">
      <ol className="flex flex-col gap-4" aria-label="Comments">
        {comments.map((comment) => (
          <li key={comment.id} className="rounded-2xl border border-current/15 p-4">
            <p className="mb-1 text-sm font-semibold">{comment.author}</p>
            <PostCanvasView content={comment.content} theme={theme} />
          </li>
        ))}
      </ol>
      <PostCanvas
        ref={editor}
        preset="comment"
        adapters={adapters}
        theme={theme}
        submitOnEnter
        placeholder="Add a comment. Type @ to mention someone."
        onSubmit={({ json }) => {
          if (getDocStats(json).words === 0) return;
          const { doc } = sanitizeDocument(json); // On your server, before you store it.
          setComments((list) => [...list, { id: crypto.randomUUID(), author: 'You', content: doc }]);
          editor.current?.clear();
        }}
      />
    </div>
  );
}

Read more in Mentions and hashtags.