deprecated
2 weeks ago
block.json
1 month ago
component.js
2 months ago
edit.js
2 months ago
icon.svg
2 months ago
index.js
2 months ago
index.php
2 months ago
save.js
2 months ago
style.scss
2 weeks ago
component.js
195 lines
| 1 | import { __ } from '@wordpress/i18n'; |
| 2 | import { Component } from '@wordpress/element'; |
| 3 | import parse from 'html-react-parser'; |
| 4 | import { isHexColor } from '@vkblocks/utils/is-hex-color'; |
| 5 | import { sanitizeSlug } from '@vkblocks/utils/sanitizeSlug'; |
| 6 | import { fixBrokenUnicode } from '@vkblocks/utils/fixBrokenUnicode'; |
| 7 | |
| 8 | export class VKBIcon extends Component { |
| 9 | render() { |
| 10 | let fontAwesomeIcon = this.props.lbFontAwesomeIcon; |
| 11 | if (fontAwesomeIcon) { |
| 12 | fontAwesomeIcon = fixBrokenUnicode(fontAwesomeIcon); |
| 13 | } |
| 14 | const iconSize = this.props.lbSize; |
| 15 | const iconSizeUnit = this.props.lbSizeUnit; |
| 16 | const iconMargin = this.props.lbMargin; |
| 17 | const iconMarginUnit = this.props.lbMarginUnit; |
| 18 | const iconRadius = this.props.lbRadius; |
| 19 | const iconAlign = this.props.lbAlign; |
| 20 | const iconType = this.props.lbType; |
| 21 | const iconColor = this.props.lbColor; |
| 22 | const iconFontColor = this.props.lbFontColor; |
| 23 | const iconUrl = this.props.lbUrl; |
| 24 | const iconTarget = this.props.lbTarget; |
| 25 | const relAttribute = this.props.lbRelAttribute; |
| 26 | const linkDescription = this.props.lbLinkDescription; |
| 27 | const linkToPost = this.props.lbLinkToPost; |
| 28 | |
| 29 | // outer & align |
| 30 | let outerClass = 'vk_icon_frame'; |
| 31 | if (iconAlign === 'center') { |
| 32 | outerClass += ' text-center'; |
| 33 | } else if (iconAlign === 'right') { |
| 34 | outerClass += ' text-right'; |
| 35 | } |
| 36 | |
| 37 | // color style |
| 38 | let borderClass = 'vk_icon_border'; |
| 39 | let borderStyle = {}; |
| 40 | |
| 41 | if (iconType === '0') { |
| 42 | // Solid color |
| 43 | if ( |
| 44 | iconColor !== 'undefined' && |
| 45 | iconColor !== null && |
| 46 | iconColor !== undefined |
| 47 | ) { |
| 48 | // Solid color |
| 49 | borderClass += ` has-background`; |
| 50 | |
| 51 | if (iconColor === 'inherit') { |
| 52 | // inherit color |
| 53 | borderStyle = { |
| 54 | backgroundColor: 'currentColor', |
| 55 | }; |
| 56 | } else if (isHexColor(iconColor)) { |
| 57 | // custom color |
| 58 | borderStyle = { |
| 59 | backgroundColor: `${iconColor}`, |
| 60 | }; |
| 61 | } else { |
| 62 | // palette color |
| 63 | borderClass += ` has-${sanitizeSlug(iconColor)}-background-color`; |
| 64 | } |
| 65 | } |
| 66 | } else { |
| 67 | if ( |
| 68 | iconColor !== 'undefined' && |
| 69 | iconColor !== null && |
| 70 | iconColor !== undefined |
| 71 | ) { |
| 72 | borderClass += ` has-text-color`; |
| 73 | |
| 74 | if (iconColor === 'inherit') { |
| 75 | // inherit color |
| 76 | borderStyle = { |
| 77 | color: 'currentColor', |
| 78 | }; |
| 79 | } else if (isHexColor(iconColor)) { |
| 80 | // custom color |
| 81 | borderStyle = { |
| 82 | color: `${iconColor}`, |
| 83 | }; |
| 84 | } else { |
| 85 | // palette color |
| 86 | borderClass += ` has-${sanitizeSlug(iconColor)}-color`; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | if (iconType === '1') { |
| 91 | // Icon & Frame |
| 92 | outerClass += ' is-style-outline'; |
| 93 | } else { |
| 94 | // icon only |
| 95 | outerClass += ' is-style-noline'; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // margin |
| 100 | if ( |
| 101 | !( |
| 102 | iconSize === 36 && |
| 103 | iconSizeUnit === 'px' && |
| 104 | iconMargin === 22 && |
| 105 | iconMarginUnit === 'px' |
| 106 | ) |
| 107 | ) { |
| 108 | borderStyle.width = |
| 109 | 'calc(' + |
| 110 | (iconSize + iconSizeUnit) + |
| 111 | ' + ' + |
| 112 | (iconMargin * 2 + iconMarginUnit) + |
| 113 | ')'; |
| 114 | borderStyle.height = borderStyle.width; |
| 115 | } |
| 116 | |
| 117 | // border radius |
| 118 | if (iconRadius !== 50) { |
| 119 | borderStyle.borderRadius = iconRadius + `%`; |
| 120 | } |
| 121 | |
| 122 | // icon font |
| 123 | let faIconTag = ''; |
| 124 | if (fontAwesomeIcon) { |
| 125 | fontAwesomeIcon = fontAwesomeIcon.replace(/ fas/g, 'fas'); |
| 126 | |
| 127 | // font size |
| 128 | let fontStyle = ``; |
| 129 | let fontClass = ` vk_icon_font `; |
| 130 | if (!(iconSize === 36 && iconSizeUnit === 'px')) { |
| 131 | fontStyle = ` font-size:${iconSize}${iconSizeUnit};`; |
| 132 | } |
| 133 | |
| 134 | // icon color |
| 135 | if ( |
| 136 | iconFontColor !== 'undefined' && |
| 137 | iconFontColor !== null && |
| 138 | iconFontColor !== undefined |
| 139 | ) { |
| 140 | if (isHexColor(iconFontColor)) { |
| 141 | // custom color |
| 142 | fontStyle += ` color:${iconFontColor};`; |
| 143 | } else { |
| 144 | // palette color |
| 145 | fontClass += ` has-text-color has-${sanitizeSlug(iconFontColor)}-color `; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // add class and inline css (join with space to preserve fa-rotate-*, fa-flip-* etc.) |
| 150 | const faIconFragment = fontAwesomeIcon.split(' '); |
| 151 | faIconFragment[0] = faIconFragment[0] + ` style="${fontStyle};"`; |
| 152 | faIconFragment[1] = ' ' + faIconFragment[1] + fontClass; |
| 153 | faIconTag = faIconFragment.join(' '); |
| 154 | } |
| 155 | |
| 156 | const blockContent = ( |
| 157 | <> |
| 158 | <div className={borderClass} style={borderStyle}> |
| 159 | {parse(faIconTag)} |
| 160 | </div> |
| 161 | </> |
| 162 | ); |
| 163 | |
| 164 | let blockContentWrapper = ''; |
| 165 | const hasLink = !!iconUrl || !!linkToPost; |
| 166 | if (hasLink) { |
| 167 | blockContentWrapper = ( |
| 168 | /* eslint-disable-next-line jsx-a11y/anchor-is-valid -- linkToPost: href replaced server-side with permalink */ |
| 169 | <a |
| 170 | href={linkToPost ? '#' : iconUrl} |
| 171 | className="vk_icon_link" |
| 172 | {...(linkToPost ? { 'data-vk-link-to-post': '1' } : {})} |
| 173 | {...(iconTarget ? { target: '_blank' } : {})} |
| 174 | {...(relAttribute ? { rel: relAttribute } : {})} |
| 175 | > |
| 176 | <span className="screen-reader-text"> |
| 177 | {linkDescription |
| 178 | ? linkDescription |
| 179 | : __('Icon link', 'vk-blocks')} |
| 180 | </span> |
| 181 | {blockContent} |
| 182 | </a> |
| 183 | ); |
| 184 | } else { |
| 185 | blockContentWrapper = blockContent; |
| 186 | } |
| 187 | |
| 188 | return ( |
| 189 | <> |
| 190 | <div className={outerClass}>{blockContentWrapper}</div> |
| 191 | </> |
| 192 | ); |
| 193 | } |
| 194 | } |
| 195 |