@suspensive/react
Version:
Suspensive interfaces for react
1 lines • 3.13 kB
Source Map (JSON)
{"version":3,"file":"Delay.cjs","names":["Message_Delay_ms_prop_should_be_greater_than_or_equal_to_0","DelayDefaultPropsContext"],"sources":["../src/Delay.tsx"],"sourcesContent":["'use client'\nimport { type ComponentProps, type ComponentType, type ReactNode, useContext, useState } from 'react'\nimport { DelayDefaultPropsContext } from './contexts/DefaultPropsContexts'\nimport { useTimeout } from './hooks/useTimeout'\nimport { Message_Delay_ms_prop_should_be_greater_than_or_equal_to_0, SuspensiveError } from './models/SuspensiveError'\nimport type { PropsWithoutChildren } from './utility-types/PropsWithoutChildren'\n\nexport type DelayProps =\n | {\n ms?: number\n fallback?: never\n children?: ({ isDelayed }: { isDelayed: boolean }) => ReactNode\n }\n | {\n ms?: number\n fallback?: ReactNode\n children?: ReactNode\n }\n\n/**\n * This component delays the rendering of its children for a specified duration.\n *\n * The Delay component provides a way to introduce intentional delays in rendering,\n * which can be useful for loading states or animations.\n * @see {@link https://suspensive.org/docs/react/Delay Suspensive Docs}\n */\nexport const Delay = Object.assign(\n (props: DelayProps) => {\n if (process.env.NODE_ENV === 'development' && typeof props.ms === 'number') {\n SuspensiveError.assert(props.ms >= 0, Message_Delay_ms_prop_should_be_greater_than_or_equal_to_0)\n }\n const defaultProps = useContext(DelayDefaultPropsContext)\n const ms = props.ms ?? defaultProps.ms ?? 0\n\n const [isDelayed, setIsDelayed] = useState(ms <= 0)\n useTimeout(() => setIsDelayed(true), ms)\n\n if (typeof props.children === 'function') {\n return <>{props.children({ isDelayed })}</>\n }\n\n if (isDelayed) {\n return <>{props.children}</>\n }\n if (props.fallback === undefined) {\n return <>{defaultProps.fallback}</>\n }\n return <>{props.fallback}</>\n },\n {\n displayName: 'Delay',\n with: <TProps extends ComponentProps<ComponentType> = Record<string, never>>(\n delayProps: PropsWithoutChildren<DelayProps>,\n Component: ComponentType<TProps>\n ) =>\n Object.assign(\n (props: TProps) => (\n <Delay {...delayProps}>\n <Component {...props} />\n </Delay>\n ),\n { displayName: `${Delay.displayName}.with(${Component.displayName || Component.name || 'Component'})` }\n ),\n }\n)\n"],"mappings":";;;;;;;;;;;;;;;;;;AA0BA,MAAa,QAAQ,OAAO,QACzB,UAAsB;;CACrB,IAAI,QAAQ,IAAI,aAAa,iBAAiB,OAAO,MAAM,OAAO,UAChE,wCAAgB,OAAO,MAAM,MAAM,GAAGA,kFAA0D;CAElG,MAAM,qCAA0BC,qDAAwB;CACxD,MAAM,0BAAK,MAAM,mDAAM,aAAa,yCAAM;CAE1C,MAAM,CAAC,WAAW,oCAAyB,MAAM,CAAC;CAClD,oCAAiB,aAAa,IAAI,GAAG,EAAE;CAEvC,IAAI,OAAO,MAAM,aAAa,YAC5B,OAAO,mFAAG,MAAM,SAAS,EAAE,UAAU,CAAC,EAAI;CAG5C,IAAI,WACF,OAAO,mFAAG,MAAM,SAAW;CAE7B,IAAI,MAAM,aAAa,QACrB,OAAO,mFAAG,aAAa,SAAW;CAEpC,OAAO,mFAAG,MAAM,SAAW;AAC7B,GACA;CACE,aAAa;CACb,OACE,YACA,cAEA,OAAO,QACJ,UACC,2CAAC,qFAAU,6BACT,2CAAC,oDAAc,KAAQ,IAClB,IAET,EAAE,aAAa,GAAG,MAAM,YAAY,QAAQ,UAAU,eAAe,UAAU,QAAQ,YAAY,GAAG,CACxG;AACJ,CACF"}