BlockControls.js
2 years ago
ButtonContentRenderer.js
3 years ago
ComponentCSS.js
4 years ago
ConditionalColors.js
2 years ago
InspectorAdvancedControls.js
3 years ago
ButtonContentRenderer.js
113 lines
| 1 | import { RichText, useBlockProps } from '@wordpress/block-editor'; |
| 2 | import IconWrapper from '../../../components/icon-wrapper'; |
| 3 | import { __ } from '@wordpress/i18n'; |
| 4 | import Element from '../../../components/element'; |
| 5 | import RootElement from '../../../components/root-element'; |
| 6 | import classnames from 'classnames'; |
| 7 | import { applyFilters, doAction } from '@wordpress/hooks'; |
| 8 | |
| 9 | export default function ButtonContentRenderer( props ) { |
| 10 | const { |
| 11 | attributes, |
| 12 | setAttributes, |
| 13 | isSelected, |
| 14 | InnerContent = RichText, |
| 15 | context, |
| 16 | name, |
| 17 | buttonRef, |
| 18 | clientId, |
| 19 | } = props; |
| 20 | |
| 21 | const { |
| 22 | uniqueId, |
| 23 | anchor, |
| 24 | text, |
| 25 | url, |
| 26 | target, |
| 27 | relNoFollow, |
| 28 | relSponsored, |
| 29 | icon, |
| 30 | iconLocation, |
| 31 | removeText, |
| 32 | ariaLabel, |
| 33 | buttonType, |
| 34 | } = attributes; |
| 35 | |
| 36 | const relAttributes = []; |
| 37 | |
| 38 | if ( relNoFollow ) { |
| 39 | relAttributes.push( 'nofollow' ); |
| 40 | } |
| 41 | |
| 42 | if ( target ) { |
| 43 | relAttributes.push( 'noopener', 'noreferrer' ); |
| 44 | } |
| 45 | |
| 46 | if ( relSponsored ) { |
| 47 | relAttributes.push( 'sponsored' ); |
| 48 | } |
| 49 | |
| 50 | let htmlAttributes = { |
| 51 | className: classnames( { |
| 52 | 'gb-button': true, |
| 53 | [ `gb-button-${ uniqueId }` ]: true, |
| 54 | 'gb-button-text': ! icon, |
| 55 | } ), |
| 56 | rel: relAttributes && relAttributes.length > 0 && 'link' === buttonType ? relAttributes.join( ' ' ) : null, |
| 57 | 'aria-label': !! ariaLabel ? ariaLabel : null, |
| 58 | id: anchor ? anchor : null, |
| 59 | ref: buttonRef, |
| 60 | }; |
| 61 | |
| 62 | htmlAttributes = applyFilters( |
| 63 | 'generateblocks.frontend.htmlAttributes', |
| 64 | htmlAttributes, |
| 65 | 'generateblocks/button', |
| 66 | attributes |
| 67 | ); |
| 68 | |
| 69 | const blockProps = useBlockProps( htmlAttributes ); |
| 70 | |
| 71 | const richTextFormats = applyFilters( |
| 72 | 'generateblocks.editor.buttonDisableFormatting', |
| 73 | false, |
| 74 | props |
| 75 | ) ? [] : [ 'core/bold', 'core/italic', 'core/strikethrough' ]; |
| 76 | |
| 77 | let buttonTagName = url ? 'a' : 'span'; |
| 78 | |
| 79 | // The `button` element prevents RichText from allowing spaces. |
| 80 | // To fix that we'll return a `span` element in the editor only. |
| 81 | if ( 'button' === buttonType ) { |
| 82 | buttonTagName = 'span'; |
| 83 | } |
| 84 | |
| 85 | doAction( 'generateblocks.editor.renderBlock', { ...props, ref: buttonRef } ); |
| 86 | |
| 87 | return ( |
| 88 | <RootElement name={ name } clientId={ clientId }> |
| 89 | <Element tagName={ buttonTagName } htmlAttrs={ blockProps }> |
| 90 | <IconWrapper |
| 91 | hasIcon={ !! icon } |
| 92 | icon={ icon } |
| 93 | direction={ iconLocation } |
| 94 | hideChildren={ removeText } |
| 95 | showWrapper={ ! removeText && !! icon } |
| 96 | wrapperClassname={ 'gb-button-text' } |
| 97 | > |
| 98 | <InnerContent |
| 99 | name={ name } |
| 100 | placeholder={ __( 'Add text…', 'generateblocks' ) } |
| 101 | value={ text } |
| 102 | onChange={ ( value ) => setAttributes( { text: value } ) } |
| 103 | allowedFormats={ richTextFormats } |
| 104 | isSelected={ isSelected } |
| 105 | attributes={ attributes } |
| 106 | context={ context } |
| 107 | /> |
| 108 | </IconWrapper> |
| 109 | </Element> |
| 110 | </RootElement> |
| 111 | ); |
| 112 | } |
| 113 |