Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

2.1 KB
Raw Blame
const SETTINGS_SCROLL_SELECTOR = "[data-settings-scroll]";

export const SETTINGS_ANCHOR_EVENT = "deeptutor:settings-anchor";

export type SettingsAnchorEvent = CustomEvent<{ key: string }>;

/** Notify the resident settings document that a same-page anchor was chosen. */
export function requestSettingsSection(key: string): void {
  window.dispatchEvent(
    new CustomEvent(SETTINGS_ANCHOR_EVENT, { detail: { key } }),
  );
}

/**
 * Scroll a settings anchor inside the settings document only.
 *
 * Element.scrollIntoView() walks every scrollable ancestor, including the
 * browser viewport.  In the full-height app shell that can nudge the entire UI
 * upward even though only the settings document is meant to move.
 */
export function scrollToSettingsSection(
  key: string,
  behavior: ScrollBehavior = "smooth",
): boolean {
  const target = document.getElementById(key);
  return scrollToSettingsElement(target, behavior);
}

/** Reveal a detail pane without scrolling the full-height app or the browser viewport. */
export function scrollToSettingsElement(
  target: HTMLElement | null,
  behavior: ScrollBehavior = "smooth",
): boolean {
  const scroller = target?.closest<HTMLElement>(SETTINGS_SCROLL_SELECTOR);
  if (!target || !scroller) return false;

  const targetRect = target.getBoundingClientRect();
  const scrollerRect = scroller.getBoundingClientRect();
  const scrollMargin = Number.parseFloat(
    window.getComputedStyle(target).scrollMarginTop,
  );
  const top =
    scroller.scrollTop +
    targetRect.top -
    scrollerRect.top -
    scroller.clientTop -
    (Number.isFinite(scrollMargin) ? scrollMargin : 0);

  scroller.scrollTo({ top: Math.max(0, top), behavior });

  // Fragment navigation may move the root scrolling element before or after
  // the client handler runs. Keep the full-height application shell pinned
  // now and after the anchor's default-action frame.
  const resetDocumentScroll = () => {
    window.scrollTo({ top: 0, left: 0, behavior: "auto" });
    document.documentElement.scrollTop = 0;
    document.body.scrollTop = 0;
  };
  resetDocumentScroll();
  requestAnimationFrame(resetDocumentScroll);
  return true;
}