Michael Rabinovich Cursor commited on
Commit
528723d
·
1 Parent(s): 1a5a9a2

leaderboard: fix metrics 'On this page' links on mobile

Browse files

When embedded on huggingface.co the Space is in a cross-origin outer
frame that owns the scroll, so a content-sized metrics iframe couldn't be
scrolled from inside and the anchor links silently did nothing. Give the
mobile metrics iframe its own bounded height so it scrolls its own
document; scrollIntoView then works for the 'On this page' links. Desktop
unchanged.

Co-authored-by: Cursor <cursoragent@cursor.com>

Files changed (1) hide show
  1. metrics_page.py +21 -15
metrics_page.py CHANGED
@@ -112,10 +112,9 @@ figure.fig figcaption { font-size: 0.84em; color: #5b6170; margin-top: 6px;
112
  .weight-pill { font-family: monospace; font-size: 0.8em; padding: 1px 7px;
113
  border-radius: 6px; background: #eceff1; color: #37474f; }
114
  @media (max-width: 760px) {
115
- /* On phones the page flows in a single scroll (the embedding iframe is
116
- auto-sized to content, see the inline script), so the big vh end-spacer
117
- would add a screenful of blank -- shrink it to a fixed nub. Tighten the
118
- reading frame a touch too. */
119
  body { padding: 16px 14px 28px; }
120
  h1 { font-size: 1.5em; }
121
  .card { padding: 16px 16px; }
@@ -123,7 +122,6 @@ figure.fig figcaption { font-size: 0.84em; color: #5b6170; margin-top: 6px;
123
  pre.formula { font-size: 0.78em; padding: 12px 13px; }
124
  table { font-size: 0.86em; }
125
  th, td { padding: 6px 8px; }
126
- .endspace { height: 24px; }
127
  }
128
  """
129
 
@@ -374,14 +372,20 @@ def build_metrics_page() -> str:
374
  )
375
 
376
 
377
- # On phones, size the embedding iframe to the page's content so the Metrics tab
378
- # is a single page scroll instead of a nested 85vh scroll-box (the desktop tab
379
- # keeps its fixed height -- it reads well there). In-page anchor links use
380
- # scrollIntoView so they still work when the iframe has no internal scroll (it
381
- # walks up to scroll the parent frame); on the standalone /metrics route there
382
- # is no frame and the browser just scrolls normally. All wrapped in try/catch:
383
- # frameElement/parent access is fine for this same-origin iframe but must never
384
- # throw if that ever changes.
 
 
 
 
 
 
385
  _JS = """
386
  (function () {
387
  var narrow = (window.innerWidth || 1000) < 760;
@@ -389,7 +393,10 @@ _JS = """
389
  if (!narrow) return;
390
  try {
391
  var fe = window.frameElement;
392
- if (fe) fe.style.height = Math.ceil(document.documentElement.scrollHeight) + 'px';
 
 
 
393
  } catch (e) { /* keep the CSS fallback height */ }
394
  }
395
  document.addEventListener('click', function (e) {
@@ -400,7 +407,6 @@ _JS = """
400
  });
401
  window.addEventListener('load', fit);
402
  window.addEventListener('resize', function () { narrow = (window.innerWidth || 1000) < 760; fit(); });
403
- if (window.ResizeObserver) { try { new ResizeObserver(fit).observe(document.body); } catch (e) {} }
404
  fit();
405
  })();
406
  """
 
112
  .weight-pill { font-family: monospace; font-size: 0.8em; padding: 1px 7px;
113
  border-radius: 6px; background: #eceff1; color: #37474f; }
114
  @media (max-width: 760px) {
115
+ /* Tighten the reading frame for phones. The end-spacer stays (vh of the
116
+ bounded mobile iframe) so the last "On this page" target can scroll to the
117
+ top of the box. */
 
118
  body { padding: 16px 14px 28px; }
119
  h1 { font-size: 1.5em; }
120
  .card { padding: 16px 16px; }
 
122
  pre.formula { font-size: 0.78em; padding: 12px 13px; }
123
  table { font-size: 0.86em; }
124
  th, td { padding: 6px 8px; }
 
125
  }
126
  """
127
 
 
372
  )
373
 
374
 
375
+ # Mobile sizing for the embedding iframe.
376
+ #
377
+ # We deliberately give the phone iframe its OWN bounded height (an internal
378
+ # scroll) rather than auto-sizing it to the full content. Reason: when embedded
379
+ # on huggingface.co the Space sits in a cross-origin outer frame that owns the
380
+ # real scroll, so a content-sized iframe can't be scrolled from inside -- which
381
+ # silently breaks the "On this page" anchor links (scrollIntoView has nothing it
382
+ # is allowed to move). A bounded iframe scrolls its own document, so the anchors
383
+ # work again. Desktop keeps its fixed CSS height (reads well there).
384
+ #
385
+ # Anchor clicks use scrollIntoView (smooth, scrolls whichever ancestor actually
386
+ # scrolls -- the bounded iframe when embedded, the window on the standalone
387
+ # /metrics route). All wrapped in try/catch: frameElement access is fine for
388
+ # this same-origin iframe but must never throw if that ever changes.
389
  _JS = """
390
  (function () {
391
  var narrow = (window.innerWidth || 1000) < 760;
 
393
  if (!narrow) return;
394
  try {
395
  var fe = window.frameElement;
396
+ if (!fe) return; // standalone /metrics: window scrolls
397
+ var avail = (window.screen && window.screen.availHeight) || 740;
398
+ var h = Math.max(340, Math.min(640, Math.round(avail - 360)));
399
+ fe.style.height = h + 'px'; // bounded -> the iframe scrolls itself
400
  } catch (e) { /* keep the CSS fallback height */ }
401
  }
402
  document.addEventListener('click', function (e) {
 
407
  });
408
  window.addEventListener('load', fit);
409
  window.addEventListener('resize', function () { narrow = (window.innerWidth || 1000) < 760; fit(); });
 
410
  fit();
411
  })();
412
  """