| 1 |
const { registerBlockType } = wp.blocks; |
| 2 |
const { __ } = wp.i18n; |
| 3 |
const { PanelBody, ColorPicker } = wp.components; |
| 4 |
const { InspectorControls, useBlockProps, InnerBlocks } = |
| 5 |
wp.blockEditor || wp.editor; |
| 6 |
|
| 7 |
import "./style.css"; |
| 8 |
|
| 9 |
// Parent Tabs block |
| 10 |
registerBlockType("ai-builder/aibui-tabs", { |
| 11 |
title: __("Tabs (AI Builder)", "ai-builder"), |
| 12 |
icon: "index-card", |
| 13 |
category: "widgets", |
| 14 |
supports: { align: ["wide", "full"], html: false }, |
| 15 |
attributes: { |
| 16 |
activeColor: { type: "string", default: "#4f46e5" }, |
| 17 |
}, |
| 18 |
edit: (props) => { |
| 19 |
const { attributes, setAttributes } = props; |
| 20 |
const { activeColor } = attributes; |
| 21 |
|
| 22 |
function hexToRgba(hex, alpha) { |
| 23 |
const match = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec( |
| 24 |
hex || "#4f46e5" |
| 25 |
); |
| 26 |
if (!match) return `rgba(79,70,229,${alpha})`; |
| 27 |
const r = parseInt(match[1], 16); |
| 28 |
const g = parseInt(match[2], 16); |
| 29 |
const b = parseInt(match[3], 16); |
| 30 |
return `rgba(${r}, ${g}, ${b}, ${alpha})`; |
| 31 |
} |
| 32 |
|
| 33 |
const blockProps = useBlockProps({ |
| 34 |
className: "aibui-tabs", |
| 35 |
style: { |
| 36 |
"--aibui-active": activeColor, |
| 37 |
"--aibui-active-shadow": hexToRgba(activeColor, 0.15), |
| 38 |
}, |
| 39 |
}); |
| 40 |
const TEMPLATE = [ |
| 41 |
["ai-builder/aibui-tab", { title: "Tab 1" }], |
| 42 |
["ai-builder/aibui-tab", { title: "Tab 2" }], |
| 43 |
]; |
| 44 |
return ( |
| 45 |
<> |
| 46 |
<InspectorControls> |
| 47 |
<PanelBody |
| 48 |
title={__("Tabs Settings", "ai-builder")} |
| 49 |
initialOpen={true} |
| 50 |
> |
| 51 |
<div style={{ padding: 12 }}> |
| 52 |
<p style={{ margin: "0 0 8px" }}> |
| 53 |
{__("Active tab color", "ai-builder")} |
| 54 |
</p> |
| 55 |
<ColorPicker |
| 56 |
color={activeColor} |
| 57 |
onChangeComplete={(value) => { |
| 58 |
const next = |
| 59 |
value?.hex || value?.rgb ? value.hex || activeColor : value; |
| 60 |
setAttributes({ activeColor: next || "#4f46e5" }); |
| 61 |
}} |
| 62 |
onChange={(value) => { |
| 63 |
const next = |
| 64 |
value?.hex || value?.rgb ? value.hex || activeColor : value; |
| 65 |
setAttributes({ activeColor: next || "#4f46e5" }); |
| 66 |
}} |
| 67 |
disableAlpha |
| 68 |
/> |
| 69 |
</div> |
| 70 |
</PanelBody> |
| 71 |
</InspectorControls> |
| 72 |
<div {...blockProps}> |
| 73 |
<InnerBlocks |
| 74 |
allowedBlocks={["ai-builder/aibui-tab"]} |
| 75 |
template={TEMPLATE} |
| 76 |
templateLock={false} |
| 77 |
/> |
| 78 |
</div> |
| 79 |
</> |
| 80 |
); |
| 81 |
}, |
| 82 |
save: (props) => { |
| 83 |
const { attributes } = props; |
| 84 |
const { activeColor } = attributes; |
| 85 |
function hexToRgba(hex, alpha) { |
| 86 |
const match = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec( |
| 87 |
hex || "#4f46e5" |
| 88 |
); |
| 89 |
if (!match) return `rgba(79,70,229,${alpha})`; |
| 90 |
const r = parseInt(match[1], 16); |
| 91 |
const g = parseInt(match[2], 16); |
| 92 |
const b = parseInt(match[3], 16); |
| 93 |
return `rgba(${r}, ${g}, ${b}, ${alpha})`; |
| 94 |
} |
| 95 |
const blockProps = wp.blockEditor |
| 96 |
? wp.blockEditor.useBlockProps.save({ |
| 97 |
className: "aibui-tabs", |
| 98 |
style: { |
| 99 |
"--aibui-active": activeColor || "#4f46e5", |
| 100 |
"--aibui-active-shadow": hexToRgba(activeColor, 0.15), |
| 101 |
}, |
| 102 |
}) |
| 103 |
: {}; |
| 104 |
return ( |
| 105 |
<div {...blockProps}> |
| 106 |
<InnerBlocks.Content /> |
| 107 |
</div> |
| 108 |
); |
| 109 |
}, |
| 110 |
}); |
| 111 |
|
| 112 |
// Child Tab block |
| 113 |
registerBlockType("ai-builder/aibui-tab", { |
| 114 |
title: __("Tab", "ai-builder"), |
| 115 |
parent: ["ai-builder/aibui-tabs"], |
| 116 |
icon: "index-card", |
| 117 |
category: "widgets", |
| 118 |
supports: { reusable: false, html: false }, |
| 119 |
attributes: { |
| 120 |
title: { type: "string", default: "Tab" }, |
| 121 |
}, |
| 122 |
edit: (props) => { |
| 123 |
const { attributes, setAttributes } = props; |
| 124 |
const { title } = attributes; |
| 125 |
const blockProps = useBlockProps({ className: "aibui-tab" }); |
| 126 |
return ( |
| 127 |
<div {...blockProps}> |
| 128 |
<input |
| 129 |
className="aibui-tab-title-input" |
| 130 |
value={title} |
| 131 |
onChange={(e) => setAttributes({ title: e.target.value })} |
| 132 |
/> |
| 133 |
<div className="aibui-tab-inner"> |
| 134 |
<InnerBlocks /> |
| 135 |
</div> |
| 136 |
</div> |
| 137 |
); |
| 138 |
}, |
| 139 |
save: (props) => { |
| 140 |
const { attributes } = props; |
| 141 |
const { title } = attributes; |
| 142 |
const blockProps = wp.blockEditor |
| 143 |
? wp.blockEditor.useBlockProps.save({ className: "aibui-tab" }) |
| 144 |
: {}; |
| 145 |
return ( |
| 146 |
<div {...blockProps}> |
| 147 |
<div className="aibui-tab-panel" data-title={title}> |
| 148 |
<InnerBlocks.Content /> |
| 149 |
</div> |
| 150 |
</div> |
| 151 |
); |
| 152 |
}, |
| 153 |
}); |
| 154 |
|