import * as React from 'react'; import { ReactNode, useEffect, useRef } from 'react'; import { Panel, PanelGroup, PanelResizeHandle, ImperativePanelGroupHandle, } from 'react-resizable-panels'; import './PlaygroundSplitPane.css'; interface IProps { showInspector?: boolean; editorAndDocumentationSplit?: boolean; inspectorComponent?: ReactNode; editorComponent: ReactNode; documentationComponent?: ReactNode; inspectorTabComponent?: ReactNode; } const PlaygroundSplitPane: React.FC = ({ showInspector = false, inspectorComponent, editorComponent, documentationComponent, editorAndDocumentationSplit = false, inspectorTabComponent, }: IProps) => { const containerHorizontalPanelGroupRef = useRef(null); const containerVerticalPanelGroupRef = useRef(null); // Single effect to handle all layout changes with proper error handling useEffect(() => { // Small timeout to ensure refs are available and panels are mounted const timer = setTimeout(() => { try { // Set horizontal layout if (containerHorizontalPanelGroupRef.current) { if (editorAndDocumentationSplit) { containerHorizontalPanelGroupRef.current.setLayout([50, 50]); } else { containerHorizontalPanelGroupRef.current.setLayout([0, 100]); } } // Set vertical layout if (containerVerticalPanelGroupRef.current) { if (showInspector) { containerVerticalPanelGroupRef.current.setLayout([50, 50]); } else { containerVerticalPanelGroupRef.current.setLayout([100, 0]); } } } catch (error) { // In test environments, this error can be safely ignored console.error('Error setting panel layouts:', error); } }, 0); // Slightly longer timeout to ensure DOM is ready return () => clearTimeout(timer); }, [showInspector, editorAndDocumentationSplit]); // Calculate initial layouts for defaultSize props const initialHorizontalLayout = editorAndDocumentationSplit ? [50, 50] : [0, 100]; const initialVerticalLayout = showInspector ? [50, 50] : [100, 0]; return ( {editorComponent} {documentationComponent} {inspectorTabComponent}
{inspectorComponent}
); }; export default PlaygroundSplitPane;