├── .gitignore ├── .npmignore ├── README.md ├── example ├── basic.jsx ├── example.config.js ├── index.html ├── main.js └── server.config.js ├── mocha.opts ├── package.json ├── src └── fader.jsx └── tests ├── setup.js └── test_helper.js /.gitignore: -------------------------------------------------------------------------------- 1 | lib 2 | node_modules 3 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | src 2 | mocha.opts 3 | .gitignore 4 | tests 5 | example 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #Fader 2 | 3 | Fade a component in for x amount of seconds, then make it disappear, and completely hide (return null) until you choose to fade it again. 4 | 5 | ##Install 6 | 7 | `npm install fader` 8 | 9 | ##Example 10 | 11 | ```js 12 | //when you set this.state.fade to true, it's triggered 13 | 14 |
15 | Fading in and out! For two seconds! 16 |
17 |
18 | ``` 19 | 20 | The child component can be anything. 21 | 22 | ###Props 23 | 24 | - `time` in seconds, total time of the fade 25 | - `fade` every time it's set to true, it triggers a fade in and out 26 | -------------------------------------------------------------------------------- /example/basic.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import Fader from '../src/fader'; 3 | 4 | export default class Basic extends React.Component { 5 | 6 | constructor(){ 7 | super() 8 | this.state = { 9 | message: 'wait one second...' 10 | } 11 | } 12 | 13 | componentDidMount(){ 14 | setTimeout(() => { 15 | this.setState({ 16 | fade: true, 17 | message: 'setting fade to true again causes another fade (wait two more seconds)' 18 | }) 19 | this.fade() 20 | },2000) 21 | 22 | } 23 | 24 | fade(){ 25 | setTimeout(() => { 26 | this.setState({fade: true}) 27 | },7000) 28 | } 29 | 30 | render() { 31 | return ( 32 |
33 | {this.state.message} 34 | 35 |
36 | hey I'm fading in and out! For two seconds! 37 |
38 |
39 |
40 | ); 41 | } 42 | } 43 | 44 | React.render(, document.getElementById('react')); 45 | -------------------------------------------------------------------------------- /example/example.config.js: -------------------------------------------------------------------------------- 1 | var webpack = require('webpack'); 2 | var path = require('path'); 3 | 4 | module.exports = { 5 | entry: './example/basic.jsx', 6 | output: { 7 | path: __dirname, 8 | filename: "[name].js" 9 | }, 10 | resolve: { 11 | extensions: ['', '.js', '.jsx', '.es6'], 12 | modulesDirectories: ['node_modules'] 13 | }, 14 | module: { 15 | loaders: [ 16 | { test: /\.jsx$|\.es6$|\.js$/, loaders: ['babel-loader?stage=0'], exclude: /node_modules/ }, 17 | ] 18 | }, 19 | plugins: [ 20 | new webpack.NoErrorsPlugin() 21 | ], 22 | devtool: "eval-source-map" 23 | }; 24 | -------------------------------------------------------------------------------- /example/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Basic Example 4 | 5 | 6 | 7 |
8 |
9 |
10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /example/server.config.js: -------------------------------------------------------------------------------- 1 | var webpack = require('webpack'); 2 | var path = require('path'); 3 | 4 | module.exports = { 5 | entry: { 6 | 'basic': [ 7 | 'webpack-dev-server/client?http://localhost:8881/', 8 | 'webpack/hot/only-dev-server', 9 | './example/basic.jsx' 10 | ] 11 | }, 12 | output: { 13 | path: __dirname, 14 | filename: "[name].js", 15 | publicPath: 'http://localhost:8881/', 16 | chunkFilename: '[id].chunk.js', 17 | sourceMapFilename: '[name].map' 18 | }, 19 | resolve: { 20 | extensions: ['', '.js', '.jsx', '.es6'], 21 | modulesDirectories: ['node_modules'] 22 | }, 23 | module: { 24 | loaders: [ 25 | { test: /\.jsx$|\.es6$|\.js$/, loaders: ['react-hot', 'babel-loader?stage=0'], exclude: /node_modules/ }, 26 | { test: /\.scss$|\.css$/, loader: 'style-loader!style!css!sass' }, 27 | { test: /\.(jpe?g|png|gif)$/i, loader: 'url?limit=10000!img?progressive=true' } 28 | ] 29 | }, 30 | plugins: [ 31 | new webpack.NoErrorsPlugin() 32 | ], 33 | devtool: "eval-source-map" 34 | }; 35 | -------------------------------------------------------------------------------- /mocha.opts: -------------------------------------------------------------------------------- 1 | --require ./tests/setup 2 | --full-trace 3 | --compilers js:babel/register 4 | --recursive ./tests/**/*.jsx 5 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fader", 3 | "version": "0.0.1", 4 | "description": "fade in and out components", 5 | "main": "lib/fader.js", 6 | "scripts": { 7 | "start": "webpack-dev-server --config ./example/server.config.js --hot --port 8881", 8 | "test": "mocha --opts ./mocha.opts; eslint ./src/ --ext .jsx,.js --global require,exports:true", 9 | "compile": "babel src --stage 0 --out-dir lib;", 10 | "prepublish": "babel src --stage 0 --out-dir lib;", 11 | "example": "webpack --config ./example/example.config.js" 12 | }, 13 | "repository": { 14 | "type": "git", 15 | "url": "git repo" 16 | }, 17 | "keywords": [], 18 | "author": "Zach Silveira", 19 | "license": "ISC", 20 | "bugs": { 21 | "url": "issue page" 22 | }, 23 | "homepage": "homepage", 24 | "devDependencies": { 25 | "babel": "^5.8.23", 26 | "babel-core": "^5.8.23", 27 | "babel-eslint": "^4.1.1", 28 | "babel-loader": "^5.3.2", 29 | "chai": "^3.2.0", 30 | "eslint": "^0.24.1", 31 | "eslint-plugin-react": "^2.7.0", 32 | "expect": "^1.8.0", 33 | "legit-tests": "^0.4.1", 34 | "mocha": "^2.2.5", 35 | "mocha-babel": "^3.0.0", 36 | "react-hot-loader": "^1.3.0", 37 | "sinon": "^1.16.1", 38 | "webpack": "^1.12.1", 39 | "webpack-dev-server": "^1.10.1" 40 | }, 41 | "dependencies": { 42 | "react": "^0.13.3" 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/fader.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | export default class Fader extends React.Component{ 4 | 5 | constructor(){ 6 | super() 7 | this.state = { 8 | opacity: 0, 9 | } 10 | this.delays = [] 11 | this.running = false 12 | } 13 | 14 | componentDidMount(){ 15 | if(this.props.fade == true) this.fade() 16 | } 17 | 18 | componentWillReceiveProps(nextProps){ 19 | if(nextProps.fade == true) this.fade() 20 | } 21 | 22 | fade = () => { 23 | this.setState({show: true}) 24 | let time = parseFloat(this.props.time) * 1000 25 | let changeOpacity = time + 1000 26 | let hideComponent = changeOpacity + time + 200 27 | 28 | if(this.running) return false 29 | this.running = true 30 | 31 | this.delays.map(delay => window.clearTimeout) 32 | this.delays = [] 33 | this.delay({opacity: 1}, 10) 34 | this.delay({opacity: 0}, changeOpacity) 35 | this.delay({show: false}, hideComponent) 36 | 37 | } 38 | 39 | delay(state, time){ 40 | this.delays.push(setTimeout(() => { 41 | this.setState(state) 42 | if(state.show === false) this.running = false 43 | }, time)) 44 | } 45 | 46 | render(){ 47 | if(!this.state.show) return null 48 | let style = { 49 | opacity: this.state.opacity, 50 | transition: `opacity ${this.props.time}s ease-in-out`, 51 | ...this.props.style 52 | } 53 | return ( 54 |
55 | {this.props.children} 56 |
57 | ) 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /tests/setup.js: -------------------------------------------------------------------------------- 1 | /* globals global */ 2 | 3 | require("babel/register")({ 4 | stage: 0 5 | }); 6 | 7 | function propagateToGlobal (window) { 8 | for (let key in window) { 9 | if (!window.hasOwnProperty(key)) continue 10 | if (key in global) continue 11 | 12 | global[key] = window[key] 13 | } 14 | } 15 | 16 | var jsdom = require('node-jsdom'); 17 | 18 | var doc = jsdom.jsdom(''); 19 | var win = doc.defaultView; 20 | 21 | global.document = doc; 22 | global.window = win; 23 | 24 | propagateToGlobal(win); 25 | -------------------------------------------------------------------------------- /tests/test_helper.js: -------------------------------------------------------------------------------- 1 | import React from 'react/addons'; 2 | import { expect } from 'chai'; 3 | import { Test } from 'legit-tests'; 4 | import { SetState, Find } from 'legit-tests/lib/middleware'; 5 | import sinon from 'sinon'; 6 | 7 | let { TestUtils } = React.addons, 8 | shallowRenderer = TestUtils.createRenderer(); 9 | 10 | let createShallowComponent = (component, props) => { 11 | shallowRenderer.render(React.createElement(component, props)); 12 | return shallowRenderer.getRenderOutput(); 13 | } 14 | 15 | export { 16 | React, 17 | expect, 18 | TestUtils, 19 | createShallowComponent, 20 | Test, 21 | SetState, 22 | Find 23 | } 24 | --------------------------------------------------------------------------------