deprecated
2 weeks ago
block.json
1 day ago
component.js
2 weeks ago
edit.js
1 day ago
icon.svg
2 months ago
index.js
2 weeks ago
index.php
2 months ago
save.js
2 weeks ago
style.scss
2 weeks ago
transforms.js
2 months ago
index.js
261 lines
| 1 | /** |
| 2 | * Button block type |
| 3 | * |
| 4 | */ |
| 5 | import { ReactComponent as Icon } from './icon.svg'; |
| 6 | import { title, iconName, url, iconUser } from '@vkblocks/utils/example-data'; |
| 7 | import edit from './edit'; |
| 8 | import metadata from './block.json'; |
| 9 | import save from './save'; |
| 10 | import { deprecated } from './deprecated/save/'; |
| 11 | import deprecatedHooks from './deprecated/hooks'; |
| 12 | import transforms from './transforms'; |
| 13 | import { addFilter } from '@wordpress/hooks'; |
| 14 | import { createHigherOrderComponent } from '@wordpress/compose'; |
| 15 | import { isHexColor } from '@vkblocks/utils/is-hex-color'; |
| 16 | |
| 17 | const { name } = metadata; |
| 18 | |
| 19 | export { metadata, name }; |
| 20 | |
| 21 | export const settings = { |
| 22 | icon: <Icon />, |
| 23 | example: { |
| 24 | attributes: { |
| 25 | content: iconName, |
| 26 | subCaption: title, |
| 27 | buttonUrl: url, |
| 28 | buttonTarget: false, |
| 29 | buttonSize: 'md', |
| 30 | buttonType: '0', |
| 31 | buttonEffect: '', |
| 32 | buttonColor: 'primary', |
| 33 | buttonTextColorCustom: 'undefined', |
| 34 | buttonColorCustom: 'undefined', |
| 35 | buttonAlign: 'left', |
| 36 | buttonWidthMobile: 0, |
| 37 | buttonWidthTablet: 0, |
| 38 | outerGap: null, |
| 39 | buttonWidth: 0, |
| 40 | fontAwesomeIconBefore: iconUser, |
| 41 | fontAwesomeIconAfter: iconUser, |
| 42 | iconSizeBefore: null, |
| 43 | iconSizeAfter: null, |
| 44 | }, |
| 45 | }, |
| 46 | edit, |
| 47 | save, |
| 48 | deprecated, |
| 49 | transforms, |
| 50 | }; |
| 51 | |
| 52 | /** |
| 53 | * ホバー色のCSS値を取得する |
| 54 | * @param {string} value - 色の値(HEX、rgba、またはパレットスラッグ) |
| 55 | * @return {string|null} CSS で使用できる色の値、または null |
| 56 | */ |
| 57 | const getHoverColorCssValue = (value) => { |
| 58 | if (value === undefined || value === '' || value === null) { |
| 59 | return null; |
| 60 | } |
| 61 | // HEX カラーの場合 |
| 62 | if (isHexColor(value)) { |
| 63 | return value; |
| 64 | } |
| 65 | // rgba / rgb / hsla / hsl カラーの場合(厳格にバリデーション) |
| 66 | const cssFuncColorPattern = /^(rgba?|hsla?)\(\s*[\d.%\s,/]+\s*\)$/; |
| 67 | if (cssFuncColorPattern.test(value)) { |
| 68 | return value; |
| 69 | } |
| 70 | // パレットのスラッグの場合 → CSS カスタムプロパティで参� |
| 71 | � |
| 72 | return `var(--wp--preset--color--${value})`; |
| 73 | }; |
| 74 | |
| 75 | const generateInlineCss = (attributes) => { |
| 76 | const { |
| 77 | buttonTextColorCustom, |
| 78 | buttonColorCustom, |
| 79 | buttonHoverBgColorCustom, |
| 80 | buttonHoverTextColorCustom, |
| 81 | buttonType, |
| 82 | blockId, |
| 83 | } = attributes; |
| 84 | let inlineCss = ''; |
| 85 | |
| 86 | // カスタムカラーの場合 |
| 87 | if (buttonColorCustom !== undefined && isHexColor(buttonColorCustom)) { |
| 88 | // 塗り |
| 89 | if (buttonType === '0' || buttonType === null) { |
| 90 | inlineCss += `.vk_button-${blockId} .has-background { |
| 91 | background-color: ${buttonColorCustom}; |
| 92 | border: 1px solid ${buttonColorCustom}; |
| 93 | }`; |
| 94 | } |
| 95 | // アウトライン |
| 96 | if (buttonType === '1') { |
| 97 | inlineCss += `.vk_button-${blockId} .has-text-color.is-style-outline { |
| 98 | background-color: transparent; |
| 99 | border: 1px solid ${buttonColorCustom}; |
| 100 | color: ${buttonColorCustom}; |
| 101 | } |
| 102 | .vk_button-${blockId} .has-text-color.is-style-outline:hover { |
| 103 | background-color: ${buttonColorCustom}; |
| 104 | border: 1px solid ${buttonColorCustom}; |
| 105 | color: #fff; |
| 106 | }`; |
| 107 | } |
| 108 | // テキストのみ |
| 109 | if (buttonType === '2') { |
| 110 | inlineCss = `.vk_button-${blockId} .has-text-color.vk_button_link-type-text { |
| 111 | color: ${buttonColorCustom}; |
| 112 | }`; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // 文字色がカスタムカラーの場合 |
| 117 | if ( |
| 118 | buttonTextColorCustom !== undefined && |
| 119 | isHexColor(buttonTextColorCustom) |
| 120 | ) { |
| 121 | if (buttonType === '0' || buttonType === null) { |
| 122 | inlineCss += ` .vk_button-${blockId} .has-text-color { |
| 123 | color: ${buttonTextColorCustom}; |
| 124 | }`; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // ホバー時の背景色が設定されている場合 |
| 129 | // WP コアが .has-*-background-color に !important を付与するため |
| 130 | // !important で上書きする� |
| 131 | 要がある |
| 132 | // filter: none でテーマの brightness/saturate フィルタをリセット |
| 133 | const hoverBgCssValue = getHoverColorCssValue(buttonHoverBgColorCustom); |
| 134 | if (hoverBgCssValue) { |
| 135 | inlineCss += ` .vk_button.vk_button-${blockId} .vk_button_link:hover { |
| 136 | background-color: ${hoverBgCssValue} !important; |
| 137 | border-color: ${hoverBgCssValue} !important; |
| 138 | box-shadow: none !important; |
| 139 | opacity: 1 !important; |
| 140 | filter: none !important; |
| 141 | }`; |
| 142 | } |
| 143 | |
| 144 | // ホバー時のテキスト色が設定されている場合 |
| 145 | const hoverTextCssValue = getHoverColorCssValue(buttonHoverTextColorCustom); |
| 146 | if (hoverTextCssValue) { |
| 147 | inlineCss += ` .vk_button.vk_button-${blockId} .vk_button_link:hover { |
| 148 | color: ${hoverTextCssValue} !important; |
| 149 | }`; |
| 150 | inlineCss += ` .vk_button.vk_button-${blockId} .vk_button_link:hover .vk_button_link_txt, |
| 151 | .vk_button.vk_button-${blockId} .vk_button_link:hover .vk_button_link_subCaption, |
| 152 | .vk_button.vk_button-${blockId} .vk_button_link:hover .vk_button_link_before, |
| 153 | .vk_button.vk_button-${blockId} .vk_button_link:hover .vk_button_link_after { |
| 154 | color: ${hoverTextCssValue} !important; |
| 155 | }`; |
| 156 | } |
| 157 | |
| 158 | return inlineCss; |
| 159 | }; |
| 160 | |
| 161 | const generateInlineGapCss = (attributes, isSave) => { |
| 162 | const { |
| 163 | buttonWidthMobile, |
| 164 | buttonWidthTablet, |
| 165 | buttonWidth, |
| 166 | outerGap, |
| 167 | blockId, |
| 168 | } = attributes; |
| 169 | let inlineCss = ''; |
| 170 | const propaty = isSave |
| 171 | ? '.vk_button' |
| 172 | : '.vk_buttons .vk_buttons_col .block-editor-block-list__layout .vk_button'; |
| 173 | |
| 174 | // 親ブロックのギャップを反映 |
| 175 | if (outerGap) { |
| 176 | if (buttonWidthMobile) { |
| 177 | inlineCss += `@media (max-width: 575.98px) { |
| 178 | ${propaty}.vk_button-${blockId} { |
| 179 | width: calc(${buttonWidthMobile}% - calc(${outerGap} - calc(${outerGap} / (100 / ${buttonWidthMobile}))) - 1px); |
| 180 | } |
| 181 | }`; |
| 182 | } |
| 183 | if (buttonWidthTablet) { |
| 184 | inlineCss += `@media(min-width: 576px) and (max-width: 991.98px) { |
| 185 | ${propaty}.vk_button-${blockId} { |
| 186 | width: calc(${buttonWidthTablet}% - calc(${outerGap} - calc(${outerGap} / (100 / ${buttonWidthTablet}))) - 1px); |
| 187 | } |
| 188 | }`; |
| 189 | } |
| 190 | if (buttonWidth) { |
| 191 | inlineCss += `@media (min-width: 992px) { |
| 192 | ${propaty}.vk_button-${blockId} { |
| 193 | width: calc(${buttonWidth}% - calc(${outerGap} - calc(${outerGap} / (100 / ${buttonWidth}))) - 1px ); |
| 194 | } |
| 195 | }`; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | return inlineCss; |
| 200 | }; |
| 201 | |
| 202 | const VKButtonInlineEditorCss = createHigherOrderComponent((BlockEdit) => { |
| 203 | return (props) => { |
| 204 | const { attributes } = props; |
| 205 | |
| 206 | if ('vk-blocks/button' === props.name) { |
| 207 | const cssTag = generateInlineCss(attributes); |
| 208 | const cssEditor = generateInlineGapCss(attributes, false); |
| 209 | if (cssTag !== '' || cssEditor !== '') { |
| 210 | return ( |
| 211 | <> |
| 212 | <BlockEdit {...props} /> |
| 213 | <style type="text/css"> |
| 214 | {cssTag} {cssEditor} |
| 215 | </style> |
| 216 | </> |
| 217 | ); |
| 218 | } |
| 219 | return <BlockEdit {...props} />; |
| 220 | } |
| 221 | return <BlockEdit {...props} />; |
| 222 | }; |
| 223 | }, 'VKButtonInlineEditorCss'); |
| 224 | addFilter('editor.BlockEdit', 'vk-blocks/button', VKButtonInlineEditorCss); |
| 225 | |
| 226 | const VKButtonInlineCss = (el, type, attributes) => { |
| 227 | if ('vk-blocks/button' === type.name) { |
| 228 | //現在実行されている deprecated� |
| 229 | の save関数のindexを取得 |
| 230 | const deprecatedFuncIndex = deprecated.findIndex( |
| 231 | (item) => item.save === type.save |
| 232 | ); |
| 233 | |
| 234 | // 最新版 |
| 235 | if (-1 === deprecatedFuncIndex) { |
| 236 | // NOTE: useBlockProps + style要素を挿� |
| 237 | �する場合、useBlockPropsを使った要素が最初(上)にこないと、 |
| 238 | // カスタムクラスを追加する処理が失敗する[ |
| 239 | const cssTag = generateInlineCss(attributes); |
| 240 | const cssEditor = generateInlineGapCss(attributes, true); |
| 241 | if (cssTag !== '' || cssEditor !== '') { |
| 242 | return ( |
| 243 | <> |
| 244 | {el} |
| 245 | <style type="text/css"> |
| 246 | {cssTag} {cssEditor} |
| 247 | </style> |
| 248 | </> |
| 249 | ); |
| 250 | } |
| 251 | return el; |
| 252 | |
| 253 | //後方互換 |
| 254 | } |
| 255 | const DeprecatedHook = deprecatedHooks[deprecatedFuncIndex]; |
| 256 | return <DeprecatedHook el={el} attributes={attributes} />; |
| 257 | } |
| 258 | return el; |
| 259 | }; |
| 260 | addFilter('blocks.getSaveElement', 'vk-blocks/button', VKButtonInlineCss, 11); |
| 261 |