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.

991 B
Raw Blame
/* Copyright 2026 Marimo. All rights reserved. */
import React, {
  Activity,
  type ActivityProps,
  type PropsWithChildren,
} from "react";

interface Props {
  isOpen: boolean;
}

/**
 * Lazy-mount until it is open for the first time
 */
export const LazyMount: React.FC<PropsWithChildren<Props>> = ({
  isOpen,
  children,
}) => {
  const [hasMountedBefore, setHasMountedBefore] = React.useState(false);

  if (isOpen && !hasMountedBefore) {
    setHasMountedBefore(true);
  }

  return hasMountedBefore || isOpen ? children : null;
};

/**
 * Wraps a component in an Activity component. It is not mounted until it is open for the first time.
 */
export const LazyActivity: React.FC<PropsWithChildren<ActivityProps>> = (
  props,
) => {
  const [hasMountedBefore, setHasMountedBefore] = React.useState(false);

  if (props.mode === "visible" && !hasMountedBefore) {
    setHasMountedBefore(true);
  }

  if (hasMountedBefore) {
    return <Activity {...props} />;
  }

  return null;
};