Skip to content

Commit

Permalink
feat(transition): ✨support custom class name and styles
Browse files Browse the repository at this point in the history
  • Loading branch information
huang-xiao-jian committed Jun 23, 2020
1 parent a389a2d commit c6c59a6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
17 changes: 13 additions & 4 deletions packages/Transition/Transition.redux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface TransitionState {
// 转场 CSS 名称
name: string;
// 转场时间
duration: number;
duration: number | string;
// 自定义样式覆盖
className?: string;
// 自定义样式覆盖
Expand All @@ -36,6 +36,9 @@ export interface TransitionAction {
payload?: Partial<TransitionState>;
}

const stringifyDuration = (duration: number | string) =>
typeof duration === 'number' ? `${duration}ms` : duration;

export const transitionReducer: Reducer<TransitionState, TransitionAction> = (
state,
action
Expand All @@ -58,11 +61,14 @@ export const transitionReducer: Reducer<TransitionState, TransitionAction> = (
className: clsx(`${state.name}-enter`, `${state.name}-enter-active`),
style: {
display: 'block',
transitionDuration: `${state.duration}ms`,
transitionDuration: stringifyDuration(state.duration),
},
};
case TransitionActionType.Entered:
return { ...state, className: clsx(`${state.name}-enter-done`) };
return {
...state,
className: clsx(`${state.name}-enter-done`),
};
case TransitionActionType.Leave:
return {
...state,
Expand All @@ -72,7 +78,10 @@ export const transitionReducer: Reducer<TransitionState, TransitionAction> = (
return {
...state,
className: clsx(`${state.name}-leave`, `${state.name}-leave-active`),
style: { ...state.style, transitionDuration: `${state.duration}ms` },
style: {
...state.style,
transitionDuration: stringifyDuration(state.duration),
},
};
case TransitionActionType.Left:
return {
Expand Down
13 changes: 9 additions & 4 deletions packages/Transition/Transition.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ interface NeutralTransitionProps {
// 预设动画
name: string;
// 转场时间
duration: number;
duration: number | string;
}

interface ExogenousTransitionProps {
Expand All @@ -41,7 +41,7 @@ const DefaultTransitionProps: NeutralTransitionProps = {

// TODO - appear transition not supported
const Transition: FunctionComponent<TransitionProps> = (props) => {
const { className, visible, duration, name, children } = props;
const { className, visible, duration, name, style, children } = props;
const [state, dispatch] = useReducer(transitionReducer, {
name,
duration,
Expand All @@ -52,6 +52,11 @@ const Transition: FunctionComponent<TransitionProps> = (props) => {
display: visible ? '' : 'none',
}),
});
// 支持自定义 style 样式, className
const bindings = {
style: style ? { ...style, ...state.style } : state.style,
className: className ? `${state.className} ${className}` : state.className,
};

// TODO - 事件触发不稳定,后续处理
const onTransitionEnd = () => {
Expand Down Expand Up @@ -100,8 +105,8 @@ const Transition: FunctionComponent<TransitionProps> = (props) => {

return (
<View
style={state.style}
className={state.className}
style={bindings.style}
className={bindings.className}
onTransitionEnd={onTransitionEnd}
>
{children}
Expand Down
8 changes: 7 additions & 1 deletion storyboard/pages/transition/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
transition-property: background-color, transform;
}

.dance-enter-active {
.dance-enter,
.dance-leave-active {
background-color: red;
transform: rotate(-360deg) translate3d(-100%, -100%, 0);
}

.dance-enter-active {
background-color: blueviolet;
transform: none;
}

0 comments on commit c6c59a6

Please sign in to comment.