| 1 |
/** |
| 2 |
* Dashed "Add field" appender for container blocks (Columns, Repeater). |
| 3 |
* Opens the full mini inserter scoped to that container. |
| 4 |
*/ |
| 5 |
import { Inserter } from '@wordpress/block-editor'; |
| 6 |
import { Button } from '@wordpress/components'; |
| 7 |
import { useState } from '@wordpress/element'; |
| 8 |
|
| 9 |
import { resolveMiniInserterPositionFor } from '../lib/miniInserterPlacement'; |
| 10 |
import { setMiniInserterTarget, clearMiniInserterTarget } from '../lib/miniInserterTarget'; |
| 11 |
|
| 12 |
const PlusIcon = () => ( |
| 13 |
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" aria-hidden="true" focusable="false"> |
| 14 |
<path d="M12 5v14M5 12h14" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" /> |
| 15 |
</svg> |
| 16 |
); |
| 17 |
|
| 18 |
/** |
| 19 |
* @param {Object} props |
| 20 |
* @param {string} [props.rootClientId] Container to insert into (undefined = root list). |
| 21 |
* @param {string} props.label Translatable button label. |
| 22 |
*/ |
| 23 |
export default function AddFieldButton( { rootClientId, label } ) { |
| 24 |
const [ position, setPosition ] = useState( 'bottom center' ); |
| 25 |
|
| 26 |
return ( |
| 27 |
<Inserter |
| 28 |
rootClientId={ rootClientId } |
| 29 |
isAppender |
| 30 |
// Full menu (not quick) so Existing Fields tab is available. |
| 31 |
position={ position } |
| 32 |
onToggle={ ( isOpen ) => |
| 33 |
isOpen ? setMiniInserterTarget( { rootClientId } ) : clearMiniInserterTarget() |
| 34 |
} |
| 35 |
renderToggle={ ( { onToggle, disabled } ) => ( |
| 36 |
<Button |
| 37 |
className="wppb-fb-add-button" |
| 38 |
onClick={ ( event ) => { |
| 39 |
setPosition( resolveMiniInserterPositionFor( event.currentTarget ) ); |
| 40 |
onToggle(); |
| 41 |
} } |
| 42 |
disabled={ disabled } |
| 43 |
aria-haspopup="true" |
| 44 |
aria-label={ label } |
| 45 |
> |
| 46 |
<PlusIcon /> |
| 47 |
<span>{ label }</span> |
| 48 |
</Button> |
| 49 |
) } |
| 50 |
/> |
| 51 |
); |
| 52 |
} |
| 53 |
|