| 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 _cbTC, _cbTV; |
| 17 |
function _tc() { return _cbTC || (_cbTC = window.bkbgTypographyControl); } |
| 18 |
function _tv(obj, prefix) { var fn = _cbTV || (_cbTV = window.bkbgTypoCssVars); return fn ? fn(obj, prefix) : {}; } |
| 19 |
var IP = function () { return window.bkbgIconPicker; }; |
| 20 |
|
| 21 |
function cornerRadius(corners) { |
| 22 |
return corners === 'pill' ? 999 : corners === 'rounded' ? 8 : 0; |
| 23 |
} |
| 24 |
|
| 25 |
function getButtonStyle(attr) { |
| 26 |
var r = cornerRadius(attr.corners) + 'px'; |
| 27 |
var base = { |
| 28 |
display: 'inline-flex', alignItems: 'center', gap: 8, justifyContent: 'center', |
| 29 |
padding: attr.paddingV + 'px ' + attr.paddingH + 'px', |
| 30 |
borderRadius: r, |
| 31 |
cursor: 'pointer', |
| 32 |
textDecoration: 'none', |
| 33 |
position: 'relative', |
| 34 |
overflow: 'hidden', |
| 35 |
border: attr.borderWidth + 'px solid ' + attr.borderColor, |
| 36 |
transition: 'all 0.25s ease' |
| 37 |
}; |
| 38 |
if (attr.buttonStyle === 'outline') { |
| 39 |
base.background = 'transparent'; |
| 40 |
base.color = attr.bgColor; |
| 41 |
} else if (attr.buttonStyle === 'ghost') { |
| 42 |
base.background = 'transparent'; |
| 43 |
base.color = attr.bgColor; |
| 44 |
base.border = 'none'; |
| 45 |
} else if (attr.buttonStyle === 'gradient') { |
| 46 |
base.background = 'linear-gradient(to right, ' + attr.gradientFrom + ', ' + attr.gradientTo + ')'; |
| 47 |
base.color = attr.textColor; |
| 48 |
base.border = 'none'; |
| 49 |
} else { |
| 50 |
base.background = attr.bgColor; |
| 51 |
base.color = attr.textColor; |
| 52 |
} |
| 53 |
return base; |
| 54 |
} |
| 55 |
|
| 56 |
registerBlockType('blockenberg/creative-button', { |
| 57 |
edit: function (props) { |
| 58 |
var attr = props.attributes; |
| 59 |
var setAttr = props.setAttributes; |
| 60 |
var blockProps = useBlockProps({ className: 'bkbg-cb2-editor', style: Object.assign({}, _tv(attr.typoBtn, '--bkbg-cb2-btn-')) }); |
| 61 |
|
| 62 |
var btnStyle = getButtonStyle(attr); |
| 63 |
var wrapStyle = { textAlign: attr.centered ? 'center' : 'left', padding: '20px 0' }; |
| 64 |
|
| 65 |
return el(Fragment, null, |
| 66 |
el(InspectorControls, null, |
| 67 |
el(PanelBody, { title: __('Button', 'blockenberg'), initialOpen: true }, |
| 68 |
el(TextControl, { label: __('URL', 'blockenberg'), __nextHasNoMarginBottom: true, value: attr.url, onChange: function (v) { setAttr({ url: v }); } }), |
| 69 |
el(ToggleControl, { label: __('Open in New Tab', 'blockenberg'), __nextHasNoMarginBottom: true, checked: attr.openNewTab, onChange: function (v) { setAttr({ openNewTab: v }); } }), |
| 70 |
el(ToggleControl, { label: __('Centered', 'blockenberg'), __nextHasNoMarginBottom: true, checked: attr.centered, onChange: function (v) { setAttr({ centered: v }); } }), |
| 71 |
el(ToggleControl, { label: __('Full Width', 'blockenberg'), __nextHasNoMarginBottom: true, checked: attr.fullWidth, onChange: function (v) { setAttr({ fullWidth: v }); } }) |
| 72 |
), |
| 73 |
el(PanelBody, { title: __('Style & Effect', 'blockenberg'), initialOpen: false }, |
| 74 |
el(SelectControl, { label: __('Button Style', 'blockenberg'), __nextHasNoMarginBottom: true, value: attr.buttonStyle, |
| 75 |
options: [ |
| 76 |
{ label: __('Filled (solid)', 'blockenberg'), value: 'filled' }, |
| 77 |
{ label: __('Outline', 'blockenberg'), value: 'outline' }, |
| 78 |
{ label: __('Ghost (no border)', 'blockenberg'), value: 'ghost' }, |
| 79 |
{ label: __('Gradient', 'blockenberg'), value: 'gradient' } |
| 80 |
], |
| 81 |
onChange: function (v) { setAttr({ buttonStyle: v }); } }), |
| 82 |
el(SelectControl, { label: __('Hover Effect', 'blockenberg'), __nextHasNoMarginBottom: true, value: attr.hoverEffect, |
| 83 |
options: [ |
| 84 |
{ label: __('Slide — background wipes in', 'blockenberg'), value: 'slide' }, |
| 85 |
{ label: __('Fill — expands from center', 'blockenberg'), value: 'fill' }, |
| 86 |
{ label: __('Outline — inverts on hover', 'blockenberg'), value: 'outline' }, |
| 87 |
{ label: __('3D Press — button depresses', 'blockenberg'), value: 'press3d' }, |
| 88 |
{ label: __('Arrow — arrow slides in', 'blockenberg'), value: 'arrow' }, |
| 89 |
{ label: __('Ripple — click wave effect', 'blockenberg'), value: 'ripple' } |
| 90 |
], |
| 91 |
onChange: function (v) { setAttr({ hoverEffect: v }); } }), |
| 92 |
el(SelectControl, { label: __('Corners', 'blockenberg'), __nextHasNoMarginBottom: true, value: attr.corners, |
| 93 |
options: [{ label: __('Sharp', 'blockenberg'), value: 'sharp' }, { label: __('Rounded', 'blockenberg'), value: 'rounded' }, { label: __('Pill', 'blockenberg'), value: 'pill' }], |
| 94 |
onChange: function (v) { setAttr({ corners: v }); } }) |
| 95 |
), |
| 96 |
el(PanelBody, { title: __('Icon', 'blockenberg'), initialOpen: false }, |
| 97 |
el(ToggleControl, { label: __('Show Icon', 'blockenberg'), __nextHasNoMarginBottom: true, checked: attr.showIcon, onChange: function (v) { setAttr({ showIcon: v }); } }), |
| 98 |
attr.showIcon && el('p', { style: { fontSize: '11px', fontWeight: 600, marginTop: '12px' } }, __('Icon', 'blockenberg')), |
| 99 |
attr.showIcon && el(IP().IconPickerControl, IP().iconPickerProps(attr, setAttr)), |
| 100 |
attr.showIcon && el(SelectControl, { label: __('Icon Position', 'blockenberg'), __nextHasNoMarginBottom: true, value: attr.iconPosition, |
| 101 |
options: [{ label: __('Left', 'blockenberg'), value: 'left' }, { label: __('Right', 'blockenberg'), value: 'right' }], |
| 102 |
onChange: function (v) { setAttr({ iconPosition: v }); } }), |
| 103 |
attr.showIcon && el(RangeControl, { label: __('Icon Size (px)', 'blockenberg'), __nextHasNoMarginBottom: true, value: attr.iconSize, min: 10, max: 32, onChange: function (v) { setAttr({ iconSize: v }); } }) |
| 104 |
), |
| 105 |
el(PanelBody, { title: __('Sizing', 'blockenberg'), initialOpen: false }, |
| 106 |
el(RangeControl, { label: __('Horizontal Padding (px)', 'blockenberg'), __nextHasNoMarginBottom: true, value: attr.paddingH, min: 8, max: 80, onChange: function (v) { setAttr({ paddingH: v }); } }), |
| 107 |
el(RangeControl, { label: __('Vertical Padding (px)', 'blockenberg'), __nextHasNoMarginBottom: true, value: attr.paddingV, min: 4, max: 40, onChange: function (v) { setAttr({ paddingV: v }); } }), |
| 108 |
el(RangeControl, { label: __('Border Width (px)', 'blockenberg'), __nextHasNoMarginBottom: true, value: attr.borderWidth, min: 0, max: 6, onChange: function (v) { setAttr({ borderWidth: v }); } }) |
| 109 |
), |
| 110 |
|
| 111 |
el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false }, |
| 112 |
_tc() && el(_tc(), { label: __('Button', 'blockenberg'), value: attr.typoBtn, onChange: function (v) { setAttr({ typoBtn: v }); } }) |
| 113 |
), |
| 114 |
el(PanelColorSettings, { |
| 115 |
title: __('Colors', 'blockenberg'), initialOpen: false, |
| 116 |
colorSettings: [ |
| 117 |
{ value: attr.bgColor, onChange: function (v) { setAttr({ bgColor: v || '#7c3aed' }); }, label: __('Button BG', 'blockenberg') }, |
| 118 |
{ value: attr.textColor, onChange: function (v) { setAttr({ textColor: v || '#ffffff' }); }, label: __('Button Text', 'blockenberg') }, |
| 119 |
{ value: attr.hoverBg, onChange: function (v) { setAttr({ hoverBg: v || '#4f46e5' }); }, label: __('Hover BG', 'blockenberg') }, |
| 120 |
{ value: attr.hoverColor, onChange: function (v) { setAttr({ hoverColor: v || '#ffffff' }); }, label: __('Hover Text', 'blockenberg') }, |
| 121 |
{ value: attr.borderColor, onChange: function (v) { setAttr({ borderColor: v || '#7c3aed' }); }, label: __('Border', 'blockenberg') }, |
| 122 |
{ value: attr.gradientFrom, onChange: function (v) { setAttr({ gradientFrom: v || '#7c3aed' }); }, label: __('Gradient Start', 'blockenberg') }, |
| 123 |
{ value: attr.gradientTo, onChange: function (v) { setAttr({ gradientTo: v || '#2563eb' }); }, label: __('Gradient End', 'blockenberg') } |
| 124 |
] |
| 125 |
}) |
| 126 |
), |
| 127 |
el('div', blockProps, |
| 128 |
el('div', { style: wrapStyle }, |
| 129 |
el('span', { className: 'bkbg-cb2-btn', style: Object.assign({}, btnStyle, { width: attr.fullWidth ? '100%' : 'auto' }) }, |
| 130 |
attr.showIcon && attr.iconPosition === 'left' && el('span', { className: 'bkbg-cb2-icon', style: { fontSize: attr.iconSize + 'px' } }, |
| 131 |
(!attr.iconType || attr.iconType === 'custom-char') ? attr.icon : IP().buildEditorIcon(attr.iconType, attr.icon, attr.iconDashicon, attr.iconImageUrl, attr.iconDashiconColor) |
| 132 |
), |
| 133 |
el(RichText, { tagName: 'span', value: attr.label, onChange: function (v) { setAttr({ label: v }); }, placeholder: 'Button label…' }), |
| 134 |
attr.showIcon && attr.iconPosition === 'right' && el('span', { className: 'bkbg-cb2-icon', style: { fontSize: attr.iconSize + 'px' } }, |
| 135 |
(!attr.iconType || attr.iconType === 'custom-char') ? attr.icon : IP().buildEditorIcon(attr.iconType, attr.icon, attr.iconDashicon, attr.iconImageUrl, attr.iconDashiconColor) |
| 136 |
) |
| 137 |
), |
| 138 |
el('div', { style: { marginTop: 8, fontSize: 11, color: '#888', textAlign: 'center' } }, |
| 139 |
__('Effect: ', 'blockenberg') + attr.hoverEffect + ' — visible on frontend') |
| 140 |
) |
| 141 |
) |
| 142 |
); |
| 143 |
}, |
| 144 |
save: function (props) { |
| 145 |
var attr = props.attributes; |
| 146 |
return el('div', useBlockProps.save({ style: Object.assign({}, _tv(attr.typoBtn, '--bkbg-cb2-btn-')) }), |
| 147 |
el('div', { className: 'bkbg-creative-button-app', 'data-opts': JSON.stringify(attr) }) |
| 148 |
); |
| 149 |
} |
| 150 |
}); |
| 151 |
}() ); |
| 152 |
|