PluginProbe
Block Animations, Motion & Scroll Effects – Ghost Kit / 3.3.3
Block Animations, Motion & Scroll Effects – Ghost Kit v3.3.3
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.3.3, at gutenberg/blocks/button-single/edit.js

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