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 / tip-calculator / index.js

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

256 lines 17.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 useState = wp.element.useState;
4 var Fragment = wp.element.Fragment;
5 var registerBlockType = wp.blocks.registerBlockType;
6 var __ = wp.i18n.__;
7 var InspectorControls = wp.blockEditor.InspectorControls;
8 var PanelColorSettings = wp.blockEditor.PanelColorSettings;
9 var useBlockProps = wp.blockEditor.useBlockProps;
10 var PanelBody = wp.components.PanelBody;
11 var RangeControl = wp.components.RangeControl;
12 var SelectControl = wp.components.SelectControl;
13 var TextControl = wp.components.TextControl;
14 var ToggleControl = wp.components.ToggleControl;
15 var Button = wp.components.Button;
16
17 var _tc; function getTypoControl() { return _tc || (_tc = window.bkbgTypographyControl); }
18 var _tv; function getTypoCssVars() { return _tv || (_tv = window.bkbgTypoCssVars); }
19 var _tvf = function (typo, prefix) { var fn = getTypoCssVars(); return fn ? fn(typo, prefix) : {}; };
20
21 function fmtMoney(val, currency, pos) {
22 var s = Math.abs(val).toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 });
23 return pos === 'after' ? s + currency : currency + s;
24 }
25
26 /* ── Preview component ────────────────────────────────────────────────── */
27 function TipPreview(props) {
28 var a = props.attrs;
29 var billState = useState(a.defaultBill || 50);
30 var bill = billState[0]; var setBill = billState[1];
31 var tipState = useState(a.defaultTip || 15);
32 var tipPct = tipState[0]; var setTipPct = tipState[1];
33 var custState = useState('');
34 var customTip = custState[0]; var setCustomTip = custState[1];
35 var pplState = useState(a.defaultPeople || 2);
36 var people = pplState[0]; var setPeople = pplState[1];
37
38 var effectiveTip = customTip !== '' ? parseFloat(customTip) : tipPct;
39 var tipAmt = bill * effectiveTip / 100;
40 var total = bill + tipAmt;
41 var perPerson = people > 0 ? total / people : total;
42
43 var quickTips = a.quickTips || [10, 15, 18, 20, 25];
44 var accent = a.accentColor || '#6c3fb5';
45 var cur = a.currency || '$';
46 var pos = a.currencyPos || 'before';
47 var cRadius = (a.cardRadius || 16) + 'px';
48 var btnR = (a.btnRadius || 8) + 'px';
49
50 var cardStyle = {
51 background: a.cardBg || '#ffffff',
52 borderRadius: cRadius,
53 padding: '32px',
54 boxShadow: '0 4px 24px rgba(0,0,0,0.08)',
55 maxWidth: (a.maxWidth || 520) + 'px',
56 margin: '0 auto',
57 paddingTop: (a.paddingTop || 60) + 'px',
58 paddingBottom: (a.paddingBottom|| 60) + 'px',
59 boxSizing: 'border-box',
60 };
61
62 var inputStyle = {
63 width: '100%', padding: '12px 14px', borderRadius: '8px',
64 border: '1.5px solid #e5e7eb', fontSize: '18px',
65 boxSizing: 'border-box', outline: 'none',
66 };
67
68 return el('div', { style: cardStyle },
69 a.showTitle && el('h2', { className: 'bkbg-tc-title', style: { color: a.titleColor || '#1e1b4b', textAlign: 'center', marginTop: 0, marginBottom: 8 } }, a.title || __('Tip Calculator', 'blockenberg')),
70 a.showSubtitle && el('p', { style: { color: a.subtitleColor || '#6b7280', textAlign: 'center', marginTop: 0, marginBottom: 28 } }, a.subtitle),
71
72 /* Bill input */
73 el('div', { style: { marginBottom: '20px' } },
74 el('label', { style: { display: 'block', fontWeight: 600, fontSize: '13px', color: a.labelColor || '#374151', marginBottom: '6px' } }, __('Bill Amount', 'blockenberg')),
75 el('div', { style: { position: 'relative' } },
76 el('span', { style: { position: 'absolute', left: '14px', top: '50%', transform: 'translateY(-50%)', color: '#9ca3af', fontSize: '16px', pointerEvents: 'none' } }, pos === 'before' ? cur : ''),
77 el('input', {
78 type: 'number', value: bill, min: 0,
79 onChange: function(e) { setBill(parseFloat(e.target.value) || 0); },
80 style: Object.assign({}, inputStyle, { paddingLeft: pos === 'before' ? '32px' : '14px' })
81 })
82 )
83 ),
84
85 /* Quick tip buttons */
86 el('div', { style: { marginBottom: '20px' } },
87 el('label', { style: { display: 'block', fontWeight: 600, fontSize: '13px', color: a.labelColor || '#374151', marginBottom: '8px' } }, __('Tip Percentage', 'blockenberg')),
88 el('div', { style: { display: 'flex', flexWrap: 'wrap', gap: '8px' } },
89 quickTips.map(function(pct) {
90 var isActive = customTip === '' && tipPct === pct;
91 return el('button', {
92 key: pct,
93 onClick: function() { setTipPct(pct); setCustomTip(''); },
94 style: {
95 padding: '8px 16px',
96 borderRadius: btnR,
97 border: 'none',
98 cursor: 'pointer',
99 fontWeight: 700,
100 fontSize: '14px',
101 background: isActive ? (a.activeBtnBg || accent) : (a.quickBtnBg || '#f3f4f6'),
102 color: isActive ? (a.activeBtnColor || '#fff') : (a.quickBtnColor || '#374151'),
103 transition: 'all 0.2s',
104 }
105 }, pct + '%');
106 }),
107 a.allowCustomTip && el('input', {
108 type: 'number', value: customTip, placeholder: __('Custom %', 'blockenberg'), min: 0, max: 100,
109 onChange: function(e) { setCustomTip(e.target.value); },
110 style: {
111 width: '90px', padding: '8px 12px', borderRadius: btnR,
112 border: '1.5px solid ' + (customTip !== '' ? accent : '#e5e7eb'),
113 fontSize: '14px', outline: 'none'
114 }
115 })
116 )
117 ),
118
119 /* People */
120 a.showSplitter && el('div', { style: { marginBottom: '28px' } },
121 el('label', { style: { display: 'block', fontWeight: 600, fontSize: '13px', color: a.labelColor || '#374151', marginBottom: '8px' } }, __('Number of People', 'blockenberg')),
122 el('div', { style: { display: 'flex', alignItems: 'center', gap: '12px' } },
123 el('button', {
124 onClick: function() { setPeople(Math.max(1, people - 1)); },
125 style: { width: '36px', height: '36px', borderRadius: '50%', border: '1.5px solid ' + accent, background: '#fff', color: accent, fontSize: '20px', cursor: 'pointer', display: 'flex', alignItems: 'center', justifyContent: 'center', lineHeight: 1, fontWeight: 700 }
126 }, ''),
127 el('span', { style: { fontSize: '20px', fontWeight: 700, minWidth: '28px', textAlign: 'center', color: a.titleColor || '#1e1b4b' } }, people),
128 el('button', {
129 onClick: function() { setPeople(people + 1); },
130 style: { width: '36px', height: '36px', borderRadius: '50%', border: '1.5px solid ' + accent, background: accent, color: '#fff', fontSize: '20px', cursor: 'pointer', display: 'flex', alignItems: 'center', justifyContent: 'center', lineHeight: 1, fontWeight: 700 }
131 }, '+')
132 )
133 ),
134
135 /* Results */
136 el('div', {
137 style: {
138 background: a.resultBg || '#f5f3ff',
139 border: '1.5px solid ' + (a.resultBorder || '#ede9fe'),
140 borderRadius: cRadius,
141 padding: '24px',
142 display: 'grid',
143 gridTemplateColumns: a.showSplitter && a.showPerPerson ? '1fr 1fr' : '1fr',
144 gap: '16px',
145 }
146 },
147 a.showTipAmount && el('div', { style: { textAlign: 'center', gridColumn: (a.showTotal || (a.showSplitter && a.showPerPerson)) ? undefined : '1 / -1' } },
148 el('div', { style: { fontSize: '12px', color: '#9ca3af', marginBottom: '4px', fontWeight: 600, textTransform: 'uppercase', letterSpacing: '0.05em' } }, __('Tip Amount', 'blockenberg')),
149 el('div', { style: { fontSize: '28px', fontWeight: 800, color: accent } }, fmtMoney(tipAmt, cur, pos))
150 ),
151 a.showTotal && el('div', { style: { textAlign: 'center' } },
152 el('div', { style: { fontSize: '12px', color: '#9ca3af', marginBottom: '4px', fontWeight: 600, textTransform: 'uppercase', letterSpacing: '0.05em' } }, __('Total', 'blockenberg')),
153 el('div', { style: { fontSize: '28px', fontWeight: 800, color: a.titleColor || '#1e1b4b' } }, fmtMoney(total, cur, pos))
154 ),
155 a.showSplitter && a.showPerPerson && el('div', { style: { textAlign: 'center', gridColumn: '1 / -1', paddingTop: '12px', borderTop: '1px solid ' + (a.resultBorder || '#ede9fe') } },
156 el('div', { style: { fontSize: '12px', color: '#9ca3af', marginBottom: '4px', fontWeight: 600, textTransform: 'uppercase', letterSpacing: '0.05em' } }, __('Per Person', 'blockenberg')),
157 el('div', { className: 'bkbg-tc-result-value bkbg-tc-per-person-value', style: { color: a.resultNumColor || accent } }, fmtMoney(perPerson, cur, pos))
158 )
159 )
160 );
161 }
162
163 registerBlockType('blockenberg/tip-calculator', {
164 edit: function(props) {
165 var a = props.attributes;
166 var setAttr = props.setAttributes;
167 var blockProps = useBlockProps((function () {
168 var s = { background: a.bgColor || undefined };
169 Object.assign(s, _tvf(a.titleTypo, '--bktpc-tt-'), _tvf(a.resultTypo, '--bktpc-rt-'));
170 return { style: s };
171 })());
172
173 return el(Fragment, null,
174 el(InspectorControls, null,
175
176 el(PanelBody, { title: __('Content', 'blockenberg'), initialOpen: true },
177 el(ToggleControl, { label: __('Show Title', 'blockenberg'), checked: a.showTitle, onChange: function(v) { setAttr({ showTitle: v }); }, __nextHasNoMarginBottom: true }),
178 a.showTitle && el(TextControl, { label: __('Title', 'blockenberg'), value: a.title, onChange: function(v) { setAttr({ title: v }); } }),
179 el(ToggleControl, { label: __('Show Subtitle', 'blockenberg'), checked: a.showSubtitle, onChange: function(v) { setAttr({ showSubtitle: v }); }, __nextHasNoMarginBottom: true }),
180 a.showSubtitle && el(TextControl, { label: __('Subtitle', 'blockenberg'), value: a.subtitle, onChange: function(v) { setAttr({ subtitle: v }); } }),
181 el(TextControl, { label: __('Currency Symbol', 'blockenberg'), value: a.currency, onChange: function(v) { setAttr({ currency: v }); } }),
182 el(SelectControl, { label: __('Currency Position', 'blockenberg'), value: a.currencyPos, options: [{ label: __('Before ($50)', 'blockenberg'), value: 'before' }, { label: __('After (50$)', 'blockenberg'), value: 'after' }], onChange: function(v) { setAttr({ currencyPos: v }); } })
183 ),
184
185 el(PanelBody, { title: __('Defaults', 'blockenberg'), initialOpen: false },
186 el(RangeControl, { label: __('Default Bill Amount', 'blockenberg'), value: a.defaultBill, min: 0, max: 10000, onChange: function(v) { setAttr({ defaultBill: v }); } }),
187 el(RangeControl, { label: __('Default Tip %', 'blockenberg'), value: a.defaultTip, min: 0, max: 100, onChange: function(v) { setAttr({ defaultTip: v }); } }),
188 el(RangeControl, { label: __('Default People', 'blockenberg'), value: a.defaultPeople, min: 1, max: 20, onChange: function(v) { setAttr({ defaultPeople: v }); } })
189 ),
190
191 el(PanelBody, { title: __('Display Options', 'blockenberg'), initialOpen: false },
192 el(ToggleControl, { label: __('Show Tip Amount', 'blockenberg'), checked: a.showTipAmount, onChange: function(v) { setAttr({ showTipAmount: v }); }, __nextHasNoMarginBottom: true }),
193 el(ToggleControl, { label: __('Show Total', 'blockenberg'), checked: a.showTotal, onChange: function(v) { setAttr({ showTotal: v }); }, __nextHasNoMarginBottom: true }),
194 el(ToggleControl, { label: __('Show Bill Splitter', 'blockenberg'), checked: a.showSplitter, onChange: function(v) { setAttr({ showSplitter: v }); }, __nextHasNoMarginBottom: true }),
195 el(ToggleControl, { label: __('Show Per Person Amount', 'blockenberg'), checked: a.showPerPerson, onChange: function(v) { setAttr({ showPerPerson: v }); }, __nextHasNoMarginBottom: true }),
196 el(ToggleControl, { label: __('Allow Custom Tip Input', 'blockenberg'), checked: a.allowCustomTip, onChange: function(v) { setAttr({ allowCustomTip: v }); }, __nextHasNoMarginBottom: true })
197 ),
198
199
200 el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false },
201 getTypoControl() && el(getTypoControl(), {
202 label: __('Title Typography', 'blockenberg'),
203 value: a.titleTypo || {},
204 onChange: function(v) { setAttr({ titleTypo: v }); }
205 }),
206 getTypoControl() && el(getTypoControl(), {
207 label: __('Result Number Typography', 'blockenberg'),
208 value: a.resultTypo || {},
209 onChange: function(v) { setAttr({ resultTypo: v }); }
210 })
211 ),
212 el(PanelColorSettings, {
213 title: __('Colors', 'blockenberg'), initialOpen: false,
214 colorSettings: [
215 { label: __('Accent Color', 'blockenberg'), value: a.accentColor, onChange: function(v) { setAttr({ accentColor: v || '#6c3fb5' }); } },
216 { label: __('Active Button Background', 'blockenberg'), value: a.activeBtnBg, onChange: function(v) { setAttr({ activeBtnBg: v || '#6c3fb5' }); } },
217 { label: __('Active Button Text', 'blockenberg'), value: a.activeBtnColor, onChange: function(v) { setAttr({ activeBtnColor: v || '#ffffff' }); } },
218 { label: __('Quick Button Background', 'blockenberg'), value: a.quickBtnBg, onChange: function(v) { setAttr({ quickBtnBg: v || '#f3f4f6' }); } },
219 { label: __('Per Person Number Color', 'blockenberg'), value: a.resultNumColor, onChange: function(v) { setAttr({ resultNumColor: v || '#6c3fb5' }); } },
220 { label: __('Result Background', 'blockenberg'), value: a.resultBg, onChange: function(v) { setAttr({ resultBg: v || '#f5f3ff' }); } },
221 { label: __('Card Background', 'blockenberg'), value: a.cardBg, onChange: function(v) { setAttr({ cardBg: v || '#ffffff' }); } },
222 { label: __('Section Background', 'blockenberg'), value: a.bgColor, onChange: function(v) { setAttr({ bgColor: v || '' }); } },
223 ]
224 }),
225
226 el(PanelBody, { title: __('Sizing', 'blockenberg'), initialOpen: false },
227 el(RangeControl, { label: __('Card Radius (px)', 'blockenberg'), value: a.cardRadius, min: 0, max: 40, onChange: function(v) { setAttr({ cardRadius: v }); } }),
228 el(RangeControl, { label: __('Button Radius (px)', 'blockenberg'), value: a.btnRadius, min: 0, max: 40, onChange: function(v) { setAttr({ btnRadius: v }); } }),
229 el(RangeControl, { label: __('Max Width (px)', 'blockenberg'), value: a.maxWidth, min: 300, max: 900, onChange: function(v) { setAttr({ maxWidth: v }); } }),
230 el(RangeControl, { label: __('Padding Top (px)', 'blockenberg'), value: a.paddingTop, min: 0, max: 160, onChange: function(v) { setAttr({ paddingTop: v }); } }),
231 el(RangeControl, { label: __('Padding Bottom (px)', 'blockenberg'), value: a.paddingBottom, min: 0, max: 160, onChange: function(v) { setAttr({ paddingBottom: v }); } })
232 )
233 ),
234
235 el('div', blockProps,
236 el(TipPreview, { attrs: a })
237 )
238 );
239 },
240
241 save: function(props) {
242 var a = props.attributes;
243 var blockProps = wp.blockEditor.useBlockProps.save((function () {
244 var s = { background: a.bgColor || undefined };
245 Object.assign(s, _tvf(a.titleTypo, '--bktpc-tt-'), _tvf(a.resultTypo, '--bktpc-rt-'));
246 return { style: s };
247 })());
248 return el('div', blockProps,
249 el('div', { className: 'bkbg-tc-app', 'data-opts': JSON.stringify(a) },
250 el('p', { className: 'bkbg-tc-loading' }, 'Loading calculator…')
251 )
252 );
253 }
254 });
255 }() );
256