@filekit-studio/diagram

中文文档
1.0.10发布于 2026/11/08MITTypeScriptESM + CJS近 30 天下载 2242

Declarative Diagram-as-Code & JSON graph compiler for @filekit-studio/sketch

原始使用文档
文档目录8 节
  1. Installation
  2. Required Stylesheet Import
  3. Quickstart
  4. 1. Declarative <Diagram /> Component
  5. 2. Programmatic Compilation ( diagramToSketch )
  6. 3. Rendering Excalidraw JSON Files
  7. <Diagram /> Props Reference
  8. License

@filekit-studio/diagram

Declarative Diagram-as-Code & JSON graph compiler for @filekit-studio/sketch.

npm version License: MIT

Documentation • Quickstart Guide • Live Playground

@filekit-studio/diagram lets you generate and render beautiful hand-drawn whiteboards, architecture diagrams, and flowcharts directly from declarative JSON or Diagram-as-Code in React and Next.js.

  • Zero Coordinates Required: Provide nodes, edges, and clusters — the deterministic DAG layout engine positions and aligns everything automatically.
  • Sketch Aesthetics: Powered by @filekit-studio/sketch, rendering organic hand-drawn shapes, rough fills, and magnetic connector arrows.
  • Obstacle Evasion & Corridor Seeking: Orthogonal elbow lines automatically navigate around shapes without crossing intermediate services.
  • Submerged Passthrough Tunneling: Traversed lines automatically turn into subtle dashed X-ray paths underneath shapes.
  • Directional Flows: Supports Left-to-Right (LR), Top-to-Bottom (TB), Right-to-Left (RL), and Bottom-to-Top (BT).
  • Excalidraw Native: Direct conversion from .excalidraw schema files and element arrays.
  • 100% Client-Side: Zero server calls, zero tracking, works fully offline.

Installation

bun add @filekit-studio/diagram @filekit-studio/sketch
# or
npm install @filekit-studio/diagram @filekit-studio/sketch
# or
pnpm add @filekit-studio/diagram @filekit-studio/sketch

Required Stylesheet Import

Import the self-contained stylesheet in your root layout (app/layout.tsx or pages/_app.tsx):

import '@filekit-studio/diagram/styles.css'
// or: import '@filekit-studio/sketch/styles.css'

Zero CSS Leaks: All styles are strictly scoped under .fk-sketch-root. Importing this stylesheet will never conflict with your app's global CSS, fonts, or Tailwind config.


Quickstart

1. Declarative <Diagram /> Component

'use client'

import React from 'react'
import { Diagram, type DiagramData } from '@filekit-studio/diagram'
import '@filekit-studio/diagram/styles.css'

const architecture: DiagramData = {
  title: 'Cloud Payment Pipeline',
  direction: 'LR',
  nodes: [
    {
      id: 'client',
      label: 'Client Browser\n(React / Next.js)',
      shape: 'rectangle',
      color: 'indigo',
      badge: 'FRONTEND',
    },
    {
      id: 'gateway',
      label: 'API Gateway',
      description: 'Kong / Cloudflare',
      shape: 'rounded',
      color: 'blue',
    },
    {
      id: 'auth',
      label: 'Auth Service',
      description: 'JWT / OAuth2',
      shape: 'diamond',
      color: 'amber',
    },
    {
      id: 'db',
      label: 'Primary Database\n(PostgreSQL)',
      shape: 'cylinder',
      color: 'emerald',
      badge: 'STORAGE',
    },
  ],
  edges: [
    { from: 'client', to: 'gateway', label: 'HTTPS POST' },
    { from: 'gateway', to: 'auth', label: 'Verify Token' },
    { from: 'auth', to: 'db', label: 'SQL Query' },
  ],
  groups: [
    {
      id: 'backend',
      label: 'Internal VPC Cluster',
      nodeIds: ['gateway', 'auth', 'db'],
      color: 'indigo',
    },
  ],
}

export default function ArchitectureDoc() {
  return (
    <div className="w-full h-[500px] rounded-xl overflow-hidden border">
      {/* Automatically inherits dark/light mode and sets contrast */}
      <Diagram
        data={architecture}
        mode="viewer"
        autoFit={true}
        showCopyButton={true}
        copyFormat="elements" // 'elements' for 1-click Ctrl+V paste into Whiteboard, 'image' for PNG, or 'json'
        showZoomControls={true}
      />
    </div>
  )
}

2. Programmatic Compilation (diagramToSketch)

If you want to compile graph JSON into a SketchDocument to save, mutate, or export:

import { diagramToSketch } from '@filekit-studio/diagram'

const sketchDoc = diagramToSketch(architecture, {
  direction: 'LR',
  nodeSpacing: 80,
  rankSpacing: 220,
})

console.log(sketchDoc.elements) // Array of SketchBoardElement objects

3. Rendering Excalidraw JSON Files

import { Diagram } from '@filekit-studio/diagram'
import excalidrawFile from './diagram.excalidraw.json'

export function ExcalidrawViewer() {
  return (
    <div className="w-full h-[600px]">
      <Diagram data={excalidrawFile} mode="viewer" autoFit={true} />
    </div>
  )
}

<Diagram /> Props Reference

PropTypeDefaultDescription
dataDiagramData | stringrequiredDiagram specification object or JSON string.
mode'viewer' | 'editor''viewer'Display mode: clean doc viewer or full whiteboard studio.
direction'TB' | 'BT' | 'LR' | 'RL''LR'Flow direction for the DAG auto-layout engine.
theme'light' | 'dark' | 'system''system'Color theme mode. Automatically detects active website dark mode.
canvasBackgroundstring'#f0f4f8'Hex background color override (#121214 in dark mode).
gridStyle'grid' | 'dots' | 'cross' | 'none''dots'Canvas grid pattern.
autoFitbooleantrueScale and center elements to fit container on mount.
interactivebooleantrueAllow click-and-drag panning and wheel zooming.
showCopyButtonbooleantrueDisplay 1-click clipboard export button.
copyFormat'elements' | 'image' | 'json''elements''elements' for Ctrl+V paste into Sketch whiteboard, 'image' for PNG, or 'json'.
showZoomControlsbooleantrueDisplay minimal floating zoom percentage and +/-/Fit controls.
layoutOptionsDiagramLayoutOptions{}Fine-tuning for spacing (nodeSpacing, rankSpacing), margins, and padding.
uiConfigSketchUIConfig{}Granular toggle flags for all 54 UI controls and menus (editor mode).
controllerRefRef<SketchController>undefinedRef to imperative controller methods (zoomToFit, zoomIn, zoomOut, etc.).

License

MIT © FileKit Studio

版本与发布记录

@filekit-studio/diagram 共发布 12 个版本,首次发布于 2026/10/25;过去 12 个月发布了 12 个版本。

版本发布日期距今
1.0.10latest2026/11/086 天前
1.0.92026/11/077 天前
1.0.82026/11/068 天前
1.0.72026/11/0410 天前
1.0.62026/11/0212 天前
1.0.52026/10/2817 天前

关于 @filekit-studio/diagram 的常见问题

@filekit-studio/diagram 是什么?

Declarative Diagram-as-Code & JSON graph compiler for @filekit-studio/sketch

如何安装 @filekit-studio/diagram?

在项目目录运行 npm install @filekit-studio/diagram,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=18.0.0)、react-dom(>=18.0.0)、@filekit-studio/sketch(>=1.1.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@filekit-studio/diagram 支持 TypeScript 吗?

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

@filekit-studio/diagram 支持 ESM 还是 CommonJS?

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

@filekit-studio/diagram 可以商用吗?

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

@filekit-studio/diagram 的下载量有多少?

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

@filekit-studio/diagram 还在维护吗?

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