import { lazy, useState } from 'react'; import type { Meta, StoryObj } from '@storybook/react-vite'; import { Alert, Button, Card, Skeleton, Space, Switch, Tag, Typography } from 'antd'; import LazyMount from './LazyMount'; const meta = { title: 'Utility/LazyMount', component: LazyMount, tags: ['autodocs'], parameters: { layout: 'padded', docs: { description: { component: 'Mounts its children the first time `when` becomes true and keeps them mounted afterwards, wrapped in Suspense. The panel pairs it with React.lazy modal imports on heavy list pages so modals load on demand while their close animations still play.', }, }, }, argTypes: { when: { description: 'Children mount the first time this becomes true and stay mounted afterwards.' }, fallback: { description: 'Suspense fallback shown while a React.lazy child is still loading.' }, children: { description: 'Content to mount on demand, typically a lazily imported modal.' }, }, } satisfies Meta; export default meta; type Story = StoryObj; function MountBadge() { const [mountedAt] = useState(() => new Date().toLocaleTimeString()); return mounted at {mountedAt}; } function OnDemandDemo() { const [visible, setVisible] = useState(false); return ( when = {String(visible)} Traffic: 42.7 GB of 100 GB vless://b831381d-6324-4d53-ad4f-8cda48b30811@vpn.example.com:443?type=tcp&security=reality&fp=chrome&sni=yahoo.com#alice The card mounts the first time the switch turns on and stays mounted after turning it off; the mount time never changes. ); } const xrayConfigSnippet = JSON.stringify( { inbounds: [ { tag: 'inbound-443', protocol: 'vless', port: 443, settings: { clients: [{ id: 'b831381d-6324-4d53-ad4f-8cda48b30811', email: 'alice@corp.example', flow: 'xtls-rprx-vision' }], decryption: 'none', }, streamSettings: { network: 'tcp', security: 'reality' }, }, ], }, null, 2, ); function XrayConfigPreview() { return (
{xrayConfigSnippet}
); } const SlowXrayConfigPreview = lazy( () => new Promise<{ default: typeof XrayConfigPreview }>((resolve) => { setTimeout(() => resolve({ default: XrayConfigPreview }), 1200); }), ); function LazyChildDemo() { const [open, setOpen] = useState(false); return ( }> ); } const placeholderArgs = { when: false, children: null, }; export const MountOnDemand: Story = { args: placeholderArgs, render: () => , }; export const LazyChildWithFallback: Story = { args: placeholderArgs, render: () => , }; export const MountedImmediately: Story = { args: { when: true, children: ( ), }, };