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(Delay, SuspensiveProvider): add new experimental features
- Loading branch information
Showing
15 changed files
with
167 additions
and
130 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { ComponentType, ReactNode, createContext, useContext, useEffect, useState } from 'react' | ||
import { ComponentPropsWithoutChildren } from './types' | ||
|
||
export const DelayContext = createContext<{ ms?: number }>({ ms: 0 }) | ||
|
||
type DelayProps = { ms?: number; children: ReactNode } | ||
/** | ||
* @experimental This is experimental feature. | ||
*/ | ||
export const Delay = ({ ms, children }: DelayProps) => { | ||
const delayMs = ms ?? useContext(DelayContext).ms ?? 0 | ||
const [isDelayed, setIsDelayed] = useState(delayMs === 0) | ||
|
||
useEffect(() => { | ||
const timerId = setTimeout(() => !isDelayed && setIsDelayed(true), delayMs) | ||
return () => clearTimeout(timerId) | ||
}, [delayMs]) | ||
|
||
return <>{isDelayed ? children : null}</> | ||
} | ||
|
||
/** | ||
* @experimental This is experimental feature. | ||
*/ | ||
export const withDelay = <Props extends Record<string, unknown> = Record<string, never>>( | ||
Component: ComponentType<Props>, | ||
delayProps?: ComponentPropsWithoutChildren<typeof Delay> | ||
) => { | ||
const Wrapped = (props: Props) => ( | ||
<Delay {...delayProps}> | ||
<Component {...props} /> | ||
</Delay> | ||
) | ||
|
||
if (process.env.NODE_ENV !== 'production') { | ||
const name = Component.displayName || Component.name || 'Component' | ||
Wrapped.displayName = `withDelay(${name})` | ||
} | ||
|
||
return Wrapped | ||
} |
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
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,35 @@ | ||
import { ContextType, ReactNode, useMemo } from 'react' | ||
import { DelayContext } from './Delay' | ||
import { SuspenseContext } from './Suspense' | ||
|
||
type Configs = { | ||
defaultOptions?: { | ||
suspense?: ContextType<typeof SuspenseContext> | ||
delay?: ContextType<typeof DelayContext> | ||
} | ||
} | ||
|
||
/** | ||
* @experimental This is experimental feature. | ||
*/ | ||
export class SuspensiveConfigs { | ||
public defaultOptions?: Configs['defaultOptions'] | ||
|
||
constructor(config: Configs = {}) { | ||
this.defaultOptions = config.defaultOptions | ||
} | ||
} | ||
|
||
/** | ||
* @experimental This is experimental feature. | ||
*/ | ||
export const SuspensiveProvider = ({ configs, children }: { configs: SuspensiveConfigs; children: ReactNode }) => { | ||
const delayValue = useMemo(() => configs.defaultOptions?.delay || {}, []) | ||
const suspenseValue = useMemo(() => configs.defaultOptions?.suspense || {}, []) | ||
|
||
return ( | ||
<DelayContext.Provider value={delayValue}> | ||
<SuspenseContext.Provider value={suspenseValue}>{children}</SuspenseContext.Provider> | ||
</DelayContext.Provider> | ||
) | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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.