UNPKG

5.3 kBTypeScriptView Raw
1// Type definitions for react-motion
2// Project: https://github.com/chenglou/react-motion
3// Definitions by: Alexey Svetliakov <https://github.com/asvetliakov>
4// Dimitar Nestorov <https://github.com/dimitarnestorov>
5// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
6// TypeScript Version: 2.8
7
8import { Component, ReactElement } from 'react';
9
10
11// your typical style object given in props. Maps to a number or a spring config
12export type Style = { [key: string]: number | OpaqueConfig };
13// the interpolating style object, with the same keys as the above Style object,
14// with the values mapped to numbers, naturally
15export type PlainStyle = { [key: string]: number };
16// internal velocity object. Similar to PlainStyle, but whose numbers represent
17// speed. Might be exposed one day.
18export type Velocity = { [key: string]: number };
19
20/**
21 * Spring additional configuration
22 */
23interface SpringHelperConfig {
24 /**
25 * Specified stiffness
26 * @defaults 170
27 */
28 stiffness?: number | undefined;
29 /**
30 * Specifies damping
31 * @defaults 26
32 */
33 damping?: number | undefined;
34 /**
35 * Specifies both the rounding of the interpolated value and the speed (internal).
36 * @defaults 0.01
37 */
38 precision?: number | undefined;
39}
40
41
42export interface OpaqueConfig {
43 val: number;
44 stiffness: number;
45 damping: number;
46 precision: number;
47}
48
49/**
50 * <Motion/> properties
51 */
52interface MotionProps {
53 /**
54 * The default style. Being ignored on subsequent renders
55 * @default Object with same keys as in style whose values are the initial numbers you're interpolating on
56 */
57 defaultStyle?: PlainStyle | undefined;
58 /**
59 * Object that maps to either number or opaque config returned by spring().
60 * Must keep same keys throughout component's existence
61 */
62 style: Style;
63 /**
64 * Callback with your interpolated styles. Must return one react element to render
65 * @param interpolatedStyle
66 */
67 children?: ((interpolatedStyle: PlainStyle) => JSX.Element) | undefined;
68 /**
69 * The callback that fires when the animation comes to a rest.
70 */
71 onRest?: (() => void) | undefined;
72}
73
74export declare class Motion extends Component<MotionProps> { }
75
76// === TransitionMotion ===
77interface TransitionStyle {
78 /**
79 * The ID that TransitionMotion uses to track which configuration is which across renders, even when things are reordered.
80 * Typically reused as the component key when you map over the interpolated styles.
81 */
82 key: string;
83 /**
84 * Anything you'd like to carry along. Will be preserved on re-renders until key off
85 */
86 data?: any;
87 /**
88 * Actual starting style configuration
89 */
90 style: Style; // actual style you're passing
91}
92/**
93 * Default style for transition
94 */
95interface TransitionPlainStyle {
96 key: string;
97 data?: any;
98 // same as TransitionStyle, passed as argument to style/children function
99 style: PlainStyle;
100}
101type InterpolateFunction = (previousInterpolatedStyles?: Array<TransitionPlainStyle>) => Array<TransitionStyle>;
102/**
103 * Transition properties
104 */
105interface TransitionProps {
106 /**
107 * Default styles on first render
108 */
109 defaultStyles?: Array<TransitionPlainStyle> | undefined;
110 /**
111 * Styles to interpolate. Accepts array of TransitionStyle objects or interpolated function similar as for
112 * <StaggeredMotion/>
113 */
114 styles: Array<TransitionStyle> | InterpolateFunction;
115 children?: ((interpolatedStyles: Array<TransitionPlainStyle>) => JSX.Element) | undefined;
116 /**
117 * Triggers when a new element will appear
118 * @param styleThatEntered
119 */
120 willEnter?: ((styleThatEntered: TransitionStyle) => PlainStyle) | undefined;
121 /**
122 * Triggers when an element will disappear
123 * @param styleThatLeft
124 */
125 willLeave?: ((styleThatLeft: TransitionStyle) => Style | void) | undefined;
126 /**
127 * Triggers when an element has disappeared
128 * @param styleThatLeft
129 */
130 didLeave?: ((styleThatLeft: TransitionStyle) => void) | undefined;
131}
132export class TransitionMotion extends Component<TransitionProps> { }
133
134
135interface StaggeredMotionProps {
136 children: (interpolatedStyles: any) => React.ReactElement;
137 /**
138 * Default styles
139 */
140 defaultStyles?: Array<PlainStyle> | undefined;
141 /**
142 * Styles to interpolate
143 * @param previousInterpolatedStyles The previously interpolating (array of) styles (undefined at first render, unless defaultStyles is provided).
144 */
145 styles: (previousInterpolatedStyles?: Array<PlainStyle>) => Array<Style>;
146}
147export declare class StaggeredMotion extends Component<StaggeredMotionProps> { }
148
149
150/**
151* Used in conjunction with the components below. Specifies the how to animate to the destination value, e.g. spring(10, {stiffness: 120, damping: 17}) means "animate to value 10, with a spring of stiffness 120 and damping 17".
152*/
153export declare function spring(val: number, config?: SpringHelperConfig): OpaqueConfig;
154
155export declare class Presets {
156 noWobble: OpaqueConfig; // the default, if nothing provided
157 gentle: OpaqueConfig;
158 wobbly: OpaqueConfig;
159 stiff: OpaqueConfig;
160}
161
162export declare const presets: Presets;