Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Graham Kaemmer committed May 8, 2017
0 parents commit 34472e4
Show file tree
Hide file tree
Showing 13 changed files with 234 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .babelrc
@@ -0,0 +1,3 @@
{
"presets": ["es2015", "stage-2", "react"]
}
5 changes: 5 additions & 0 deletions .gitignore
@@ -0,0 +1,5 @@
node_modules
yarn.lock

lib
dist
3 changes: 3 additions & 0 deletions .npmignore
@@ -0,0 +1,3 @@
example
webpack.config.js
yarn.lock
2 changes: 2 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,2 @@
### v0.0.1
- Initial version
31 changes: 31 additions & 0 deletions README.md
@@ -0,0 +1,31 @@
# react-fade-in
Dead-simple and opinionated component to fade in an element's children.
![React Fade In](/example/example.gif)

## Installation

`npm install react-fade-in`

## Usage

`react-fade-in`

```
import FadeIn from 'react-fade-in';
// ...
<FadeIn>
<div>Element 1</div>
<div>Element 2</div>
<div>Element 3</div>
<div>Element 4</div>
<div>Element 5</div>
<div>Element 6</div>
</FadeIn>
```

## API

### `FadeIn`
Animates its children, one by one. Takes no props. To have things animate at different times, they must be first-level children of the `<FadeIn>` component (i.e. members of `props.children`).

Happy fading.
37 changes: 37 additions & 0 deletions example/FadeInTest.js
@@ -0,0 +1,37 @@
import React, { Component } from 'react';
import FadeIn from '../src/index';
import styled from 'styled-components';

const Container = styled.div`
text-align: center;
padding: 20px;
font-family: Helvetica, Arial, sans-serif;
`;

const Title = styled.h1`
font-weight: normal;
font-size: 2em;
margin: 0 0 10px;
`;

const Element = styled.div`
line-height: 1.5em;
`;

export default class FadeInTest extends Component {
render() {
return (
<Container>
<Title>React Fade-In</Title>
<FadeIn>
<Element>Element 1</Element>
<Element>Element 2</Element>
<Element>Element 3</Element>
<Element>Element 4</Element>
<Element>Element 5</Element>
<Element>Element 6</Element>
</FadeIn>
</Container>
);
}
}
5 changes: 5 additions & 0 deletions example/client.js
@@ -0,0 +1,5 @@
import ReactDOM from 'react-dom';
import React from 'react';
import FadeInTest from './FadeInTest';

ReactDOM.render(<FadeInTest />, document.getElementById('root'));
Binary file added example/example.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions example/index.html
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>React Fade-In Example</title>
<style>
body {
margin: 0;
}
</style>
</head>
<body>
<div id="root"></div>
</body>
</html>
37 changes: 37 additions & 0 deletions package.json
@@ -0,0 +1,37 @@
{
"name": "react-fade-in",
"version": "0.1.4",
"description": "Super-easy fade-in animation for react children",
"main": "lib/index.js",
"repository": {
"url": "git@github.com:gkaemmer/react-fade-in.git",
"type": "git"
},
"scripts": {
"build": "npm run build:clean && npm run build:lib",
"build:clean": "rm -rf lib",
"build:lib": "babel src --out-dir lib",
"prepublish": "NODE_ENV=production npm run build",
"start": "EXAMPLE=true webpack-dev-server --port 3124 --hot --inline --content-base example/public"
},
"author": "Graham Kaemmer",
"license": "MIT",
"dependencies": {},
"devDependencies": {
"babel-cli": "^6.18.0",
"babel-core": "^6.20.0",
"babel-loader": "^6.2.9",
"babel-preset-es2015": "^6.18.0",
"babel-preset-react": "^6.16.0",
"babel-preset-stage-2": "^6.18.0",
"html-webpack-plugin": "^2.24.1",
"react": "^15.4.1",
"react-dom": "^15.4.1",
"styled-components": "^1.1.3",
"webpack": "^1.14.0",
"webpack-dev-server": "^1.16.2"
},
"peerDependencies": {
"react": "^15.4.1"
}
}
41 changes: 41 additions & 0 deletions src/FadeIn.js
@@ -0,0 +1,41 @@
import React, { Component } from "react";

export default class FadeIn extends Component {
state = {
maxIsVisible: 0
};

componentDidMount() {
const count = React.Children.count(this.props.children);
let i = 0;
this.interval = setInterval(() => {
i++;
if (i > count)
clearInterval(this.interval);

this.setState({ maxIsVisible: i });
}, 50);
}

render() {
const duration = 0.4;
return (
<div>
{React.Children.map(this.props.children, (child, i) => {
return (
<div
style={{
transition: `opacity ${duration}s, top ${duration}s`,
position: "relative",
top: this.state.maxIsVisible > i ? 0 : 20,
opacity: this.state.maxIsVisible > i ? 1 : 0
}}
>
{child}
</div>
);
})}
</div>
);
}
}
1 change: 1 addition & 0 deletions src/index.js
@@ -0,0 +1 @@
export { default } from './FadeIn';
52 changes: 52 additions & 0 deletions webpack.config.js
@@ -0,0 +1,52 @@
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var path = require('path');
var env = process.env.NODE_ENV;
var isExample = process.env.EXAMPLE;

var config = {
entry: {
'react-appear': path.join(__dirname, 'src', 'index.js')
},
output: {
path: path.join(__dirname, 'dist'),
filename: '[name].js'
},
module: {
loaders: [
{ test: /\.js$/, loaders: ['babel-loader'], exclude: /node_modules/ }
]
},
plugins: [
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(env)
})
]
};

if (env === 'production') {
config.plugins.push(
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
}
})
)
}

if (isExample) {
// Build example
config.entry = {
index: path.join(__dirname, 'example', 'client.js')
};
config.output.path = path.join(__dirname, 'example', 'public');
config.plugins.push(
new HtmlWebpackPlugin({
template: './example/index.html'
})
);
}


module.exports = config

0 comments on commit 34472e4

Please sign in to comment.