Skip to content

Production-ready animations, transitions, backgrounds, and scenes for Remotion

1.3 KB
Raw Blame
"use client";

import {
  type SwitchState,
  type SwitchStyle,
  switchStyle,
  switchStyleContext,
} from "@/components/remocn/switch";
import {
  easings,
  mixOklch,
  type RemocnTheme,
  type Step,
  useRemocnTheme,
  useStateTransition,
} from "@/lib/remocn-ui";

export const DEFAULT_DURATION = 10;

export function tweenSwitchStyle(
  a: SwitchStyle,
  b: SwitchStyle,
  t: number,
): SwitchStyle {
  return {
    trackBackground: mixOklch(a.trackBackground, b.trackBackground, t),
    thumbOffset: a.thumbOffset + (b.thumbOffset - a.thumbOffset) * t,
  };
}

export interface SwitchTransitionOptions {
  theme?: Partial<RemocnTheme>;
  mode?: "light" | "dark";
  primary?: string;
  speed?: number;
  defaultDuration?: number;
}

export function useSwitchTransition(
  steps: Step<SwitchState>[],
  opts: SwitchTransitionOptions = {},
): SwitchStyle {
  const {
    theme: themeOverride,
    mode,
    primary,
    speed = 1,
    defaultDuration = DEFAULT_DURATION,
  } = opts;
  const theme = useRemocnTheme(
    { ...themeOverride, ...(primary ? { primary } : {}) },
    mode,
  );
  const ctx = switchStyleContext(theme);
  const { from, to, progress } = useStateTransition(
    steps,
    "unchecked",
    speed,
    defaultDuration,
  );
  const t = easings.out(progress);
  return tweenSwitchStyle(switchStyle(from, ctx), switchStyle(to, ctx), t);
}