mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-29 15:43:12 +00:00
1be43911b4
Set Prettier's "trailingComma" setting to "es5" GitOrigin-RevId: 9f14150511929a855b27467ad17be6ab262fe5d5
64 lines
1.5 KiB
JavaScript
64 lines
1.5 KiB
JavaScript
import { useRef, useState, useLayoutEffect } from 'react'
|
|
import classNames from 'classnames'
|
|
|
|
function useExpandCollapse({
|
|
initiallyExpanded = false,
|
|
collapsedSize = '0',
|
|
dimension = 'height',
|
|
classes = {},
|
|
} = {}) {
|
|
const ref = useRef()
|
|
const [isExpanded, setIsExpanded] = useState(initiallyExpanded)
|
|
const [sizing, setSizing] = useState({
|
|
size: null,
|
|
needsExpandCollapse: null,
|
|
})
|
|
|
|
useLayoutEffect(() => {
|
|
const expandCollapseEl = ref.current
|
|
if (expandCollapseEl) {
|
|
const expandedSize =
|
|
dimension === 'height'
|
|
? expandCollapseEl.scrollHeight
|
|
: expandCollapseEl.scrollWidth
|
|
|
|
const needsExpandCollapse = expandedSize > collapsedSize
|
|
|
|
if (isExpanded) {
|
|
setSizing({ size: expandedSize, needsExpandCollapse })
|
|
} else {
|
|
setSizing({
|
|
size: needsExpandCollapse ? collapsedSize : expandedSize,
|
|
needsExpandCollapse,
|
|
})
|
|
}
|
|
}
|
|
}, [isExpanded, collapsedSize, dimension])
|
|
|
|
const expandableClasses = classNames(
|
|
'expand-collapse-container',
|
|
classes.container,
|
|
!isExpanded ? classes.containerCollapsed : null
|
|
)
|
|
|
|
function handleToggle() {
|
|
setIsExpanded(!isExpanded)
|
|
}
|
|
|
|
return {
|
|
isExpanded,
|
|
needsExpandCollapse: sizing.needsExpandCollapse,
|
|
expandableProps: {
|
|
ref,
|
|
style: {
|
|
[dimension === 'height' ? 'height' : 'width']: `${sizing.size}px`,
|
|
},
|
|
className: expandableClasses,
|
|
},
|
|
toggleProps: {
|
|
onClick: handleToggle,
|
|
},
|
|
}
|
|
}
|
|
|
|
export default useExpandCollapse
|