Hi Dean, try this out.
- Add a custom HTML field with empty contents. Preview the form and inspect the page to get it's component-id (e.g. `dc2fcecc-efe9-4633-993a-d66a3beecf65`), copy and save this.
Add custom JS, note to replace the TOAST_COMPONENT_ID.
const TOAST_COMPONENT_ID = 'dc2fcecc-efe9-4633-993a-d66a3beecf65';
const TOAST_COLORS = {
success: ['#e8fbf6', '#0eb887'],
info: ['#eff8ff', '#5db4ff'],
warning: ['#fff8e5', '#ffb600'],
error: ['#ffeeef', '#ff5963'],
};
window.showToast = (message, type, duration) => {
const [bg, border] = TOAST_COLORS[type] ?? TOAST_COLORS.success;
const closeCode = `LFForm.changeFieldSettings({componentId:'${TOAST_COMPONENT_ID}'},{default:''})`;
const html =
`<div style="position:fixed;top:20px;left:0;right:0;margin:0 auto;min-width:320px;max-width:calc(100% -
320px);width:fit-content;z-index:2000;font-weight:600;font-size:16px;">` +
`<div style="padding:.75rem 1.25rem;padding-right:2.5rem;border:1px solid
${border};border-radius:.25rem;min-height:20px;display:flex;align-items:center;position:relative;color:#000;background:${bg};">` +
`<span style="margin-right:20px;">${message}</span>` +
`<button onclick="${closeCode}" title="Close"
style="position:absolute;right:8px;top:8px;background:transparent;border:none;cursor:pointer;padding:4px;display:flex;align-items:center;opacity:.6;">` +
`<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="#000" stroke-width="2.5" stroke-linecap="round"><line x1="18" y1="6" x2="6"
y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></svg>` +
`</button></div></div>`;
LFForm.changeFieldSettings({
componentId: TOAST_COMPONENT_ID
}, {
default:
html
});
if (duration > 0) {
setTimeout(() => LFForm.changeFieldSettings({
componentId: TOAST_COMPONENT_ID
}, {
default:
''
}), duration);
}
};
window.closeToast = () =>
LFForm.changeFieldSettings({
componentId: TOAST_COMPONENT_ID
}, {
default:
''
});
// Show on form load
window.showToast('Test load from toast', 'success', 5000);
The example above will load the toast on form load. Or we can also trigger it via custom JS such as button clicks.
<button onclick="showToast('Your form has been saved.', 'success', 0)" style="padding:6px 16px;margin-right:8px;cursor:pointer;">Open Toast</button>
<button onclick="closeToast()" style="padding:6px 16px;margin-right:8px;cursor:pointer;">Close Toast</button>
<button onclick="showToast('Your form has been saved.', 'success', 3000)" style="padding:6px 16px;cursor:pointer;">Open Toast (3s)</button>
The toast style can be modified as needed, but I've kept it similar to the default forms one.