-
Notifications
You must be signed in to change notification settings - Fork 650
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: enter animations with mountOnEnter or unmountOnExit (#749)
* fix: enter animation with mountOnEnter or unmountOnExit * chore: add stories for mountOnEnter or unmountOnExit * chore: apply prettier * Update stories/CSSTransition.js
- Loading branch information
Showing
10 changed files
with
143 additions
and
5 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,11 @@ | ||
// polyfill for requestAnimationFrame | ||
const rAF = | ||
typeof window !== 'undefined' && | ||
typeof window.requestAnimationFrame === 'function' | ||
? window.requestAnimationFrame | ||
: (cb) => setTimeout(cb, 1); | ||
|
||
// https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame | ||
// Note: Your callback routine must itself call requestAnimationFrame() again | ||
// if you want to animate another frame at the next repaint. requestAnimationFrame() is 1 shot. | ||
export const nextTick = (cb) => rAF(() => rAF(cb)); |
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,50 @@ | ||
import React, { useState } from 'react'; | ||
import { storiesOf } from '@storybook/react'; | ||
|
||
import StoryFixture from './StoryFixture'; | ||
import Fade from './transitions/CSSFade'; | ||
|
||
function ToggleFixture({ defaultIn, description, children }) { | ||
const [show, setShow] = useState(defaultIn || false); | ||
|
||
return ( | ||
<StoryFixture description={description}> | ||
<div style={{ marginBottom: 10 }}> | ||
<button | ||
onClick={() => { | ||
setShow(!show); | ||
}} | ||
> | ||
Toggle | ||
</button> | ||
</div> | ||
{React.cloneElement(children, { in: show })} | ||
</StoryFixture> | ||
); | ||
} | ||
|
||
storiesOf('CSSTransition', module) | ||
.add('Fade', () => ( | ||
<ToggleFixture> | ||
<Fade>asaghasg asgasg</Fade> | ||
</ToggleFixture> | ||
)) | ||
.add('Fade with appear', () => ( | ||
<ToggleFixture defaultIn> | ||
<Fade appear>asaghasg asgasg</Fade> | ||
</ToggleFixture> | ||
)) | ||
.add('Fade with mountOnEnter', () => { | ||
return ( | ||
<ToggleFixture> | ||
<Fade mountOnEnter>Fade with mountOnEnter</Fade> | ||
</ToggleFixture> | ||
); | ||
}) | ||
.add('Fade with unmountOnExit', () => { | ||
return ( | ||
<ToggleFixture> | ||
<Fade unmountOnExit>Fade with unmountOnExit</Fade> | ||
</ToggleFixture> | ||
); | ||
}); |
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
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import './Transition'; | ||
import './CSSTransition'; | ||
import './TransitionGroup'; | ||
import './ReplaceTransition'; |
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,53 @@ | ||
import { css } from 'astroturf'; | ||
import React, { useRef } from 'react'; | ||
|
||
import CSSTransition from '../../src/CSSTransition'; | ||
|
||
export const FADE_TIMEOUT = 1000; | ||
|
||
const styles = css` | ||
.default { | ||
opacity: 0; | ||
} | ||
.enter-done { | ||
opacity: 1; | ||
} | ||
.enter, | ||
.appear { | ||
opacity: 0.01; | ||
} | ||
.enter.enter-active, | ||
.appear.appear-active { | ||
opacity: 1; | ||
transition: opacity ${FADE_TIMEOUT}ms ease-in; | ||
} | ||
.exit { | ||
opacity: 1; | ||
} | ||
.exit.exit-active { | ||
opacity: 0.01; | ||
transition: opacity ${0.8 * FADE_TIMEOUT}ms ease-in; | ||
} | ||
`; | ||
|
||
const defaultProps = { | ||
in: false, | ||
timeout: FADE_TIMEOUT, | ||
}; | ||
function Fade(props) { | ||
const nodeRef = useRef(); | ||
return ( | ||
<CSSTransition {...props} classNames={styles} nodeRef={nodeRef}> | ||
<div ref={nodeRef} className={styles.default}> | ||
{props.children} | ||
</div> | ||
</CSSTransition> | ||
); | ||
} | ||
|
||
Fade.defaultProps = defaultProps; | ||
|
||
export default Fade; |
File renamed without changes.