mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
9d8b21edc0
React IDE page: hide editor and PDF during resize GitOrigin-RevId: bad1a7601d1706e684c91c88c3239a6618479681
90 lines
2.9 KiB
TypeScript
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>
|
|
)
|
|
}
|