PluginProbe
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor / 2.0.7
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor v2.0.7
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 / category-grid / index.js

index.js in Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor 2.0.7, at blocks/category-grid/index.js

424 lines 25.6 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 useState = wp.element.useState;
5 var useEffect = wp.element.useEffect;
6 var __ = wp.i18n.__;
7 var registerBlockType = wp.blocks.registerBlockType;
8 var InspectorControls = wp.blockEditor.InspectorControls;
9 var useBlockProps = wp.blockEditor.useBlockProps;
10 var PanelColorSettings = wp.blockEditor.PanelColorSettings;
11 var PanelBody = wp.components.PanelBody;
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 var Button = wp.components.Button;
17 var ColorPicker = wp.components.ColorPicker;
18 var Popover = wp.components.Popover;
19 var Spinner = wp.components.Spinner;
20
21 function getTypographyControl() { return (window.bkbgTypographyControl || function () { return null; }); }
22 function getTypoCssVars() { return (window.bkbgTypoCssVars || function () { return {}; }); }
23 function _tv(typo, prefix) { var fn = getTypoCssVars(); return fn(typo || {}, prefix); }
24 var IP = function () { return window.bkbgIconPicker; };
25
26 /* ── demo categories for editor preview ──────────────────────────────────── */
27 var DEMO_CATS = [
28 { id: 1, name: 'Technology', count: 42, description: 'The latest in tech news and reviews.', link: '#', emoji: '💻' },
29 { id: 2, name: 'Design', count: 28, description: 'UI/UX, branding, and visual art.', link: '#', emoji: '🎨' },
30 { id: 3, name: 'Business', count: 35, description: 'Strategy, finance, and growth.', link: '#', emoji: '📊' },
31 { id: 4, name: 'Travel', count: 19, description: 'Destinations and travel guides.', link: '#', emoji: '✈️' },
32 { id: 5, name: 'Food', count: 24, description: 'Recipes, restaurants, and culture.', link: '#', emoji: '🍕' },
33 { id: 6, name: 'Science', count: 16, description: 'Discoveries and research.', link: '#', emoji: '🔬' },
34 ];
35
36 /* ── color picker helper ─────────────────────────────────────────────────── */
37 function getCatColor(index, a) {
38 if (a.colorMode === 'multi') {
39 var cols = a.multiColors.split(',').map(function (c) { return c.trim(); }).filter(Boolean);
40 return cols[index % cols.length] || a.accentColor;
41 }
42 return a.accentColor;
43 }
44
45 /* ── single category card ────────────────────────────────────────────────── */
46 function CatCard(props) {
47 var cat = props.cat;
48 var index = props.index;
49 var a = props.attrs;
50 var isEdit = props.isEdit;
51
52 var accent = getCatColor(index, a);
53 var useChar = !a.iconType || a.iconType === 'custom-char';
54 var emoji = useChar ? (cat.emoji || a.defaultIcon || '📁') : null;
55
56 var cardStyle = {
57 background: a.cardBg,
58 borderRadius: a.cardRadius + 'px',
59 padding: a.cardPadding + 'px',
60 boxShadow: a.cardShadow ? '0 2px 16px rgba(0,0,0,0.08)' : 'none',
61 border: a.cardBorder ? '1px solid ' + a.borderColor : 'none',
62 textAlign: a.textAlign,
63 display: 'flex',
64 flexDirection: a.iconPosition === 'left' ? 'row' : 'column',
65 alignItems: a.iconPosition === 'left' ? 'flex-start' : (a.textAlign === 'center' ? 'center' : 'flex-start'),
66 gap: a.iconPosition === 'left' ? '12px' : '8px',
67 textDecoration: 'none',
68 color: 'inherit',
69 transition: 'all 0.25s ease',
70 cursor: 'pointer',
71 position: 'relative',
72 overflow: 'hidden',
73 };
74
75 /* top accent bar */
76 var showBar = a.cardStyle === 'card-accent';
77
78 return el('a', {
79 href: isEdit ? undefined : (cat.link || '#'),
80 className: 'bkcg-card',
81 target: a.linkTarget,
82 rel: a.linkTarget === '_blank' ? 'noopener noreferrer' : undefined,
83 style: cardStyle,
84 'data-bg-hover': a.cardBgHover,
85 'data-bg': a.cardBg,
86 },
87 showBar && el('div', { style: { position: 'absolute', top: 0, left: 0, right: 0, height: '3px', background: accent } }),
88
89 a.showIcon && el('div', {
90 className: 'bkcg-icon',
91 style: {
92 fontSize: a.iconSize + 'px',
93 lineHeight: 1,
94 flexShrink: 0,
95 background: a.cardStyle === 'icon-circle' ? accent + '18' : 'transparent',
96 width: a.cardStyle === 'icon-circle' ? (a.iconSize * 1.8) + 'px' : undefined,
97 height: a.cardStyle === 'icon-circle' ? (a.iconSize * 1.8) + 'px' : undefined,
98 borderRadius: a.cardStyle === 'icon-circle' ? '50%' : undefined,
99 display: a.cardStyle === 'icon-circle' ? 'flex' : 'block',
100 alignItems: 'center',
101 justifyContent: 'center',
102 }
103 }, useChar ? emoji : IP().buildEditorIcon(a.iconType, a.defaultIcon, a.iconDashicon, a.iconImageUrl, a.iconDashiconColor)),
104
105 el('div', { className: 'bkcg-body', style: { flex: 1, minWidth: 0 } },
106 el('p', {
107 className: 'bkcg-name',
108 style: { margin: '0 0 4px', color: a.nameColor }
109 }, cat.name),
110
111 a.showCount && el('span', {
112 className: 'bkcg-count',
113 style: {
114 display: 'inline-block', background: a.countBg, color: a.countColor,
115 borderRadius: '999px', padding: '1px 8px', fontSize: '11px', fontWeight: 600, marginBottom: '6px'
116 }
117 }, cat.count + ' ' + (a.countLabel || 'posts')),
118
119 a.showDescription && cat.description && el('p', {
120 className: 'bkcg-desc',
121 style: { margin: '4px 0 0', color: a.descColor, overflow: 'hidden', display: '-webkit-box', WebkitLineClamp: 2, WebkitBoxOrient: 'vertical' }
122 }, cat.description),
123
124 a.showArrow && el('span', {
125 className: 'bkcg-arrow',
126 style: { display: 'block', marginTop: '8px', color: accent, fontSize: '18px', transition: 'transform 0.2s ease' }
127 }, '')
128 )
129 );
130 }
131
132 /* ── edit ───────────────────────────────────────────────────────────────── */
133 function Edit(props) {
134 var attributes = props.attributes;
135 var setAttributes = props.setAttributes;
136 var a = attributes;
137
138 /* In editor: load real categories via apiFetch for live preview */
139 var catsState = useState(null);
140 var cats = catsState[0];
141 var setCats = catsState[1];
142 var errorState = useState(null);
143 var loadErr = errorState[0];
144 var setErr = errorState[1];
145
146 useEffect(function () {
147 if (!wp.apiFetch) { setCats(DEMO_CATS); return; }
148 var params = '?per_page=' + a.maxCategories + '&orderby=' + a.orderBy + '&order=' + a.order;
149 if (a.hideEmpty) { params += '&hide_empty=true'; }
150 if (a.excludeIds) { params += '&exclude=' + a.excludeIds; }
151 if (a.includeIds) { params += '&include=' + a.includeIds; }
152
153 wp.apiFetch({ path: '/wp/v2/categories' + params })
154 .then(function (data) {
155 var mapped = data.map(function (c, i) {
156 return {
157 id: c.id,
158 name: c.name,
159 count: c.count,
160 description: c.description ? c.description.replace(/<[^>]+>/g, '').slice(0, a.maxDescriptionLength) : '',
161 link: c.link || '#',
162 emoji: DEMO_CATS[i % DEMO_CATS.length].emoji,
163 };
164 });
165 setCats(mapped.length ? mapped : DEMO_CATS);
166 })
167 .catch(function () {
168 setErr(true);
169 setCats(DEMO_CATS);
170 });
171 }, [a.maxCategories, a.orderBy, a.order, a.hideEmpty, a.excludeIds, a.includeIds]);
172
173 var blockProps = useBlockProps({ style: Object.assign({ display: 'block' }, _tv(a.typoName, '--bkcg-n-'), _tv(a.typoDesc, '--bkcg-d-')) });
174
175 var gridStyle = {
176 display: 'grid',
177 gridTemplateColumns: 'repeat(' + a.columns + ', 1fr)',
178 gap: a.gap + 'px',
179 };
180
181 var displayCats = (cats || DEMO_CATS).slice(0, a.maxCategories);
182
183 return el(Fragment, null,
184 el(InspectorControls, null,
185
186 /* Query */
187 el(PanelBody, { title: __('Query', 'blockenberg'), initialOpen: true },
188 el(RangeControl, { label: __('Max Categories', 'blockenberg'), value: a.maxCategories, min: 1, max: 50, onChange: function (v) { setAttributes({ maxCategories: v }); } }),
189 el(SelectControl, {
190 label: __('Order By', 'blockenberg'), value: a.orderBy,
191 options: [
192 { label: __('Post Count', 'blockenberg'), value: 'count' },
193 { label: __('Name (A–Z)', 'blockenberg'), value: 'name' },
194 { label: __('ID', 'blockenberg'), value: 'id' },
195 { label: __('Slug', 'blockenberg'), value: 'slug' },
196 ],
197 onChange: function (v) { setAttributes({ orderBy: v }); }
198 }),
199 el(SelectControl, {
200 label: __('Order', 'blockenberg'), value: a.order,
201 options: [
202 { label: __('Descending', 'blockenberg'), value: 'desc' },
203 { label: __('Ascending', 'blockenberg'), value: 'asc' },
204 ],
205 onChange: function (v) { setAttributes({ order: v }); }
206 }),
207 el(ToggleControl, { label: __('Hide Empty Categories', 'blockenberg'), checked: a.hideEmpty, onChange: function (v) { setAttributes({ hideEmpty: v }); }, __nextHasNoMarginBottom: true }),
208 el(TextControl, { label: __('Include IDs (comma-separated)', 'blockenberg'), value: a.includeIds, placeholder: '1,3,7', onChange: function (v) { setAttributes({ includeIds: v }); } }),
209 el(TextControl, { label: __('Exclude IDs (comma-separated)', 'blockenberg'), value: a.excludeIds, placeholder: '1,2', onChange: function (v) { setAttributes({ excludeIds: v }); } })
210 ),
211
212 /* Layout */
213 el(PanelBody, { title: __('Layout', 'blockenberg'), initialOpen: false },
214 el(RangeControl, { label: __('Columns', 'blockenberg'), value: a.columns, min: 1, max: 6, onChange: function (v) { setAttributes({ columns: v }); } }),
215 el(RangeControl, { label: __('Gap (px)', 'blockenberg'), value: a.gap, min: 0, max: 48, onChange: function (v) { setAttributes({ gap: v }); } }),
216 el(SelectControl, {
217 label: __('Card Style', 'blockenberg'), value: a.cardStyle,
218 options: [
219 { label: __('Card (shadow)', 'blockenberg'), value: 'card' },
220 { label: __('Card with accent bar', 'blockenberg'), value: 'card-accent' },
221 { label: __('Icon circle', 'blockenberg'), value: 'icon-circle' },
222 { label: __('Minimal (no background)', 'blockenberg'), value: 'minimal' },
223 ],
224 onChange: function (v) { setAttributes({ cardStyle: v }); }
225 }),
226 el(SelectControl, {
227 label: __('Icon Position', 'blockenberg'), value: a.iconPosition,
228 options: [
229 { label: __('Top', 'blockenberg'), value: 'top' },
230 { label: __('Left (inline)', 'blockenberg'), value: 'left' },
231 ],
232 onChange: function (v) { setAttributes({ iconPosition: v }); }
233 }),
234 el(SelectControl, {
235 label: __('Text Align', 'blockenberg'), value: a.textAlign,
236 options: [
237 { label: __('Left', 'blockenberg'), value: 'left' },
238 { label: __('Center', 'blockenberg'), value: 'center' },
239 { label: __('Right', 'blockenberg'), value: 'right' },
240 ],
241 onChange: function (v) { setAttributes({ textAlign: v }); }
242 }),
243 el(SelectControl, {
244 label: __('Link Target', 'blockenberg'), value: a.linkTarget,
245 options: [
246 { label: __('Same tab', 'blockenberg'), value: '_self' },
247 { label: __('New tab', 'blockenberg'), value: '_blank' },
248 ],
249 onChange: function (v) { setAttributes({ linkTarget: v }); }
250 })
251 ),
252
253 /* Content */
254 el(PanelBody, { title: __('Content', 'blockenberg'), initialOpen: false },
255 el(ToggleControl, { label: __('Show Icon / Emoji', 'blockenberg'), checked: a.showIcon, onChange: function (v) { setAttributes({ showIcon: v }); }, __nextHasNoMarginBottom: true }),
256 a.showIcon && el(Fragment, null,
257 el(RangeControl, { label: __('Icon Size (px)', 'blockenberg'), value: a.iconSize, min: 16, max: 80, onChange: function (v) { setAttributes({ iconSize: v }); } }),
258 el(IP().IconPickerControl, IP().iconPickerProps(a, setAttributes, { charAttr: 'defaultIcon' }))
259 ),
260 el(ToggleControl, { label: __('Show Post Count', 'blockenberg'), checked: a.showCount, onChange: function (v) { setAttributes({ showCount: v }); }, __nextHasNoMarginBottom: true }),
261 a.showCount && el(TextControl, { label: __('Count Label', 'blockenberg'), value: a.countLabel, placeholder: 'posts', onChange: function (v) { setAttributes({ countLabel: v }); } }),
262 el(ToggleControl, { label: __('Show Description', 'blockenberg'), checked: a.showDescription, onChange: function (v) { setAttributes({ showDescription: v }); }, __nextHasNoMarginBottom: true }),
263 a.showDescription && el(RangeControl, { label: __('Max Description Length', 'blockenberg'), value: a.maxDescriptionLength, min: 20, max: 200, onChange: function (v) { setAttributes({ maxDescriptionLength: v }); } }),
264 el(ToggleControl, { label: __('Show Arrow →', 'blockenberg'), checked: a.showArrow, onChange: function (v) { setAttributes({ showArrow: v }); }, __nextHasNoMarginBottom: true }),
265 el(ToggleControl, { label: __('Animate on Hover', 'blockenberg'), checked: a.animateOnHover, onChange: function (v) { setAttributes({ animateOnHover: v }); }, __nextHasNoMarginBottom: true })
266 ),
267
268 /* Card Style */
269 el(PanelBody, { title: __('Card Style', 'blockenberg'), initialOpen: false },
270 el(RangeControl, { label: __('Border Radius (px)', 'blockenberg'), value: a.cardRadius, min: 0, max: 32, onChange: function (v) { setAttributes({ cardRadius: v }); } }),
271 el(RangeControl, { label: __('Padding (px)', 'blockenberg'), value: a.cardPadding, min: 8, max: 60, onChange: function (v) { setAttributes({ cardPadding: v }); } }),
272 el(ToggleControl, { label: __('Show Shadow', 'blockenberg'), checked: a.cardShadow, onChange: function (v) { setAttributes({ cardShadow: v }); }, __nextHasNoMarginBottom: true }),
273 el(ToggleControl, { label: __('Show Border', 'blockenberg'), checked: a.cardBorder, onChange: function (v) { setAttributes({ cardBorder: v }); }, __nextHasNoMarginBottom: true })
274 ),
275
276 /* Color Mode */
277
278 el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false },
279 el(getTypographyControl(), { label: __('Name', 'blockenberg'), value: a.typoName, onChange: function (v) { setAttributes({ typoName: v }); } }),
280 el(getTypographyControl(), { label: __('Description', 'blockenberg'), value: a.typoDesc, onChange: function (v) { setAttributes({ typoDesc: v }); } })
281 ),
282 el(PanelBody, { title: __('Color Mode', 'blockenberg'), initialOpen: false },
283 el(SelectControl, {
284 label: __('Color Mode', 'blockenberg'), value: a.colorMode,
285 options: [
286 { label: __('Single accent', 'blockenberg'), value: 'single' },
287 { label: __('Multi-color', 'blockenberg'), value: 'multi' },
288 ],
289 onChange: function (v) { setAttributes({ colorMode: v }); }
290 }),
291 a.colorMode === 'multi' && el(BkbgMultiColorControl, {
292 label: __('Colors (comma-separated hex)', 'blockenberg'), value: a.multiColors,
293 onChange: function (v) { setAttributes({ multiColors: v }); }
294 })
295 ),
296
297 /* Colors */
298 el(PanelColorSettings, {
299 title: __('Colors', 'blockenberg'),
300 initialOpen: false,
301 colorSettings: [
302 { label: __('Card Background', 'blockenberg'), value: a.cardBg, onChange: function (v) { setAttributes({ cardBg: v || '#ffffff' }); } },
303 { label: __('Card Hover Background', 'blockenberg'), value: a.cardBgHover, onChange: function (v) { setAttributes({ cardBgHover: v || '#f5f3ff' }); } },
304 { label: __('Accent', 'blockenberg'), value: a.accentColor, onChange: function (v) { setAttributes({ accentColor: v || '#6c3fb5' }); } },
305 { label: __('Name Text', 'blockenberg'), value: a.nameColor, onChange: function (v) { setAttributes({ nameColor: v || '#1e1e1e' }); } },
306 { label: __('Post Count Badge', 'blockenberg'), value: a.countColor, onChange: function (v) { setAttributes({ countColor: v || '#6c3fb5' }); } },
307 { label: __('Count Badge Bg', 'blockenberg'), value: a.countBg, onChange: function (v) { setAttributes({ countBg: v || '#ede9fe' }); } },
308 { label: __('Description', 'blockenberg'), value: a.descColor, onChange: function (v) { setAttributes({ descColor: v || '#6b7280' }); } },
309 { label: __('Card Border', 'blockenberg'), value: a.borderColor, onChange: function (v) { setAttributes({ borderColor: v || '#e5e7eb' }); } },
310 ]
311 })
312 ),
313
314 el('div', blockProps,
315 !cats && el('div', { style: { textAlign: 'center', padding: '32px', color: '#888' } },
316 el(Spinner),
317 el('p', null, __('Loading categories…', 'blockenberg'))
318 ),
319 cats && el('div', {
320 className: 'bkcg-grid',
321 'data-columns': a.columns,
322 'data-gap': a.gap,
323 'data-animate': a.animateOnHover ? '1' : '0',
324 'data-bg-hover': a.cardBgHover,
325 'data-color-mode': a.colorMode,
326 'data-multi-colors': a.multiColors,
327 'data-accent': a.accentColor,
328 style: gridStyle
329 },
330 displayCats.map(function (cat, idx) {
331 return el(CatCard, { key: cat.id || idx, cat: cat, index: idx, attrs: a, isEdit: true });
332 })
333 ),
334 loadErr && el('p', { style: { fontSize: '11px', color: '#888', marginTop: '4px' } }, __('Preview uses demo data. Real categories will show on the front end.', 'blockenberg'))
335 )
336 );
337 }
338
339 /* ── save ───────────────────────────────────────────────────────────────── */
340 function Save(props) {
341 var a = props.attributes;
342 var blockProps = wp.blockEditor.useBlockProps.save({
343 className: 'bkcg-wrap',
344 });
345
346 return el('div', blockProps,
347 el('div', {
348 className: 'bkcg-grid',
349 'data-max': a.maxCategories,
350 'data-order-by': a.orderBy,
351 'data-order': a.order,
352 'data-hide-empty': a.hideEmpty ? '1' : '0',
353 'data-exclude': a.excludeIds,
354 'data-include': a.includeIds,
355 'data-columns': a.columns,
356 'data-gap': a.gap,
357 'data-card-style': a.cardStyle,
358 'data-icon-pos': a.iconPosition,
359 'data-show-icon': a.showIcon ? '1' : '0',
360 'data-icon-size': a.iconSize,
361 'data-default-icon': a.defaultIcon,
362 'data-icon-type': a.iconType,
363 'data-icon-dashicon': a.iconDashicon,
364 'data-icon-image-url': a.iconImageUrl,
365 'data-show-count': a.showCount ? '1' : '0',
366 'data-count-label': a.countLabel,
367 'data-show-desc': a.showDescription ? '1' : '0',
368 'data-desc-len': a.maxDescriptionLength,
369 'data-show-arrow': a.showArrow ? '1' : '0',
370 'data-link-target': a.linkTarget,
371 'data-text-align': a.textAlign,
372 'data-animate': a.animateOnHover ? '1' : '0',
373 'data-color-mode': a.colorMode,
374 'data-multi-colors': a.multiColors,
375 'data-accent': a.accentColor,
376 'data-name-color': a.nameColor,
377 'data-name-size': a.nameSize,
378 'data-count-color': a.countColor,
379 'data-count-bg': a.countBg,
380 'data-desc-color': a.descColor,
381 'data-desc-size': a.descSize,
382 'data-card-bg': a.cardBg,
383 'data-card-bg-hover': a.cardBgHover,
384 'data-card-radius': a.cardRadius,
385 'data-card-padding': a.cardPadding,
386 'data-card-shadow': a.cardShadow ? '1' : '0',
387 'data-card-border': a.cardBorder ? '1' : '0',
388 'data-border-color': a.borderColor,
389 'data-typo-name': JSON.stringify(a.typoName || {}),
390 'data-typo-desc': JSON.stringify(a.typoDesc || {}),
391 })
392 );
393 }
394
395 /* ── BkbgMultiColorControl ──────────────────────────────────────── */
396 function BkbgMultiColorControl(props) {
397 var label = props.label;
398 var value = props.value || '';
399 var onChange = props.onChange;
400 var colors = value.split(',').map(function (c) { return c.trim(); }).filter(Boolean);
401 var st = useState(-1); var openIdx = st[0]; var setOpenIdx = st[1];
402 return el(Fragment, null,
403 el('div', { style: { marginBottom: '8px' } },
404 el('span', { style: { fontSize: '11px', fontWeight: 500, textTransform: 'uppercase', display: 'block', marginBottom: '4px' } }, label),
405 el('div', { style: { display: 'flex', flexWrap: 'wrap', gap: '6px', alignItems: 'center' } },
406 colors.map(function (c, i) {
407 return el('div', { key: i, style: { position: 'relative', display: 'inline-flex' } },
408 el('button', { type: 'button', style: { width: 28, height: 28, borderRadius: 4, border: '1px solid #ccc', background: c, cursor: 'pointer', padding: 0 }, onClick: function () { setOpenIdx(openIdx === i ? -1 : i); } }),
409 el('button', { type: 'button', 'aria-label': 'Remove', style: { position: 'absolute', top: -6, right: -6, width: 14, height: 14, borderRadius: '50%', border: 'none', background: '#d00', color: '#fff', fontSize: '10px', lineHeight: '14px', cursor: 'pointer', padding: 0, textAlign: 'center' }, onClick: function () { var n = colors.slice(); n.splice(i, 1); onChange(n.join(',')); } }, '×'),
410 openIdx === i && el(Popover, { onClose: function () { setOpenIdx(-1); } }, el('div', { style: { padding: '8px' } }, el(ColorPicker, { color: c, enableAlpha: true, onChange: function (v) { var n = colors.slice(); n[i] = v; onChange(n.join(',')); } })))
411 );
412 }),
413 el(Button, { isSmall: true, variant: 'secondary', onClick: function () { onChange(value ? value + ',#6c3fb5' : '#6c3fb5'); }, style: { height: 28, minWidth: 28 } }, '+')
414 )
415 )
416 );
417 }
418
419 registerBlockType('blockenberg/category-grid', {
420 edit: Edit,
421 save: Save,
422 });
423 }() );
424