2020-06-05 10:00:19 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { useState } from '@wordpress/element';
|
2021-03-09 10:55:24 +00:00
|
|
|
import type { ReactNode, ReactElement } from 'react';
|
2020-06-05 10:00:19 +00:00
|
|
|
import classNames from 'classnames';
|
|
|
|
import { Icon, chevronUp, chevronDown } from '@woocommerce/icons';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
|
|
|
|
2021-03-05 14:03:48 +00:00
|
|
|
interface PanelProps {
|
2021-03-09 10:55:24 +00:00
|
|
|
children?: ReactNode;
|
2021-03-05 14:03:48 +00:00
|
|
|
className?: string;
|
|
|
|
initialOpen?: boolean;
|
|
|
|
hasBorder?: boolean;
|
|
|
|
title?: ReactNode;
|
|
|
|
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"
|
2020-06-05 10:00:19 +00:00
|
|
|
srcElement={ isOpen ? chevronUp : chevronDown }
|
|
|
|
/>
|
|
|
|
{ title }
|
|
|
|
</button>
|
|
|
|
</TitleTag>
|
|
|
|
<div
|
2021-02-17 13:48:53 +00:00
|
|
|
className="wc-block-components-panel__content"
|
2020-06-05 10:00:19 +00:00
|
|
|
hidden={ ! isOpen }
|
|
|
|
>
|
|
|
|
{ children }
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Panel;
|