PluginProbe
Block Animations, Motion & Scroll Effects – Ghost Kit / 3.7.2
Block Animations, Motion & Scroll Effects – Ghost Kit v3.7.2
3.7.2 3.7.1 3.7.0 3.6.1 trunk 1.6.3 2.25.0 3.3.0 3.3.1 3.3.2 3.3.3 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6 3.5.0 3.5.1 3.6.0
ghostkit / gutenberg / blocks / button-single / edit.js

edit.js in Block Animations, Motion & Scroll Effects – Ghost Kit 3.7.2, at gutenberg/blocks/button-single/edit.js

414 lines 9.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import {
2 InspectorControls,
3 RichText,
4 useBlockProps,
5 } from '@wordpress/block-editor';
6 import {
7 PanelBody,
8 SelectControl,
9 TabPanel,
10 ToggleControl,
11 } from '@wordpress/components';
12 import { useEffect, useState } from '@wordpress/element';
13 import { applyFilters } from '@wordpress/hooks';
14 import { __ } from '@wordpress/i18n';
15 import classnames from 'classnames/dedupe';
16
17 import ApplyFilters from '../../components/apply-filters';
18 import ColorIndicator from '../../components/color-indicator';
19 import ColorPicker from '../../components/color-picker';
20 import IconPicker from '../../components/icon-picker';
21 import RangeControl from '../../components/range-control';
22 import ToggleGroup from '../../components/toggle-group';
23 import URLPicker from '../../components/url-picker';
24
25 const SIZES = [
26 {
27 label: 'XS',
28 value: 'xs',
29 },
30 {
31 label: 'S',
32 value: 'sm',
33 },
34 {
35 label: 'M',
36 value: 'md',
37 },
38 {
39 label: 'L',
40 value: 'lg',
41 },
42 {
43 label: 'XL',
44 value: 'xl',
45 },
46 ];
47
48 /**
49 * Block Edit Class.
50 *
51 * @param props
52 */
53 export default function BlockEdit(props) {
54 const { attributes, setAttributes, isSelected } = props;
55
56 const {
57 tagName,
58 text,
59 icon,
60 iconPosition,
61 hideText,
62 url,
63 ariaLabel,
64 target,
65 rel,
66 size,
67 color,
68 textColor,
69 borderRadius,
70 borderWeight,
71 borderColor,
72 focusOutlineWeight,
73 focusOutlineColor,
74 hoverColor,
75 hoverTextColor,
76 hoverBorderColor,
77 } = attributes;
78
79 let { className = '' } = attributes;
80
81 const [selectedColorState, setSelectedColorState] = useState('normal');
82
83 // Reset selected color state when block is not selected.
84 useEffect(() => {
85 if (!isSelected) {
86 setSelectedColorState('normal');
87 }
88 }, [isSelected]);
89
90 let isNormalState = false;
91 let isHoveredState = false;
92 let isFocusedState = false;
93
94 if (isSelected) {
95 isNormalState = true;
96
97 if (selectedColorState === 'hover') {
98 isNormalState = false;
99 isHoveredState = true;
100 } else if (selectedColorState === 'focus') {
101 isNormalState = false;
102 isFocusedState = true;
103 }
104 }
105
106 className = classnames(
107 'ghostkit-button',
108 size && `ghostkit-button-${size}`,
109 hideText && 'ghostkit-button-icon-only',
110 isNormalState && 'ghostkit-button-is-normal-state',
111 isHoveredState && 'ghostkit-button-is-hover-state',
112 isFocusedState && 'ghostkit-button-is-focus-state',
113 className
114 );
115
116 // focus outline
117 if (focusOutlineWeight && focusOutlineColor) {
118 className = classnames(className, 'ghostkit-button-with-outline');
119 }
120
121 className = applyFilters('ghostkit.editor.className', className, props);
122
123 const colorsTabs = [
124 {
125 name: 'normal',
126 title: __('Normal', 'ghostkit'),
127 className: 'ghostkit-control-tabs-tab',
128 },
129 {
130 name: 'hover',
131 title: __('Hover', 'ghostkit'),
132 className: 'ghostkit-control-tabs-tab',
133 },
134 ];
135
136 if (focusOutlineWeight && focusOutlineColor) {
137 colorsTabs.push({
138 name: 'focus',
139 title: __('Focus', 'ghostkit'),
140 className: 'ghostkit-control-tabs-tab',
141 });
142 }
143
144 const blockProps = useBlockProps({
145 className,
146 });
147
148 return (
149 <div {...blockProps}>
150 <InspectorControls>
151 <PanelBody>
152 <div className="blocks-size__main">
153 <ToggleGroup
154 value={size}
155 options={SIZES}
156 onChange={(value) => {
157 setAttributes({ size: value });
158 }}
159 />
160 </div>
161 </PanelBody>
162 <PanelBody>
163 <RangeControl
164 label={__('Corner Radius', 'ghostkit')}
165 value={borderRadius}
166 min={0}
167 max={50}
168 onChange={(value) =>
169 setAttributes({ borderRadius: value })
170 }
171 allowCustomMax
172 />
173 <RangeControl
174 label={__('Border Size', 'ghostkit')}
175 value={borderWeight}
176 min={0}
177 max={6}
178 onChange={(value) =>
179 setAttributes({ borderWeight: value })
180 }
181 allowCustomMax
182 />
183 <RangeControl
184 label={__('Focus Outline Size', 'ghostkit')}
185 value={focusOutlineWeight}
186 min={0}
187 max={6}
188 onChange={(value) =>
189 setAttributes({ focusOutlineWeight: value })
190 }
191 allowCustomMax
192 />
193 </PanelBody>
194 <PanelBody>
195 <IconPicker
196 label={__('Icon', 'ghostkit')}
197 value={icon}
198 onChange={(value) => setAttributes({ icon: value })}
199 insideInspector
200 />
201 {icon ? (
202 <ToggleControl
203 label={__('Show Icon Only', 'ghostkit')}
204 checked={!!hideText}
205 onChange={(val) => setAttributes({ hideText: val })}
206 />
207 ) : null}
208 {icon && !hideText ? (
209 <SelectControl
210 label={__('Icon Position', 'ghostkit')}
211 value={iconPosition}
212 options={[
213 {
214 value: 'left',
215 label: __('Left', 'ghostkit'),
216 },
217 {
218 value: 'right',
219 label: __('Right', 'ghostkit'),
220 },
221 ]}
222 onChange={(value) =>
223 setAttributes({ iconPosition: value })
224 }
225 />
226 ) : null}
227 </PanelBody>
228 <PanelBody
229 title={
230 <>
231 {__('Colors', 'ghostkit')}
232 <ColorIndicator colorValue={color} />
233 <ColorIndicator colorValue={textColor} />
234 {borderWeight ? (
235 <ColorIndicator colorValue={borderColor} />
236 ) : null}
237 {focusOutlineWeight && focusOutlineColor ? (
238 <ColorIndicator
239 colorValue={focusOutlineColor}
240 />
241 ) : null}
242 </>
243 }
244 initialOpen={false}
245 >
246 <TabPanel
247 className="ghostkit-control-tabs ghostkit-control-tabs-wide"
248 tabs={colorsTabs}
249 onSelect={(state) => {
250 setSelectedColorState(state);
251 }}
252 >
253 {(tabData) => {
254 const isHover = tabData.name === 'hover';
255
256 // focus tab
257 if (tabData.name === 'focus') {
258 return (
259 <ApplyFilters
260 name="ghostkit.editor.controls"
261 attribute="focusOutlineColor"
262 props={props}
263 >
264 <ColorPicker
265 label={__('Outline', 'ghostkit')}
266 value={focusOutlineColor}
267 onChange={(val) =>
268 setAttributes({
269 focusOutlineColor: val,
270 })
271 }
272 alpha
273 />
274 </ApplyFilters>
275 );
276 }
277
278 return (
279 <>
280 <ApplyFilters
281 name="ghostkit.editor.controls"
282 attribute={
283 isHover ? 'hoverColor' : 'color'
284 }
285 props={props}
286 >
287 <ColorPicker
288 label={__('Background', 'ghostkit')}
289 value={isHover ? hoverColor : color}
290 onChange={(val) =>
291 setAttributes(
292 isHover
293 ? { hoverColor: val }
294 : { color: val }
295 )
296 }
297 alpha
298 gradient
299 />
300 </ApplyFilters>
301 <ApplyFilters
302 name="ghostkit.editor.controls"
303 attribute={
304 isHover
305 ? 'hoverTextColor'
306 : 'textColor'
307 }
308 props={props}
309 >
310 <ColorPicker
311 label={__('Text', 'ghostkit')}
312 value={
313 isHover
314 ? hoverTextColor
315 : textColor
316 }
317 onChange={(val) =>
318 setAttributes(
319 isHover
320 ? {
321 hoverTextColor:
322 val,
323 }
324 : { textColor: val }
325 )
326 }
327 alpha
328 />
329 </ApplyFilters>
330 {borderWeight ? (
331 <ApplyFilters
332 name="ghostkit.editor.controls"
333 attribute={
334 isHover
335 ? 'hoverBorderColor'
336 : 'borderColor'
337 }
338 props={props}
339 >
340 <ColorPicker
341 label={__('Border', 'ghostkit')}
342 value={
343 isHover
344 ? hoverBorderColor
345 : borderColor
346 }
347 onChange={(val) =>
348 setAttributes(
349 isHover
350 ? {
351 hoverBorderColor:
352 val,
353 }
354 : {
355 borderColor:
356 val,
357 }
358 )
359 }
360 alpha
361 />
362 </ApplyFilters>
363 ) : null}
364 </>
365 );
366 }}
367 </TabPanel>
368 </PanelBody>
369 </InspectorControls>
370 {!tagName || tagName === 'a' ? (
371 <URLPicker
372 url={url}
373 rel={rel}
374 ariaLabel={ariaLabel}
375 target={target}
376 onChange={(data) => {
377 setAttributes(data);
378 }}
379 isSelected={isSelected}
380 toolbarSettings
381 inspectorSettings
382 />
383 ) : null}
384 {icon ? (
385 <div
386 className={`ghostkit-button-icon ghostkit-button-icon-${
387 iconPosition === 'right' ? 'right' : 'left'
388 }`}
389 >
390 <IconPicker.Dropdown
391 onChange={(value) => setAttributes({ icon: value })}
392 value={icon}
393 renderToggle={({ onToggle }) => (
394 <IconPicker.Preview
395 onClick={onToggle}
396 name={icon}
397 />
398 )}
399 />
400 </div>
401 ) : null}
402 {!hideText ? (
403 <RichText
404 placeholder={__('Write text…', 'ghostkit')}
405 value={text}
406 onChange={(value) => setAttributes({ text: value })}
407 isSelected={isSelected}
408 withoutInteractiveFormatting
409 />
410 ) : null}
411 </div>
412 );
413 }
414