# Markdown editor

Edit controlled Markdown source and inspect a safe local preview.

Access: pro. Category: Forms. Kind: composite.

## When to use

- Use it when a page needs edit controlled markdown source and inspect a safe local preview.
- Choose it when the host application can supply the required state and keep business logic outside the presentation component.

## Alternatives

- Choose form-field instead when a labeled email field with validation and help text is the actual interface need, rather than markdown editor.

## Setup for agents

Use React 19 and TypeScript. Initialize shadcn for your framework, then merge this exact entry into components.json:

```json
{
  "registries": {
    "@tidal-pro": {
      "url": "https://tidalui.com/r/pro/{name}.json",
      "headers": {
        "Authorization": "Bearer ${TIDALUI_TOKEN}"
      }
    }
  }
}
```

The namespace must be named **@tidal-pro** because transitive Pro dependencies use that namespace. Obtain an agent key from your active Pro account at https://tidalui.com/account. Keep TIDALUI_TOKEN in your local environment, never in components.json, client code, Git or logs.

```sh
export TIDALUI_TOKEN='your-private-agent-key'
npx shadcn@latest add @tidal-pro/waitlist-form
npx shadcn@latest add https://tidalui.com/r/tidal-button.json
```

Free command: `npx shadcn@latest add https://tidalui.com/r/<name>.json`.
Pro command: `npx shadcn@latest add @tidal-pro/<name>`.

Import the base stylesheet and every stylesheet listed on the installed item's metadata. For example:

```tsx
import "./components/ui/tidal.css";
import "./components/ui/hero-section.css";
// Place installed components inside this wrapper:
<div className="tidal-scope" data-theme="dark">...</div>
```

Omit data-theme for the default light theme. Put the attribute on the same tidal-scope element for dark mode. Every component installs the shared free tidal-base dependency. After upgrading, run `npx shadcn@latest add https://tidalui.com/r/tidal-base.json --overwrite` to receive current tokens. Review local modifications before overwriting.

Vite: Use React + TypeScript. Map @/* to ./src/* in paths and Vite resolve.alias. Import tidal.css and each component stylesheet in your application entry.

Next.js 15 App Router: Next.js App Router: import tidal.css and component CSS in app/layout.tsx. Interactive examples belong in a client component with "use client". Map @/* to ./* or ./src/* to match your project.

TypeScript 6: use paths without baseUrl:

```json
{
  "compilerOptions": {
    "paths": {
      "@/*": [
        "./src/*"
      ]
    }
  }
}
```

Pro registry requests are server/CLI-only. Use the same-origin account interface in a browser; cross-origin Authorization preflights are intentionally unsupported.


## Install Markdown editor

```sh
npx shadcn@latest add @tidal-pro/markdown-editor
```

Stylesheets: `components/ui/tidal.css`, `components/ui/markdown-editor.css`.

## Minimal example

```tsx
import"./components/ui/tidal.css";
import"./components/ui/markdown-editor.css";
import{TidalMarkdownEditor}from"./components/ui/markdown-editor";
export default()=><div className="tidal-scope"><TidalMarkdownEditor label={"Demo"} mode={"split"} onModeChange={() => {}} onValueChange={() => {}} value={"value"}/></div>;
```

## Structured props

- `TidalMarkdownEditor.label: string`: Required label value accepted by this exported component.
- `TidalMarkdownEditor.mode: TidalMarkdownEditorMode`: Required mode value accepted by this exported component.
- `TidalMarkdownEditor.onModeChange: (mode: TidalMarkdownEditorMode, event: ReactMouseEvent<HTMLButtonElement>) => void`: Callback invoked when the mode change interaction is proposed.
- `TidalMarkdownEditor.onValueChange: (value: string, event: ChangeEvent<HTMLTextAreaElement> | ReactMouseEvent<HTMLButtonElement>) => void`: Callback invoked when the value change interaction is proposed.
- `TidalMarkdownEditor.value: string`: Required value value accepted by this exported component.
- `TidalMarkdownEditor.description?: string | undefined`: Optional description value accepted by this exported component.
- `TidalMarkdownEditor.dir?: "ltr" | "rtl" | undefined` (default "ltr"): Optional dir value accepted by this exported component.
- `TidalMarkdownEditor.disabled?: boolean | undefined` (default false): Optional disabled value accepted by this exported component.
- `TidalMarkdownEditor.error?: string | undefined`: Optional error value accepted by this exported component.
- `TidalMarkdownEditor.formatButtonProps?: Partial<Record<TidalMarkdownFormat, EditorButtonProps>> | undefined` (default {}): Optional format button props value accepted by this exported component.
- `TidalMarkdownEditor.formatButtonRefs?: Partial<Record<TidalMarkdownFormat, Ref<HTMLButtonElement>>> | undefined` (default {}): Optional format button refs value accepted by this exported component.
- `TidalMarkdownEditor.labels?: TidalMarkdownEditorLabels | undefined` (default {}): Optional labels value accepted by this exported component.
- `TidalMarkdownEditor.modeButtonProps?: Partial<Record<TidalMarkdownEditorMode, ModeButtonProps>> | undefined` (default {}): Optional mode button props value accepted by this exported component.
- `TidalMarkdownEditor.modeButtonRefs?: Partial<Record<TidalMarkdownEditorMode, Ref<HTMLButtonElement>>> | undefined` (default {}): Optional mode button refs value accepted by this exported component.
- `TidalMarkdownEditor.previewHeadingLevel?: 2 | 3 | 4 | undefined` (default 3): Optional preview heading level value accepted by this exported component.
- `TidalMarkdownEditor.previewProps?: Omit<HTMLAttributes<HTMLDivElement>, "id" | "role" | "aria-disabled" | "aria-label" | "children" | "dangerouslySetInnerHTML"> | undefined` (default {}): Optional preview props value accepted by this exported component.
- `TidalMarkdownEditor.readOnly?: boolean | undefined` (default false): Optional read only value accepted by this exported component.
- `TidalMarkdownEditor.textareaProps?: EditorTextareaProps | undefined` (default {}): Optional textarea props value accepted by this exported component.
- `TidalMarkdownEditor.textareaRef?: Ref<HTMLTextAreaElement> | undefined`: Optional textarea ref value accepted by this exported component.

## Complete usage example

[Source](https://tidalui.com/examples/markdown-editor.tsx) | [Manifest](https://tidalui.com/examples/markdown-editor.json). Install the listed additional dependencies. Consumer fixture: TypeScript pass; Vite pass. This fixture check is distinct from the actual shadcn install receipt.

```tsx
import "./components/ui/tidal.css";

import "./components/ui/markdown-editor.css";

import { useId, useState, type FormEvent, type MouseEvent as ReactMouseEvent } from "react";

import { TidalMarkdownEditor, type TidalMarkdownEditorMode } from "./components/ui/markdown-editor";

import "./components/ui/textarea.css";

import "./components/ui/toolbar.css";



const tidalExampleStyles = ".tidal-scope .markdown-editor-demo {\n  display: grid;\n  width: 100%;\n  max-width: 980px;\n  min-width: 0;\n  gap: 12px;\n  margin: 0 auto;\n}\n\n.tidal-scope .markdown-editor-demo-footer {\n  display: flex;\n  min-width: 0;\n  align-items: center;\n  justify-content: space-between;\n  gap: 12px;\n}\n\n.tidal-scope .markdown-editor-demo-footer button {\n  min-width: 112px;\n  min-height: 44px;\n  padding: 9px 15px;\n  color: var(--t-surface);\n  background: var(--t-ink);\n  border: 1px solid var(--t-ink);\n  border-radius: var(--t-radius-control);\n  font: inherit;\n  font-size: 12px;\n  font-weight: 700;\n  cursor: pointer;\n}\n\n.tidal-scope .markdown-editor-demo-footer button:focus-visible {\n  outline: 2px solid var(--t-accent);\n  outline-offset: 3px;\n}\n\n.tidal-scope .markdown-editor-demo-footer output {\n  min-width: 0;\n  color: var(--t-muted);\n  font-size: 11px;\n  line-height: 1.5;\n  text-align: end;\n  overflow-wrap: anywhere;\n}\n\n.tidal-scope .markdown-editor-guide {\n  padding: 0 12px;\n  background: var(--t-subtle);\n  border: 1px solid var(--t-border);\n  border-radius: var(--t-radius-control);\n}\n\n.tidal-scope .markdown-editor-guide summary {\n  min-height: 44px;\n  padding-block: 12px;\n  font-size: 12px;\n  font-weight: 700;\n  cursor: pointer;\n}\n\n.tidal-scope .markdown-editor-guide summary:focus-visible {\n  outline: 2px solid var(--t-accent);\n  outline-offset: 3px;\n  border-radius: 3px;\n}\n\n.tidal-scope .markdown-editor-guide p,\n.tidal-scope .markdown-editor-sample-note {\n  margin: 0;\n  color: var(--t-muted);\n  font-size: 11px;\n  line-height: 1.6;\n  overflow-wrap: anywhere;\n}\n\n.tidal-scope .markdown-editor-guide p {\n  padding: 0 0 12px;\n}\n\n@media (max-width: 390px) {\n  .tidal-scope .markdown-editor-demo-footer {\n    align-items: stretch;\n    flex-direction: column;\n  }\n\n  .tidal-scope .markdown-editor-demo-footer button {\n    width: 100%;\n  }\n\n  .tidal-scope .markdown-editor-demo-footer output {\n    text-align: start;\n  }\n}";

function fragmentId(value: string) {
  return `markdown-guide-${value.replace(/[^a-zA-Z0-9_-]/g, "")}`;
}

export function MarkdownEditorDemo() {
  const generatedId = useId();
  const guideId = fragmentId(generatedId);
  const [mode, setMode] = useState<TidalMarkdownEditorMode>("split");
  const [value, setValue] = useState(
    () => `# Release notes

Workspace search now keeps recent filters between local views.

## Included

- Faster keyboard navigation
- Clearer empty states
- A [writing guide](#${guideId}) for reviewers

\`\`\`ts
const mode = "preview";
\`\`\`

> This preview renders a small, safe Markdown subset.`,
  );
  const [status, setStatus] = useState("Draft is ready for local review.");

  function review(event: FormEvent<HTMLFormElement>) {
    event.preventDefault();
    setStatus(`Reviewed locally at ${value.length} characters.`);
  }

  function revealGuide(event: ReactMouseEvent<HTMLDivElement>) {
    if (
      event.button !== 0 ||
      event.metaKey ||
      event.ctrlKey ||
      event.shiftKey ||
      event.altKey
    )
      return;
    const target = event.target;
    if (!(target instanceof Element)) return;
    const anchor = target.closest<HTMLAnchorElement>("a");
    if (anchor?.getAttribute("href") !== `#${guideId}`) return;
    const guide = document.getElementById(guideId);
    if (guide instanceof HTMLDetailsElement) guide.open = true;
  }

  return (
    <form className="markdown-editor-demo" onSubmit={review}>
      <TidalMarkdownEditor
        label="Release note"
        value={value}
        onValueChange={(next) => {
          setValue(next);
          setStatus("Draft has local changes.");
        }}
        mode={mode}
        onModeChange={setMode}
        description="Write Markdown or inspect the safe local preview."
        textareaProps={{
          name: "release-note",
          placeholder: "Write a release note…",
          rows: 12,
          required: true,
          showCount: true,
        }}
        previewProps={{ onClick: revealGuide }}
      />

      <div className="markdown-editor-demo-footer">
        <button type="submit">Review draft</button>
        <output>{status}</output>
      </div>

      <details id={guideId} className="markdown-editor-guide">
        <summary>Writing guide</summary>
        <p>
          Use headings, lists, emphasis, links, quotes and fenced code. Other
          syntax stays plain text.
        </p>
      </details>

      <p className="markdown-editor-sample-note">
        Local sample only. Editing and review do not publish or store this
        draft.
      </p>
    </form>
  );
}

export default function AgentExample() {
  return (
    <div className="tidal-scope" data-theme="light" dir="ltr">
      <style>{tidalExampleStyles}</style>
      <MarkdownEditorDemo />
    </div>
  );
}

```

## Gotchas

- Keep field state, validation results, submission, and persistence in the host form workflow.

## Theme and accessibility

{"tokens":["--t-accent","--t-border","--t-border-strong","--t-canvas","--t-danger","--t-display","--t-ease-out","--t-ease-spring","--t-faint","--t-font","--t-ink","--t-line","--t-muted","--t-primary","--t-primary-contrast","--t-radius-card","--t-radius-control","--t-radius-overlay","--t-radius-panel","--t-ring","--t-shadow-lg","--t-shadow-soft","--t-soft","--t-subtle","--t-surface","--t-wash"],"darkMode":true}

{"keyboard":["Tab and Shift+Tab follow the component's native focus order.","Arrow-key behavior follows the exported composite-control implementation."],"aria":"The component wires labels and relationships from consumer-provided visible text or ARIA props; preserve those labels in production.","notes":"Verify focus visibility, text alternatives, contrast, and reduced motion in the final composition."}

Required visible label, controlled value:string/onValueChange(value,event), controlled mode ('write'|'preview'|'split') and onModeChange(mode,event). The editor composes TidalToolbar and TidalTextarea. The same successful native textarea remains mounted in every view, preserving exact FormData, required validation, ref and selection; in Preview it is removed from sequential focus and visually concealed until native invalid/focus handling reveals the actual field. textareaProps preserves native name/form/required/placeholder/rows/maxLength/input/change/composition/selection attributes while the editor owns its visible/accessibility label, controlled value, availability and change proposal; textareaRef is supported. Native textarea onChange runs first and prevention suppresses onValueChange. Formatting actions cover bold, italic, level-two heading source, bulleted lists, links and inline/fenced code; they use the textarea's native selection, propose exact controlled source, and restore focus/selection only after synchronous acceptance while the formatting source or body still owns focus. Consumer-directed external focus is preserved. Declined proposals are consumed immediately and cannot steal focus after an unrelated later change. formatButtonProps/refs and modeButtonProps/refs preserve caller-first native handlers while excluding replacement markup, submit/form/value, owned availability and pressed state. disabled blocks editing, mode changes, formatting and preview links; readOnly blocks source edits/formatting while keeping view switching and preview links available. Optional description, error, dir('ltr' default or 'rtl'), localized labels, previewHeadingLevel(2|3|4), native preview region attributes and root div attributes/ref are supported; dir also configures the composed toolbar keyboard direction. Owned markup, content, naming, role and aggregate aria-disabled channels are type-excluded and runtime-stripped. The safe React preview recognizes source headings # through ###, paragraphs, bold, italic, inline/fenced code, ordered/unordered lists, single-line quotes and safe hash/root/http/https/mailto links. Raw HTML is never parsed, unsafe links and unsupported syntax remain plain text, and no Markdown package, code execution, request, clipboard, upload, storage, persistence or publishing behavior is included.
