forked from toss/suspensive
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react): add experimental DelaySuspense in v1.8.1
- Loading branch information
Showing
22 changed files
with
210 additions
and
60 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file removed
BIN
-14.4 KB
.yarn/cache/@suspensive-react-query-npm-1.7.3-150c153164-4837bc5f86.zip
Binary file not shown.
Binary file added
BIN
+16.6 KB
.yarn/cache/@suspensive-react-query-npm-1.8.0-a72d19915b-b1f350f4f4.zip
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@suspensive/react", | ||
"version": "1.7.4", | ||
"version": "1.8.1", | ||
"keywords": [ | ||
"suspensive", | ||
"react" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { ComponentProps, ComponentType, useEffect, useState } from 'react' | ||
import { Suspense } from './Suspense' | ||
import { ComponentPropsWithoutChildren } from './types' | ||
|
||
type DelaySuspenseProps = ComponentProps<typeof Suspense> & { | ||
ms?: number | ||
} | ||
|
||
const DefaultDelaySuspense = (props: DelaySuspenseProps) => ( | ||
<Suspense {...props} fallback={<Delay ms={props.ms}>{props.fallback}</Delay>} /> | ||
) | ||
if (process.env.NODE_ENV !== 'production') { | ||
DefaultDelaySuspense.displayName = 'DelaySuspense' | ||
} | ||
const CSROnlyDelaySuspense = (props: DelaySuspenseProps) => ( | ||
<Suspense.CSROnly {...props} fallback={<Delay ms={props.ms}>{props.fallback}</Delay>} /> | ||
) | ||
if (process.env.NODE_ENV !== 'production') { | ||
CSROnlyDelaySuspense.displayName = 'DelaySuspense.CSROnly' | ||
} | ||
|
||
const Delay = ({ ms = 0, children }: Pick<DelaySuspenseProps, 'ms'> & { children: DelaySuspenseProps['fallback'] }) => { | ||
const [isDelayed, setIsDelayed] = useState(ms === 0) | ||
|
||
useEffect(() => { | ||
const timerId = setTimeout(() => !isDelayed && setIsDelayed(true), ms) | ||
return () => clearTimeout(timerId) | ||
}, []) | ||
|
||
return <>{isDelayed ? children : null}</> | ||
} | ||
|
||
/** | ||
* This component can accept delay time(ms) as prop to prevent to show fallback directly when children is suspended | ||
* @experimental This component can be renamed itself or be merged to default Suspense of suspensive/react. | ||
*/ | ||
export const DelaySuspense = DefaultDelaySuspense as typeof DefaultDelaySuspense & { | ||
/** | ||
* CSROnly mode make DelaySuspense can be used in SSR framework like Next.js with React 17 or under | ||
* @experimental This component can be renamed itself or be merged to default Suspense of suspensive/react. | ||
*/ | ||
CSROnly: typeof CSROnlyDelaySuspense | ||
} | ||
DelaySuspense.CSROnly = CSROnlyDelaySuspense | ||
|
||
export function withDelaySuspense<Props extends Record<string, unknown> = Record<string, never>>( | ||
Component: ComponentType<Props>, | ||
suspenseProps?: ComponentPropsWithoutChildren<typeof DelaySuspense> | ||
) { | ||
const Wrapped = (props: Props) => ( | ||
<DelaySuspense {...suspenseProps}> | ||
<Component {...props} /> | ||
</DelaySuspense> | ||
) | ||
|
||
if (process.env.NODE_ENV !== 'production') { | ||
const name = Component.displayName || Component.name || 'Component' | ||
Wrapped.displayName = `withDelaySuspense(${name})` | ||
} | ||
|
||
return Wrapped | ||
} | ||
|
||
withDelaySuspense.CSROnly = function withDelaySuspenseCSROnly< | ||
Props extends Record<string, unknown> = Record<string, never> | ||
>(Component: ComponentType<Props>, suspenseProps?: ComponentPropsWithoutChildren<typeof DelaySuspense.CSROnly>) { | ||
const Wrapped = (props: Props) => ( | ||
<DelaySuspense.CSROnly {...suspenseProps}> | ||
<Component {...props} /> | ||
</DelaySuspense.CSROnly> | ||
) | ||
|
||
if (process.env.NODE_ENV !== 'production') { | ||
const name = Component.displayName || Component.name || 'Component' | ||
Wrapped.displayName = `withDelaySuspense.CSROnly(${name})` | ||
} | ||
|
||
return Wrapped | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
export { default as isDifferentArray } from './isDifferentArray' | ||
export { default as isDevelopment } from './isDevelopment' |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 28 additions & 8 deletions
36
websites/visualization/components/forPlayground/suspensive.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.