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 / edit.js

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

155 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import {
2 BlockAlignmentToolbar,
3 BlockControls,
4 InspectorControls,
5 useBlockProps,
6 useInnerBlocksProps,
7 } from '@wordpress/block-editor';
8 import { createBlock } from '@wordpress/blocks';
9 import { BaseControl, Button, PanelBody, Tooltip } from '@wordpress/components';
10 import { useDispatch, useSelect } from '@wordpress/data';
11 import { useEffect } from '@wordpress/element';
12 import { applyFilters } from '@wordpress/hooks';
13 import { __ } from '@wordpress/i18n';
14 import classnames from 'classnames/dedupe';
15
16 import GapSettings from '../../components/gap-settings';
17
18 const buttonBlockName = 'ghostkit/button-single';
19
20 /**
21 * Block Edit Class.
22 *
23 * @param props
24 */
25 export default function BlockEdit(props) {
26 const { attributes, setAttributes, clientId } = props;
27
28 let { className = '' } = props;
29
30 const { align, gap, gapCustom, gapVerticalCustom, count } = attributes;
31
32 const { block, isSelectedBlockInRoot } = useSelect((select) => {
33 const { getBlock, isBlockSelected, hasSelectedInnerBlock } =
34 select('core/block-editor');
35
36 return {
37 block: getBlock(clientId),
38 isSelectedBlockInRoot:
39 isBlockSelected(clientId) ||
40 hasSelectedInnerBlock(clientId, true),
41 };
42 // eslint-disable-next-line react-hooks/exhaustive-deps
43 }, []);
44
45 const { insertBlock } = useDispatch('core/block-editor');
46
47 useEffect(() => {
48 if (block && block.innerBlocks && count !== block.innerBlocks.length) {
49 setAttributes({
50 count: block.innerBlocks.length,
51 });
52 }
53 }, [block, block?.innerBlocks?.length, count, setAttributes]);
54
55 /**
56 * Insert a single button.
57 */
58 function insertButtonSingle() {
59 insertBlock(createBlock(buttonBlockName), undefined, clientId);
60 }
61
62 className = classnames(
63 'ghostkit-button-wrapper',
64 gap ? `ghostkit-button-wrapper-gap-${gap}` : false,
65 align && align !== 'none'
66 ? `ghostkit-button-wrapper-align-${align}`
67 : false,
68 className
69 );
70
71 className = applyFilters('ghostkit.editor.className', className, props);
72
73 const blockProps = useBlockProps({
74 className,
75 });
76
77 const { children, ...innerBlocksProps } = useInnerBlocksProps(
78 {
79 className: 'ghostkit-button-wrapper-inner',
80 },
81 {
82 allowedBlocks: [buttonBlockName],
83 template: [[buttonBlockName]],
84 orientation: 'horizontal',
85 directInsert: true,
86 templateInsertUpdatesSelection: true,
87 renderAppender: isSelectedBlockInRoot
88 ? () => (
89 <Tooltip text={__('Add Button', 'ghostkit')}>
90 <Button
91 className="block-list-appender__toggle block-editor-button-block-appender"
92 icon={
93 <svg
94 xmlns="http://www.w3.org/2000/svg"
95 viewBox="0 0 24 24"
96 width="24"
97 height="24"
98 aria-hidden="true"
99 focusable="false"
100 >
101 <path d="M18 11.2h-5.2V6h-1.6v5.2H6v1.6h5.2V18h1.6v-5.2H18z" />
102 </svg>
103 }
104 onClick={() => {
105 insertButtonSingle();
106 }}
107 />
108 </Tooltip>
109 )
110 : undefined,
111 }
112 );
113
114 return (
115 <div {...blockProps}>
116 <BlockControls>
117 <BlockAlignmentToolbar
118 value={align}
119 onChange={(value) => setAttributes({ align: value })}
120 controls={['left', 'center', 'right']}
121 />
122 </BlockControls>
123 <InspectorControls>
124 <PanelBody>
125 <GapSettings
126 gap={gap}
127 gapCustom={gapCustom}
128 gapVerticalCustom={gapVerticalCustom}
129 onChange={(data) => {
130 setAttributes(data);
131 }}
132 allowVerticalGap
133 />
134 <BaseControl
135 id={__('Align', 'ghostkit')}
136 label={__('Align', 'ghostkit')}
137 >
138 <div>
139 <BlockAlignmentToolbar
140 value={align}
141 onChange={(value) =>
142 setAttributes({ align: value })
143 }
144 controls={['left', 'center', 'right']}
145 isCollapsed={false}
146 />
147 </div>
148 </BaseControl>
149 </PanelBody>
150 </InspectorControls>
151 <div {...innerBlocksProps}>{children}</div>
152 </div>
153 );
154 }
155