Annotations

Add annotation capabilities to documents.

Overview

Document.Annotatable is a feature provider that enables annotation functionality. Users can select text in the document to create annotations, which appear as highlights in the content and are managed through a sidebar. You control the annotation data through callbacks, making it easy to sync with your backend or local state. The active annotation state can be managed internally or controlled externally for features like URL-based deep linking.

Usage

import {
  Document,
  type AnnotatableProps,
  type AnnotationAuthor,
  type AnnotationData,
  useAnnotatable,
  useAnnotationCard,
  useHasSelection,
} from "@tilt-legal/cubitt-components/document";
// Basic - Cubitt handles UI
<Document.Annotatable
  annotations={annotations}
  onAnnotationCreate={handleCreate}
  onAnnotationUpdate={handleUpdate}
  onAnnotationDelete={handleDelete}
>
  <Document.Viewer />
  <Document.Annotation.Sidebar />
  <Document.Toolbar.Selection>
    <Document.Annotation.AddButton />
  </Document.Toolbar.Selection>
</Document.Annotatable>
// Custom structure
<Document.Annotatable
  annotations={annotations}
  onAnnotationCreate={handleCreate}
  onAnnotationUpdate={handleUpdate}
  onAnnotationDelete={handleDelete}
>
  <Document.Viewer />

  <Document.Annotation.Sidebar>
    <Document.Annotation.Sidebar.Header />
    <Document.Annotation.Sidebar.List>
      <Document.Annotation.Sidebar.Card annotation={annotation}>
        <Document.Annotation.Sidebar.Card.Citation />
        <Document.Annotation.Sidebar.Card.Textarea />
        <Document.Annotation.Sidebar.Card.Delete />
      </Document.Annotation.Sidebar.Card>
    </Document.Annotation.Sidebar.List>
    <Document.Annotation.Sidebar.Empty />
  </Document.Annotation.Sidebar>

  <Document.Toolbar.Selection>
    <Document.Annotation.AddButton />
  </Document.Toolbar.Selection>
</Document.Annotatable>

Controlled vs Uncontrolled Active State

By default, the component manages which annotation is "active" (selected) internally.

// Uncontrolled - component manages active state
<Document.Annotatable
  annotations={annotations}
  onAnnotationCreate={handleCreate}
  onAnnotationUpdate={handleUpdate}
  onAnnotationDelete={handleDelete}
>

To control it externally (e.g., for URL sync):

// Controlled - you manage active state
const [activeId, setActiveId] = useState<string | null>(null);

<Document.Annotatable
  annotations={annotations}
  activeAnnotationId={activeId}
  onActiveAnnotationChange={setActiveId}
  onAnnotationCreate={handleCreate}
  onAnnotationUpdate={handleUpdate}
  onAnnotationDelete={handleDelete}
>

API Reference

Document.Annotatable

PropTypeDescription
annotationsAnnotationData[]Current annotations (required)
onAnnotationCreate(annotation: AnnotationData) => voidCalled when annotation created (required)
onAnnotationUpdate(id: string, text: string) => voidCalled when annotation text changes (required)
onAnnotationDelete(id: string) => voidCalled when annotation deleted (required)
activeAnnotationIdstring | nullControlled active state (optional)
onActiveAnnotationChange(id: string | null) => voidActive state callback (optional)
defaultAuthorAnnotationAuthorDefault author for new annotations (optional)

AnnotationData

Core data structure for annotations.

PropertyTypeDescription
idstringUnique identifier
textstringUser's annotation text
citedTextstringThe highlighted text from the document
fromnumberStart position in document
tonumberEnd position in document
createdAtnumberUnix timestamp
authorAnnotationAuthorOptional author info

AnnotationAuthor

Author information for annotations.

PropertyTypeDescription
namestringDisplay name (required)
avatarUrlstringURL to avatar image (optional)

On this page