Benat Froemming-Aldanondo commited on
Commit
d8bb7ea
·
1 Parent(s): 6b133f1

Update scroll behavior

Browse files
Files changed (2) hide show
  1. script.js +10 -36
  2. style.css +2 -5
script.js CHANGED
@@ -268,48 +268,22 @@ function setupChatScroll() {
268
  const chatDisplay = document.getElementById('chat-display');
269
  if (!chatDisplay) { setTimeout(setupChatScroll, 200); return; }
270
 
271
- function updateScroll() {
 
 
272
  const wrap = document.getElementById('chat-scroll-wrap');
273
  if (!wrap) return;
274
-
275
- // Force a layout recalc before measuring
276
- void wrap.offsetHeight;
277
-
278
- const needsScroll = wrap.scrollHeight > wrap.clientHeight + 2;
279
- wrap.classList.toggle('can-scroll', needsScroll);
280
-
281
- if (needsScroll) {
282
- wrap.scrollTop = wrap.scrollHeight;
283
- }
284
  }
285
 
286
  function onMutate() {
287
- // Give browser time to render + load images
288
- setTimeout(() => {
289
- const images = chatDisplay.querySelectorAll('img');
290
- let pending = 0;
291
- images.forEach(img => {
292
- if (!img.complete) pending++;
293
- });
294
- if (pending === 0) {
295
- updateScroll();
296
- return;
297
- }
298
- let done = 0;
299
- images.forEach(img => {
300
- if (img.complete) return;
301
- const finish = () => {
302
- done++;
303
- if (done >= pending) updateScroll();
304
- };
305
- img.addEventListener('load', finish, { once: true });
306
- img.addEventListener('error', finish, { once: true });
307
- });
308
- }, 50);
309
  }
310
 
311
  const observer = new MutationObserver(onMutate);
312
  observer.observe(chatDisplay, { childList: true, subtree: true });
313
- window.addEventListener('resize', updateScroll);
314
- }
315
- setupChatScroll();
 
268
  const chatDisplay = document.getElementById('chat-display');
269
  if (!chatDisplay) { setTimeout(setupChatScroll, 200); return; }
270
 
271
+ // Always keep overflow-y: auto — no toggling, no flash
272
+ // Just hide the scrollbar visually when not needed via CSS instead
273
+ function scrollToBottom() {
274
  const wrap = document.getElementById('chat-scroll-wrap');
275
  if (!wrap) return;
276
+ wrap.scrollTop = wrap.scrollHeight;
 
 
 
 
 
 
 
 
 
277
  }
278
 
279
  function onMutate() {
280
+ // Use rAF x2 to ensure the browser has painted the new content
281
+ requestAnimationFrame(() => {
282
+ requestAnimationFrame(scrollToBottom);
283
+ });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
284
  }
285
 
286
  const observer = new MutationObserver(onMutate);
287
  observer.observe(chatDisplay, { childList: true, subtree: true });
288
+ window.addEventListener('resize', scrollToBottom);
289
+ }
 
style.css CHANGED
@@ -148,16 +148,13 @@ footer, .svelte-1ipelgc, .built-with { display: none !important; }
148
  #chat-scroll-wrap {
149
  flex: 1;
150
  min-height: 0;
151
- overflow-y: hidden;
152
  padding: 0 16px;
153
  display: flex;
154
  flex-direction: column;
155
  gap: 10px;
156
  scrollbar-width: thin;
157
- scrollbar-color: #1e2330 transparent;
158
- }
159
- #chat-scroll-wrap.can-scroll {
160
- overflow-y: auto;
161
  }
162
 
163
  /* Hide the Gradio HTML component wrapper padding */
 
148
  #chat-scroll-wrap {
149
  flex: 1;
150
  min-height: 0;
151
+ overflow-y: auto;
152
  padding: 0 16px;
153
  display: flex;
154
  flex-direction: column;
155
  gap: 10px;
156
  scrollbar-width: thin;
157
+ scrollbar-color: #2a3040 transparent;
 
 
 
158
  }
159
 
160
  /* Hide the Gradio HTML component wrapper padding */