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