Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 66cab4d

Browse files
committed
add zoom and panning to mermaid svgs
1 parent 909d705 commit 66cab4d

File tree

6 files changed

+68
-5
lines changed

6 files changed

+68
-5
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
"react-dom": "^18.3.1",
7373
"react-highlight-words": "^0.20.0",
7474
"react-icons": "^5.3.0",
75+
"react-zoom-pan-pinch": "^3.7.0",
7576
"rehype-autolink-headings": "^7.1.0",
7677
"remark": "^15.0.1",
7778
"remark-gfm": "^4.0.0",

src/components/GitHubStarCount.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use client'
22

3-
import { getStarGazers } from '@/lib/stargazers'
43
import clsx from 'clsx'
54
import React, { FC, useEffect, useState } from 'react'
65

src/components/MermaidZoom.tsx

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use client'
2+
3+
import { cn } from '@/lib/utils'
4+
import React from 'react'
5+
6+
import { TransformWrapper, TransformComponent } from 'react-zoom-pan-pinch'
7+
import { Button } from './ui/button'
8+
import {
9+
ArrowPathIcon,
10+
MagnifyingGlassMinusIcon,
11+
MagnifyingGlassPlusIcon,
12+
} from '@heroicons/react/24/outline'
13+
14+
interface MermaidZoomProps extends React.ComponentPropsWithoutRef<'svg'> {}
15+
16+
const MermaidZoom: React.FC<MermaidZoomProps> = (props) => (
17+
<div className="relative flex items-center justify-center">
18+
<TransformWrapper centerOnInit initialScale={0.9} minScale={0.75}>
19+
{({ zoomIn, zoomOut, resetTransform, instance }) => (
20+
<React.Fragment>
21+
<div className="absolute right-2 top-2 z-10 space-x-1">
22+
<Button variant="outline" size="icon" onClick={() => zoomIn()}>
23+
<MagnifyingGlassPlusIcon className="size-5" />
24+
</Button>
25+
<Button variant="outline" size="icon" onClick={() => zoomOut()}>
26+
<MagnifyingGlassMinusIcon className="size-5" />
27+
</Button>
28+
<Button
29+
variant="outline"
30+
size="icon"
31+
onClick={() => resetTransform()}
32+
>
33+
<ArrowPathIcon className="size-5" />
34+
</Button>
35+
</div>
36+
<TransformComponent
37+
wrapperClass="bg-white rounded-lg !w-full cursor-move"
38+
contentClass={'!w-full !h-full'}
39+
>
40+
<svg
41+
{...props}
42+
className={cn('mx-auto px-2 py-4', props.className)}
43+
/>
44+
</TransformComponent>
45+
</React.Fragment>
46+
)}
47+
</TransformWrapper>
48+
</div>
49+
)
50+
51+
export default MermaidZoom

src/components/mdx.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Link from 'next/link'
22
import clsx from 'clsx'
33
import { Table } from '@/components/ui/table'
4+
import MermaidZoom from './MermaidZoom'
45

56
export {
67
TableHead as th,
@@ -110,4 +111,14 @@ export { CodeTabs } from '@/components/code/CodeTabs'
110111

111112
export { Mermaid } from 'mdx-mermaid/Mermaid'
112113

114+
export const svg = (props: React.ComponentPropsWithoutRef<'svg'>) => {
115+
const { id } = props
116+
117+
if (id?.startsWith('mermaid-svg')) {
118+
return <MermaidZoom {...props} />
119+
}
120+
121+
return <svg {...props} />
122+
}
123+
113124
// see if we need to remove these

src/styles/mermaid.css

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
svg[id^='mermaid-svg-'] {
2-
@apply mx-auto block rounded-lg bg-white p-4;
3-
}
4-
51
svg[id^='mermaid-svg-'] .nodeLabel,
62
svg[id^='mermaid-svg-'] .edgeLabel,
73
svg[id^='mermaid-svg-'] tspan {

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8152,6 +8152,11 @@ react-style-singleton@^2.2.1:
81528152
invariant "^2.2.4"
81538153
tslib "^2.0.0"
81548154

8155+
react-zoom-pan-pinch@^3.7.0:
8156+
version "3.7.0"
8157+
resolved "https://registry.yarnpkg.com/react-zoom-pan-pinch/-/react-zoom-pan-pinch-3.7.0.tgz#7db4d2ec49c316eb20f71c56e9012eeb3ef4b504"
8158+
integrity sha512-UmReVZ0TxlKzxSbYiAj+LeGRW8s8LraAFTXRAxzMYnNRgGPsxCudwZKVkjvGmjtx7SW/hZamt69NUmGf4xrkXA==
8159+
81558160
react@^18.3.1:
81568161
version "18.3.1"
81578162
resolved "https://registry.yarnpkg.com/react/-/react-18.3.1.tgz#49ab892009c53933625bd16b2533fc754cab2891"

0 commit comments

Comments
 (0)