2020-06-05 10:00:19 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { useState } from '@wordpress/element';
|
|
|
|
import classNames from 'classnames';
|
2022-02-01 16:54:38 +00:00
|
|
|
import { Icon, chevronUp, chevronDown } from '@wordpress/icons';
|
2023-03-02 14:26:00 +00:00
|
|
|
import type { ReactNode, ReactElement } from 'react';
|
2020-06-05 10:00:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
|
|
|
|
2023-11-20 15:26:21 +00:00
|
|
|
export interface PanelProps {
|
|
|
|
children: ReactNode;
|
|
|
|
className?: string | undefined;
|
2021-03-05 14:03:48 +00:00
|
|
|
initialOpen?: boolean;
|
|
|
|
hasBorder?: boolean;
|
2023-11-20 15:26:21 +00:00
|
|
|
title: ReactNode;
|
2021-03-05 14:03:48 +00:00
|
|
|
titleTag?: keyof JSX.IntrinsicElements;
|
|
|
|
}
|
|
|
|
|
2020-06-05 10:00:19 +00:00
|
|
|
const Panel = ( {
|
|
|
|
children,
|
|
|
|
className,
|
|
|
|
initialOpen = false,
|
2020-06-12 12:07:02 +00:00
|
|
|
hasBorder = false,
|
2020-06-05 10:00:19 +00:00
|
|
|
title,
|
|
|
|
titleTag: TitleTag = 'div',
|
2021-03-05 14:03:48 +00:00
|
|
|
}: PanelProps ): ReactElement => {
|
|
|
|
const [ isOpen, setIsOpen ] = useState< boolean >( initialOpen );
|
2020-06-05 10:00:19 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
2021-02-17 13:48:53 +00:00
|
|
|
className={ classNames( className, 'wc-block-components-panel', {
|
2020-06-12 12:07:02 +00:00
|
|
|
'has-border': hasBorder,
|
|
|
|
} ) }
|
2020-06-05 10:00:19 +00:00
|
|
|
>
|
|
|
|
<TitleTag>
|
|
|
|
<button
|
|
|
|
aria-expanded={ isOpen }
|
2021-02-17 13:48:53 +00:00
|
|
|
className="wc-block-components-panel__button"
|
2020-06-05 10:00:19 +00:00
|
|
|
onClick={ () => setIsOpen( ! isOpen ) }
|
|
|
|
>
|
|
|
|
<Icon
|
|
|
|
aria-hidden="true"
|
2021-02-17 13:48:53 +00:00
|
|
|
className="wc-block-components-panel__button-icon"
|
2022-02-01 16:54:38 +00:00
|
|
|
icon={ isOpen ? chevronUp : chevronDown }
|
2020-06-05 10:00:19 +00:00
|
|
|
/>
|
|
|
|
{ title }
|
|
|
|
</button>
|
|
|
|
</TitleTag>
|
2021-08-13 13:42:09 +00:00
|
|
|
{ isOpen && (
|
|
|
|
<div className="wc-block-components-panel__content">
|
|
|
|
{ children }
|
|
|
|
</div>
|
|
|
|
) }
|
2020-06-05 10:00:19 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Panel;
|