@avol-io/react-mermaid

中文文档
0.3.1发布于 2026/04/19MITTypeScriptESM + CJS近 30 天下载 1953

React component for rendering Mermaid diagrams with zoom, pan, and fullscreen support.

原始使用文档
文档目录13 节
  1. Installation
  2. Peer dependencies
  3. Usage
  4. API
  5. MermaidDiagram
  6. Modal
  7. Toolbar
  8. Top-right
  9. Bottom-right (3×3 grid)
  10. Styling
  11. Available tokens
  12. Live docs
  13. License

@avol-io/react-mermaid

React component that renders Mermaid diagrams with zoom, pan, directional controls, fullscreen, download, and draw.io integration.

Storybook npm license


Installation

npm install @avol-io/react-mermaid

Peer dependencies

npm install react react-dom mermaid react-zoom-pan-pinch
PackageRequired version
react>=19
react-dom>=19
mermaid>=11
react-zoom-pan-pinch>=3

Usage

import { MermaidDiagram } from '@avol-io/react-mermaid'

export function App() {
  return (
    <MermaidDiagram
      chart={`
        graph TD
          A[Start] --> B{Decision}
          B -- Yes --> C[Done]
          B -- No  --> D[Skip]
      `}
      width="100%"
      height="400px"
    />
  )
}

API

MermaidDiagram

PropTypeDefaultDescription
chartstringrequiredMermaid source text
widthstring"100%"CSS width of the container
heightstring"400px"CSS height of the container
configMermaidConfig—Options forwarded to mermaid.initialize()
enableDownloadSvgbooleantrueShow "SVG" option in the download dropdown
enableDownloadMmdbooleantrueShow ".mmd" option in the download dropdown
enableDrawiobooleantrueShow "Open in draw.io" button in the toolbar

When both enableDownloadSvg and enableDownloadMmd are false, the download dropdown button is not rendered at all.

Modal

PropTypeDefaultDescription
titlestring"Error"Modal heading
onClose() => voidrequiredCalled on Escape, overlay click, or close button
childrenReactNoderequiredModal body content

Toolbar

Top-right

  • Download — dropdown with SVG and .mmd options (each independently toggleable via props)
  • Open in draw.io — opens app.diagrams.net with the diagram preloaded via Mermaid import URL
  • Fullscreen — toggles a fixed fullscreen overlay

Bottom-right (3×3 grid)

  • Arrow buttons — pan the diagram in all four directions
  • Center button — resets the view to fit the content in the container
  • Zoom in / Zoom out — step zoom (wheel zoom also supported, follows the cursor)

Styling

No CSS import required. On first render a <style id="avol-react-mermaid-styles"> tag is injected into <head> once and reused by all instances.

All colors and sizes are exposed as CSS custom properties. Override them on :root for a global theme, or scope to a container class for per-instance theming:

/* dark theme example */
:root {
  --mermaid-bg: #1f2937;
  --mermaid-border-color: #374151;
  --mermaid-toolbar-bg: rgba(17, 24, 39, 0.9);
  --mermaid-toolbar-color: #f9fafb;
  --mermaid-toolbar-hover-bg: #111827;
  --mermaid-toolbar-border: #4b5563;
  --mermaid-fullscreen-bg: #111827;
  --mermaid-dropdown-bg: #1f2937;
  --mermaid-dropdown-item-color: #f3f4f6;
  --mermaid-dropdown-item-hover: #374151;
}

Available tokens

TokenDefaultControls
--mermaid-bgtransparentcontainer background
--mermaid-border-colorrgba(0,0,0,0.12)container border color
--mermaid-border-radius8pxcontainer corner radius
--mermaid-toolbar-bgrgba(255,255,255,0.9)button background
--mermaid-toolbar-borderrgba(0,0,0,0.12)button border
--mermaid-toolbar-color#374151button icon/text color
--mermaid-toolbar-hover-bg#ffffffbutton hover background
--mermaid-toolbar-hover-shadow0 2px 8px rgba(0,0,0,0.1)button hover shadow
--mermaid-toolbar-focus-ring#01696ffocus outline color
--mermaid-toolbar-btn-size30pxbutton width & height
--mermaid-toolbar-btn-radius6pxbutton corner radius
--mermaid-dropdown-bg#ffffffdropdown panel background
--mermaid-dropdown-borderrgba(0,0,0,0.1)dropdown border
--mermaid-dropdown-shadow0 4px 12px rgba(0,0,0,0.12)dropdown shadow
--mermaid-dropdown-item-hover#f3f4f6dropdown item hover background
--mermaid-dropdown-item-color#374151dropdown item text color
--mermaid-error-bg#fef2f2error banner background
--mermaid-error-border#fecacaerror banner border
--mermaid-error-color#dc2626error banner text
--mermaid-fullscreen-bg#fffffffullscreen overlay background
--mermaid-fullscreen-zindex9998fullscreen z-index

Live docs

https://www.avol.io/react-mermaid/


License

MIT © avol.io

版本与发布记录

@avol-io/react-mermaid 共发布 5 个版本,首次发布于 2026/04/09;过去 12 个月发布了 5 个版本。

版本发布日期距今
0.3.1latest2026/04/196 个月前
0.3.02026/04/186 个月前
0.2.22026/04/126 个月前
0.2.12026/04/096 个月前
0.2.02026/04/096 个月前

关于 @avol-io/react-mermaid 的常见问题

@avol-io/react-mermaid 是什么?

React component for rendering Mermaid diagrams with zoom, pan, and fullscreen support.

如何安装 @avol-io/react-mermaid?

在项目目录运行 npm install @avol-io/react-mermaid,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:mermaid(>=11)、react(>=19)、react-dom(>=19)、react-zoom-pan-pinch(>=3)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@avol-io/react-mermaid 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@avol-io/react-mermaid 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@avol-io/react-mermaid 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@avol-io/react-mermaid 可以商用吗?

@avol-io/react-mermaid 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@avol-io/react-mermaid 的下载量有多少?

根据 npm 官方下载统计,@avol-io/react-mermaid 在 2026/10/07 – 2026/11/05 这 30 天内被下载 1,953 次,日均约 65 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@avol-io/react-mermaid 还在维护吗?

最新版本 0.3.1 发布于 2026/04/19(6 个月前);至今共发布 5 个版本,其中过去 12 个月发布了 5 个;首次发布于 2026/04/09。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。