Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

985 B
Raw Blame
/* Copyright 2026 Marimo. All rights reserved. */

/**
 * Scroll the element into view if it is not fully visible.
 */
export function smartScrollIntoView(
  element: HTMLElement,
  {
    offset,
    body = window,
    behavior = "smooth",
  }: {
    offset?: { top: number; bottom: number };
    body?: HTMLElement | typeof window;
    behavior?: "smooth" | "instant";
  },
) {
  const topOffset = offset?.top ?? 0;
  const bottomOffset = offset?.bottom ?? 0;
  const rect = element.getBoundingClientRect();

  if (rect.top < topOffset) {
    // Element is above the viewport, scroll to its top
    body.scrollBy({
      top: rect.top - topOffset,
      behavior,
    });
    return;
  }
  if (rect.bottom > window.innerHeight - bottomOffset) {
    // Element is below the viewport, scroll to its bottom
    body.scrollBy({
      top: rect.bottom - window.innerHeight + bottomOffset,
      behavior,
    });
    return;
  }

  // If the element is already fully in view, do nothing
}