/
index.js
89 lines (85 loc) · 1.97 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { Button, Tooltip, VisuallyHidden } from '@wordpress/components';
import { forwardRef } from '@wordpress/element';
import { _x, sprintf } from '@wordpress/i18n';
import { Icon, create } from '@wordpress/icons';
/**
* Internal dependencies
*/
import Inserter from '../inserter';
function ButtonBlockAppender(
{
rootClientId,
className,
__experimentalSelectBlockOnInsert: selectBlockOnInsert,
onFocus,
tabIndex,
},
ref
) {
return (
<Inserter
position="bottom center"
rootClientId={ rootClientId }
__experimentalSelectBlockOnInsert={ selectBlockOnInsert }
renderToggle={ ( {
onToggle,
disabled,
isOpen,
blockTitle,
hasSingleBlockType,
} ) => {
let label;
if ( hasSingleBlockType ) {
label = sprintf(
// translators: %s: the name of the block when there is only one
_x( 'Add %s', 'directly add the only allowed block' ),
blockTitle
);
} else {
label = _x(
'Add block',
'Generic label for block inserter button'
);
}
const isToggleButton = ! hasSingleBlockType;
return (
<Tooltip text={ label }>
<Button
ref={ ref }
onFocus={ onFocus }
tabIndex={ tabIndex }
className={ classnames(
className,
'block-editor-button-block-appender'
) }
onClick={ onToggle }
aria-haspopup={
isToggleButton ? 'true' : undefined
}
aria-expanded={
isToggleButton ? isOpen : undefined
}
disabled={ disabled }
label={ label }
>
<VisuallyHidden as="span">{ label }</VisuallyHidden>
<Icon icon={ create } />
</Button>
</Tooltip>
);
} }
isAppender
/>
);
}
/**
* @see https://github.com/WordPress/gutenberg/blob/master/packages/block-editor/src/components/button-block-appender/README.md
*/
export default forwardRef( ButtonBlockAppender );