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