import { ComponentProps, ExoticComponent, JSXElementConstructor } from 'react'; import { HasRequiredKeys } from 'type-fest'; import { IsAny } from 'type-fest/source/is-any'; import { ModalWindow } from './ModalWindow'; export interface ExternalStore { subscribe(callback: () => void): () => void; getSnapshot(): T; } export interface ModalSnapshot { active: boolean; windows: ModalWindow[]; } /** * A modal component can be either a function component or a class component. */ export type ModalComponent

= JSXElementConstructor

| ExoticComponent

; export type ModalComponentProps = IsAny> extends true ? never : ComponentProps; export type ModalNamedComponents = Record>; export interface ModalParams { /** * Usually used to close the modal by `closeById` method. * @default -1 */ id: string | number; /** * Whether to enable built-in closing mechanisms. * * - `ESC` key * - `click` on the overlay * * @default true */ closable: boolean; /** * Forks the modal window to a new layer. * * @default 0 */ layer: number; /** * Keep all open modals mounted until the last one is closed. */ keepMounted: boolean; /** * *Usually* used to set the `aria-label` attribute. */ label: string; } /** * Gets either a tuple with required or optional parameters depending on whether `P` has any required keys. * * Can be used in function argument to make `params` optional if `P` has only optional keys. * * @example * function open

(component: ModalComponent

, ...[modalParams]: ModalWindowParams

) {} * * const OkComponent = () =>

* open(OkComponent, { id: 1 }) // OK * open(OkComponent) // OK * * const FailComponent = (props: { required: boolean }) =>
* open(FailComponent, { required: true }) // OK * open(FailComponent) // Error: missing required property `required` */ export type ModalWindowParams

= HasRequiredKeys> extends true ? [Partial & P] : [(Partial & P)?]; /** * This is intented to fix errors related to passing `ModalWindowParams` to spreaded array of `ModalWindowParams`. * * Removes `undefined` from `ModalWindowParams`, otherwise it will show `"'P' could be instantiated with an arbitrary type..."` error. * Even though it's ok to pass `undefined` and arbitrary type there. */ export type MODAL_WINDOW_PARAMS_EXPLANATION

= Partial & P;