const { registerBlockType } = wp.blocks; const { __ } = wp.i18n; const { PanelBody, TextControl, Button, Notice, ToggleControl, RangeControl } = wp.components; const { InspectorControls, useBlockProps } = wp.blockEditor || wp.editor; // Use HTML5 color input instead of WordPress ColorPicker to avoid compatibility issues const SafeColorPicker = ({ color, onChange, disableAlpha }) => (
onChange && onChange(e.target.value)} style={{ width: "100%", height: "40px", border: "1px solid #ddd", borderRadius: "4px", cursor: "pointer", }} />
); registerBlockType("ai-builder/aibui-snackbar", { title: __("Snackbar (AI Builder)", "ai-builder"), icon: "info", category: "widgets", supports: { align: ["wide", "full"], html: false }, attributes: { message: { type: "string", default: "This is a snackbar message" }, buttons: { type: "array", default: [ { id: "btn1", text: "Action", textColor: "#ffffff", backgroundColor: "#4f46e5", link: "", autoClose: true, }, ], }, globalAutoClose: { type: "boolean", default: true }, globalCloseDelay: { type: "number", default: 5000 }, }, edit: (props) => { const { attributes, setAttributes } = props; const { message, buttons, globalAutoClose, globalCloseDelay } = attributes; const blockProps = useBlockProps({ className: "aibui-snackbar" }); const addButton = () => { const newButton = { id: `btn${Date.now()}`, text: "New Button", textColor: "#ffffff", backgroundColor: "#4f46e5", link: "", autoClose: true, }; setAttributes({ buttons: [...buttons, newButton], }); }; const removeButton = (buttonId) => { setAttributes({ buttons: buttons.filter((btn) => btn.id !== buttonId), }); }; const updateButton = (buttonId, field, value) => { setAttributes({ buttons: buttons.map((btn) => btn.id === buttonId ? { ...btn, [field]: value } : btn ), }); }; return (
setAttributes({ message: val || "" })} /> setAttributes({ globalAutoClose: !!val })} help={__( "When enabled, the snackbar will auto-hide after the specified delay", "ai-builder" )} /> { const numValue = parseInt(val) || 0; const clampedValue = Math.max(0, Math.min(60, numValue)); setAttributes({ globalCloseDelay: clampedValue * 1000 }); }} type="number" min="0" max="60" help={__("", "ai-builder")} />

{__("Buttons", "ai-builder")}

{buttons.map((button, index) => (
{__("Button", "ai-builder")} {index + 1}
updateButton(button.id, "text", val || "") } /> updateButton(button.id, "link", val || "") } help={__("Leave empty for no link", "ai-builder")} />

{__("Text Color", "ai-builder")}

updateButton(button.id, "textColor", val || "#ffffff") } />

{__("Background Color", "ai-builder")}

updateButton( button.id, "backgroundColor", val || "#4f46e5" ) } />
updateButton(button.id, "autoClose", !!val) } style={{ marginTop: "8px" }} help={__( "When enabled, clicking this button will close the snackbar", "ai-builder" )} />
))}
); }, save: (props) => { const { attributes } = props; const { message, buttons, globalAutoClose, globalCloseDelay } = attributes; const blockProps = wp.blockEditor ? wp.blockEditor.useBlockProps.save({ className: "aibui-snackbar" }) : {}; return (
{message}
{buttons.map((button) => { const buttonElement = ( ); return button.link ? ( {buttonElement} ) : ( buttonElement ); })}
); }, }); function SnackbarPreview({ message, buttons }) { return (
{message}
{buttons.map((button) => { const buttonElement = ( ); return button.link ? ( {buttonElement} ) : ( buttonElement ); })}
); }