Skip to content
This repository was archived by the owner on Apr 8, 2020. It is now read-only.

Commit cf01c53

Browse files
committed
Support separate delays
1 parent 4312d39 commit cf01c53

File tree

6 files changed

+50
-9
lines changed

6 files changed

+50
-9
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-css-transition",
3-
"version": "0.3.0-beta.2",
3+
"version": "0.4.0-beta",
44
"description": "CSS Transition Component for React",
55
"homepage": "https://github.com/wikiwi/react-css-transition",
66
"repository": {

src/csstransition.tsx

+7-5
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,18 @@ import {
1616

1717
import { resolveTransit } from "./transit";
1818
import { TransitionObserver } from "./transitionobserver";
19+
import { TransitionDelay, getEnterDelay, getLeaveDelay } from "./utils";
1920

2021
const TICK = 17;
2122

23+
export type CSSTransitionDelay = TransitionDelay;
2224
export type CSSTransitionEventHandler = () => void;
2325

2426
export interface CSSTransitionProps
2527
extends HTMLAttributes<any> {
2628
active?: boolean;
2729
transitionAppear?: boolean;
28-
transitionDelay?: number;
30+
transitionDelay?: CSSTransitionDelay;
2931
onTransitionComplete?: CSSTransitionEventHandler;
3032
component?: string | ComponentClass<any> | StatelessComponent<any>;
3133
children?: ReactNode;
@@ -84,22 +86,22 @@ const transitToActiveAppearingState = (props: CSSTransitionProps) => ({
8486

8587
const transitToActiveRunningState = (props: CSSTransitionProps) => ({
8688
id: StateID.TransitToActiveRunning,
87-
style: { ...props.style, ...resolveTransit(props.enterStyle, props.transitionDelay) },
89+
style: { ...props.style, ...resolveTransit(props.enterStyle, getEnterDelay(props.transitionDelay)) },
8890
});
8991

9092
const transitToActiveStartedState = (props: CSSTransitionProps) => ({
9193
id: StateID.TransitToActiveStarted,
92-
style: { ...props.style, ...resolveTransit(props.enterStyle, props.transitionDelay) },
94+
style: { ...props.style, ...resolveTransit(props.enterStyle, getEnterDelay(props.transitionDelay)) },
9395
});
9496

9597
const transitToDefaultRunningState = (props: CSSTransitionProps) => ({
9698
id: StateID.TransitToDefaultRunning,
97-
style: { ...props.style, ...resolveTransit(props.leaveStyle, props.transitionDelay) },
99+
style: { ...props.style, ...resolveTransit(props.leaveStyle, getLeaveDelay(props.transitionDelay)) },
98100
});
99101

100102
const transitToDefaultStartedState = (props: CSSTransitionProps) => ({
101103
id: StateID.TransitToDefaultStarted,
102-
style: { ...props.style, ...resolveTransit(props.leaveStyle, props.transitionDelay) },
104+
style: { ...props.style, ...resolveTransit(props.leaveStyle, getLeaveDelay(props.transitionDelay)) },
103105
});
104106

105107
export class CSSTransition extends Component<CSSTransitionProps, CSSTransitionState> {

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
* of the MIT license. See the LICENSE file for details.
77
*/
88

9-
export { CSSTransition, CSSTransitionProps, CSSTransitionEventHandler } from "./csstransition";
9+
export { CSSTransition, CSSTransitionProps, CSSTransitionEventHandler, CSSTransitionDelay } from "./csstransition";
1010
export { CSSTransitionGroup, CSSTransitionGroupProps } from "./csstransitiongroup";
1111
export { transit } from "./transit";

src/utils.spec.ts

+21
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
convertToCSSPrefix, removeVendorPrefix,
1313
matchTransitionProperty, parseDuration,
1414
parseTransition,
15+
getEnterDelay, getLeaveDelay,
1516
} from "./utils";
1617

1718
describe("utils.ts", () => {
@@ -139,4 +140,24 @@ describe("utils.ts", () => {
139140
cases.forEach((c) => assert.deepEqual(parseTransition(c[0] as string), c[1]));
140141
});
141142
});
143+
144+
describe("getEnterDelay", () => {
145+
it("should process number", () => {
146+
assert.strictEqual(getEnterDelay(200), 200);
147+
});
148+
149+
it("should process object", () => {
150+
assert.strictEqual(getEnterDelay({ enter: 100 }), 100);
151+
});
152+
});
153+
154+
describe("getLeaveDelay", () => {
155+
it("should process number", () => {
156+
assert.strictEqual(getLeaveDelay(200), 200);
157+
});
158+
159+
it("should process object", () => {
160+
assert.strictEqual(getLeaveDelay({ leave: 100 }), 100);
161+
});
162+
});
142163
});

src/utils.ts

+18
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,21 @@ export function parseTransition(transition: string): [TransitionEntry, Transitio
6767
);
6868
return [firstProperty, lastProperty];
6969
}
70+
71+
export type TransitionDelay = number | { enter?: number; leave?: number };
72+
73+
export function getEnterDelay(delay: TransitionDelay): number {
74+
if (!delay) { return 0; }
75+
if (typeof delay === "number") {
76+
return delay as number;
77+
}
78+
return delay.enter;
79+
}
80+
81+
export function getLeaveDelay(delay: TransitionDelay): number {
82+
if (!delay) { return 0; }
83+
if (typeof delay === "number") {
84+
return delay as number;
85+
}
86+
return delay.leave;
87+
}

test/integration/delay.test.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ const TICK = 17;
1818

1919
describe("delay integration test", () => {
2020
describe("<CSSTransition>", () => {
21-
const transitionDelay = 30;
21+
const transitionDelay = { enter: 30, leave: 20 };
2222

2323
const activeStyle: CSSProperties = { width: "100px" };
2424
const defaultStyle: CSSProperties = { width: "50px" };
2525
const enterStyle: CSSProperties = { width: transit("100px", 150, "ease", 25) };
2626
const leaveStyle: CSSProperties = { width: transit("50px", 150, "ease", 25) };
2727
const enterStyleProcessed: CSSProperties = { width: "100px", transition: "width 150ms ease 55ms" };
28-
const leaveStyleProcessed: CSSProperties = { width: "50px", transition: "width 150ms ease 55ms" };
28+
const leaveStyleProcessed: CSSProperties = { width: "50px", transition: "width 150ms ease 45ms" };
2929
let getWrapper: (props?: CSSTransitionProps) => ReactWrapper<any, {}>;
3030
let wrapper: ReactWrapper<any, {}>;
3131
let target: ReactWrapper<any, {}>;

0 commit comments

Comments
 (0)