addons/isl/src/CommitInfoView/SeeMoreContainer.tsxblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318import type {ReactNode} from 'react';
b69ab319
b69ab3110import {Button} from 'isl-components/Button';
b69ab3111import {Icon} from 'isl-components/Icon';
b69ab3112import {useLayoutEffect, useRef, useState} from 'react';
b69ab3113import {T} from '../i18n';
b69ab3114
b69ab3115import './SeeMoreContainer.css';
b69ab3116
b69ab3117const MAX_NON_EXPANDBLE_HEIGHT_PX = 375;
b69ab3118
b69ab3119export function SeeMoreContainer({
b69ab3120 children,
b69ab3121 maxHeight = MAX_NON_EXPANDBLE_HEIGHT_PX,
b69ab3122 className,
b69ab3123}: {
b69ab3124 children: ReactNode;
b69ab3125 maxHeight?: number;
b69ab3126 className?: string;
b69ab3127}) {
b69ab3128 const ref = useRef<null | HTMLDivElement>(null);
b69ab3129 const [shouldShowExpandButton, setShouldShowExpandbutton] = useState(false);
b69ab3130 const [isExpanded, setIsExpanded] = useState(false);
b69ab3131
b69ab3132 useLayoutEffect(() => {
b69ab3133 const element = ref.current;
b69ab3134 if (element != null && element.scrollHeight > maxHeight) {
b69ab3135 shouldShowExpandButton === false && setShouldShowExpandbutton(true);
b69ab3136 } else {
b69ab3137 shouldShowExpandButton === true && setShouldShowExpandbutton(false);
b69ab3138 }
b69ab3139 // Weird: we pass children to trick it to rerun this effect when the selected commit changes
b69ab3140 // We could also do this by passing a new key to <SeeMoreContainer> in the caller
b69ab3141 }, [ref, shouldShowExpandButton, children, maxHeight]);
b69ab3142
b69ab3143 return (
b69ab3144 <div className={'see-more-container ' + (className ?? '')}>
b69ab3145 <div
b69ab3146 className={`see-more-container-${isExpanded ? 'expanded' : 'collapsed'}`}
b69ab3147 ref={ref}
b69ab3148 style={{maxHeight: isExpanded ? undefined : maxHeight}}>
b69ab3149 {children}
b69ab3150 </div>
b69ab3151 {shouldShowExpandButton ? (
b69ab3152 <div className={`see-${isExpanded ? 'less' : 'more'}-button`}>
b69ab3153 <Button icon onClick={() => setIsExpanded(val => !val)}>
b69ab3154 <Icon icon={isExpanded ? 'fold-up' : 'fold-down'} slot="start" />
b69ab3155 {isExpanded ? <T>See less</T> : <T>See more</T>}
b69ab3156 </Button>
b69ab3157 </div>
b69ab3158 ) : null}
b69ab3159 </div>
b69ab3160 );
b69ab3161}