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 / page-header / index.js

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

268 lines 19.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /* ====================================================
2 Page Header Block — editor (index.js)
3 Block: blockenberg/page-header
4 ==================================================== */
5 ( function () {
6 var el = wp.element.createElement;
7 var Fragment = wp.element.Fragment;
8 var RichText = wp.blockEditor.RichText;
9 var MediaUpload = wp.blockEditor.MediaUpload;
10 var MediaUploadCheck = wp.blockEditor.MediaUploadCheck;
11 var registerBlockType = wp.blocks.registerBlockType;
12 var InspectorControls = wp.blockEditor.InspectorControls;
13 var useBlockProps = wp.blockEditor.useBlockProps;
14 var PanelBody = wp.components.PanelBody;
15 var PanelColorSettings = wp.blockEditor.PanelColorSettings;
16 var RangeControl = wp.components.RangeControl;
17 var SelectControl = wp.components.SelectControl;
18 var ToggleControl = wp.components.ToggleControl;
19 var TextControl = wp.components.TextControl;
20 var Button = wp.components.Button;
21 var __ = wp.i18n.__;
22
23 var _tc, _tv;
24 function getTypoControl() { return _tc || (_tc = window.bkbgTypographyControl); }
25 function getTypoCssVars() { return _tv || (_tv = window.bkbgTypoCssVars); }
26
27 function buildStyle(a) {
28 var bg = '';
29 if (a.bgType === 'gradient') bg = a.bgGradient;
30 else if (a.bgType === 'image' && a.bgImageUrl) bg = 'url(' + a.bgImageUrl + ') center/cover no-repeat';
31 else bg = a.bgColor;
32 return {
33 '--bkbg-pghd-pt': a.paddingTop + 'px',
34 '--bkbg-pghd-pb': a.paddingBottom + 'px',
35 '--bkbg-pghd-mw': a.contentMaxWidth + 'px',
36 '--bkbg-pghd-h-sz': a.headlineSize + 'px',
37 '--bkbg-pghd-sub-sz': a.subtitleSize + 'px',
38 '--bkbg-pghd-ey-sz': a.eyebrowSize + 'px',
39 '--bkbg-pghd-btn-r': a.btnRadius + 'px',
40 '--bkbg-pghd-eyebrow-bg': a.eyebrowBg,
41 '--bkbg-pghd-eyebrow-c': a.eyebrowColor,
42 '--bkbg-pghd-h-c': a.headlineColor,
43 '--bkbg-pghd-sub-c': a.subtitleColor,
44 '--bkbg-pghd-pbtn-bg': a.primaryBtnBg,
45 '--bkbg-pghd-pbtn-c': a.primaryBtnColor,
46 '--bkbg-pghd-sbtn-c': a.secBtnColor,
47 '--bkbg-pghd-bc-c': a.breadcrumbColor,
48 background: bg || undefined,
49 };
50 }
51
52 function buildOverlayStyle(a) {
53 if (a.bgType !== 'image' || !a.bgImageUrl) return { display: 'none' };
54 return { background: a.bgOverlayColor, opacity: a.bgOverlayOpacity / 100 };
55 }
56
57 registerBlockType('blockenberg/page-header', {
58 edit: function (props) {
59 var a = props.attributes;
60 var setAttr = props.setAttributes;
61
62 var blockProps = useBlockProps((function () {
63 var tv = getTypoCssVars();
64 var bg = '';
65 if (a.bgType === 'gradient') bg = a.bgGradient;
66 else if (a.bgType === 'image' && a.bgImageUrl) bg = 'url(' + a.bgImageUrl + ') center/cover no-repeat';
67 else bg = a.bgColor;
68 var s = {
69 '--bkbg-pghd-pt': a.paddingTop + 'px',
70 '--bkbg-pghd-pb': a.paddingBottom + 'px',
71 '--bkbg-pghd-mw': a.contentMaxWidth + 'px',
72 '--bkbg-pghd-btn-r': a.btnRadius + 'px',
73 '--bkbg-pghd-eyebrow-bg': a.eyebrowBg,
74 '--bkbg-pghd-eyebrow-c': a.eyebrowColor,
75 '--bkbg-pghd-h-c': a.headlineColor,
76 '--bkbg-pghd-sub-c': a.subtitleColor,
77 '--bkbg-pghd-pbtn-bg': a.primaryBtnBg,
78 '--bkbg-pghd-pbtn-c': a.primaryBtnColor,
79 '--bkbg-pghd-sbtn-c': a.secBtnColor,
80 '--bkbg-pghd-bc-c': a.breadcrumbColor,
81 background: bg || undefined,
82 };
83 Object.assign(s, tv(a.headlineTypo, '--bkbg-pghd-hl-'));
84 Object.assign(s, tv(a.subtitleTypo, '--bkbg-pghd-st-'));
85 Object.assign(s, tv(a.eyebrowTypo, '--bkbg-pghd-eb-'));
86 return { className: 'bkbg-pghd-wrap bkbg-pghd-style--' + a.style, style: s };
87 })());
88
89 var inspector = el(InspectorControls, null,
90 /* Style */
91 el(PanelBody, { title: __('Style & Layout', 'blockenberg'), initialOpen: true },
92 el(SelectControl, { label: __('Style', 'blockenberg'), value: a.style, options: [
93 { label: 'Centered', value: 'centered' },
94 { label: 'Left-aligned', value: 'left' },
95 { label: 'Split (text+bg)',value: 'split' },
96 { label: 'Minimal', value: 'minimal' },
97 { label: 'Dark hero', value: 'dark' },
98 ], onChange: function(v){ setAttr({style:v}); } }),
99 el(SelectControl, { label: __('Background type', 'blockenberg'), value: a.bgType, options: [
100 { label: 'Solid color', value: 'color' },
101 { label: 'Gradient', value: 'gradient' },
102 { label: 'Image', value: 'image' },
103 ], onChange: function(v){ setAttr({bgType:v}); } }),
104 a.bgType === 'image' ? el(Fragment, null,
105 el(MediaUploadCheck, null,
106 el(MediaUpload, {
107 onSelect: function(m){ setAttr({bgImageUrl:m.url,bgImageId:m.id}); },
108 allowedTypes: ['image'], value: a.bgImageId,
109 render: function(o){ return el(Button, { onClick:o.open, variant:'secondary', isSmall:true }, a.bgImageUrl ? __('Change image','blockenberg') : __('Upload bg image','blockenberg')); }
110 })
111 ),
112 el(RangeControl, { label: __('Overlay opacity (%)', 'blockenberg'), value: a.bgOverlayOpacity, min: 0, max: 100, onChange: function(v){ setAttr({bgOverlayOpacity:v}); } })
113 ) : null,
114 el(SelectControl, { label: __('Text alignment', 'blockenberg'), value: a.textAlign, options: [
115 {label:'Left',value:'left'},{label:'Center',value:'center'},{label:'Right',value:'right'}
116 ], onChange: function(v){ setAttr({textAlign:v}); } }),
117 el(RangeControl, { label: __('Content max-width (px)', 'blockenberg'), value: a.contentMaxWidth, min: 400, max: 1400, onChange: function(v){ setAttr({contentMaxWidth:v}); } })
118 ),
119 /* Content toggles */
120 el(PanelBody, { title: __('Content', 'blockenberg'), initialOpen: false },
121 el(ToggleControl, { label: __('Show eyebrow', 'blockenberg'), checked: a.showEyebrow, onChange: function(v){ setAttr({showEyebrow:v}); } }),
122 el(ToggleControl, { label: __('Show subtitle', 'blockenberg'), checked: a.showSubtitle, onChange: function(v){ setAttr({showSubtitle:v}); } }),
123 el(ToggleControl, { label: __('Show breadcrumbs', 'blockenberg'), checked: a.showBreadcrumbs, onChange: function(v){ setAttr({showBreadcrumbs:v}); } }),
124 el(ToggleControl, { label: __('Primary button', 'blockenberg'), checked: a.showPrimaryBtn, onChange: function(v){ setAttr({showPrimaryBtn:v}); } }),
125 a.showPrimaryBtn ? el(Fragment, null,
126 el(TextControl, { label: __('Primary label', 'blockenberg'), value: a.primaryLabel, onChange: function(v){ setAttr({primaryLabel:v}); } }),
127 el(TextControl, { label: __('Primary URL', 'blockenberg'), value: a.primaryUrl, onChange: function(v){ setAttr({primaryUrl:v}); } })
128 ) : null,
129 el(ToggleControl, { label: __('Secondary button', 'blockenberg'), checked: a.showSecondaryBtn, onChange: function(v){ setAttr({showSecondaryBtn:v}); } }),
130 a.showSecondaryBtn ? el(Fragment, null,
131 el(TextControl, { label: __('Secondary label', 'blockenberg'), value: a.secondaryLabel, onChange: function(v){ setAttr({secondaryLabel:v}); } }),
132 el(TextControl, { label: __('Secondary URL', 'blockenberg'), value: a.secondaryUrl, onChange: function(v){ setAttr({secondaryUrl:v}); } })
133 ) : null
134 ),
135 /* Spacing & Typography */
136 el(PanelBody, { title: __('Typography', 'blockenberg'), initialOpen: false },
137 el(getTypoControl(), { label: __('Headline', 'blockenberg'), value: a.headlineTypo, onChange: function (v) { setAttr({ headlineTypo: v }); } }),
138 el(getTypoControl(), { label: __('Subtitle', 'blockenberg'), value: a.subtitleTypo, onChange: function (v) { setAttr({ subtitleTypo: v }); } }),
139 el(getTypoControl(), { label: __('Eyebrow', 'blockenberg'), value: a.eyebrowTypo, onChange: function (v) { setAttr({ eyebrowTypo: v }); } })
140 ),
141 el(PanelBody, { title: __('Spacing', 'blockenberg'), initialOpen: false },
142 el(RangeControl, { label: __('Padding top (px)', 'blockenberg'), value: a.paddingTop, min: 0, max: 240, onChange: function(v){ setAttr({paddingTop:v}); } }),
143 el(RangeControl, { label: __('Padding bottom (px)', 'blockenberg'), value: a.paddingBottom, min: 0, max: 240, onChange: function(v){ setAttr({paddingBottom:v}); } }),
144 el(RangeControl, { label: __('Button radius (px)', 'blockenberg'), value: a.btnRadius, min: 0, max: 50, onChange: function(v){ setAttr({btnRadius:v}); } })
145 ),
146 /* Colors */
147 el(PanelColorSettings, { title: __('Colors', 'blockenberg'), initialOpen: false, colorSettings: [
148 {label: __('Background color','blockenberg'), value: a.bgColor, onChange: function(v){ setAttr({bgColor:v||''}); }},
149 {label: __('Overlay color','blockenberg'), value: a.bgOverlayColor, onChange: function(v){ setAttr({bgOverlayColor:v||''}); }},
150 {label: __('Eyebrow bg','blockenberg'), value: a.eyebrowBg, onChange: function(v){ setAttr({eyebrowBg:v||''}); }},
151 {label: __('Eyebrow text','blockenberg'), value: a.eyebrowColor, onChange: function(v){ setAttr({eyebrowColor:v||''}); }},
152 {label: __('Headline','blockenberg'), value: a.headlineColor, onChange: function(v){ setAttr({headlineColor:v||''}); }},
153 {label: __('Subtitle','blockenberg'), value: a.subtitleColor, onChange: function(v){ setAttr({subtitleColor:v||''}); }},
154 {label: __('Primary btn bg','blockenberg'), value: a.primaryBtnBg, onChange: function(v){ setAttr({primaryBtnBg:v||''}); }},
155 {label: __('Primary btn text','blockenberg'), value: a.primaryBtnColor, onChange: function(v){ setAttr({primaryBtnColor:v||''}); }},
156 {label: __('Secondary btn text','blockenberg'),value: a.secBtnColor, onChange: function(v){ setAttr({secBtnColor:v||''}); }},
157 {label: __('Breadcrumb color','blockenberg'), value: a.breadcrumbColor, onChange: function(v){ setAttr({breadcrumbColor:v||''}); }},
158 ] })
159 );
160
161 /* Breadcrumbs */
162 var bcEl = a.showBreadcrumbs ? el('nav', { className: 'bkbg-pghd-breadcrumbs', style: { textAlign: a.textAlign } },
163 a.breadcrumbs.map(function(bc, i) {
164 return el('span', { key: i },
165 el('span', { className: 'bkbg-pghd-bc-item' }, bc.label),
166 i < a.breadcrumbs.length - 1 ? el('span', { className: 'bkbg-pghd-bc-sep' }, ' / ') : null
167 );
168 })
169 ) : null;
170
171 var eyebrowEl = a.showEyebrow ? el('div', { className: 'bkbg-pghd-eyebrow' }, a.eyebrow) : null;
172
173 var buttons = (a.showPrimaryBtn || a.showSecondaryBtn) ? el('div', { className: 'bkbg-pghd-buttons' },
174 a.showPrimaryBtn ? el('a', { href: a.primaryUrl, className: 'bkbg-pghd-btn bkbg-pghd-btn--primary', onClick: function(e){ e.preventDefault(); } }, a.primaryLabel) : null,
175 a.showSecondaryBtn ? el('a', { href: a.secondaryUrl, className: 'bkbg-pghd-btn bkbg-pghd-btn--secondary', onClick: function(e){ e.preventDefault(); } }, a.secondaryLabel) : null
176 ) : null;
177
178 return el(Fragment, null,
179 inspector,
180 el('div', blockProps,
181 el('div', { className: 'bkbg-pghd-overlay', style: buildOverlayStyle(a) }),
182 el('div', { className: 'bkbg-pghd-inner', style: { textAlign: a.textAlign } },
183 bcEl,
184 eyebrowEl,
185 el(RichText, { tagName: 'h1', className: 'bkbg-pghd-headline', value: a.headline, onChange: function(v){ setAttr({headline:v}); }, placeholder: __('Page headline…','blockenberg') }),
186 a.showSubtitle ? el(RichText, { tagName: 'p', className: 'bkbg-pghd-subtitle', value: a.subtitle, onChange: function(v){ setAttr({subtitle:v}); }, placeholder: __('Subtitle…','blockenberg') }) : null,
187 buttons
188 )
189 )
190 );
191 },
192
193 save: function (props) {
194 var a = props.attributes;
195 var blockProps = wp.blockEditor.useBlockProps.save({
196 className: 'bkbg-pghd-wrap bkbg-pghd-style--' + a.style,
197 style: (function(){
198 var tv = getTypoCssVars();
199 var s = { '--bkbg-pghd-pt': a.paddingTop+'px', '--bkbg-pghd-pb': a.paddingBottom+'px', '--bkbg-pghd-mw': a.contentMaxWidth+'px', '--bkbg-pghd-btn-r': a.btnRadius+'px', '--bkbg-pghd-eyebrow-bg': a.eyebrowBg, '--bkbg-pghd-eyebrow-c': a.eyebrowColor, '--bkbg-pghd-h-c': a.headlineColor, '--bkbg-pghd-sub-c': a.subtitleColor, '--bkbg-pghd-pbtn-bg': a.primaryBtnBg, '--bkbg-pghd-pbtn-c': a.primaryBtnColor, '--bkbg-pghd-sbtn-c': a.secBtnColor, '--bkbg-pghd-bc-c': a.breadcrumbColor };
200 Object.assign(s, tv(a.headlineTypo, '--bkbg-pghd-hl-'));
201 Object.assign(s, tv(a.subtitleTypo, '--bkbg-pghd-st-'));
202 Object.assign(s, tv(a.eyebrowTypo, '--bkbg-pghd-eb-'));
203 if (a.bgType === 'gradient') s.background = a.bgGradient;
204 else if (a.bgType === 'image' && a.bgImageUrl) s.background = 'url('+a.bgImageUrl+') center/cover no-repeat';
205 else s.background = a.bgColor;
206 return s;
207 })()
208 });
209
210 return el('div', blockProps,
211 (a.bgType === 'image' && a.bgImageUrl) ? el('div', { className: 'bkbg-pghd-overlay', style: { background: a.bgOverlayColor, opacity: a.bgOverlayOpacity/100 } }) : null,
212 el('div', { className: 'bkbg-pghd-inner', style: { textAlign: a.textAlign } },
213 a.showBreadcrumbs ? el('nav', { className: 'bkbg-pghd-breadcrumbs', 'aria-label': 'Breadcrumb' },
214 a.breadcrumbs.map(function(bc, i) {
215 return el('span', { key: i },
216 bc.url ? el('a', { href: bc.url, className: 'bkbg-pghd-bc-item' }, bc.label) : el('span', { className: 'bkbg-pghd-bc-item bkbg-pghd-bc-current' }, bc.label),
217 i < a.breadcrumbs.length - 1 ? el('span', { className: 'bkbg-pghd-bc-sep' }, ' / ') : null
218 );
219 })
220 ) : null,
221 a.showEyebrow ? el('div', { className: 'bkbg-pghd-eyebrow' }, a.eyebrow) : null,
222 el(RichText.Content, { tagName: 'h1', className: 'bkbg-pghd-headline', value: a.headline }),
223 a.showSubtitle ? el(RichText.Content, { tagName: 'p', className: 'bkbg-pghd-subtitle', value: a.subtitle }) : null,
224 (a.showPrimaryBtn || a.showSecondaryBtn) ? el('div', { className: 'bkbg-pghd-buttons' },
225 a.showPrimaryBtn ? el('a', { href: a.primaryUrl, className: 'bkbg-pghd-btn bkbg-pghd-btn--primary', target: a.primaryTarget ? '_blank' : undefined, rel: a.primaryTarget ? 'noopener noreferrer' : undefined }, a.primaryLabel) : null,
226 a.showSecondaryBtn ? el('a', { href: a.secondaryUrl, className: 'bkbg-pghd-btn bkbg-pghd-btn--secondary' }, a.secondaryLabel) : null
227 ) : null
228 )
229 );
230 },
231
232 deprecated: [{
233 save: function (props) {
234 var a = props.attributes;
235 var blockProps = wp.blockEditor.useBlockProps.save({
236 className: 'bkbg-pghd-wrap bkbg-pghd-style--' + a.style,
237 style: (function(){
238 var s = { '--bkbg-pghd-pt': a.paddingTop+'px', '--bkbg-pghd-pb': a.paddingBottom+'px', '--bkbg-pghd-mw': a.contentMaxWidth+'px', '--bkbg-pghd-h-sz': a.headlineSize+'px', '--bkbg-pghd-sub-sz': a.subtitleSize+'px', '--bkbg-pghd-ey-sz': a.eyebrowSize+'px', '--bkbg-pghd-btn-r': a.btnRadius+'px', '--bkbg-pghd-eyebrow-bg': a.eyebrowBg, '--bkbg-pghd-eyebrow-c': a.eyebrowColor, '--bkbg-pghd-h-c': a.headlineColor, '--bkbg-pghd-sub-c': a.subtitleColor, '--bkbg-pghd-pbtn-bg': a.primaryBtnBg, '--bkbg-pghd-pbtn-c': a.primaryBtnColor, '--bkbg-pghd-sbtn-c': a.secBtnColor, '--bkbg-pghd-bc-c': a.breadcrumbColor };
239 if (a.bgType === 'gradient') s.background = a.bgGradient;
240 else if (a.bgType === 'image' && a.bgImageUrl) s.background = 'url('+a.bgImageUrl+') center/cover no-repeat';
241 else s.background = a.bgColor;
242 return s;
243 })()
244 });
245 return el('div', blockProps,
246 (a.bgType === 'image' && a.bgImageUrl) ? el('div', { className: 'bkbg-pghd-overlay', style: { background: a.bgOverlayColor, opacity: a.bgOverlayOpacity/100 } }) : null,
247 el('div', { className: 'bkbg-pghd-inner', style: { textAlign: a.textAlign } },
248 a.showBreadcrumbs ? el('nav', { className: 'bkbg-pghd-breadcrumbs', 'aria-label': 'Breadcrumb' },
249 a.breadcrumbs.map(function(bc, i) {
250 return el('span', { key: i },
251 bc.url ? el('a', { href: bc.url, className: 'bkbg-pghd-bc-item' }, bc.label) : el('span', { className: 'bkbg-pghd-bc-item bkbg-pghd-bc-current' }, bc.label),
252 i < a.breadcrumbs.length - 1 ? el('span', { className: 'bkbg-pghd-bc-sep' }, ' / ') : null
253 );
254 })
255 ) : null,
256 a.showEyebrow ? el('div', { className: 'bkbg-pghd-eyebrow' }, a.eyebrow) : null,
257 el(RichText.Content, { tagName: 'h1', className: 'bkbg-pghd-headline', value: a.headline }),
258 a.showSubtitle ? el(RichText.Content, { tagName: 'p', className: 'bkbg-pghd-subtitle', value: a.subtitle }) : null,
259 (a.showPrimaryBtn || a.showSecondaryBtn) ? el('div', { className: 'bkbg-pghd-buttons' },
260 a.showPrimaryBtn ? el('a', { href: a.primaryUrl, className: 'bkbg-pghd-btn bkbg-pghd-btn--primary', target: a.primaryTarget ? '_blank' : undefined, rel: a.primaryTarget ? 'noopener noreferrer' : undefined }, a.primaryLabel) : null,
261 a.showSecondaryBtn ? el('a', { href: a.secondaryUrl, className: 'bkbg-pghd-btn bkbg-pghd-btn--secondary' }, a.secondaryLabel) : null
262 ) : null
263 )
264 );
265 }
266 }]
267 }() );
268