UNPKG

8.65 kBTypeScriptView Raw
1import { Component, ReactNode } from "react";
2
3type RefHandler<
4 RefElement extends undefined | HTMLElement,
5 ImplicitRefHandler extends (node: HTMLElement, ...args: any[]) => void,
6 ExplicitRefHandler extends (...args: any[]) => void,
7> = {
8 implicit: ImplicitRefHandler;
9 explicit: ExplicitRefHandler;
10}[RefElement extends undefined ? "implicit" : "explicit"];
11
12export type EndHandler<RefElement extends undefined | HTMLElement> = RefHandler<
13 RefElement,
14 (node: HTMLElement, done: () => void) => void,
15 (done: () => void) => void
16>;
17
18export type EnterHandler<RefElement extends undefined | HTMLElement> = RefHandler<
19 RefElement,
20 (node: HTMLElement, isAppearing: boolean) => void,
21 (isAppearing: boolean) => void
22>;
23
24export type ExitHandler<E extends undefined | HTMLElement> = RefHandler<E, (node: HTMLElement) => void, () => void>;
25
26export const UNMOUNTED = "unmounted";
27export const EXITED = "exited";
28export const ENTERING = "entering";
29export const ENTERED = "entered";
30export const EXITING = "exiting";
31
32export interface TransitionActions {
33 /**
34 * Normally a component is not transitioned if it is shown when the
35 * `<Transition>` component mounts. If you want to transition on the first
36 * mount set appear to true, and the component will transition in as soon
37 * as the `<Transition>` mounts. Note: there are no specific "appear" states.
38 * appear only adds an additional enter transition.
39 */
40 appear?: boolean | undefined;
41
42 /**
43 * Enable or disable enter transitions.
44 */
45 enter?: boolean | undefined;
46
47 /**
48 * Enable or disable exit transitions.
49 */
50 exit?: boolean | undefined;
51}
52
53interface BaseTransitionProps<RefElement extends undefined | HTMLElement> extends TransitionActions {
54 /**
55 * Show the component; triggers the enter or exit states
56 */
57 in?: boolean | undefined;
58
59 /**
60 * By default the child component is mounted immediately along with the
61 * parent Transition component. If you want to "lazy mount" the component on
62 * the first `in={true}` you can set `mountOnEnter`. After the first enter
63 * transition the component will stay mounted, even on "exited", unless you
64 * also specify `unmountOnExit`.
65 */
66 mountOnEnter?: boolean | undefined;
67
68 /**
69 * By default the child component stays mounted after it reaches the
70 * 'exited' state. Set `unmountOnExit` if you'd prefer to unmount the
71 * component after it finishes exiting.
72 */
73 unmountOnExit?: boolean | undefined;
74
75 /**
76 * Callback fired before the "entering" status is applied. An extra
77 * parameter `isAppearing` is supplied to indicate if the enter stage is
78 * occurring on the initial mount
79 */
80 onEnter?: EnterHandler<RefElement> | undefined;
81
82 /**
83 * Callback fired after the "entering" status is applied. An extra parameter
84 * isAppearing is supplied to indicate if the enter stage is occurring on
85 * the initial mount
86 */
87 onEntering?: EnterHandler<RefElement> | undefined;
88
89 /**
90 * Callback fired after the "entered" status is applied. An extra parameter
91 * isAppearing is supplied to indicate if the enter stage is occurring on
92 * the initial mount
93 */
94 onEntered?: EnterHandler<RefElement> | undefined;
95
96 /**
97 * Callback fired before the "exiting" status is applied.
98 */
99 onExit?: ExitHandler<RefElement> | undefined;
100
101 /**
102 * Callback fired after the "exiting" status is applied.
103 */
104 onExiting?: ExitHandler<RefElement> | undefined;
105
106 /**
107 * Callback fired after the "exited" status is applied.
108 */
109 onExited?: ExitHandler<RefElement> | undefined;
110
111 /**
112 * A function child can be used instead of a React element. This function is
113 * called with the current transition status ('entering', 'entered',
114 * 'exiting', 'exited', 'unmounted'), which can be used to apply context
115 * specific props to a component.
116 * ```jsx
117 * <Transition in={this.state.in} timeout={150}>
118 * {state => (
119 * <MyComponent className={`fade fade-${state}`} />
120 * )}
121 * </Transition>
122 * ```
123 */
124 children?: TransitionChildren | undefined;
125
126 /**
127 * A React reference to DOM element that need to transition: https://stackoverflow.com/a/51127130/4671932
128 * When `nodeRef` prop is used, node is not passed to callback functions (e.g. onEnter) because user already has direct access to the node.
129 * When changing `key` prop of `Transition` in a `TransitionGroup` a new `nodeRef` need to be provided to `Transition` with changed `key`
130 * prop (@see https://github.com/reactjs/react-transition-group/blob/master/test/Transition-test.js).
131 */
132 nodeRef?: React.Ref<RefElement> | undefined;
133
134 [prop: string]: any;
135}
136
137export type TransitionStatus = typeof ENTERING | typeof ENTERED | typeof EXITING | typeof EXITED | typeof UNMOUNTED;
138export type TransitionChildren =
139 | ReactNode
140 | ((status: TransitionStatus, childProps?: Record<string, unknown>) => ReactNode);
141
142export interface TimeoutProps<RefElement extends undefined | HTMLElement> extends BaseTransitionProps<RefElement> {
143 /**
144 * The duration of the transition, in milliseconds. Required unless addEndListener is provided.
145 *
146 * You may specify a single timeout for all transitions:
147 * ```js
148 * timeout={500}
149 * ```
150 * or individually:
151 * ```js
152 * timeout={{
153 * appear: 500,
154 * enter: 300,
155 * exit: 500,
156 * }}
157 * ```
158 * - appear defaults to the value of `enter`
159 * - enter defaults to `0`
160 * - exit defaults to `0`
161 */
162 timeout: number | { appear?: number | undefined; enter?: number | undefined; exit?: number | undefined };
163
164 /**
165 * Add a custom transition end trigger. Called with the transitioning DOM
166 * node and a done callback. Allows for more fine grained transition end
167 * logic. Note: Timeouts are still used as a fallback if provided.
168 */
169 addEndListener?: EndHandler<RefElement> | undefined;
170}
171
172export interface EndListenerProps<Ref extends undefined | HTMLElement> extends BaseTransitionProps<Ref> {
173 /**
174 * The duration of the transition, in milliseconds. Required unless addEndListener is provided.
175 *
176 * You may specify a single timeout for all transitions:
177 * ```js
178 * timeout={500}
179 * ```
180 * or individually:
181 * ```js
182 * timeout={{
183 * appear: 500,
184 * enter: 300,
185 * exit: 500,
186 * }}
187 * ```
188 * - appear defaults to the value of `enter`
189 * - enter defaults to `0`
190 * - exit defaults to `0`
191 */
192 timeout?:
193 | number
194 | { appear?: number | undefined; enter?: number | undefined; exit?: number | undefined }
195 | undefined;
196 /**
197 * Add a custom transition end trigger. Called with the transitioning DOM
198 * node and a done callback. Allows for more fine grained transition end
199 * logic. Note: Timeouts are still used as a fallback if provided.
200 */
201 addEndListener: EndHandler<Ref>;
202}
203
204export type TransitionProps<RefElement extends undefined | HTMLElement = undefined> =
205 | TimeoutProps<RefElement>
206 | EndListenerProps<RefElement>;
207
208/**
209 * The Transition component lets you describe a transition from one component
210 * state to another _over time_ with a simple declarative API. Most commonly
211 * It's used to animate the mounting and unmounting of Component, but can also
212 * be used to describe in-place transition states as well.
213 *
214 * By default the `Transition` component does not alter the behavior of the
215 * component it renders, it only tracks "enter" and "exit" states for the components.
216 * It's up to you to give meaning and effect to those states. For example we can
217 * add styles to a component when it enters or exits:
218 *
219 * ```jsx
220 * import Transition from 'react-transition-group/Transition';
221 *
222 * const duration = 300;
223 *
224 * const defaultStyle = {
225 * transition: `opacity ${duration}ms ease-in-out`,
226 * opacity: 0,
227 * }
228 *
229 * const transitionStyles = {
230 * entering: { opacity: 1 },
231 * entered: { opacity: 1 },
232 * };
233 *
234 * const Fade = ({ in: inProp }) => (
235 * <Transition in={inProp} timeout={duration}>
236 * {(state) => (
237 * <div style={{
238 * ...defaultStyle,
239 * ...transitionStyles[state]
240 * }}>
241 * I'm A fade Transition!
242 * </div>
243 * )}
244 * </Transition>
245 * );
246 * ```
247 */
248declare class Transition<RefElement extends HTMLElement | undefined> extends Component<TransitionProps<RefElement>> {}
249
250export default Transition;
251
\No newline at end of file