🧜 refactor: Focus Rendering for Mermaid Diagram (#11181)
Some checks are pending
Docker Dev Branch Images Build / build (Dockerfile, lc-dev, node) (push) Waiting to run
Docker Dev Branch Images Build / build (Dockerfile.multi, lc-dev-api, api-build) (push) Waiting to run
Docker Dev Images Build / build (Dockerfile, librechat-dev, node) (push) Waiting to run
Docker Dev Images Build / build (Dockerfile.multi, librechat-dev-api, api-build) (push) Waiting to run
Sync Locize Translations & Create Translation PR / Sync Translation Keys with Locize (push) Waiting to run
Sync Locize Translations & Create Translation PR / Create Translation PR on Version Published (push) Blocked by required conditions

- Increased the minimum container height from 100 to 200 for improved layout.
- Added state management for focus within the Mermaid component to enhance user interaction.
- Updated control visibility logic to include focus state, improving accessibility and user experience during interactions.
This commit is contained in:
Danny Avila 2026-01-02 13:17:07 -05:00 committed by GitHub
parent b1a2b96276
commit cda6d589d6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -27,7 +27,7 @@ interface MermaidProps {
const MIN_ZOOM = 0.25;
const MAX_ZOOM = 4;
const ZOOM_STEP = 0.25;
const MIN_CONTAINER_HEIGHT = 100;
const MIN_CONTAINER_HEIGHT = 200;
const MAX_CONTAINER_HEIGHT = 500;
const Mermaid: React.FC<MermaidProps> = memo(({ children, id, theme }) => {
@ -50,6 +50,7 @@ const Mermaid: React.FC<MermaidProps> = memo(({ children, id, theme }) => {
);
const [containerWidth, setContainerWidth] = useState(700);
const [isHovered, setIsHovered] = useState(false);
const [isFocusWithin, setIsFocusWithin] = useState(false);
const [isTouchDevice, setIsTouchDevice] = useState(false);
const [showMobileControls, setShowMobileControls] = useState(false);
@ -408,7 +409,9 @@ const Mermaid: React.FC<MermaidProps> = memo(({ children, id, theme }) => {
};
}, [isPanning]);
const showControls = isTouchDevice ? showMobileControls || showCode : isHovered || showCode;
const showControls = isTouchDevice
? showMobileControls || showCode
: isHovered || isFocusWithin || showCode;
const handleContainerClick = useCallback(
(e: React.MouseEvent | React.TouchEvent) => {
@ -647,6 +650,12 @@ const Mermaid: React.FC<MermaidProps> = memo(({ children, id, theme }) => {
)}
onMouseEnter={() => setIsHovered(true)}
onMouseLeave={() => setIsHovered(false)}
onFocus={() => setIsFocusWithin(true)}
onBlur={(e) => {
if (!e.currentTarget.contains(e.relatedTarget as Node)) {
setIsFocusWithin(false);
}
}}
onClick={handleContainerClick}
>
<MermaidHeader
@ -765,6 +774,12 @@ const Mermaid: React.FC<MermaidProps> = memo(({ children, id, theme }) => {
)}
onMouseEnter={() => setIsHovered(true)}
onMouseLeave={() => setIsHovered(false)}
onFocus={() => setIsFocusWithin(true)}
onBlur={(e) => {
if (!e.currentTarget.contains(e.relatedTarget as Node)) {
setIsFocusWithin(false);
}
}}
onClick={handleContainerClick}
>
<MermaidHeader