import type { AssertExtends } from './types/assert-extends.cjs'; import type { PropsArgs } from './types/props-args.cjs'; import type { FC, RefCallback } from 'react'; /** * Type of the `resolve` callback passed to a component rendered by {@link toi}, * which resolves the promise it returned with the given response. */ export type ToiResolve = [Response] extends [void] ? () => void : (response: Response) => void; /** * Type of the `reject` callback passed to a component rendered by {@link toi}, * which rejects the promise it returned with the given reason. * * Intended for when the component can no longer answer: work it performs * before resolving fails, or the user navigates away. Ordinary dismissals, * such as a cancel button, should `resolve` instead. * * Note that navigating to another page doesn't call `reject` on its own: * components rendered by {@link toi} live in the {@link ToiHost}, which usually * sits outside the routed part of the tree, so they stay mounted across page * transitions. To close a component on navigation, listen for the * {@link https://developer.mozilla.org/docs/Web/API/Navigation/currententrychange_event | `currententrychange`} * event on `navigation` and call `reject()` from it. * * When `reason` is omitted, the promise is rejected with a `DOMException` named * `AbortError`, following the {@link AbortSignal} convention. */ export type ToiReject = (reason?: unknown) => void; /** The `Response` type a component resolves with, extracted from its props or its own type. */ export type InferToiResponse = T extends FC ? InferToiResponse : T extends { resolve: () => void; } ? void : T extends { resolve: (response: infer Response) => void; } ? Response : never; /** * Props injected into the component passed to {@link toi}. * * `Response` defaults to `void`, for components that don't resolve with a value. * * @example * Resolving without a value: * ```tsx * const Confirm: FC = ({ ref, resolve }) => ( * * * * ); * ``` * * @example * Resolving with a value: * ```tsx * const Confirm: FC> = ({ ref, resolve }) => ( * * * * * ); * ``` * * @example * Rejecting when work done before resolving fails, so `await toi(Confirm)` throws: * ```tsx * const Confirm: FC> = ({ ref, resolve, reject }) => ( * * * * * ); * ``` * * @example * Rejecting when the user navigates away, since components in the * {@link ToiHost} aren't unmounted by page transitions on their own: * ```tsx * const Confirm: FC> = ({ ref, resolve, reject }) => { * useEffect(() => { * const abandon = () => reject(); * navigation.addEventListener('currententrychange', abandon); * return () => navigation.removeEventListener('currententrychange', abandon); * }, [reject]); * * return ( * * * * * ); * }; * ``` * * @example * Extending a native element's props, resolving with a value: * ```tsx * type ConfirmProps = ComponentProps<'dialog'> & ToiProps; * * const Confirm: FC = ({ ref, resolve, ...props }) => ( * * * * * ); * ``` */ export type ToiProps = { /** * Ref to attach to the animatable root element of the component, used to * detect when its exit animations have finished before settling. */ ref?: RefCallback | undefined; /** * Resolves the promise returned by {@link toi} with the given response. * Has no effect once either `resolve` or `reject` has been called. */ resolve: ToiResolve; /** * Rejects the promise returned by {@link toi} with the given reason, or with * a `DOMException` named `AbortError` when called without one. * * Use it when the component can no longer answer, such as when work done * before resolving fails or the user navigates away. Ordinary dismissals, * such as a cancel button, should `resolve` instead. * * Page transitions don't call it automatically, since the {@link ToiHost} * usually sits outside the routed part of the tree. To close the component * on navigation, listen for the `currententrychange` event on `navigation` * and call `reject()` from it. See {@link ToiReject}. * * Has no effect once either `resolve` or `reject` has been called. */ reject: ToiReject; }; type ToiPropsLike = Omit, 'reject'> & { reject?: ToiReject | undefined; }; type AnyToiProps = Omit, 'resolve'> & { resolve: (response: never) => void; }; /** * Mounts `Component` into the {@link ToiHost} and returns a promise * that resolves with the value passed to `resolve`, or rejects with the * reason passed to `reject`. * * Once `resolve` or `reject` is called, the component is kept mounted until any * running animations (excluding infinite ones) on its ref'd element finish, then * it is removed from the host and the promise settles. * * `Response` and any additional props are inferred from `Component`'s own props * type; pass a second argument for any additional props it requires beyond * {@link ToiProps}. * * @param Component - Component to render, receiving {@link ToiProps}. * @param props - Additional props to pass to `Component`, if it requires any. * @returns A promise resolving with the response passed to `resolve`, or * rejecting with the reason passed to `reject`. * * @example * Passing a predefined component that resolves without a value: * ```tsx * const Confirm: FC = ({ ref, resolve }) => ( * * * * ); * * await toi(Confirm); * ``` * * @example * Passing a predefined component that resolves with a value: * ```tsx * const Confirm: FC> = ({ ref, resolve }) => ( * * * * * ); * * const confirmed = await toi(Confirm); * ``` */ export declare function toi>(Component: Component & AssertExtends[0], AnyToiProps>, ...[props]: PropsArgs[0], keyof ToiProps>>): Promise>; /** * Mounts `Component` into the {@link ToiHost} and returns a promise * that resolves with the value passed to `resolve`, or rejects with the * reason passed to `reject`. * * Once `resolve` or `reject` is called, the component is kept mounted until any * running animations (excluding infinite ones) on its ref'd element finish, then * it is removed from the host and the promise settles. * * Use this overload to pass an inline `Component` whose props can't be inferred * on their own; annotate `Response` explicitly so its `props` get typed as * {@link ToiProps}. If `Component` requires additional props beyond * {@link ToiProps}, annotate `Props` explicitly as well and pass them as `props`. * * @param Component - Component to render, receiving {@link ToiProps} and `props`. * @param props - Additional props to pass to `Component`, if it requires any. * @returns A promise resolving with the response passed to `resolve`, or * rejecting with the reason passed to `reject`. * * @example * Resolving without a value: * ```tsx * await toi(({ ref, resolve }) => ( * * * * )); * ``` * * @example * Resolving with a value: * ```tsx * const confirmed = await toi(({ ref, resolve }) => ( * * * * * )); * ``` * * @example * With additional props, resolving without a value: * ```tsx * type ConfirmProps = ToiProps & { message: string }; * * await toi(({ ref, resolve, message }) => ( * *

{message}

* *
* ), { message: 'Saved!' }); * ``` * * @example * With additional props, resolving with a value: * ```tsx * type ConfirmProps = ToiProps & { message: string }; * * const confirmed = await toi(({ ref, resolve, message }) => ( * *

{message}

* * *
* ), { message: 'Are you sure?' }); * ``` */ export declare function toi = ToiProps>(Component: FC & AssertExtends>, ...[props]: PropsArgs>>): Promise; export declare namespace toi { /** * Binds `Component` to {@link toi}, returning a reusable function that * mounts it and resolves with its response each time it's called. * * `Response` and any additional props are inferred from `Component`'s own * props type; the returned function takes a second argument for any * additional props it requires beyond {@link ToiProps}. * * @param Component - Component to render, receiving {@link ToiProps}. * @returns A function that invokes {@link toi} with `Component`, taking any * additional props `Component` requires. * * @example * Passing a predefined component that resolves without a value: * ```tsx * const Confirm: FC = ({ ref, resolve }) => ( * * * * ); * * const confirm = toi.fn(Confirm); * await confirm(); * ``` * * @example * Passing a predefined component that resolves with a value: * ```tsx * const Confirm: FC> = ({ ref, resolve }) => ( * * * * * ); * * const confirm = toi.fn(Confirm); * const confirmed = await confirm(); * ``` */ function fn>(Component: FC & AssertExtends): (...[props]: PropsArgs>>) => Promise>; /** * Binds `Component` to {@link toi}, returning a reusable function that * mounts it and resolves with its response each time it's called. * * `Response` and any additional props are inferred from `Component`'s own * props type. `defaultProps` is used whenever the returned function is * called without its own `props` argument; pass one to override it. * * @param Component - Component to render, receiving {@link ToiProps}. * @param defaultProps - Default additional props to pass to `Component`. * @returns A function that invokes {@link toi} with `Component`, optionally * overriding `defaultProps`. * * @example * Resolving without a value: * ```tsx * const Confirm: FC = ({ ref, resolve, message }) => ( * *

{message}

* *
* ); * * const confirm = toi.fn(Confirm, { message: 'Saved!' }); * await confirm(); * await confirm({ message: 'Deleted!' }); * ``` * * @example * Resolving with a value: * ```tsx * const Confirm: FC & { message: string }> = ({ ref, resolve, message }) => ( * *

{message}

* * *
* ); * * const confirm = toi.fn(Confirm, { message: 'Are you sure?' }); * const confirmed = await confirm(); * const confirmedWithOverride = await confirm({ message: 'Really?' }); * ``` */ function fn>(Component: FC & AssertExtends, defaultProps: Omit>): (props?: Omit>) => Promise>; /** * Binds `Component` to {@link toi}, returning a reusable function that * mounts it and resolves with its response each time it's called. * * Use this overload to pass an inline `Component` whose props can't be * inferred on their own; annotate `Response` explicitly so its `props` get * typed as {@link ToiProps}. * * @param Component - Component to render, receiving {@link ToiProps}. * @returns A function that invokes {@link toi} with `Component`. * * @example * Resolving without a value: * ```tsx * const confirm = toi.fn(({ ref, resolve }) => ( * * * * )); * * await confirm(); * ``` * * @example * Resolving with a value: * ```tsx * const confirm = toi.fn(({ ref, resolve }) => ( * * * * * )); * * const confirmed = await confirm(); * ``` */ function fn = ToiProps>(Component: FC & AssertExtends>): (...[props]: PropsArgs>>) => Promise; /** * Binds `Component` to {@link toi}, returning a reusable function that * mounts it and resolves with its response each time it's called. * * Use this overload to pass an inline `Component` together with the * additional props it requires beyond {@link ToiProps}; annotate `Response` * (and, if it can't be inferred from `defaultProps`, `Props`) explicitly. * `defaultProps` is used whenever the returned function is called without * its own `props` argument; pass one to override it. * * @param Component - Component to render, receiving {@link ToiProps} and `props`. * @param defaultProps - Default additional props to pass to `Component`. * @returns A function that invokes {@link toi} with `Component`, optionally * overriding `defaultProps`. * * @example * Resolving without a value: * ```tsx * type ConfirmProps = ToiProps & { message: string }; * * const confirm = toi.fn((props) => ( * *

{props.message}

* *
* ), { message: 'Saved!' }); * * await confirm(); * await confirm({ message: 'Deleted!' }); * ``` * * @example * Resolving with a value: * ```tsx * type ConfirmProps = ToiProps & { message: string }; * * const confirm = toi.fn((props) => ( * *

{props.message}

* * *
* ), { message: 'Are you sure?' }); * * const confirmed = await confirm(); * const confirmedWithOverride = await confirm({ message: 'Really?' }); * ``` */ function fn = ToiProps>(Component: FC & AssertExtends>, defaultProps: Omit>): (props?: Omit>) => Promise; } /** * Renders all components currently requested via {@link toi}. * * Must be mounted once for {@link toi} to have anywhere to render its components. * * @example * ```tsx * const App = () => ( * <> * * * * ); * ``` */ export declare const ToiHost: FC; export {};