-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 进入视图才执行动画 * 入场动画支持配置重复 * 优化 * bugfxi * 样式微调
- Loading branch information
Showing
7 changed files
with
210 additions
and
88 deletions.
There are no files selected for viewing
This file contains 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 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,158 @@ | ||
import React, {useEffect, useMemo, useState, useCallback} from 'react'; | ||
import {CSSTransition} from 'react-transition-group'; | ||
import {Schema} from '../types'; | ||
import {formateId} from '../utils'; | ||
import {createAnimationStyle} from '../utils/animations'; | ||
import styleManager from '../StyleManager'; | ||
|
||
function Animations({ | ||
schema, | ||
component, | ||
show | ||
}: { | ||
schema: Schema; | ||
component: any; | ||
show: boolean; | ||
}) { | ||
const {enter} = schema.animations || {}; | ||
const [animationShow, setAnimationShow] = useState(!enter?.inView); | ||
const [placeholderShow, setPlaceholderShow] = useState(!!enter?.inView); | ||
|
||
const id = useMemo(() => formateId(schema.id), []); | ||
const observer = useMemo(newObserver, []); | ||
const animationClassNames = useMemo(initAnimationClassNames, []); | ||
const animationTimeout = useMemo(initAnimationTimeout, []); | ||
|
||
useEffect(() => { | ||
createAnimationStyle(id, schema.animations!); | ||
return () => { | ||
if (schema.animations) { | ||
styleManager.removeStyles(id); | ||
} | ||
observer.disconnect(); | ||
}; | ||
}, []); | ||
|
||
function newObserver() { | ||
return new IntersectionObserver( | ||
(entries, observer) => { | ||
entries.forEach(entry => { | ||
if ( | ||
entry.target.getAttribute('data-role') === 'animation-placeholder' | ||
) { | ||
if (entry.isIntersecting) { | ||
setAnimationShow(true); | ||
setPlaceholderShow(false); | ||
observer.unobserve(entry.target); | ||
} | ||
} else { | ||
if (!entry.isIntersecting) { | ||
setAnimationShow(false); | ||
observer.unobserve(entry.target); | ||
} | ||
} | ||
}); | ||
}, | ||
{ | ||
root: null, | ||
rootMargin: '0px', | ||
threshold: 0.1 | ||
} | ||
); | ||
} | ||
|
||
function initAnimationClassNames() { | ||
const animations = schema?.animations; | ||
const animationClassNames = { | ||
appear: '', | ||
enter: '', | ||
exit: '' | ||
}; | ||
if (animations) { | ||
if (animations.enter) { | ||
animationClassNames.enter = `${animations.enter.type}-${id}-enter`; | ||
animationClassNames.appear = animationClassNames.enter; | ||
} | ||
if (animations.exit) { | ||
animationClassNames.exit = `${animations.exit.type}-${id}-exit`; | ||
} | ||
} | ||
return animationClassNames; | ||
} | ||
|
||
function initAnimationTimeout() { | ||
const animations = schema?.animations; | ||
const animationTimeout = { | ||
enter: 0, | ||
exit: 0 | ||
}; | ||
if (animations) { | ||
if (animations.enter) { | ||
animationTimeout.enter = | ||
((animations.enter.duration || 1) + (animations.enter.delay || 0)) * | ||
1000; | ||
} | ||
if (animations.exit) { | ||
animationTimeout.exit = | ||
((animations.exit.duration || 1) + (animations.exit.delay || 0)) * | ||
1000; | ||
} | ||
} | ||
return animationTimeout; | ||
} | ||
|
||
function refFn(ref: HTMLElement | null) { | ||
if (ref) { | ||
observer.observe(ref); | ||
} | ||
} | ||
|
||
const handleEntered = useCallback((node: HTMLElement) => { | ||
const {attention, exit, enter} = schema.animations || {}; | ||
if (attention) { | ||
node.classList.add(`${attention.type}-${id}-attention`); | ||
} | ||
if (exit?.outView || enter?.repeat) { | ||
observer.observe(node); | ||
} | ||
}, []); | ||
|
||
const handleExit = useCallback((node: HTMLElement) => { | ||
const {attention} = schema.animations || {}; | ||
if (attention) { | ||
node.classList.remove(`${attention.type}-${id}-attention`); | ||
} | ||
}, []); | ||
|
||
const handleExited = useCallback(() => { | ||
setPlaceholderShow(true); | ||
}, []); | ||
|
||
return ( | ||
<> | ||
{!animationShow && show && placeholderShow && ( | ||
<div | ||
ref={refFn} | ||
className="amis-animation-placeholder" | ||
data-role="animation-placeholder" | ||
> | ||
{component} | ||
</div> | ||
)} | ||
<CSSTransition | ||
in={animationShow && show} | ||
timeout={animationTimeout} | ||
classNames={animationClassNames} | ||
onEntered={handleEntered} | ||
onExit={handleExit} | ||
onExited={handleExited} | ||
appear | ||
unmountOnExit | ||
> | ||
{component} | ||
</CSSTransition> | ||
</> | ||
); | ||
} | ||
|
||
export default Animations; |
This file contains 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 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 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
Oops, something went wrong.