mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
e61eb1b220
Improve calculations of empty state, mini state and sizes variables in review panel GitOrigin-RevId: 41bcb3b67c9f0019c11b4de0e4590b0407e04e66
54 lines
1.6 KiB
TypeScript
54 lines
1.6 KiB
TypeScript
import { CSSProperties, useCallback, useEffect, useState } from 'react'
|
|
import { useCodeMirrorViewContext } from '@/features/source-editor/components/codemirror-editor'
|
|
|
|
export const useReviewPanelStyles = (mini: boolean) => {
|
|
const view = useCodeMirrorViewContext()
|
|
const [styles, setStyles] = useState<CSSProperties>()
|
|
|
|
const updateScrollDomVariables = useCallback((element: HTMLDivElement) => {
|
|
const { top, bottom } = element.getBoundingClientRect()
|
|
|
|
setStyles(value => ({
|
|
...value,
|
|
'--review-panel-top': `${top}px`,
|
|
'--review-panel-bottom': `${bottom}px`,
|
|
}))
|
|
}, [])
|
|
|
|
const updateContentDomVariables = useCallback((element: HTMLDivElement) => {
|
|
const { height } = element.getBoundingClientRect()
|
|
|
|
setStyles(value => ({
|
|
...value,
|
|
'--review-panel-height': `${height}px`,
|
|
}))
|
|
}, [])
|
|
|
|
useEffect(() => {
|
|
setStyles(value => ({
|
|
...value,
|
|
'--review-panel-width': mini ? '22px' : '230px',
|
|
}))
|
|
}, [mini])
|
|
|
|
useEffect(() => {
|
|
if ('ResizeObserver' in window) {
|
|
const scrollDomObserver = new window.ResizeObserver(entries =>
|
|
updateScrollDomVariables(entries[0]?.target as HTMLDivElement)
|
|
)
|
|
scrollDomObserver.observe(view.scrollDOM)
|
|
|
|
const contentDomObserver = new window.ResizeObserver(entries =>
|
|
updateContentDomVariables(entries[0]?.target as HTMLDivElement)
|
|
)
|
|
contentDomObserver.observe(view.contentDOM)
|
|
|
|
return () => {
|
|
scrollDomObserver.disconnect()
|
|
contentDomObserver.disconnect()
|
|
}
|
|
}
|
|
}, [view, updateScrollDomVariables, updateContentDomVariables])
|
|
|
|
return styles
|
|
}
|