Files
shared/packages/media-shared-assets/public/js/ui/toast.js
T
Gabriel20xxandClaude Sonnet 5 79d9461769 Add media-shared-assets package (from MediaCollection/MediaShared)
Browser theme CSS/JS assets shared across MediaCollection's client apps,
moved here per owner request to centralize shared components. The
build:theme sync script now lives inside the package itself rather than
assuming a monorepo-root scripts/ sibling; its 'test' script is a no-op
placeholder since the package has no tests of its own yet.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-31 21:45:47 +02:00

74 lines
3.1 KiB
JavaScript

(function () {
if (window.toast) return;
const rootId = 'toast-root';
function ensureRoot() {
let el = document.getElementById(rootId);
if (!el) {
el = document.createElement('div');
el.id = rootId;
el.setAttribute('data-toast-root', '1');
el.className =
'fixed right-3 z-[10050] flex w-[min(340px,calc(100vw-1.5rem))] flex-col gap-2 hidden';
el.style.top = 'calc(var(--bar-height, 56px) + 0.75rem)';
document.body.appendChild(el);
}
return el;
}
function create(type, message, opts = {}) {
const root = ensureRoot();
const ttl = opts.duration ?? 4800;
const title =
opts.title ||
{ info: 'Info', success: 'Success', warn: 'Warning', error: 'Error' }[type] ||
'Notice';
const accent =
{
info: 'var(--color-info)',
success: 'var(--color-success)',
warn: 'var(--color-warning)',
error: 'var(--color-danger)',
}[type] || 'var(--color-accent)';
const el = document.createElement('div');
el.setAttribute('data-toast-item', '1');
el.className =
'relative isolate flex items-start gap-3 overflow-hidden rounded-md border border-[var(--color-border)] border-l-4 bg-[var(--color-surface)] px-4 py-3 text-[var(--font-size-sm)] leading-[1.3] text-[var(--color-text)] shadow-[var(--shadow-lg)]';
el.style.setProperty('--_accent', accent);
el.style.borderLeftColor = 'var(--_accent)';
el.setAttribute('role', 'status');
el.setAttribute('aria-live', 'polite');
el.innerHTML = `<div data-toast-body="1" class="flex-1 text-left"><h4 class="mb-1 text-[var(--font-size-sm)] font-semibold tracking-[0.3px]">${title}</h4><div data-toast-msg="1">${message}</div></div><button data-toast-close="1" class="absolute right-1.5 top-1.5 rounded bg-transparent p-2 text-sm leading-none text-[var(--color-text-dim)] hover:bg-[var(--color-surface-alt)] hover:text-[var(--color-text)]" aria-label="Dismiss">✕</button><div data-toast-progress="1"></div>`;
el.style.setProperty('--toast-ttl', `${ttl}ms`);
const progress = el.querySelector('[data-toast-progress]');
if (progress) {
progress.className =
'absolute bottom-0 left-0 h-[3px] w-full origin-left bg-[var(--_accent)]';
progress.style.animation = 'toast-progress linear forwards';
progress.style.animationDuration = `${ttl}ms`;
}
const closeBtn = el.querySelector('[data-toast-close]');
let closed = false;
function remove() {
if (closed) return;
closed = true;
el.style.animation = 'toast-out .35s forwards';
setTimeout(() => {
el.remove();
if (root.childElementCount === 0) root.classList.add('hidden');
}, 340);
}
closeBtn.addEventListener('click', remove);
if (ttl > 0) {
setTimeout(remove, ttl);
}
root.classList.remove('hidden');
root.appendChild(el);
return { remove };
}
window.toast = {
info: (m, o) => create('info', m, o),
success: (m, o) => create('success', m, o),
warn: (m, o) => create('warn', m, o),
error: (m, o) => create('error', m, o),
};
})();