overleaf/services/web/frontend/js/features/ide-react/components/layout/two-column-main-content.tsx
Tim Down 9d8b21edc0 Merge pull request #15766 from overleaf/td-ide-page-hide-content-during-resize
React IDE page: hide editor and PDF during resize

GitOrigin-RevId: bad1a7601d1706e684c91c88c3239a6618479681
2023-11-16 09:03:11 +00:00

90 lines
2.9 KiB
TypeScript

import React, { ReactNode, useEffect, useState } from 'react'
import { Panel, PanelGroup } from 'react-resizable-panels'
import { HorizontalResizeHandle } from '@/features/ide-react/components/resize/horizontal-resize-handle'
import { useTranslation } from 'react-i18next'
import { HorizontalToggler } from '@/features/ide-react/components/resize/horizontal-toggler'
import useFixedSizeColumn from '@/features/ide-react/hooks/use-fixed-size-column'
import useCollapsiblePanel from '@/features/ide-react/hooks/use-collapsible-panel'
import classNames from 'classnames'
type TwoColumnMainContentProps = {
leftColumnId: string
leftColumnContent: ReactNode
leftColumnDefaultSize: number
setLeftColumnDefaultSize: React.Dispatch<React.SetStateAction<number>>
rightColumnContent: ReactNode
leftColumnIsOpen: boolean
setLeftColumnIsOpen: (
leftColumnIsOpen: TwoColumnMainContentProps['leftColumnIsOpen']
) => void
shouldPersistLayout?: boolean
}
export default function TwoColumnMainContent({
leftColumnId,
leftColumnContent,
leftColumnDefaultSize,
setLeftColumnDefaultSize,
rightColumnContent,
leftColumnIsOpen,
setLeftColumnIsOpen,
shouldPersistLayout = false,
}: TwoColumnMainContentProps) {
const { t } = useTranslation()
const {
fixedPanelRef: leftColumnPanelRef,
fixedPanelWidthRef: leftColumnWidthRef,
handleLayout,
} = useFixedSizeColumn(leftColumnDefaultSize, leftColumnIsOpen)
useCollapsiblePanel(leftColumnIsOpen, leftColumnPanelRef)
const [resizing, setResizing] = useState(false)
// Update the left column default size on unmount rather than doing it on
// every resize, which causes ResizeObserver errors
useEffect(() => {
if (leftColumnWidthRef.current) {
setLeftColumnDefaultSize(leftColumnWidthRef.current.size)
}
}, [leftColumnWidthRef, setLeftColumnDefaultSize])
return (
<PanelGroup
autoSaveId={
shouldPersistLayout ? 'ide-react-main-content-layout' : undefined
}
direction="horizontal"
onLayout={handleLayout}
className={classNames({
'ide-react-main-content-resizing': resizing,
})}
>
<Panel
ref={leftColumnPanelRef}
defaultSize={leftColumnDefaultSize}
minSize={5}
collapsible
onCollapse={collapsed => setLeftColumnIsOpen(!collapsed)}
>
{leftColumnContent}
</Panel>
<HorizontalResizeHandle
onDoubleClick={() => setLeftColumnIsOpen(!leftColumnIsOpen)}
resizable={leftColumnIsOpen}
onDragging={setResizing}
>
<HorizontalToggler
id={leftColumnId}
togglerType="west"
isOpen={leftColumnIsOpen}
setIsOpen={setLeftColumnIsOpen}
tooltipWhenOpen={t('tooltip_hide_filetree')}
tooltipWhenClosed={t('tooltip_show_filetree')}
/>
</HorizontalResizeHandle>
<Panel className="ide-react-panel">{rightColumnContent}</Panel>
</PanelGroup>
)
}