PluginProbe
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor / 2.0.11
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor v2.0.11
2.0.13 2.0.12 2.0.11 2.0.10 2.0.9 trunk 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8
blockenberg / blocks / toggle / index.js

index.js in Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor 2.0.11, at blocks/toggle/index.js

293 lines 15.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 ( function () {
2 var el = wp.element.createElement;
3 var Fragment = wp.element.Fragment;
4 var __ = wp.i18n.__;
5 var useState = wp.element.useState;
6 var registerBlockType = wp.blocks.registerBlockType;
7 var InspectorControls = wp.blockEditor.InspectorControls;
8 var useBlockProps = wp.blockEditor.useBlockProps;
9 var RichText = wp.blockEditor.RichText;
10 var PanelBody = wp.components.PanelBody;
11 var PanelColorSettings = wp.blockEditor.PanelColorSettings;
12 var ToggleControl = wp.components.ToggleControl;
13 var RangeControl = wp.components.RangeControl;
14 var SelectControl = wp.components.SelectControl;
15 var TextControl = wp.components.TextControl;
16
17 var _tc, _tvf;
18 Object.defineProperty(window, '_bkbgTypoCtrlCache', { get: function () { if (!_tc) { _tc = window.bkbgTypographyControl; } return _tc; } });
19 Object.defineProperty(window, '_bkbgTypoVarsCache', { get: function () { if (!_tvf) { _tvf = window.bkbgTypoCssVars; } return _tvf; } });
20 function getTypoControl(props, attrName, label) { return window._bkbgTypoCtrlCache(props, attrName, label); }
21
22 var icons = {
23 chevron: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="6 9 12 15 18 9"/></svg>',
24 plus: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>',
25 arrow: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="12" y1="5" x2="12" y2="19"/><polyline points="19 12 12 19 5 12"/></svg>'
26 };
27
28 function getIconEl(type) {
29 if (type === 'none') return null;
30 return el('span', {
31 className: 'bkbg-tgl-icon',
32 'aria-hidden': 'true',
33 dangerouslySetInnerHTML: { __html: icons[type] || icons.chevron }
34 });
35 }
36
37 var iconTypeOptions = [
38 { label: __('Chevron', 'blockenberg'), value: 'chevron' },
39 { label: __('Plus / Minus', 'blockenberg'), value: 'plus' },
40 { label: __('Arrow', 'blockenberg'), value: 'arrow' },
41 { label: __('None', 'blockenberg'), value: 'none' }
42 ];
43
44 var iconPositionOptions = [
45 { label: __('Right', 'blockenberg'), value: 'right' },
46 { label: __('Left', 'blockenberg'), value: 'left' }
47 ];
48
49 var layoutOptions = [
50 { label: __('Default', 'blockenberg'), value: 'default' },
51 { label: __('Boxed', 'blockenberg'), value: 'boxed' },
52 { label: __('Minimal', 'blockenberg'), value: 'minimal' },
53 { label: __('Bordered Left', 'blockenberg'), value: 'bordered' }
54 ];
55
56 var titleTagOptions = [
57 { label: 'H3', value: 'h3' },
58 { label: 'H4', value: 'h4' },
59 { label: 'H5', value: 'h5' },
60 { label: 'H6', value: 'h6' },
61 { label: 'p', value: 'p' },
62 { label: 'div', value: 'div' }
63 ];
64
65 registerBlockType('blockenberg/toggle', {
66 edit: function (props) {
67 var a = props.attributes;
68 var setAttributes = props.setAttributes;
69 var isOpen = useState(a.defaultOpen)[0];
70
71 var wrapStyle = {
72 '--bkbg-tgl-radius': a.borderRadius + 'px',
73 '--bkbg-tgl-padding': a.padding + 'px',
74 '--bkbg-tgl-speed': a.animationSpeed + 'ms',
75 '--bkbg-tgl-title-color': a.titleColor,
76 '--bkbg-tgl-title-bg': a.titleBg,
77 '--bkbg-tgl-title-hover-bg': a.titleHoverBg,
78 '--bkbg-tgl-active-title-bg': a.activeTitleBg,
79 '--bkbg-tgl-active-title-color': a.activeTitleColor,
80 '--bkbg-tgl-content-color': a.contentColor,
81 '--bkbg-tgl-content-bg': a.contentBg,
82 '--bkbg-tgl-icon-color': a.iconColor,
83 '--bkbg-tgl-icon-active-color': a.iconActiveColor,
84 '--bkbg-tgl-border-color': a.borderColor,
85 '--bkbg-tgl-divider-color': a.dividerColor
86 };
87 Object.assign(wrapStyle, window._bkbgTypoVarsCache(a.titleTypo, '--bktgl-tt-'), window._bkbgTypoVarsCache(a.contentTypo, '--bktgl-ct-'));
88
89 var blockProps = useBlockProps({
90 className: 'bkbg-tgl-wrap bkbg-tgl-editor' + (isOpen ? ' is-open' : ''),
91 style: wrapStyle,
92 'data-layout': a.layout,
93 'data-icon': a.iconType,
94 'data-icon-pos': a.iconPosition
95 });
96
97 var inspector = el(InspectorControls, {},
98
99 el(PanelBody, { title: __('Toggle Settings', 'blockenberg'), initialOpen: true },
100 el(ToggleControl, {
101 label: __('Open by default', 'blockenberg'),
102 help: __('Start the toggle in expanded state on page load.', 'blockenberg'),
103 checked: a.defaultOpen,
104 __nextHasNoMarginBottom: true,
105 onChange: function (v) { setAttributes({ defaultOpen: v }); }
106 }),
107 el(SelectControl, {
108 label: __('Title Tag', 'blockenberg'),
109 value: a.titleTag,
110 options: titleTagOptions,
111 onChange: function (v) { setAttributes({ titleTag: v }); }
112 })
113 ),
114
115 el(PanelBody, { title: __('Style', 'blockenberg'), initialOpen: false },
116 el(SelectControl, {
117 label: __('Layout', 'blockenberg'),
118 value: a.layout,
119 options: layoutOptions,
120 onChange: function (v) { setAttributes({ layout: v }); }
121 }),
122 el(SelectControl, {
123 label: __('Icon Type', 'blockenberg'),
124 value: a.iconType,
125 options: iconTypeOptions,
126 onChange: function (v) { setAttributes({ iconType: v }); }
127 }),
128 el(SelectControl, {
129 label: __('Icon Position', 'blockenberg'),
130 value: a.iconPosition,
131 options: iconPositionOptions,
132 onChange: function (v) { setAttributes({ iconPosition: v }); }
133 }),
134 el(RangeControl, {
135 label: __('Border Radius (px)', 'blockenberg'),
136 value: a.borderRadius,
137 min: 0, max: 24,
138 onChange: function (v) { setAttributes({ borderRadius: v }); }
139 }),
140 el(RangeControl, {
141 label: __('Padding (px)', 'blockenberg'),
142 value: a.padding,
143 min: 8, max: 48,
144 onChange: function (v) { setAttributes({ padding: v }); }
145 }),
146 el(RangeControl, {
147 label: __('Animation Speed (ms)', 'blockenberg'),
148 value: a.animationSpeed,
149 min: 0, max: 800, step: 50,
150 onChange: function (v) { setAttributes({ animationSpeed: v }); }
151 })
152 ),
153
154 el(PanelBody, { title: __('Typography', 'blockenberg'), initialOpen: false },
155 getTypoControl(props, 'titleTypo', __('Title', 'blockenberg')),
156 getTypoControl(props, 'contentTypo', __('Content', 'blockenberg'))
157 ),
158
159 el(PanelBody, { title: __('Colors', 'blockenberg'), initialOpen: false },
160 el(PanelColorSettings, {
161 title: __('Title Colors', 'blockenberg'),
162 initialOpen: false,
163 colorSettings: [
164 { value: a.titleColor, onChange: function (c) { setAttributes({ titleColor: c || '#111827' }); }, label: __('Text', 'blockenberg') },
165 { value: a.titleBg, onChange: function (c) { setAttributes({ titleBg: c || '#ffffff' }); }, label: __('Background', 'blockenberg') },
166 { value: a.titleHoverBg, onChange: function (c) { setAttributes({ titleHoverBg: c || '#f9fafb' }); }, label: __('Background (Hover)', 'blockenberg') },
167 { value: a.activeTitleColor, onChange: function (c) { setAttributes({ activeTitleColor: c || '#1d4ed8' }); }, label: __('Text (Open)', 'blockenberg') },
168 { value: a.activeTitleBg, onChange: function (c) { setAttributes({ activeTitleBg: c || '#eff6ff' }); }, label: __('Background (Open)', 'blockenberg') }
169 ]
170 }),
171 el(PanelColorSettings, {
172 title: __('Content Colors', 'blockenberg'),
173 initialOpen: false,
174 colorSettings: [
175 { value: a.contentColor, onChange: function (c) { setAttributes({ contentColor: c || '#374151' }); }, label: __('Text', 'blockenberg') },
176 { value: a.contentBg, onChange: function (c) { setAttributes({ contentBg: c || '#ffffff' }); }, label: __('Background', 'blockenberg') }
177 ]
178 }),
179 el(PanelColorSettings, {
180 title: __('Icon & Border Colors', 'blockenberg'),
181 initialOpen: false,
182 colorSettings: [
183 { value: a.iconColor, onChange: function (c) { setAttributes({ iconColor: c || '#6b7280' }); }, label: __('Icon', 'blockenberg') },
184 { value: a.iconActiveColor, onChange: function (c) { setAttributes({ iconActiveColor: c || '#2563eb' }); }, label: __('Icon (Open)', 'blockenberg') },
185 { value: a.borderColor, onChange: function (c) { setAttributes({ borderColor: c || '#e5e7eb' }); }, label: __('Border', 'blockenberg') },
186 { value: a.dividerColor, onChange: function (c) { setAttributes({ dividerColor: c || '#f3f4f6' }); }, label: __('Divider', 'blockenberg') }
187 ]
188 })
189 )
190 );
191
192 return el(Fragment, {},
193 inspector,
194 el('div', blockProps,
195 el('button', {
196 className: 'bkbg-tgl-trigger',
197 'aria-expanded': isOpen ? 'true' : 'false'
198 },
199 a.iconPosition === 'left' && getIconEl(a.iconType),
200 el(a.titleTag, { className: 'bkbg-tgl-title' },
201 el(TextControl, {
202 value: a.title,
203 onChange: function (v) { setAttributes({ title: v }); },
204 placeholder: __('Toggle title...', 'blockenberg'),
205 hideLabelFromVision: true,
206 label: __('Toggle Title', 'blockenberg')
207 })
208 ),
209 a.iconPosition === 'right' && getIconEl(a.iconType)
210 ),
211 el('div', { className: 'bkbg-tgl-content', 'aria-hidden': isOpen ? 'false' : 'true' },
212 el('div', { className: 'bkbg-tgl-content-inner' },
213 el(RichText, {
214 tagName: 'div',
215 className: 'bkbg-tgl-body',
216 value: a.content,
217 onChange: function (v) { setAttributes({ content: v }); },
218 placeholder: __('Add toggle content here...', 'blockenberg')
219 })
220 )
221 )
222 )
223 );
224 },
225
226 save: function (props) {
227 var a = props.attributes;
228 var RichTextContent = wp.blockEditor.RichText.Content;
229
230 var wrapParts = [
231 '--bkbg-tgl-radius:' + a.borderRadius + 'px',
232 '--bkbg-tgl-padding:' + a.padding + 'px',
233 '--bkbg-tgl-speed:' + a.animationSpeed + 'ms',
234 '--bkbg-tgl-title-color:' + a.titleColor,
235 '--bkbg-tgl-title-bg:' + a.titleBg,
236 '--bkbg-tgl-title-hover-bg:' + a.titleHoverBg,
237 '--bkbg-tgl-active-title-bg:' + a.activeTitleBg,
238 '--bkbg-tgl-active-title-color:' + a.activeTitleColor,
239 '--bkbg-tgl-content-color:' + a.contentColor,
240 '--bkbg-tgl-content-bg:' + a.contentBg,
241 '--bkbg-tgl-icon-color:' + a.iconColor,
242 '--bkbg-tgl-icon-active-color:' + a.iconActiveColor,
243 '--bkbg-tgl-border-color:' + a.borderColor,
244 '--bkbg-tgl-divider-color:' + a.dividerColor
245 ];
246 var tv = Object.assign({}, window._bkbgTypoVarsCache(a.titleTypo, '--bktgl-tt-'), window._bkbgTypoVarsCache(a.contentTypo, '--bktgl-ct-'));
247 Object.keys(tv).forEach(function (k) { wrapParts.push(k + ':' + tv[k]); });
248 var wrapStyle = wrapParts.join(';');
249
250 var iconSvgs = {
251 chevron: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="6 9 12 15 18 9"/></svg>',
252 plus: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>',
253 arrow: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="12" y1="5" x2="12" y2="19"/><polyline points="19 12 12 19 5 12"/></svg>'
254 };
255
256 var iconEl = a.iconType !== 'none' ? el('span', {
257 className: 'bkbg-tgl-icon',
258 'aria-hidden': 'true',
259 dangerouslySetInnerHTML: { __html: iconSvgs[a.iconType] || iconSvgs.chevron }
260 }) : null;
261
262 var blockProps = wp.blockEditor.useBlockProps.save({
263 className: 'bkbg-tgl-wrap' + (a.defaultOpen ? ' is-open' : ''),
264 style: wrapStyle,
265 'data-layout': a.layout,
266 'data-icon': a.iconType,
267 'data-icon-pos': a.iconPosition,
268 'data-default-open': a.defaultOpen ? '1' : '0',
269 'data-speed': a.animationSpeed
270 });
271
272 return el('div', blockProps,
273 el('button', {
274 className: 'bkbg-tgl-trigger',
275 'aria-expanded': a.defaultOpen ? 'true' : 'false'
276 },
277 a.iconPosition === 'left' && iconEl,
278 el(a.titleTag, { className: 'bkbg-tgl-title' }, a.title),
279 a.iconPosition === 'right' && iconEl
280 ),
281 el('div', {
282 className: 'bkbg-tgl-content',
283 'aria-hidden': a.defaultOpen ? 'false' : 'true'
284 },
285 el('div', { className: 'bkbg-tgl-content-inner' },
286 el(RichTextContent, { tagName: 'div', className: 'bkbg-tgl-body', value: a.content })
287 )
288 )
289 );
290 }
291 });
292 }() );
293