-
Notifications
You must be signed in to change notification settings - Fork 275
docs: add mermaid modal #288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rootfs
merged 10 commits into
vllm-project:main
from
yuluo-yx:0928-yuluo/feat-mermaid-1
Sep 29, 2025
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
19e17ee
docs: add mermaid modal
yuluo-yx 0766888
fix
yuluo-yx d803b06
fix
yuluo-yx 6864469
Merge branch 'main' into 0928-yuluo/feat-mermaid-1
yuluo-yx 1f909b1
fix: fix lit
yuluo-yx 290e1f3
fix
yuluo-yx 56edc9e
fix
yuluo-yx 6712860
Fix the issue where the top scroll bar is not visible when the chart …
yuluo-yx a7c7650
Merge branch 'main' into 0928-yuluo/feat-mermaid-1
rootfs ea0259e
fix lint
yuluo-yx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,230 @@ | ||
| import React, { useState, useRef, useEffect, useCallback } from 'react' | ||
| import { createPortal } from 'react-dom' | ||
| import Mermaid from '@theme/Mermaid' | ||
| import styles from './styles.module.css' | ||
|
|
||
| const ZoomableMermaid = ({ children, title, defaultZoom = 1.2 }) => { | ||
| const [isModalOpen, setIsModalOpen] = useState(false) | ||
| const [isHovered, setIsHovered] = useState(false) | ||
| const [zoomLevel, setZoomLevel] = useState(defaultZoom) // Use defaultZoom prop | ||
| const modalRef = useRef(null) | ||
| const containerRef = useRef(null) | ||
|
|
||
| const openModal = useCallback(() => { | ||
| setIsModalOpen(true) | ||
| setZoomLevel(defaultZoom) // Reset to default zoom when opening | ||
| document.body.style.overflow = 'hidden' | ||
| }, [defaultZoom]) | ||
|
|
||
| const closeModal = useCallback(() => { | ||
| setIsModalOpen(false) | ||
| document.body.style.overflow = 'unset' | ||
| // Return focus to the original container | ||
| if (containerRef.current) { | ||
| containerRef.current.focus() | ||
| } | ||
| }, []) | ||
|
|
||
| const zoomIn = useCallback(() => { | ||
| setZoomLevel(prev => Math.min(prev + 0.2, 3.0)) // Max 300% | ||
| }, []) | ||
|
|
||
| const zoomOut = useCallback(() => { | ||
| setZoomLevel(prev => Math.max(prev - 0.2, 0.5)) // Min 50% | ||
| }, []) | ||
|
|
||
| const resetZoom = useCallback(() => { | ||
| setZoomLevel(defaultZoom) // Reset to custom default instead of hardcoded 1.2 | ||
| }, [defaultZoom]) | ||
|
|
||
| useEffect(() => { | ||
| const handleEscape = (e) => { | ||
| if (e.key === 'Escape' && isModalOpen) { | ||
| closeModal() | ||
| } | ||
| } | ||
|
|
||
| const handleClickOutside = (e) => { | ||
| if (modalRef.current && !modalRef.current.contains(e.target)) { | ||
| closeModal() | ||
| } | ||
| } | ||
|
|
||
| const handleKeydown = (e) => { | ||
| if (!isModalOpen) return | ||
|
|
||
| if (e.key === '=' || e.key === '+') { | ||
| e.preventDefault() | ||
| zoomIn() | ||
| } | ||
| else if (e.key === '-') { | ||
| e.preventDefault() | ||
| zoomOut() | ||
| } | ||
| else if (e.key === '0') { | ||
| e.preventDefault() | ||
| resetZoom() | ||
| } | ||
| } | ||
|
|
||
| if (isModalOpen) { | ||
| document.addEventListener('keydown', handleEscape) | ||
| document.addEventListener('mousedown', handleClickOutside) | ||
| document.addEventListener('keydown', handleKeydown) | ||
|
|
||
| // Focus the modal content when opened | ||
| setTimeout(() => { | ||
| if (modalRef.current) { | ||
| modalRef.current.focus() | ||
| } | ||
| }, 100) | ||
| } | ||
|
|
||
| return () => { | ||
| document.removeEventListener('keydown', handleEscape) | ||
| document.removeEventListener('mousedown', handleClickOutside) | ||
| document.removeEventListener('keydown', handleKeydown) | ||
| } | ||
| }, [isModalOpen, closeModal, zoomIn, zoomOut, resetZoom]) | ||
|
|
||
| // Cleanup on unmount | ||
| useEffect(() => { | ||
| return () => { | ||
| document.body.style.overflow = 'unset' | ||
| } | ||
| }, []) | ||
|
|
||
| const handleKeyDown = (e) => { | ||
| if (e.key === 'Enter' || e.key === ' ') { | ||
| e.preventDefault() | ||
| openModal() | ||
| } | ||
| } | ||
|
|
||
| const modalContent = ( | ||
| <div | ||
| className={styles.modal} | ||
| role="dialog" | ||
| aria-modal="true" | ||
| aria-labelledby={title ? 'modal-title' : undefined} | ||
| aria-describedby="modal-description" | ||
| > | ||
| <div | ||
| className={styles.modalContent} | ||
| ref={modalRef} | ||
| tabIndex={-1} | ||
| > | ||
| <div className={styles.modalHeader}> | ||
| {title && ( | ||
| <h3 id="modal-title" className={styles.modalTitle}> | ||
| {title} | ||
| </h3> | ||
| )} | ||
| <div className={styles.modalControls}> | ||
| <span className={styles.zoomIndicator}> | ||
| {Math.round(zoomLevel * 100)} | ||
| % | ||
| </span> | ||
| <button | ||
| className={styles.zoomButton} | ||
| onClick={zoomOut} | ||
| disabled={zoomLevel <= 0.5} | ||
| aria-label="Reduce the size of the chart" | ||
| type="button" | ||
| title="Reduce (Shortcut key: -)" | ||
| > | ||
| <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"> | ||
| <circle cx="11" cy="11" r="8" /> | ||
| <path d="M8 11h6" /> | ||
| <path d="m21 21-4.35-4.35" /> | ||
| </svg> | ||
| </button> | ||
| <button | ||
| className={styles.resetButton} | ||
| onClick={resetZoom} | ||
| aria-label={`Reset to default zoom level ${Math.round(defaultZoom * 100)}%`} | ||
| type="button" | ||
| title={`Reset to default zoom level ${Math.round(defaultZoom * 100)}% (Shortcut key: 0)`} | ||
| > | ||
| <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"> | ||
| <path d="M3 3l18 18" /> | ||
| <path d="m19 4-7 7-7-7" /> | ||
| <path d="m5 20 7-7 7 7" /> | ||
| </svg> | ||
| </button> | ||
| <button | ||
| className={styles.zoomButton} | ||
| onClick={zoomIn} | ||
| disabled={zoomLevel >= 3.0} | ||
| aria-label="Enlarge the chart" | ||
| type="button" | ||
| title="Enlarge (Shortcut key: +)" | ||
| > | ||
| <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"> | ||
| <circle cx="11" cy="11" r="8" /> | ||
| <path d="M8 11h6" /> | ||
| <path d="M11 8v6" /> | ||
| <path d="m21 21-4.35-4.35" /> | ||
| </svg> | ||
| </button> | ||
| <button | ||
| className={styles.closeButton} | ||
| onClick={closeModal} | ||
| aria-label="Close the zoomed view" | ||
| type="button" | ||
| > | ||
| <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"> | ||
| <line x1="18" y1="6" x2="6" y2="18" /> | ||
| <line x1="6" y1="6" x2="18" y2="18" /> | ||
| </svg> | ||
| </button> | ||
| </div> | ||
| </div> | ||
| <div | ||
| className={styles.modalBody} | ||
| id="modal-description" | ||
| aria-label="Enlarged Mermaid diagram" | ||
| > | ||
| <div | ||
| className={styles.diagramContainer} | ||
| style={{ transform: `scale(${zoomLevel})`, transformOrigin: 'center' }} | ||
| > | ||
| <Mermaid value={children} /> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ) | ||
|
|
||
| return ( | ||
| <> | ||
| <div | ||
| ref={containerRef} | ||
| className={`${styles.mermaidContainer} ${isHovered ? styles.hovered : ''}`} | ||
| onClick={openModal} | ||
| onMouseEnter={() => setIsHovered(true)} | ||
| onMouseLeave={() => setIsHovered(false)} | ||
| role="button" | ||
| tabIndex={0} | ||
| onKeyDown={handleKeyDown} | ||
| aria-label={`Click to enlarge ${title || 'Mermaid diagram'}`} | ||
| aria-expanded={isModalOpen} | ||
| > | ||
| <div className={styles.zoomHint} aria-hidden="true"> | ||
| <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"> | ||
| <circle cx="11" cy="11" r="8" /> | ||
| <path d="m21 21-4.35-4.35" /> | ||
| <path d="M11 8v6" /> | ||
| <path d="M8 11h6" /> | ||
| </svg> | ||
| <span>Click to enlarge</span> | ||
| </div> | ||
| <Mermaid value={children} /> | ||
| </div> | ||
|
|
||
| {isModalOpen && createPortal(modalContent, document.body)} | ||
| </> | ||
| ) | ||
| } | ||
|
|
||
| export default ZoomableMermaid |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.