-
Notifications
You must be signed in to change notification settings - Fork 273
refactor: 重构引入的css transition 库 #3236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xiaoyatong
wants to merge
4
commits into
jdf2e:feat_v3.x
Choose a base branch
from
xiaoyatong:feat3/csstransition
base: feat_v3.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
import addOneClass from 'dom-helpers/addClass' | ||
|
||
import removeOneClass from 'dom-helpers/removeClass' | ||
import React, { FunctionComponent } from 'react' | ||
|
||
import { Transition, TransitionProps } from './Transition' | ||
import { forceReflow } from './utils/reflow' | ||
|
||
const addClassCommon = (node: HTMLElement | null, classes: string) => | ||
node && classes && classes.split(' ').forEach((c) => addOneClass(node, c)) | ||
const removeClassCommon = (node: HTMLElement | null, classes: string) => | ||
node && classes && classes.split(' ').forEach((c) => removeOneClass(node, c)) | ||
|
||
type ClassNamesShape = | ||
| string | ||
| { | ||
appear: string | ||
appearActive: string | ||
appearDone: string | ||
enter: string | ||
enterActive: string | ||
enterDone: string | ||
exit: string | ||
exitActive: string | ||
exitDone: string | ||
} | ||
|
||
interface CSSTransitionProps extends TransitionProps { | ||
classNames: ClassNamesShape | ||
children: React.ReactNode | ||
} | ||
|
||
const defaultProps = { | ||
classNames: '', | ||
} | ||
|
||
const CSSTransition: FunctionComponent<Partial<CSSTransitionProps>> = ( | ||
props | ||
) => { | ||
const { | ||
classNames, | ||
onEnter: _onEnter, | ||
onEntering: _onEntering, | ||
onEntered: _onEntered, | ||
onExit: _onExit, | ||
onExiting: _onExiting, | ||
onExited: _onExited, | ||
nodeRef: _nodeRef, | ||
...childProps | ||
} = { ...defaultProps, ...props } | ||
|
||
const appliedClasses = { | ||
appear: {}, | ||
enter: {}, | ||
exit: {}, | ||
} | ||
|
||
const onEnter = (maybeNode: any, maybeAppearing: boolean) => { | ||
const [node, appearing] = resolveArguments(maybeNode, maybeAppearing) | ||
removeClasses(node, 'exit') | ||
addClass(node, appearing ? 'appear' : 'enter', 'base') | ||
|
||
if (_onEnter) { | ||
_onEnter(maybeNode, maybeAppearing) | ||
} | ||
} | ||
|
||
const onEntering = (maybeNode: any, maybeAppearing: boolean) => { | ||
const [node, appearing] = resolveArguments(maybeNode, maybeAppearing) | ||
const type = appearing ? 'appear' : 'enter' | ||
addClass(node, type, 'active') | ||
|
||
if (_onEntering) { | ||
_onEntering(maybeNode, maybeAppearing) | ||
} | ||
} | ||
|
||
const onEntered = (maybeNode: any, maybeAppearing: boolean) => { | ||
const [node, appearing] = resolveArguments(maybeNode, maybeAppearing) | ||
const type = appearing ? 'appear' : 'enter' | ||
removeClasses(node, type) | ||
addClass(node, type, 'done') | ||
|
||
if (_onEntered) { | ||
_onEntered(maybeNode, maybeAppearing) | ||
} | ||
} | ||
|
||
const onExit = (maybeNode: any) => { | ||
const [node] = resolveArguments(maybeNode) | ||
removeClasses(node, 'appear') | ||
removeClasses(node, 'enter') | ||
addClass(node, 'exit', 'base') | ||
|
||
if (_onExit) { | ||
_onExit(maybeNode) | ||
} | ||
} | ||
|
||
const onExiting = (maybeNode: any) => { | ||
const [node] = resolveArguments(maybeNode) | ||
addClass(node, 'exit', 'active') | ||
|
||
if (_onExiting) { | ||
_onExiting(maybeNode) | ||
} | ||
} | ||
|
||
const onExited = (maybeNode: any) => { | ||
const [node] = resolveArguments(maybeNode) | ||
removeClasses(node, 'exit') | ||
addClass(node, 'exit', 'done') | ||
|
||
if (_onExited) { | ||
_onExited(maybeNode) | ||
} | ||
} | ||
|
||
// when prop `nodeRef` is provided `node` is excluded | ||
const resolveArguments = (maybeNode: any, maybeAppearing: boolean) => | ||
_nodeRef | ||
? [_nodeRef.current, maybeNode] // here `maybeNode` is actually `appearing` | ||
: [maybeNode, maybeAppearing] // `findDOMNode` was used | ||
|
||
const getClassNames = (type: string) => { | ||
const isStringClassNames = typeof classNames === 'string' | ||
const prefix = isStringClassNames && classNames ? `${classNames}-` : '' | ||
|
||
const baseClassName = isStringClassNames | ||
? `${prefix}${type}` | ||
: classNames[type] | ||
|
||
const activeClassName = isStringClassNames | ||
? `${baseClassName}-active` | ||
: classNames[`${type}Active`] | ||
|
||
const doneClassName = isStringClassNames | ||
? `${baseClassName}-done` | ||
: classNames[`${type}Done`] | ||
|
||
return { | ||
baseClassName, | ||
activeClassName, | ||
doneClassName, | ||
} | ||
} | ||
|
||
const addClass = (node: HTMLElement | null, type: string, phase: string) => { | ||
let className = getClassNames(type)[`${phase}ClassName`] | ||
const { doneClassName } = getClassNames('enter') | ||
|
||
if (type === 'appear' && phase === 'done' && doneClassName) { | ||
className += ` ${doneClassName}` | ||
} | ||
|
||
// This is to force a repaint, | ||
// which is necessary in order to transition styles when adding a class name. | ||
if (phase === 'active') { | ||
if (node) forceReflow(node) | ||
} | ||
|
||
if (className) { | ||
appliedClasses[type][phase] = className | ||
addClassCommon(node, className) | ||
} | ||
} | ||
|
||
const removeClasses = (node: HTMLElement | null, type: string) => { | ||
const { | ||
base: baseClassName, | ||
active: activeClassName, | ||
done: doneClassName, | ||
} = appliedClasses[type] | ||
|
||
appliedClasses[type] = {} | ||
|
||
if (baseClassName) { | ||
removeClassCommon(node, baseClassName) | ||
} | ||
if (activeClassName) { | ||
removeClassCommon(node, activeClassName) | ||
} | ||
if (doneClassName) { | ||
removeClassCommon(node, doneClassName) | ||
} | ||
} | ||
|
||
return ( | ||
<Transition | ||
{...childProps} | ||
onEnter={onEnter} | ||
onEntered={onEntered} | ||
onEntering={onEntering} | ||
onExit={onExit} | ||
onExiting={onExiting} | ||
onExited={onExited} | ||
/> | ||
) | ||
} | ||
|
||
export default CSSTransition |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
类名管理逻辑实现良好,但存在小问题
addClass
函数正确处理了各个阶段的类名,包括强制重排以触发过渡效果。但第156行的实现有潜在问题:这里
phase
的值是 "base"、"active" 或 "done",但getClassNames
返回的对象键是baseClassName
、activeClassName
、doneClassName
。建议修正:🤖 Prompt for AI Agents