mirror of
https://github.com/aaif-goose/goose.git
synced 2026-07-03 14:10:03 +02:00
5.2 KiB
5.2 KiB
🔧 Code Mode Height Calculation Fix
Problem
When entering code mode with #python (or other languages), the IDE code block container wasn't properly passing its height to the chat input component. This caused two issues:
- Initial load: The IDE space was "below the fold" - not visible without scrolling
- Height not updating: The container didn't expand to show the full code block
Root Cause
The height synchronization logic (syncDisplayHeight) was measuring the textarea's scrollHeight, which only accounts for the raw text content. However, in code mode:
- The display layer renders a styled code block with:
- Padding (
p-2) - Borders (
border border-gray-700/50) - Margins (
mt-1,mb-1) - Language badge
- Syntax highlighting container
- Padding (
The textarea has no knowledge of these visual elements, so its scrollHeight was much smaller than the actual rendered height of the display layer.
Solution
Added a dedicated useEffect that:
- Triggers when
codeModechanges - detects when entering/exiting code mode - Measures the display layer's
scrollHeight- gets the actual rendered height including all styling - Uses a 50ms delay - ensures
SyntaxHighlighterhas fully rendered before measuring - Updates both layers and container - sets textarea, display, and containerHeight to match
Implementation
// Sync height when code mode changes or when in code mode (for initial render and updates)
useEffect(() => {
if (codeMode) {
// When code mode is active, we need to measure the actual display height
// because the textarea doesn't know about the styled code block
console.log('💻 CODE MODE: Triggering height sync for code mode');
// Use a small delay to ensure the SyntaxHighlighter has rendered
const timer = setTimeout(() => {
if (displayRef.current && hiddenTextareaRef.current) {
const display = displayRef.current;
const textarea = hiddenTextareaRef.current;
// Get the actual rendered height of the display content
const displayScrollHeight = display.scrollHeight;
console.log('💻 CODE MODE: Display scrollHeight:', displayScrollHeight);
// Calculate line height
const computedStyle = window.getComputedStyle(textarea);
const fontSize = parseFloat(computedStyle.fontSize);
const lineHeightValue = computedStyle.lineHeight;
const lineHeight = Math.round(lineHeightValue === "normal" ? fontSize * 1.2 : parseFloat(lineHeightValue));
const minHeight = rows * lineHeight;
const maxHeight = style?.maxHeight ? parseInt(style.maxHeight.toString()) : 300;
// Use the display's scroll height instead of textarea's
const desiredHeight = Math.min(displayScrollHeight, maxHeight);
const finalHeight = Math.max(desiredHeight, minHeight);
console.log('💻 CODE MODE: Setting height to', finalHeight);
// Update both layers
textarea.style.height = `${finalHeight}px`;
display.style.height = `${finalHeight}px`;
setContainerHeight(finalHeight);
}
}, 50); // Small delay to let SyntaxHighlighter render
return () => clearTimeout(timer);
}
}, [codeMode, value, rows, style]);
How It Works
Before the Fix
- User types
#python - Code mode activates
- Display layer renders styled code block (100px tall)
syncDisplayHeightmeasures textarea scrollHeight (30px - just the raw text)- Container height set to 30px
- Code block extends beyond container (70px hidden below the fold)
After the Fix
- User types
#python - Code mode activates
- Display layer renders styled code block (100px tall)
- New
useEffecttriggers after 50ms - Measures display layer's scrollHeight (100px - includes all styling)
- Container height set to 100px
- Code block fully visible ✅
Dependencies
The useEffect depends on:
codeMode- triggers when entering/exiting code modevalue- re-measures when code content changesrows- for minHeight calculationstyle- for maxHeight constraint
Testing
To verify the fix:
-
Initial Load Test:
- Type
#pythonin the chat input - ✅ The code block should immediately be fully visible
- ✅ No scrolling required to see the IDE container
- Type
-
Height Update Test:
- Type
#pythonand add multiple lines of code - ✅ The container should expand as you type
- ✅ All code should remain visible
- Type
-
Exit Code Mode Test:
- Delete the
#pythontrigger - ✅ The container should shrink back to normal text height
- Delete the
Related Files
ui/desktop/src/components/RichChatInput.tsx- Main implementation
Commits
commit 90b384c1c9f
Fix code mode height calculation to use display layer scrollHeight
commit 83eba3f3a79
Fix code block width constraints and ensure proper height calculation
Notes
- The 50ms delay is necessary because
SyntaxHighlighterrenders asynchronously - The effect cleans up the timer to prevent memory leaks
- This works alongside the existing
syncDisplayHeightfor normal text - The display layer's scrollHeight is the source of truth in code mode