| 1 |
( function () { |
| 2 |
var el = wp.element.createElement; |
| 3 |
var Fragment = wp.element.Fragment; |
| 4 |
var __ = wp.i18n.__; |
| 5 |
var registerBlockType = wp.blocks.registerBlockType; |
| 6 |
var InspectorControls = wp.blockEditor.InspectorControls; |
| 7 |
var useBlockProps = wp.blockEditor.useBlockProps; |
| 8 |
var RichText = wp.blockEditor.RichText; |
| 9 |
var PanelBody = wp.components.PanelBody; |
| 10 |
var PanelColorSettings = wp.blockEditor.PanelColorSettings; |
| 11 |
var ToggleControl = wp.components.ToggleControl; |
| 12 |
var RangeControl = wp.components.RangeControl; |
| 13 |
var SelectControl = wp.components.SelectControl; |
| 14 |
var TextControl = wp.components.TextControl; |
| 15 |
|
| 16 |
var _tc, _tv; |
| 17 |
function getTypoControl() { return _tc || (_tc = window.bkbgTypographyControl); } |
| 18 |
function getTypoCssVars() { return _tv || (_tv = window.bkbgTypoCssVars); } |
| 19 |
|
| 20 |
var iconSVGs = { |
| 21 |
none: '', |
| 22 |
info: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="16" height="16" fill="currentColor"><circle cx="12" cy="12" r="10"/><line x1="12" y1="8" x2="12" y2="12" stroke="#fff" stroke-width="2"/><line x1="12" y1="16" x2="12" y2="16" stroke="#fff" stroke-width="2" stroke-linecap="round"/></svg>', |
| 23 |
warning: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2"><path d="M10.29 3.86L1.82 18a2 2 0 001.71 3h16.94a2 2 0 001.71-3L13.71 3.86a2 2 0 00-3.42 0z"/><line x1="12" y1="9" x2="12" y2="13"/><line x1="12" y1="17" x2="12.01" y2="17"/></svg>', |
| 24 |
star: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="16" height="16" fill="currentColor"><polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"/></svg>', |
| 25 |
megaphone: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2"><path d="M3 11l19-9-9 19-2-8-8-2z"/></svg>', |
| 26 |
bell: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2"><path d="M18 8A6 6 0 006 8c0 7-3 9-3 9h18s-3-2-3-9M13.73 21a2 2 0 01-3.46 0"/></svg>' |
| 27 |
}; |
| 28 |
|
| 29 |
registerBlockType('blockenberg/notification-bar', { |
| 30 |
title: __('Notification Bar', 'blockenberg'), |
| 31 |
description: __('Dismissible sticky announcement bar.', 'blockenberg'), |
| 32 |
category: 'bkbg-marketing', |
| 33 |
icon: el('svg', { viewBox: '0 0 24 24', xmlns: 'http://www.w3.org/2000/svg' }, |
| 34 |
el('path', { d: 'M20 2H4a2 2 0 00-2 2v14a2 2 0 002 2h16a2 2 0 002-2V4a2 2 0 00-2-2zM4 6h16v2H4V6z' }) |
| 35 |
), |
| 36 |
attributes: { |
| 37 |
message: { type: 'string', default: '🎉 Special offer! Save 20% today only.' }, |
| 38 |
linkLabel: { type: 'string', default: 'Shop Now' }, |
| 39 |
linkUrl: { type: 'string', default: '' }, |
| 40 |
linkTarget: { type: 'string', default: '_self' }, |
| 41 |
position: { type: 'string', default: 'top' }, |
| 42 |
icon: { type: 'string', default: 'none' }, |
| 43 |
showClose: { type: 'boolean', default: true }, |
| 44 |
cookieName: { type: 'string', default: 'bkbg_nb_dismissed' }, |
| 45 |
cookieDays: { type: 'number', default: 1 }, |
| 46 |
bgColor: { type: 'string', default: '#2563eb' }, |
| 47 |
textColor: { type: 'string', default: '#ffffff' }, |
| 48 |
linkColor: { type: 'string', default: '#bfdbfe' }, |
| 49 |
closeBtnColor: { type: 'string', default: '#93c5fd' }, |
| 50 |
fontSize: { type: 'number', default: 14 }, |
| 51 |
padding: { type: 'number', default: 12 }, |
| 52 |
animation: { type: 'string', default: 'slide' }, |
| 53 |
sticky: { type: 'boolean', default: true }, |
| 54 |
showAfterDelay: { type: 'number', default: 0 } |
| 55 |
}, |
| 56 |
|
| 57 |
edit: function (props) { |
| 58 |
var a = props.attributes; |
| 59 |
var setAttr = props.setAttributes; |
| 60 |
|
| 61 |
var blockProps = useBlockProps((function () { |
| 62 |
var _tvf = getTypoCssVars(); |
| 63 |
var s = {}; |
| 64 |
if (_tvf) { Object.assign(s, _tvf(a.messageTypo, '--bkbg-nb-msg-')); } |
| 65 |
return { className: 'bkbg-nb-editor-wrap', style: s }; |
| 66 |
})()); |
| 67 |
|
| 68 |
var inspector = el(InspectorControls, null, |
| 69 |
|
| 70 |
el(PanelBody, { title: __('Content', 'blockenberg'), initialOpen: true }, |
| 71 |
el(TextControl, { |
| 72 |
label: __('Link label', 'blockenberg'), |
| 73 |
value: a.linkLabel, |
| 74 |
onChange: function (v) { setAttr({ linkLabel: v }); } |
| 75 |
}), |
| 76 |
el(TextControl, { |
| 77 |
label: __('Link URL', 'blockenberg'), |
| 78 |
type: 'url', |
| 79 |
value: a.linkUrl, |
| 80 |
onChange: function (v) { setAttr({ linkUrl: v }); } |
| 81 |
}), |
| 82 |
el(SelectControl, { |
| 83 |
label: __('Link target', 'blockenberg'), |
| 84 |
value: a.linkTarget, |
| 85 |
options: [ |
| 86 |
{ label: 'Same tab', value: '_self' }, |
| 87 |
{ label: 'New tab', value: '_blank' } |
| 88 |
], |
| 89 |
onChange: function (v) { setAttr({ linkTarget: v }); } |
| 90 |
}), |
| 91 |
el(SelectControl, { |
| 92 |
label: __('Icon', 'blockenberg'), |
| 93 |
value: a.icon, |
| 94 |
options: [ |
| 95 |
{ label: 'None', value: 'none' }, |
| 96 |
{ label: 'Info', value: 'info' }, |
| 97 |
{ label: 'Warning', value: 'warning' }, |
| 98 |
{ label: 'Star', value: 'star' }, |
| 99 |
{ label: 'Megaphone', value: 'megaphone' }, |
| 100 |
{ label: 'Bell', value: 'bell' } |
| 101 |
], |
| 102 |
onChange: function (v) { setAttr({ icon: v }); } |
| 103 |
}) |
| 104 |
), |
| 105 |
|
| 106 |
el(PanelBody, { title: __('Behaviour', 'blockenberg'), initialOpen: false }, |
| 107 |
el(SelectControl, { |
| 108 |
label: __('Position', 'blockenberg'), |
| 109 |
value: a.position, |
| 110 |
options: [ |
| 111 |
{ label: 'Top of page', value: 'top' }, |
| 112 |
{ label: 'Bottom of page', value: 'bottom' } |
| 113 |
], |
| 114 |
onChange: function (v) { setAttr({ position: v }); } |
| 115 |
}), |
| 116 |
el(ToggleControl, { |
| 117 |
label: __('Sticky (fixed to viewport)', 'blockenberg'), |
| 118 |
checked: a.sticky, |
| 119 |
onChange: function (v) { setAttr({ sticky: v }); }, |
| 120 |
__nextHasNoMarginBottom: true |
| 121 |
}), |
| 122 |
el(ToggleControl, { |
| 123 |
label: __('Show close button', 'blockenberg'), |
| 124 |
checked: a.showClose, |
| 125 |
onChange: function (v) { setAttr({ showClose: v }); }, |
| 126 |
__nextHasNoMarginBottom: true |
| 127 |
}), |
| 128 |
el(SelectControl, { |
| 129 |
label: __('Animation', 'blockenberg'), |
| 130 |
value: a.animation, |
| 131 |
options: [ |
| 132 |
{ label: 'Slide', value: 'slide' }, |
| 133 |
{ label: 'Fade', value: 'fade' }, |
| 134 |
{ label: 'None', value: 'none' } |
| 135 |
], |
| 136 |
onChange: function (v) { setAttr({ animation: v }); } |
| 137 |
}), |
| 138 |
el(RangeControl, { |
| 139 |
label: __('Show after delay (ms)', 'blockenberg'), |
| 140 |
value: a.showAfterDelay, min: 0, max: 10000, step: 250, |
| 141 |
onChange: function (v) { setAttr({ showAfterDelay: v }); } |
| 142 |
}), |
| 143 |
el(TextControl, { |
| 144 |
label: __('Cookie / storage key', 'blockenberg'), |
| 145 |
value: a.cookieName, |
| 146 |
onChange: function (v) { setAttr({ cookieName: v }); } |
| 147 |
}), |
| 148 |
el(RangeControl, { |
| 149 |
label: __('Remember dismissal (days)', 'blockenberg'), |
| 150 |
value: a.cookieDays, min: 0, max: 365, |
| 151 |
onChange: function (v) { setAttr({ cookieDays: v }); } |
| 152 |
}) |
| 153 |
), |
| 154 |
|
| 155 |
el(PanelBody, { title: __('Style', 'blockenberg'), initialOpen: false }, |
| 156 |
el(RangeControl, { |
| 157 |
label: __('Vertical padding (px)', 'blockenberg'), |
| 158 |
value: a.padding, min: 4, max: 32, |
| 159 |
onChange: function (v) { setAttr({ padding: v }); } |
| 160 |
}), |
| 161 |
el(PanelColorSettings, { |
| 162 |
title: __('Colors', 'blockenberg'), |
| 163 |
initialOpen: false, |
| 164 |
colorSettings: [ |
| 165 |
{ label: __('Background', 'blockenberg'), value: a.bgColor, onChange: function(v){ setAttr({ bgColor: v||'#2563eb' }); } }, |
| 166 |
{ label: __('Text', 'blockenberg'), value: a.textColor, onChange: function(v){ setAttr({ textColor: v||'#ffffff' }); } }, |
| 167 |
{ label: __('Link', 'blockenberg'), value: a.linkColor, onChange: function(v){ setAttr({ linkColor: v||'#bfdbfe' }); } }, |
| 168 |
{ label: __('Close button', 'blockenberg'), value: a.closeBtnColor, onChange: function(v){ setAttr({ closeBtnColor: v||'#93c5fd' }); } } |
| 169 |
] |
| 170 |
}) |
| 171 |
), |
| 172 |
el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false }, |
| 173 |
getTypoControl() && el(getTypoControl(), { label: __('Message Typography', 'blockenberg'), value: a.messageTypo || {}, onChange: function (v) { setAttr({ messageTypo: v }); } }) |
| 174 |
) |
| 175 |
); |
| 176 |
|
| 177 |
/* editor preview (static, not sticky) */ |
| 178 |
var previewStyle = { |
| 179 |
background: a.bgColor, |
| 180 |
color: a.textColor, |
| 181 |
padding: a.padding + 'px 20px', |
| 182 |
borderRadius: 6, |
| 183 |
display: 'flex', |
| 184 |
alignItems: 'center', |
| 185 |
gap: 12, |
| 186 |
flexWrap: 'wrap', |
| 187 |
position: 'relative' |
| 188 |
}; |
| 189 |
|
| 190 |
return el(Fragment, null, |
| 191 |
inspector, |
| 192 |
el('div', blockProps, |
| 193 |
el('div', { className: 'bkbg-nb-preview-label' }, |
| 194 |
'🔧 Notification Bar Preview (' + a.position + (a.sticky ? ', sticky' : '') + ')' |
| 195 |
), |
| 196 |
el('div', { className: 'bkbg-nb-bar', style: previewStyle }, |
| 197 |
a.icon !== 'none' && el('span', { |
| 198 |
className: 'bkbg-nb-icon', |
| 199 |
dangerouslySetInnerHTML: { __html: iconSVGs[a.icon] || '' } |
| 200 |
}), |
| 201 |
el(RichText, { |
| 202 |
tagName: 'span', |
| 203 |
className: 'bkbg-nb-message', |
| 204 |
value: a.message, |
| 205 |
onChange: function (v) { setAttr({ message: v }); }, |
| 206 |
placeholder: __('Type your announcement…', 'blockenberg'), |
| 207 |
allowedFormats: ['core/bold', 'core/italic', 'core/link'] |
| 208 |
}), |
| 209 |
a.linkUrl && a.linkLabel && el('a', { |
| 210 |
href: '#', |
| 211 |
className: 'bkbg-nb-link', |
| 212 |
style: { color: a.linkColor } |
| 213 |
}, a.linkLabel), |
| 214 |
a.showClose && el('button', { |
| 215 |
className: 'bkbg-nb-close', |
| 216 |
style: { color: a.closeBtnColor }, |
| 217 |
'aria-label': 'Close' |
| 218 |
}, '✕') |
| 219 |
) |
| 220 |
) |
| 221 |
); |
| 222 |
}, |
| 223 |
|
| 224 |
save: function (props) { |
| 225 |
var a = props.attributes; |
| 226 |
var _tvf = getTypoCssVars(); |
| 227 |
var barStyle = { |
| 228 |
'--bkbg-nb-bg': a.bgColor, |
| 229 |
'--bkbg-nb-text': a.textColor, |
| 230 |
'--bkbg-nb-link': a.linkColor, |
| 231 |
'--bkbg-nb-close': a.closeBtnColor, |
| 232 |
'--bkbg-nb-fs': a.fontSize + 'px', |
| 233 |
'--bkbg-nb-fw': a.fontWeight || 500, |
| 234 |
'--bkbg-nb-lh': a.lineHeight || 1.4, |
| 235 |
'--bkbg-nb-pad': a.padding + 'px' |
| 236 |
}; |
| 237 |
if (_tvf) { Object.assign(barStyle, _tvf(a.messageTypo, '--bkbg-nb-msg-')); } |
| 238 |
var blockProps = wp.blockEditor.useBlockProps.save({ |
| 239 |
'data-position': a.position, |
| 240 |
'data-animation': a.animation, |
| 241 |
'data-sticky': a.sticky ? '1' : '0', |
| 242 |
'data-delay': a.showAfterDelay, |
| 243 |
'data-cookie': a.cookieName, |
| 244 |
'data-cookie-days': a.cookieDays, |
| 245 |
'data-show-close': a.showClose ? '1' : '0' |
| 246 |
}); |
| 247 |
|
| 248 |
return el('div', blockProps, |
| 249 |
el('div', { |
| 250 |
className: 'bkbg-nb-bar', |
| 251 |
style: barStyle |
| 252 |
}, |
| 253 |
el('div', { className: 'bkbg-nb-inner' }, |
| 254 |
el('div', { className: 'bkbg-nb-content' }, |
| 255 |
a.icon !== 'none' && el('span', { className: 'bkbg-nb-icon bkbg-nb-icon--' + a.icon }), |
| 256 |
el(RichText.Content, { tagName: 'span', className: 'bkbg-nb-message', value: a.message }), |
| 257 |
a.linkUrl && a.linkLabel && el('a', { |
| 258 |
href: a.linkUrl, |
| 259 |
className: 'bkbg-nb-link', |
| 260 |
target: a.linkTarget, |
| 261 |
rel: a.linkTarget === '_blank' ? 'noopener noreferrer' : undefined |
| 262 |
}, a.linkLabel) |
| 263 |
), |
| 264 |
a.showClose && el('button', { |
| 265 |
className: 'bkbg-nb-close', |
| 266 |
type: 'button', |
| 267 |
'aria-label': 'Close notification' |
| 268 |
}, el('svg', { viewBox: '0 0 24 24', xmlns: 'http://www.w3.org/2000/svg' }, |
| 269 |
el('path', { d: 'M18 6L6 18M6 6l12 12', stroke: 'currentColor', strokeWidth: '2', strokeLinecap: 'round' }) |
| 270 |
)) |
| 271 |
) |
| 272 |
) |
| 273 |
); |
| 274 |
}, |
| 275 |
|
| 276 |
deprecated: [{ |
| 277 |
attributes: { |
| 278 |
message: { type: 'string', default: '🎉 Special offer! Save 20% today only.' }, |
| 279 |
linkLabel: { type: 'string', default: 'Shop Now' }, |
| 280 |
linkUrl: { type: 'string', default: '' }, |
| 281 |
linkTarget: { type: 'string', default: '_self' }, |
| 282 |
position: { type: 'string', default: 'top' }, |
| 283 |
icon: { type: 'string', default: 'none' }, |
| 284 |
showClose: { type: 'boolean', default: true }, |
| 285 |
cookieName: { type: 'string', default: 'bkbg_nb_dismissed' }, |
| 286 |
cookieDays: { type: 'number', default: 1 }, |
| 287 |
bgColor: { type: 'string', default: '#2563eb' }, |
| 288 |
textColor: { type: 'string', default: '#ffffff' }, |
| 289 |
linkColor: { type: 'string', default: '#bfdbfe' }, |
| 290 |
closeBtnColor: { type: 'string', default: '#93c5fd' }, |
| 291 |
fontSize: { type: 'number', default: 14 }, |
| 292 |
fontWeight: { type: 'number', default: 500 }, |
| 293 |
lineHeight: { type: 'number', default: 1.4 }, |
| 294 |
padding: { type: 'number', default: 12 }, |
| 295 |
animation: { type: 'string', default: 'slide' }, |
| 296 |
sticky: { type: 'boolean', default: true }, |
| 297 |
showAfterDelay: { type: 'number', default: 0 } |
| 298 |
}, |
| 299 |
save: function (props) { |
| 300 |
var a = props.attributes; |
| 301 |
var blockProps = wp.blockEditor.useBlockProps.save({ |
| 302 |
'data-position': a.position, |
| 303 |
'data-animation': a.animation, |
| 304 |
'data-sticky': a.sticky ? '1' : '0', |
| 305 |
'data-delay': a.showAfterDelay, |
| 306 |
'data-cookie': a.cookieName, |
| 307 |
'data-cookie-days': a.cookieDays, |
| 308 |
'data-show-close': a.showClose ? '1' : '0' |
| 309 |
}); |
| 310 |
|
| 311 |
return el('div', blockProps, |
| 312 |
el('div', { |
| 313 |
className: 'bkbg-nb-bar', |
| 314 |
style: { |
| 315 |
'--bkbg-nb-bg': a.bgColor, |
| 316 |
'--bkbg-nb-text': a.textColor, |
| 317 |
'--bkbg-nb-link': a.linkColor, |
| 318 |
'--bkbg-nb-close': a.closeBtnColor, |
| 319 |
'--bkbg-nb-fs': a.fontSize + 'px', |
| 320 |
'--bkbg-nb-fw': a.fontWeight || 500, |
| 321 |
'--bkbg-nb-lh': a.lineHeight || 1.4, |
| 322 |
'--bkbg-nb-pad': a.padding + 'px' |
| 323 |
} |
| 324 |
}, |
| 325 |
el('div', { className: 'bkbg-nb-inner' }, |
| 326 |
el('div', { className: 'bkbg-nb-content' }, |
| 327 |
a.icon !== 'none' && el('span', { className: 'bkbg-nb-icon bkbg-nb-icon--' + a.icon }), |
| 328 |
el(RichText.Content, { tagName: 'span', className: 'bkbg-nb-message', value: a.message }), |
| 329 |
a.linkUrl && a.linkLabel && el('a', { |
| 330 |
href: a.linkUrl, |
| 331 |
className: 'bkbg-nb-link', |
| 332 |
target: a.linkTarget, |
| 333 |
rel: a.linkTarget === '_blank' ? 'noopener noreferrer' : undefined |
| 334 |
}, a.linkLabel) |
| 335 |
), |
| 336 |
a.showClose && el('button', { |
| 337 |
className: 'bkbg-nb-close', |
| 338 |
type: 'button', |
| 339 |
'aria-label': 'Close notification' |
| 340 |
}, el('svg', { viewBox: '0 0 24 24', xmlns: 'http://www.w3.org/2000/svg' }, |
| 341 |
el('path', { d: 'M18 6L6 18M6 6l12 12', stroke: 'currentColor', strokeWidth: '2', strokeLinecap: 'round' }) |
| 342 |
)) |
| 343 |
) |
| 344 |
) |
| 345 |
); |
| 346 |
} |
| 347 |
}] |
| 348 |
}); |
| 349 |
}() ); |
| 350 |
|