LinkBlockToolbar.jsx
46 lines
| 1 | import { __ } from '@wordpress/i18n'; |
| 2 | import { ToolbarButton, Dropdown } from '@wordpress/components'; |
| 3 | import { BlockControls } from '@wordpress/block-editor'; |
| 4 | import { link } from '@wordpress/icons'; |
| 5 | import { URLControls } from '../../components/url-controls'; |
| 6 | |
| 7 | export function LinkBlockToolbar( { tagName, setAttributes, htmlAttributes, context } ) { |
| 8 | const POPOVER_PROPS = { |
| 9 | position: 'bottom right', |
| 10 | }; |
| 11 | |
| 12 | if ( 'a' !== tagName ) { |
| 13 | return null; |
| 14 | } |
| 15 | |
| 16 | const url = htmlAttributes?.href ?? ''; |
| 17 | |
| 18 | return ( |
| 19 | <BlockControls group="inline"> |
| 20 | <Dropdown |
| 21 | contentClassName="gblocks-button-link-dropdown" |
| 22 | popoverProps={ POPOVER_PROPS } |
| 23 | renderToggle={ ( { isOpen, onToggle } ) => ( |
| 24 | <ToolbarButton |
| 25 | icon={ link } |
| 26 | label={ ! url ? __( 'Add Link', 'generateblocks' ) : __( 'Change Link', 'generateblocks' ) } |
| 27 | onClick={ onToggle } |
| 28 | aria-expanded={ isOpen } |
| 29 | isPressed={ !! url } |
| 30 | /> |
| 31 | ) } |
| 32 | renderContent={ () => ( |
| 33 | <> |
| 34 | <URLControls |
| 35 | htmlAttributes={ htmlAttributes } |
| 36 | setAttributes={ setAttributes } |
| 37 | context={ context } |
| 38 | tagName={ tagName } |
| 39 | /> |
| 40 | </> |
| 41 | ) } |
| 42 | /> |
| 43 | </BlockControls> |
| 44 | ); |
| 45 | } |
| 46 |