components
1 year ago
css
2 years ago
attributes.js
3 years ago
block.js
1 year ago
deprecated.js
3 years ago
edit.js
2 years ago
editor.scss
2 years ago
save.js
3 years ago
transforms.js
1 year ago
save.js
89 lines
| 1 | /** |
| 2 | * Block: Buttons |
| 3 | */ |
| 4 | |
| 5 | import classnames from 'classnames'; |
| 6 | import Element from '../../components/element'; |
| 7 | import { RichText, useBlockProps } from '@wordpress/block-editor'; |
| 8 | import { applyFilters } from '@wordpress/hooks'; |
| 9 | import IconWrapper from '../../components/icon-wrapper'; |
| 10 | |
| 11 | export default ( props ) => { |
| 12 | const { |
| 13 | attributes, |
| 14 | } = props; |
| 15 | |
| 16 | const { |
| 17 | uniqueId, |
| 18 | text, |
| 19 | url, |
| 20 | target, |
| 21 | relNoFollow, |
| 22 | relSponsored, |
| 23 | icon, |
| 24 | iconLocation, |
| 25 | removeText, |
| 26 | ariaLabel, |
| 27 | anchor, |
| 28 | buttonType, |
| 29 | } = attributes; |
| 30 | |
| 31 | const relAttributes = []; |
| 32 | |
| 33 | if ( relNoFollow ) { |
| 34 | relAttributes.push( 'nofollow' ); |
| 35 | } |
| 36 | |
| 37 | if ( target ) { |
| 38 | relAttributes.push( 'noopener', 'noreferrer' ); |
| 39 | } |
| 40 | |
| 41 | if ( relSponsored ) { |
| 42 | relAttributes.push( 'sponsored' ); |
| 43 | } |
| 44 | |
| 45 | let htmlAttributes = { |
| 46 | className: classnames( { |
| 47 | 'gb-button': true, |
| 48 | [ `gb-button-${ uniqueId }` ]: true, |
| 49 | 'gb-button-text': ! icon, |
| 50 | } ), |
| 51 | href: !! url && 'link' === buttonType ? url : null, |
| 52 | target: !! target && 'link' === buttonType ? '_blank' : null, |
| 53 | rel: relAttributes && relAttributes.length > 0 && 'link' === buttonType ? relAttributes.join( ' ' ) : null, |
| 54 | 'aria-label': !! ariaLabel ? ariaLabel : null, |
| 55 | id: anchor ? anchor : null, |
| 56 | }; |
| 57 | |
| 58 | htmlAttributes = applyFilters( |
| 59 | 'generateblocks.frontend.htmlAttributes', |
| 60 | htmlAttributes, |
| 61 | 'generateblocks/button', |
| 62 | attributes |
| 63 | ); |
| 64 | |
| 65 | const blockProps = useBlockProps.save( htmlAttributes ); |
| 66 | const linkButtonTagName = url ? 'a' : 'span'; |
| 67 | const buttonTagName = 'button' === buttonType |
| 68 | ? 'button' |
| 69 | : linkButtonTagName; |
| 70 | |
| 71 | return ( |
| 72 | <Element tagName={ buttonTagName } htmlAttrs={ blockProps }> |
| 73 | <IconWrapper |
| 74 | hasIcon={ !! icon } |
| 75 | direction={ iconLocation } |
| 76 | icon={ icon } |
| 77 | hideChildren={ removeText } |
| 78 | showWrapper={ false } |
| 79 | > |
| 80 | <RichText.Content |
| 81 | value={ text } |
| 82 | tagName={ !! icon ? 'span' : undefined } |
| 83 | className={ !! icon ? 'gb-button-text' : undefined } |
| 84 | /> |
| 85 | </IconWrapper> |
| 86 | </Element> |
| 87 | ); |
| 88 | }; |
| 89 |