diff --git a/crates/im-web/assets/main.css b/crates/im-web/assets/main.css index d8360dd..bd029d5 100644 --- a/crates/im-web/assets/main.css +++ b/crates/im-web/assets/main.css @@ -477,6 +477,32 @@ body:has(.auth-stage) > .auth-stage::-webkit-scrollbar { border-radius: var(--radius); } +.profile-upload-row { + display: flex; + gap: 8px; + align-items: center; +} + +.upload-progress { + flex: 1; + height: 2px; + background: var(--panel-sunken); + border-radius: 1px; + overflow: hidden; +} + +.upload-progress-fill { + height: 100%; + width: 0; + background: var(--accent); + transition: width var(--dur-fast) var(--ease); +} + +.upload-cancel { + height: 24px; + padding: 0 8px; +} + .profile-stats { margin: 0; display: flex; diff --git a/crates/im-web/src/layout.rs b/crates/im-web/src/layout.rs index 731203e..d95e4be 100644 --- a/crates/im-web/src/layout.rs +++ b/crates/im-web/src/layout.rs @@ -95,7 +95,50 @@ pub async fn avatar_script(cx: &Cx) -> Result { }); \ document.addEventListener('change', function (e) { \ var input = e.target.closest ? e.target.closest('[data-autosubmit]') : null; \ - if (input && input.form) { input.form.submit(); } \ + if (!input || !input.form || input.form.__imUploading) { return; } \ + var form = input.form; \ + var head = input.closest('.profile-head'); \ + if (!head) { form.submit(); return; } \ + /* Captured before the input is disabled — a disabled control \ + is no longer listed, and the post would carry no file. */ \ + var data = new FormData(form); \ + form.__imUploading = true; \ + input.disabled = true; \ + var row = document.createElement('div'); \ + row.className = 'profile-upload-row'; \ + var bar = document.createElement('div'); \ + bar.className = 'upload-progress'; \ + var fill = document.createElement('div'); \ + fill.className = 'upload-progress-fill'; \ + bar.appendChild(fill); \ + row.appendChild(bar); \ + var cancel = document.createElement('button'); \ + cancel.type = 'button'; \ + cancel.className = 'admin-action upload-cancel'; \ + cancel.textContent = '\\u00d7'; \ + cancel.setAttribute('aria-label', 'Cancel upload'); \ + row.appendChild(cancel); \ + head.insertAdjacentElement('afterend', row); \ + var settle = function () { \ + form.__imUploading = false; \ + input.disabled = false; \ + if (row.parentNode) { row.parentNode.removeChild(row); } \ + }; \ + var x = new XMLHttpRequest(); \ + x.open('POST', form.getAttribute('action')); \ + x.upload.onprogress = function (ev) { \ + if (ev.lengthComputable && ev.total > 0) { \ + fill.style.width = Math.min(100, Math.round((ev.loaded / ev.total) * 100)) + '%'; \ + } \ + }; \ + x.onload = function () { \ + settle(); \ + window.location.href = x.responseURL || form.getAttribute('action'); \ + }; \ + x.onerror = function () { settle(); form.submit(); }; \ + x.onabort = function () { input.value = ''; settle(); }; \ + cancel.addEventListener('click', function () { x.abort(); }); \ + x.send(data); \ }); \ })();"; view! { cx => } diff --git a/style/main.scss b/style/main.scss index 688370f..f16a9d8 100644 --- a/style/main.scss +++ b/style/main.scss @@ -538,6 +538,34 @@ body:has(.auth-stage) { border-radius: var(--radius); } +// Upload progress: a client-made hairline under the profile head while the +// photo crosses, a quiet cancel beside it — no native chrome. +.profile-upload-row { + display: flex; + gap: 8px; + align-items: center; +} + +.upload-progress { + flex: 1; + height: 2px; + background: var(--panel-sunken); + border-radius: 1px; + overflow: hidden; +} + +.upload-progress-fill { + height: 100%; + width: 0; + background: var(--accent); + transition: width var(--dur-fast) var(--ease); +} + +.upload-cancel { + height: 24px; + padding: 0 8px; +} + .profile-stats { margin: 0; display: flex;