overleaf/services/web/frontend/js/features/review-panel-new/components/review-panel-expandable-content.tsx
ilkin-overleaf 8bc374c916 Merge pull request #21115 from overleaf/ii-bs5-review-panel
[web] BS5 review panel new

GitOrigin-RevId: c65d17d0053858bd74984ba746a620b89d900606
2024-10-21 08:04:37 +00:00

72 lines
1.8 KiB
TypeScript

import { FC, useCallback, useEffect, useRef, useState } from 'react'
import classNames from 'classnames'
import OLButton from '@/features/ui/components/ol/ol-button'
import { useTranslation } from 'react-i18next'
export const ExpandableContent: FC<{ className?: string }> = ({
children,
className,
}) => {
const { t } = useTranslation()
const contentRef = useRef<HTMLDivElement>(null)
const [isExpanded, setIsExpanded] = useState(false)
const [isOverflowing, setIsOverflowing] = useState(false)
useEffect(() => {
if (contentRef.current) {
setIsOverflowing(
contentRef.current.scrollHeight > contentRef.current.clientHeight
)
}
}, [])
const handleShowMore = useCallback(() => {
setIsExpanded(true)
contentRef.current?.dispatchEvent(
new CustomEvent('review-panel:position', { bubbles: true })
)
}, [])
const handleShowLess = useCallback(() => {
setIsExpanded(false)
contentRef.current?.dispatchEvent(
new CustomEvent('review-panel:position', { bubbles: true })
)
}, [])
return (
<div>
<div
ref={contentRef}
className={classNames(
'review-panel-content-expandable',
{
'review-panel-content-expanded': isExpanded,
},
className
)}
>
{children}
</div>
{isExpanded ? (
<OLButton
variant="link"
className="btn-inline-link"
onClick={handleShowLess}
>
{t('show_less')}
</OLButton>
) : (
isOverflowing && (
<OLButton
variant="link"
className="btn-inline-link"
onClick={handleShowMore}
>
{t('show_more')}
</OLButton>
)
)}
</div>
)
}