PluginProbe ʕ •ᴥ•ʔ
VK Blocks / trunk
VK Blocks vtrunk
1.123.0 1.122.0 1.121.0 1.120.0 1.119.2 1.119.1 1.119.0 1.118.7 1.82.0.0 1.82.0.1 1.83.0.0 1.83.0.1 1.84.0.0 1.84.0.1 1.85.0.2 1.85.0.3 1.85.1.0 1.85.1.1 1.86.0.0 1.86.0.1 1.86.1.0 1.87.0.0 1.87.0.1 1.88.0.1 1.88.0.2 1.89.0.0 1.9.2 1.90.0.0 1.90.0.1 1.90.1.1 1.91.0.0 1.91.1.1 1.92.0.0 1.92.0.1 1.92.1.0 1.92.1.1 1.93.0.0 trunk 1.93.0.1 0.1.1 1.93.1.0 0.10.3 1.93.1.1 0.11.0 1.94.0.0 0.12.6 1.94.0.1 0.15.1 1.94.1.0 0.16.2 1.94.2.1 0.17.6 1.94.2.2 0.2.2 1.95.0.3 0.22.4 1.95.0.4 0.26.7 1.96.2.0 0.3.2 1.96.2.1 0.31.0 1.97.0.1 0.35.5 1.97.0.2 0.37.5 1.99.0.0 0.38.8 1.99.0.1 0.39.4 0.4.7 0.41.0 0.42.0 0.44.13 0.45.2 0.46.1 0.47.0 0.48.0 0.49.8 0.5.2 0.50.3 0.51.0 0.52.2 0.53.2 0.54.0 0.55.0 0.56.2 0.56.3 0.57.6 0.58.10 0.59.0 0.6.0 0.60.1 0.61.2 0.7.1 0.8.2 1.0.16 1.10.0 1.100.0.0 1.100.0.1 1.102.0.0 1.102.0.1 1.103.0.0 1.104.0.0 1.104.0.1 1.105.1.0 1.105.1.1 1.106.0.0 1.106.0.1 1.107.0.0 1.107.0.1 1.107.0.2 1.108.0.0 1.108.0.1 1.109.0.0 1.109.0.1 1.11.4 1.110.0.0 1.110.0.1 1.111.0.0 1.111.0.1 1.111.0.2 1.112.0.0 1.112.0.1 1.113.0.0 1.113.0.1 1.114.0.0 1.114.0.1 1.114.2.0 1.114.2.1 1.115.0.0 1.115.0.1 1.115.1.0 1.115.1.1 1.115.2.0 1.115.2.1 1.116.0.0 1.116.0.1 1.116.1.0 1.116.1.1 1.116.2.0 1.117.0.0 1.117.0.1 1.117.1.0 1.118.2 1.118.5 1.12.0 1.13.2 1.14.1 1.15.1 1.16.11 1.17.0 1.18.6 1.19.0 1.19.1 1.2.3 1.20.7 1.21.0 1.22.4 1.23.0 1.24.5 1.25.1 1.26.2 1.27.7.2 1.28.0.1 1.29.2.0 1.3.9 1.30.0.1 1.31.0.1 1.32.0.2 1.33.2.1 1.36.1.5 1.37.0.0 1.39.2.1 1.4.6 1.40.0.1 1.40.1.0 1.40.1.1 1.41.0.0 1.41.0.1 1.41.2.2 1.41.2.3 1.43.0.0 1.43.0.1 1.43.0.2 1.44.0.0 1.44.0.1 1.45.0.0 1.45.0.1 1.46.0.0 1.46.0.1 1.47.0.0 1.47.0.1 1.47.1.0 1.48.0.0 1.48.0.1 1.48.0.2 1.48.1.0 1.48.1.1 1.5.0 1.50.0.0 1.50.0.1 1.51.0.0 1.51.0.1 1.52.0.0 1.52.0.1 1.53.0.0 1.53.0.1 1.54.0.0 1.54.0.1 1.55.0.0 1.55.0.1 1.56.0.0 1.56.0.1 1.57.0.0 1.57.0.2 1.57.0.3 1.57.0.4 1.57.0.5 1.57.1.0 1.57.1.1 1.57.1.2 1.58.0.0 1.58.0.1 1.59.0.0 1.59.0.1 1.6.0 1.60.0.0 1.60.0.1 1.63.0.0 1.63.0.1 1.64.0.0 1.64.0.1 1.64.1.0 1.64.1.2 1.67.0.0 1.67.0.1 1.68.0.0 1.68.0.1 1.69.0.0 1.69.0.1 1.69.1.1 1.69.1.2 1.7.1 1.70.0.0 1.70.0.1 1.71.0.0 1.71.0.1 1.72.0.0 1.72.1.0 1.72.1.1 1.73.0.0 1.73.0.1 1.74.0.0 1.74.0.1 1.75.0.0 1.75.1.0 1.75.1.1 1.76.0.0 1.76.0.1 1.76.1.0 1.76.1.1 1.76.2.0 1.76.2.1 1.77.0.0 1.77.0.1 1.78.0.0 1.78.0.1 1.79.0.0 1.79.0.1 1.79.0.2 1.79.0.3 1.79.1.0 1.79.1.1 1.8.2 1.80.1.0 1.80.1.1 1.80.1.2 1.81.0.1 1.81.0.2
vk-blocks / src / blocks / button / index.js
vk-blocks / src / blocks / button Last commit date
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