-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ref GH-1
- Loading branch information
Showing
5 changed files
with
134 additions
and
1 deletion.
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,7 @@ | ||
{ | ||
"name": "hz-fade-into-view", | ||
"version": "0.0.1", | ||
"description": "Fades into view its child component", | ||
"main": "index.js", | ||
"license": "MIT" | ||
} |
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,16 @@ | ||
```js | ||
<FadeIntoView direction="Up" activate={true}> | ||
<div>Boo!</div> | ||
</FadeIntoView> | ||
``` | ||
|
||
|
||
|
||
```js | ||
<FadeIntoView direction="Left" activate={true}> | ||
<div style={{backgroundColor: 'orange', display: 'inline-block', padding: 10}}> | ||
<a href="http://www.google.com">Go to Google</a> | ||
<p>Lorem ipsum dolor sit amet.</p> | ||
</div> | ||
</FadeIntoView> | ||
``` |
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,95 @@ | ||
// @flow | ||
import React, {Component} from 'react'; | ||
import {Motion, spring} from 'react-motion'; | ||
|
||
/** | ||
* Various Directions | ||
*/ | ||
type Direction = 'Up' | 'Down' | 'Left' | 'Right'; | ||
|
||
type Props = { | ||
/** | ||
* One node component that contains the content to fade in | ||
*/ | ||
children: React.Node, | ||
/** | ||
* The direction for the content to move into view. <br /> | ||
* Valid Directions are: "Up", "Down", "Left", "Right" | ||
*/ | ||
direction: Direction<string>, | ||
/** | ||
* Control whether not item should be active | ||
*/ | ||
activate?: boolean, | ||
/** | ||
* Initial position of content before it fades into view | ||
*/ | ||
offsetPosition?: number, | ||
}; | ||
|
||
type State = { | ||
activate: boolean, | ||
}; | ||
|
||
/** | ||
* Fades and moves its children into view. | ||
* @extends Component | ||
*/ | ||
class FadeIntoView extends Component<Props, State> { | ||
static defaultProps = { | ||
activate: false, | ||
offsetPosition: 100, | ||
}; | ||
|
||
getDirectionStyle(transformNum) { | ||
switch (this.props.direction) { | ||
case 'Up': | ||
return `translateY(${transformNum}px)`; | ||
case 'Down': | ||
return `translateY(-${transformNum}px)`; | ||
case 'Left': | ||
return `translateX(${transformNum}px)`; | ||
case 'Right': | ||
return `translateX(-${transformNum}px)`; | ||
default: | ||
throw 'Error'; | ||
} | ||
} | ||
|
||
render() { | ||
return ( | ||
<Motion | ||
defaultStyle={{ | ||
opacityNum: 0, | ||
transformNum: this.props.offsetPosition, | ||
}} | ||
style={{ | ||
opacityNum: spring(this.props.activate ? 1 : 0, { | ||
stiffness: 30, | ||
damping: 20, | ||
}), | ||
transformNum: spring( | ||
this.props.activate ? 0 : this.props.offsetPosition, | ||
{ | ||
stiffness: 120, | ||
damping: 20, | ||
}, | ||
), | ||
}} | ||
> | ||
{({opacityNum, transformNum}) => ( | ||
<div | ||
style={{ | ||
opacity: opacityNum, | ||
transform: this.getDirectionStyle(transformNum), | ||
}} | ||
> | ||
{this.props.children} | ||
</div> | ||
)} | ||
</Motion> | ||
); | ||
} | ||
} | ||
|
||
export default FadeIntoView; |
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