diff --git a/packages/react-uploader/src/SSR/ConditionalSuspense/ConditionalSuspense.tsx b/packages/react-uploader/src/SSR/ConditionalSuspense/ConditionalSuspense.tsx new file mode 100644 index 0000000..ff57b32 --- /dev/null +++ b/packages/react-uploader/src/SSR/ConditionalSuspense/ConditionalSuspense.tsx @@ -0,0 +1,17 @@ +import React, { FC, Suspense } from 'react'; + +export type TProps = { + fallback?: React.ReactChild | React.ReactFragment | React.ReactPortal | null; + condition: boolean; + children: React.ReactNode; +}; + +export const ConditionalSuspense: FC = ({ condition, fallback, children, ...rest }) => { + return condition ? ( + + {children} + + ) : ( + <>{fallback} + ); +}; \ No newline at end of file diff --git a/packages/react-uploader/src/SSR/ConditionalSuspense/index.ts b/packages/react-uploader/src/SSR/ConditionalSuspense/index.ts new file mode 100644 index 0000000..7e1c09b --- /dev/null +++ b/packages/react-uploader/src/SSR/ConditionalSuspense/index.ts @@ -0,0 +1,2 @@ +export { ConditionalSuspense, type TProps } from './ConditionalSuspense'; +export { useIsBrowser } from './useIsBrowser'; \ No newline at end of file diff --git a/packages/react-uploader/src/SSR/ConditionalSuspense/useIsBrowser.ts b/packages/react-uploader/src/SSR/ConditionalSuspense/useIsBrowser.ts new file mode 100644 index 0000000..c7774c2 --- /dev/null +++ b/packages/react-uploader/src/SSR/ConditionalSuspense/useIsBrowser.ts @@ -0,0 +1,13 @@ +import { useState, useEffect } from 'react'; + +export const useIsBrowser = () => { + const [isBrowser, setIsBrowser] = useState(false); + + useEffect(() => { + if (typeof window !== 'undefined') { + setIsBrowser(true); + } + }, []); + + return isBrowser; +}; diff --git a/packages/react-uploader/src/Uploader/Inline/FileUploaderInline.tsx b/packages/react-uploader/src/Uploader/Inline/FileUploaderInline.tsx index 9943cff..b052fc2 100644 --- a/packages/react-uploader/src/Uploader/Inline/FileUploaderInline.tsx +++ b/packages/react-uploader/src/Uploader/Inline/FileUploaderInline.tsx @@ -7,6 +7,7 @@ import { AdapterUploadCtxProvider } from "../core/AdapterUploadCtxProvider"; import type { TProps } from "../types"; import { getCalcPropertyOfProps } from "../../utils/getCalcPropertyOfProps"; import { getUserAgentIntegration } from "../../utils/getUserAgentIntegration"; +import { ConditionalSuspense, useIsBrowser } from "../../SSR/ConditionalSuspense"; LR.registerBlocks(LR); @@ -21,6 +22,7 @@ export const FileUploaderInline: FC = ({ className, classNameUploader, apiRef, + fallback, ...props }) => { const CTX_NAME = useMemo(() => ctxName ?? LR.UID.generate(), [ctxName]); @@ -30,19 +32,23 @@ export const FileUploaderInline: FC = ({ [props], ); + const isBrowser = useIsBrowser(); + return ( -
- {/* @ts-ignore */} - - {/* @ts-ignore */} - + +
+ {/* @ts-ignore */} + + {/* @ts-ignore */} + - {/* @ts-ignore */} - -
+ {/* @ts-ignore */} + +
+ ); }; diff --git a/packages/react-uploader/src/Uploader/Minimal/FileUploaderMinimal.tsx b/packages/react-uploader/src/Uploader/Minimal/FileUploaderMinimal.tsx index 4ac5aee..9477bb2 100644 --- a/packages/react-uploader/src/Uploader/Minimal/FileUploaderMinimal.tsx +++ b/packages/react-uploader/src/Uploader/Minimal/FileUploaderMinimal.tsx @@ -7,6 +7,7 @@ import { AdapterUploadCtxProvider } from "../core/AdapterUploadCtxProvider"; import type { TProps } from "../types"; import { getCalcPropertyOfProps } from "../../utils/getCalcPropertyOfProps"; import { getUserAgentIntegration } from "../../utils/getUserAgentIntegration"; +import { ConditionalSuspense, useIsBrowser } from "../../SSR/ConditionalSuspense"; LR.registerBlocks(LR); @@ -22,6 +23,7 @@ export const FileUploaderMinimal: FC = ({ className, classNameUploader, apiRef, + fallback, ...props }) => { const CTX_NAME = useMemo(() => ctxName ?? LR.UID.generate(), [ctxName]); @@ -31,18 +33,22 @@ export const FileUploaderMinimal: FC = ({ [props], ); + const isBrowser = useIsBrowser(); + return ( -
- {/* @ts-ignore */} - - {/* @ts-ignore */} - - {/* @ts-ignore */} - -
+ +
+ {/* @ts-ignore */} + + {/* @ts-ignore */} + + {/* @ts-ignore */} + +
+
); }; diff --git a/packages/react-uploader/src/Uploader/Regular/FileUploaderRegular.tsx b/packages/react-uploader/src/Uploader/Regular/FileUploaderRegular.tsx index 52eeb3e..0b7aead 100644 --- a/packages/react-uploader/src/Uploader/Regular/FileUploaderRegular.tsx +++ b/packages/react-uploader/src/Uploader/Regular/FileUploaderRegular.tsx @@ -1,5 +1,6 @@ import React, { type FC, useMemo } from "react"; import * as LR from "@uploadcare/blocks"; +import "@uploadcare/blocks/web/lr-file-uploader-regular.min.css"; import { customElementToReactComponent } from "@uploadcare/react-adapter"; import { AdapterConfig } from "../core/AdapterConfig"; import { AdapterUploadCtxProvider } from "../core/AdapterUploadCtxProvider"; @@ -7,6 +8,7 @@ import type { TProps } from "../types"; import { getCalcPropertyOfProps } from "../../utils/getCalcPropertyOfProps"; import { getUserAgentIntegration } from "../../utils/getUserAgentIntegration"; +import { ConditionalSuspense, useIsBrowser } from "../../SSR/ConditionalSuspense"; LR.registerBlocks(LR); @@ -21,6 +23,7 @@ export const FileUploaderRegular: FC = ({ className, classNameUploader, apiRef, + fallback, ...props }) => { const CTX_NAME = useMemo(() => ctxName ?? LR.UID.generate(), [ctxName]); @@ -30,18 +33,22 @@ export const FileUploaderRegular: FC = ({ [props], ); + const isBrowser = useIsBrowser(); + return ( -
- {/* @ts-ignore */} - - {/* @ts-ignore */} - - {/* @ts-ignore */} - -
+ +
+ {/* @ts-ignore */} + + {/* @ts-ignore */} + + {/* @ts-ignore */} + +
+
); }; diff --git a/packages/react-uploader/src/Uploader/types.ts b/packages/react-uploader/src/Uploader/types.ts index 881839b..cba10b9 100644 --- a/packages/react-uploader/src/Uploader/types.ts +++ b/packages/react-uploader/src/Uploader/types.ts @@ -1,9 +1,13 @@ + import type { Ref } from "react"; import type { ConfigType, UploadCtxProvider, EventMap, } from "@uploadcare/blocks"; +import type { + TProps as TPropsConditionalSuspense +} from "../SSR/ConditionalSuspense" type TToCamelCase = S extends `${infer Head}-${infer Tail}` ? `${Lowercase}${Capitalize>}` @@ -31,7 +35,7 @@ type TDefaultProps = { className?: string; classNameUploader?: string; ctxName?: string; -}; +} & Pick; export type TProps = TDefaultProps & TRefUploadCtxProvider & diff --git a/packages/react-uploader/vite.config.js b/packages/react-uploader/vite.config.js index 1697947..fdcccdc 100644 --- a/packages/react-uploader/vite.config.js +++ b/packages/react-uploader/vite.config.js @@ -17,7 +17,7 @@ export default defineConfig({ fileName: "react-uploader", }, rollupOptions: { - external: ["react"], + external: ["react", "@uploadcare/blocks"], output: { globals: { react: "React",