addons/isl/src/Collapsable.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 {Icon} from 'isl-components/Icon';
b69ab319import {useState} from 'react';
b69ab3110import './Collapsable.css';
b69ab3111
b69ab3112export function Collapsable({
b69ab3113 startExpanded,
b69ab3114 children,
b69ab3115 title,
b69ab3116 className,
b69ab3117 onToggle,
b69ab3118}: {
b69ab3119 startExpanded?: boolean;
b69ab3120 children: React.ReactNode;
b69ab3121 title: React.ReactNode;
b69ab3122 className?: string;
b69ab3123 onToggle?: (expanded: boolean) => unknown;
b69ab3124}) {
b69ab3125 const [isExpanded, setIsExpanded] = useState(startExpanded === true);
b69ab3126 return (
b69ab3127 <div className={'collapsable' + (className ? ` ${className}` : '')}>
b69ab3128 <div
b69ab3129 className="collapsable-title"
b69ab3130 onClick={() => {
b69ab3131 const newState = !isExpanded;
b69ab3132 setIsExpanded(newState);
b69ab3133 onToggle?.(newState);
b69ab3134 }}>
b69ab3135 <Icon icon={isExpanded ? 'chevron-down' : 'chevron-right'} /> {title}
b69ab3136 </div>
b69ab3137 {isExpanded ? <div className="collapsable-contents">{children}</div> : null}
b69ab3138 </div>
b69ab3139 );
b69ab3140}