File size: 2,515 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { Count, Gridicon, MaterialIcon } from '@automattic/components';
import { Button } from '@wordpress/components';
import { Icon, chevronDownSmall } from '@wordpress/icons';
import { useTranslate } from 'i18n-calypso';
import PropTypes from 'prop-types';
import TranslatableString from 'calypso/components/translatable/proptype';
import SidebarHeading from 'calypso/layout/sidebar/heading';
import { decodeEntities } from 'calypso/lib/formatting';

const ExpandableSidebarHeading = ( {
	title,
	count,
	icon,
	customIcon,
	materialIcon,
	materialIconStyle,
	expanded,
	menuId,
	hideExpandableIcon,
	inlineText,
	expandableIconClick,
	...props
} ) => {
	const translate = useTranslate();
	return (
		<SidebarHeading
			aria-controls={ menuId }
			aria-expanded={ expanded ? 'true' : 'false' }
			{ ...props }
		>
			{ icon && <Gridicon className="sidebar__menu-icon" icon={ icon } /> }
			{ materialIcon && (
				<MaterialIcon
					className="sidebar__menu-icon"
					icon={ materialIcon }
					style={ materialIconStyle }
				/>
			) }
			{ undefined !== customIcon && customIcon }
			<span className="sidebar__expandable-title">
				{ decodeEntities( title ) }
				{ undefined !== count && <Count count={ count } /> }
				{ inlineText && <span className="sidebar__inline-text">{ inlineText }</span> }
			</span>
			{ ! hideExpandableIcon &&
				( expandableIconClick ? (
					<Button
						variant="link"
						className="sidebar__expandable-button"
						onClick={ ( ev ) => {
							ev.stopPropagation();
							expandableIconClick();
						} }
						onKeyDown={ ( ev ) => {
							// Prevent bubbling or the SidebarHeading's onClick will also trigger.
							if ( ev.key === 'Enter' ) {
								ev.stopPropagation();
							}
						} }
						aria-label={ expanded ? translate( 'Collapse menu' ) : translate( 'Expand menu' ) }
						icon={
							<Icon icon={ chevronDownSmall } className="sidebar__expandable-arrow" size={ 24 } />
						}
					/>
				) : (
					<Icon icon={ chevronDownSmall } className="sidebar__expandable-arrow" size={ 24 } />
				) ) }
		</SidebarHeading>
	);
};

ExpandableSidebarHeading.propTypes = {
	title: PropTypes.oneOfType( [ TranslatableString, PropTypes.element ] ).isRequired,
	count: PropTypes.number,
	onClick: PropTypes.func,
	customIcon: PropTypes.node,
	icon: PropTypes.string,
	materialIcon: PropTypes.string,
	materialIconStyle: PropTypes.string,
	hideExpandableIcon: PropTypes.bool,
	expandableIconClick: PropTypes.func,
};

export default ExpandableSidebarHeading;