-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
63 lines (55 loc) · 1.54 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
59
60
61
62
63
/**
* External dependencies
*/
import classnames from 'classnames';
import { isString, isArray } from 'lodash';
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
/**
* Internal dependencies
*/
import './style.scss';
import Tooltip from '../tooltip';
import Button from '../button';
import Dashicon from '../dashicon';
// This is intentionally a Component class, not a function component because it
// is common to apply a ref to the button element (only supported in class)
class IconButton extends Component {
render() {
const { icon, children, label, className, tooltip, focus, shortcut, ...additionalProps } = this.props;
const classes = classnames( 'components-icon-button', className );
const tooltipText = tooltip || label;
// Should show the tooltip if...
const showTooltip = (
// an explicit tooltip is passed or...
tooltip ||
// there's a shortcut or...
shortcut ||
(
// there's a label and...
!! label &&
// the children are empty and...
( ! children || ( isArray( children ) && ! children.length ) ) &&
// the tooltip is not explicitly disabled.
false !== tooltip
)
);
let element = (
<Button { ...additionalProps } aria-label={ label } className={ classes } focus={ focus }>
{ isString( icon ) ? <Dashicon icon={ icon } /> : icon }
{ children }
</Button>
);
if ( showTooltip ) {
element = (
<Tooltip text={ tooltipText } shortcut={ shortcut }>
{ element }
</Tooltip>
);
}
return element;
}
}
export default IconButton;