mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-11-12 16:15:02 +03:00
1796352600
While a lazily loaded dialog's code downloaded, LazyMount's default Spin fallback was laid out beside the page content, narrowing it and poking past the right edge, so scrollbars flashed. The default fallback is now a fullscreen Spin outside the layout; a story pins the content width while the dialog loads.
24 lines
767 B
TypeScript
24 lines
767 B
TypeScript
import { Suspense, useState, type ReactNode } from 'react';
|
|
import { Spin } from 'antd';
|
|
|
|
interface LazyMountProps {
|
|
when: boolean;
|
|
fallback?: ReactNode;
|
|
children: ReactNode;
|
|
}
|
|
|
|
// Mounts children only after `when` first becomes true and keeps them mounted
|
|
// thereafter, so React.lazy modals get loaded on demand but their close
|
|
// animations still play out. Pair with `lazy(() => import(...))` modal imports
|
|
// on heavy list pages to keep the initial bundle small.
|
|
export default function LazyMount({
|
|
when,
|
|
fallback = <Spin fullscreen />,
|
|
children,
|
|
}: LazyMountProps) {
|
|
const [mounted, setMounted] = useState(when);
|
|
if (when && !mounted) setMounted(true);
|
|
if (!mounted) return null;
|
|
return <Suspense fallback={fallback}>{children}</Suspense>;
|
|
}
|