Skip to content

The open alternative to Salesforce, designed for AI.

4.1 KB
Raw Blame
// The product hero's scroll choreography, as one pure function of the
// track's geometry. Every keyframe below is the old site's law; jest pins
// them. The component layer writes the continuous values to CSS custom
// properties and keeps React state only for the discrete flips.

import { clampProgress } from '@/platform/motion';
import { MENU_HEIGHT_PX, MENU_WIPE } from '@/tokens';

type HeroScrollInput = {
  // getBoundingClientRect().top of the 200vh track.
  trackTop: number;
  // The track's full height (offsetHeight).
  trackHeight: number;
  viewportHeight: number;
};

export type HeroScrollModel = {
  aiPanelProgress: number;
  aiPlaybackEnabled: boolean;
  aiPointerEventsEnabled: boolean;
  controlsMenu: boolean;
  heroAtStart: boolean;
  introCursorsActive: boolean;
  isCrossing: boolean;
  menuBackground: string;
  menuElevated: boolean;
  menuDark: boolean;
  morphProgress: number;
  navProgress: number;
  selectorRevealProgress: number;
  selectorRevealReady: boolean;
  stackAppearProgress: number;
  stackAlignProgress: number;
  stackSpreadEasedProgress: number;
  stackSpreadProgress: number;
};

// The morph plays out over the first 55% of the track's scrollable run.
const MORPH_START = 0;
const MORPH_END = 0.55;

// The Ask-AI panel holds back until the wipe has fully risen, then slides
// in during the post-morph hold as its own beat — so the eye, having
// followed the black edge up, lands on the panel instead of competing
// with it. Keyed to raw scroll progress: morph is pinned at 1 through
// this whole stretch and so can't time anything past MORPH_END.
const AI_PANEL_REVEAL_START = 0.6;
const AI_PANEL_REVEAL_END = 0.7;

function smoothstep(value: number): number {
  return value * value * (3 - 2 * value);
}

function mapToMorphProgress(scrollProgress: number): number {
  if (scrollProgress <= MORPH_START) return 0;
  if (scrollProgress >= MORPH_END) return 1;

  const linear = (scrollProgress - MORPH_START) / (MORPH_END - MORPH_START);

  return smoothstep(linear);
}

export function computeHeroScrollModel({
  trackTop,
  trackHeight,
  viewportHeight,
}: HeroScrollInput): HeroScrollModel {
  const scrollableDistance = trackHeight - viewportHeight;
  const progress =
    scrollableDistance <= 0 ? 0 : clampProgress(-trackTop / scrollableDistance);
  const morphProgress = mapToMorphProgress(progress);

  // The wipe line descends as the dark layer reveals; the menu restyles
  // while that line crosses the nav band, then settles dark.
  const wipeLineY = viewportHeight * (1 - morphProgress);
  const trackBottom = trackTop + trackHeight;

  let navProgress = 0;
  if (wipeLineY <= 0) {
    navProgress = 1;
  } else if (wipeLineY < MENU_HEIGHT_PX) {
    navProgress = smoothstep(1 - wipeLineY / MENU_HEIGHT_PX);
  }

  if (trackBottom <= 0) {
    navProgress = 0;
  } else if (trackBottom < MENU_HEIGHT_PX) {
    navProgress *= smoothstep(trackBottom / MENU_HEIGHT_PX);
  }

  const isCrossing =
    morphProgress < 1 &&
    wipeLineY <= MENU_HEIGHT_PX &&
    trackBottom > MENU_HEIGHT_PX;

  const menuBackground = isCrossing
    ? MENU_WIPE.transparent
    : MENU_WIPE.backgroundAt(navProgress);

  const stackSpreadProgress = clampProgress((morphProgress - 0.66) / 0.27);
  const selectorRevealProgress = clampProgress((morphProgress - 0.94) / 0.06);

  return {
    aiPanelProgress: clampProgress(
      (progress - AI_PANEL_REVEAL_START) /
        (AI_PANEL_REVEAL_END - AI_PANEL_REVEAL_START),
    ),
    aiPlaybackEnabled: progress >= AI_PANEL_REVEAL_END,
    aiPointerEventsEnabled: morphProgress > 0.5,
    controlsMenu: trackBottom > MENU_HEIGHT_PX,
    heroAtStart: morphProgress <= 0,
    introCursorsActive: morphProgress < 0.5,
    isCrossing,
    menuBackground,
    menuElevated: navProgress < 0.02 && !isCrossing,
    menuDark: navProgress >= 0.5,
    morphProgress,
    navProgress,
    selectorRevealProgress,
    selectorRevealReady: selectorRevealProgress > 0.96,
    stackAppearProgress: clampProgress((morphProgress - 0.4) / 0.16),
    stackAlignProgress: clampProgress((morphProgress - 0.62) / 0.04),
    stackSpreadEasedProgress: 1 - Math.pow(1 - stackSpreadProgress, 2.6),
    stackSpreadProgress,
  };
}