| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | import {Icon} from 'isl-components/Icon'; |
| 9 | import {useState} from 'react'; |
| 10 | import './Collapsable.css'; |
| 11 | |
| 12 | export function Collapsable({ |
| 13 | startExpanded, |
| 14 | children, |
| 15 | title, |
| 16 | className, |
| 17 | onToggle, |
| 18 | }: { |
| 19 | startExpanded?: boolean; |
| 20 | children: React.ReactNode; |
| 21 | title: React.ReactNode; |
| 22 | className?: string; |
| 23 | onToggle?: (expanded: boolean) => unknown; |
| 24 | }) { |
| 25 | const [isExpanded, setIsExpanded] = useState(startExpanded === true); |
| 26 | return ( |
| 27 | <div className={'collapsable' + (className ? ` ${className}` : '')}> |
| 28 | <div |
| 29 | className="collapsable-title" |
| 30 | onClick={() => { |
| 31 | const newState = !isExpanded; |
| 32 | setIsExpanded(newState); |
| 33 | onToggle?.(newState); |
| 34 | }}> |
| 35 | <Icon icon={isExpanded ? 'chevron-down' : 'chevron-right'} /> {title} |
| 36 | </div> |
| 37 | {isExpanded ? <div className="collapsable-contents">{children}</div> : null} |
| 38 | </div> |
| 39 | ); |
| 40 | } |
| 41 | |