| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {ReactNode} from 'react'; |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | import {Button} from 'isl-components/Button'; |
| b69ab31 | | | 11 | import {Icon} from 'isl-components/Icon'; |
| b69ab31 | | | 12 | import {useLayoutEffect, useRef, useState} from 'react'; |
| b69ab31 | | | 13 | import {T} from '../i18n'; |
| b69ab31 | | | 14 | |
| b69ab31 | | | 15 | import './SeeMoreContainer.css'; |
| b69ab31 | | | 16 | |
| b69ab31 | | | 17 | const MAX_NON_EXPANDBLE_HEIGHT_PX = 375; |
| b69ab31 | | | 18 | |
| b69ab31 | | | 19 | export function SeeMoreContainer({ |
| b69ab31 | | | 20 | children, |
| b69ab31 | | | 21 | maxHeight = MAX_NON_EXPANDBLE_HEIGHT_PX, |
| b69ab31 | | | 22 | className, |
| b69ab31 | | | 23 | }: { |
| b69ab31 | | | 24 | children: ReactNode; |
| b69ab31 | | | 25 | maxHeight?: number; |
| b69ab31 | | | 26 | className?: string; |
| b69ab31 | | | 27 | }) { |
| b69ab31 | | | 28 | const ref = useRef<null | HTMLDivElement>(null); |
| b69ab31 | | | 29 | const [shouldShowExpandButton, setShouldShowExpandbutton] = useState(false); |
| b69ab31 | | | 30 | const [isExpanded, setIsExpanded] = useState(false); |
| b69ab31 | | | 31 | |
| b69ab31 | | | 32 | useLayoutEffect(() => { |
| b69ab31 | | | 33 | const element = ref.current; |
| b69ab31 | | | 34 | if (element != null && element.scrollHeight > maxHeight) { |
| b69ab31 | | | 35 | shouldShowExpandButton === false && setShouldShowExpandbutton(true); |
| b69ab31 | | | 36 | } else { |
| b69ab31 | | | 37 | shouldShowExpandButton === true && setShouldShowExpandbutton(false); |
| b69ab31 | | | 38 | } |
| b69ab31 | | | 39 | // Weird: we pass children to trick it to rerun this effect when the selected commit changes |
| b69ab31 | | | 40 | // We could also do this by passing a new key to <SeeMoreContainer> in the caller |
| b69ab31 | | | 41 | }, [ref, shouldShowExpandButton, children, maxHeight]); |
| b69ab31 | | | 42 | |
| b69ab31 | | | 43 | return ( |
| b69ab31 | | | 44 | <div className={'see-more-container ' + (className ?? '')}> |
| b69ab31 | | | 45 | <div |
| b69ab31 | | | 46 | className={`see-more-container-${isExpanded ? 'expanded' : 'collapsed'}`} |
| b69ab31 | | | 47 | ref={ref} |
| b69ab31 | | | 48 | style={{maxHeight: isExpanded ? undefined : maxHeight}}> |
| b69ab31 | | | 49 | {children} |
| b69ab31 | | | 50 | </div> |
| b69ab31 | | | 51 | {shouldShowExpandButton ? ( |
| b69ab31 | | | 52 | <div className={`see-${isExpanded ? 'less' : 'more'}-button`}> |
| b69ab31 | | | 53 | <Button icon onClick={() => setIsExpanded(val => !val)}> |
| b69ab31 | | | 54 | <Icon icon={isExpanded ? 'fold-up' : 'fold-down'} slot="start" /> |
| b69ab31 | | | 55 | {isExpanded ? <T>See less</T> : <T>See more</T>} |
| b69ab31 | | | 56 | </Button> |
| b69ab31 | | | 57 | </div> |
| b69ab31 | | | 58 | ) : null} |
| b69ab31 | | | 59 | </div> |
| b69ab31 | | | 60 | ); |
| b69ab31 | | | 61 | } |