-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
index.js
58 lines (53 loc) · 1.37 KB
/
index.js
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
/**
* External dependencies
*/
import classnames from 'classnames';
import { flatMap } from 'lodash';
/**
* Internal dependencies
*/
import './style.scss';
import IconButton from '../icon-button';
function Toolbar( { controls = [], children, className } ) {
if (
( ! controls || ! controls.length ) &&
! children
) {
return null;
}
// Normalize controls to nested array of objects (sets of controls)
let controlSets = controls;
if ( ! Array.isArray( controlSets[ 0 ] ) ) {
controlSets = [ controlSets ];
}
return (
<div className={ classnames( 'components-toolbar', className ) }>
{ flatMap( controlSets, ( controlSet, setIndex ) => (
controlSet.map( ( control, controlIndex ) => (
<div
key={ [ setIndex, controlIndex ].join() }
className={ setIndex > 0 && controlIndex === 0 ? 'has-left-divider' : null }
>
<IconButton
icon={ control.icon }
label={ control.title }
data-subscript={ control.subscript }
onClick={ ( event ) => {
event.stopPropagation();
control.onClick();
} }
className={ classnames( 'components-toolbar__control', {
'is-active': control.isActive,
} ) }
aria-pressed={ control.isActive }
disabled={ control.isDisabled }
/>
{ control.children }
</div>
) )
) ) }
{ children }
</div>
);
}
export default Toolbar;