├── .eslintignore ├── .prettierignore ├── examples └── src │ ├── styles │ ├── fonts │ │ ├── font.css │ │ ├── Archia-Regular.eot │ │ ├── Archia-Regular.ttf │ │ ├── Campton-Bold.eot │ │ ├── Campton-Bold.ttf │ │ ├── Campton-Bold.woff │ │ ├── Campton-Bold.woff2 │ │ ├── Archia-Regular.woff │ │ ├── Archia-Regular.woff2 │ │ ├── Archia-SemiBold.eot │ │ ├── Archia-SemiBold.ttf │ │ ├── Archia-SemiBold.woff │ │ └── Archia-SemiBold.woff2 │ ├── global.css │ └── normalize.css │ ├── components │ ├── Clock │ │ ├── 0.png │ │ ├── 1.png │ │ ├── 2.png │ │ ├── 3.png │ │ ├── 4.png │ │ ├── 5.png │ │ ├── 6.png │ │ ├── 7.png │ │ ├── 8.png │ │ ├── 9.png │ │ ├── digits.js │ │ └── index.js │ └── GithubIcon.js │ ├── index.html │ └── index.js ├── .prettierrc ├── .gitignore ├── .npmignore ├── .babelrc ├── jest.config.js ├── .eslintrc ├── webpack.config.js ├── src ├── setupTests.js ├── index.js ├── theme │ ├── keyframes.css │ └── index.js ├── hooks │ ├── usePreloadImage.js │ └── usePreloadImage.test.js ├── HideUntilLoaded │ ├── index.js │ └── HideUntilLoaded.test.js ├── AnimateOnChange │ ├── index.js │ └── AnimateOnChange.test.js └── AnimateGroup │ ├── index.js │ └── AnimateGroup.test.js ├── .circleci └── config.yml ├── webpack.dist.config.js ├── webpack.common.js ├── package.json ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── README.md └── LICENSE /.eslintignore: -------------------------------------------------------------------------------- 1 | dist 2 | -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | dist 2 | -------------------------------------------------------------------------------- /examples/src/styles/fonts/font.css: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "printWidth": 80, 3 | "singleQuote": true, 4 | "semi": false 5 | } 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_STORE 2 | node_modules 3 | dist 4 | package-lock.json 5 | yarn.lock 6 | coverage 7 | .*.swp 8 | -------------------------------------------------------------------------------- /examples/src/components/Clock/0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nearform/react-animation/HEAD/examples/src/components/Clock/0.png -------------------------------------------------------------------------------- /examples/src/components/Clock/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nearform/react-animation/HEAD/examples/src/components/Clock/1.png -------------------------------------------------------------------------------- /examples/src/components/Clock/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nearform/react-animation/HEAD/examples/src/components/Clock/2.png -------------------------------------------------------------------------------- /examples/src/components/Clock/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nearform/react-animation/HEAD/examples/src/components/Clock/3.png -------------------------------------------------------------------------------- /examples/src/components/Clock/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nearform/react-animation/HEAD/examples/src/components/Clock/4.png -------------------------------------------------------------------------------- /examples/src/components/Clock/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nearform/react-animation/HEAD/examples/src/components/Clock/5.png -------------------------------------------------------------------------------- /examples/src/components/Clock/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nearform/react-animation/HEAD/examples/src/components/Clock/6.png -------------------------------------------------------------------------------- /examples/src/components/Clock/7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nearform/react-animation/HEAD/examples/src/components/Clock/7.png -------------------------------------------------------------------------------- /examples/src/components/Clock/8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nearform/react-animation/HEAD/examples/src/components/Clock/8.png -------------------------------------------------------------------------------- /examples/src/components/Clock/9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nearform/react-animation/HEAD/examples/src/components/Clock/9.png -------------------------------------------------------------------------------- /examples/src/styles/fonts/Archia-Regular.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nearform/react-animation/HEAD/examples/src/styles/fonts/Archia-Regular.eot -------------------------------------------------------------------------------- /examples/src/styles/fonts/Archia-Regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nearform/react-animation/HEAD/examples/src/styles/fonts/Archia-Regular.ttf -------------------------------------------------------------------------------- /examples/src/styles/fonts/Campton-Bold.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nearform/react-animation/HEAD/examples/src/styles/fonts/Campton-Bold.eot -------------------------------------------------------------------------------- /examples/src/styles/fonts/Campton-Bold.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nearform/react-animation/HEAD/examples/src/styles/fonts/Campton-Bold.ttf -------------------------------------------------------------------------------- /examples/src/styles/fonts/Campton-Bold.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nearform/react-animation/HEAD/examples/src/styles/fonts/Campton-Bold.woff -------------------------------------------------------------------------------- /examples/src/styles/fonts/Campton-Bold.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nearform/react-animation/HEAD/examples/src/styles/fonts/Campton-Bold.woff2 -------------------------------------------------------------------------------- /examples/src/styles/fonts/Archia-Regular.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nearform/react-animation/HEAD/examples/src/styles/fonts/Archia-Regular.woff -------------------------------------------------------------------------------- /examples/src/styles/fonts/Archia-Regular.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nearform/react-animation/HEAD/examples/src/styles/fonts/Archia-Regular.woff2 -------------------------------------------------------------------------------- /examples/src/styles/fonts/Archia-SemiBold.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nearform/react-animation/HEAD/examples/src/styles/fonts/Archia-SemiBold.eot -------------------------------------------------------------------------------- /examples/src/styles/fonts/Archia-SemiBold.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nearform/react-animation/HEAD/examples/src/styles/fonts/Archia-SemiBold.ttf -------------------------------------------------------------------------------- /examples/src/styles/fonts/Archia-SemiBold.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nearform/react-animation/HEAD/examples/src/styles/fonts/Archia-SemiBold.woff -------------------------------------------------------------------------------- /examples/src/styles/fonts/Archia-SemiBold.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nearform/react-animation/HEAD/examples/src/styles/fonts/Archia-SemiBold.woff2 -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | src 2 | examples 3 | .babelrc 4 | .eslintrc 5 | .eslintignore 6 | .prettierignore 7 | .prettierrc 8 | jest.config.js 9 | .gitignore 10 | webpack.config.js 11 | coverage 12 | -------------------------------------------------------------------------------- /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [["env", { "modules": false }], "react"], 3 | "plugins": ["transform-object-rest-spread"], 4 | "ignore": [], 5 | "env": { 6 | "test": { 7 | "presets": [["env"], "react"], 8 | "plugins": ["transform-es2015-modules-commonjs"] 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | verbose: true, 3 | setupTestFrameworkScriptFile: '/src/setupTests.js', 4 | moduleNameMapper: { 5 | '.+\\.(css|png|jpg)$': 'identity-obj-proxy' 6 | }, 7 | coverageReporters: ['json-summary', 'text', 'lcov'], 8 | collectCoverage: true 9 | } 10 | -------------------------------------------------------------------------------- /examples/src/components/Clock/digits.js: -------------------------------------------------------------------------------- 1 | import d0 from './0.png' 2 | import d1 from './1.png' 3 | import d2 from './2.png' 4 | import d3 from './3.png' 5 | import d4 from './4.png' 6 | import d5 from './5.png' 7 | import d6 from './6.png' 8 | import d7 from './7.png' 9 | import d8 from './8.png' 10 | import d9 from './9.png' 11 | 12 | const digits = [ d0, d1, d2, d3, d4, d5, d6, d7, d8, d9 ] 13 | 14 | export default digits 15 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "es6": true, 4 | "browser": true, 5 | "jest": true, 6 | "serviceworker": true 7 | }, 8 | "extends": ["standard", "standard-react", "prettier", "prettier/react"], 9 | "parser": "babel-eslint", 10 | "rules": { 11 | "strict": 0, 12 | "jest/no-focused-tests": "error", 13 | "react-hooks/rules-of-hooks": "error" 14 | }, 15 | "globals": { 16 | "fixture": true 17 | }, 18 | "plugins": ["jest", "react-hooks"] 19 | } 20 | -------------------------------------------------------------------------------- /examples/src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | React Animation 👌 4 | 5 | 6 | 7 | 8 | 9 | 12 |
13 | 14 | 15 | -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | const path = require('path') 2 | const HtmlWebpackPlugin = require('html-webpack-plugin') 3 | const merge = require('webpack-merge') 4 | const commonConfig = require('./webpack.common') 5 | const htmlWebpackPlugin = new HtmlWebpackPlugin({ 6 | template: path.join(__dirname, 'examples/src/index.html'), 7 | filename: './index.html' 8 | }) 9 | module.exports = merge(commonConfig, { 10 | entry: path.join(__dirname, 'examples/src/index.js'), 11 | output: { 12 | path: path.join(__dirname, 'examples/dist'), 13 | filename: 'bundle.js' 14 | }, 15 | plugins: [htmlWebpackPlugin], 16 | devServer: { 17 | port: 3000 18 | } 19 | }) 20 | -------------------------------------------------------------------------------- /src/setupTests.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 NearForm Ltd. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | const { configure } = require('enzyme') 18 | const Adapter = require('enzyme-adapter-react-16') 19 | 20 | configure({ adapter: new Adapter() }) 21 | -------------------------------------------------------------------------------- /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | # Javascript Node CircleCI 2.0 configuration file 2 | # 3 | # Check https://circleci.com/docs/2.0/language-javascript/ for more details 4 | # 5 | version: 2 6 | jobs: 7 | build: 8 | working_directory: ~/react-animation 9 | docker: 10 | - image: circleci/node:10.15-browsers 11 | steps: 12 | - checkout 13 | # Download and cache dependencies 14 | - restore_cache: 15 | key: dependency-cache-{{ checksum "package.json" }} 16 | - run: npm install 17 | - save_cache: 18 | key: dependency-cache-{{ checksum "package.json" }} 19 | paths: 20 | - node_modules 21 | - run: npm run test:coverage 22 | - run: | 23 | npm install coveralls 24 | cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js 25 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 NearForm Ltd. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | export { default as AnimateOnChange } from './AnimateOnChange' 18 | export { default as HideUntilLoaded } from './HideUntilLoaded' 19 | export { default as AnimateGroup } from './AnimateGroup' 20 | export { animations } from './theme' 21 | export { easings } from './theme' 22 | -------------------------------------------------------------------------------- /examples/src/components/GithubIcon.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | 3 | const GithubIcon = props => ( 4 | 16 | 17 | 25 | 29 | 30 | ) 31 | 32 | export default GithubIcon 33 | -------------------------------------------------------------------------------- /webpack.dist.config.js: -------------------------------------------------------------------------------- 1 | const path = require('path') 2 | const merge = require('webpack-merge') 3 | const commonConfig = require('./webpack.common') 4 | const CopyWebpackPlugin = require('copy-webpack-plugin') 5 | 6 | module.exports = merge(commonConfig, { 7 | entry: path.join(__dirname, './src/index.js'), 8 | output: { 9 | path: path.join(__dirname, './dist'), 10 | filename: 'react-animation.js', 11 | library: 'ReactAnimation', 12 | libraryTarget: 'umd', 13 | publicPath: '/dist/', 14 | umdNamedDefine: true, 15 | globalObject: 'this' 16 | }, 17 | externals: { 18 | // Don't bundle react or react-dom 19 | react: { 20 | commonjs: 'react', 21 | commonjs2: 'react', 22 | amd: 'React', 23 | root: 'React' 24 | }, 25 | 'react-dom': { 26 | commonjs: 'react-dom', 27 | commonjs2: 'react-dom', 28 | amd: 'ReactDOM', 29 | root: 'ReactDOM' 30 | } 31 | }, 32 | plugins: [ 33 | new CopyWebpackPlugin([ 34 | { 35 | from: 'src/theme/keyframes.css', 36 | to: '' 37 | } 38 | ]) 39 | ] 40 | }) 41 | -------------------------------------------------------------------------------- /src/theme/keyframes.css: -------------------------------------------------------------------------------- 1 | @keyframes fade-in { 2 | from { 3 | opacity: 0; 4 | visibility: hidden; 5 | } 6 | to { 7 | opacity: 1; 8 | visibility: visible; 9 | } 10 | } 11 | 12 | @keyframes fade-out { 13 | from { 14 | opacity: 1; 15 | visibility: visible; 16 | } 17 | to { 18 | opacity: 0; 19 | visibility: hidden; 20 | } 21 | } 22 | 23 | @keyframes fade-in-up { 24 | from { 25 | opacity: 0; 26 | transform: translateY(10em); 27 | visibility: hidden; 28 | } 29 | to { 30 | opacity: 1; 31 | transform: none; 32 | visibility: visible; 33 | } 34 | } 35 | 36 | @keyframes pop-in { 37 | 0% { 38 | opacity: 0; 39 | transform: scale(0); 40 | } 41 | 1% { 42 | opacity: 1; 43 | } 44 | 100% { 45 | opacity: 1; 46 | transform: none; 47 | } 48 | } 49 | 50 | @keyframes pop-out { 51 | 0% { 52 | opacity: 1; 53 | transform: none; 54 | } 55 | 99% { 56 | opacity: 0; 57 | } 58 | 100% { 59 | opacity: 0; 60 | transform: scale(0); 61 | } 62 | } 63 | 64 | @keyframes slide-in { 65 | 0% { 66 | transform: translateY(100%); 67 | } 68 | 100% { 69 | transform: none; 70 | } 71 | } 72 | 73 | @keyframes slide-out { 74 | 0% { 75 | transform: translateY(0%); 76 | } 77 | 100% { 78 | transform: translateY(-100%); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /webpack.common.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | module: { 3 | rules: [ 4 | { 5 | test: /\.(js|jsx)$/, 6 | use: 'babel-loader', 7 | exclude: /node_modules/ 8 | }, 9 | { 10 | test: /\.css$/, 11 | use: ['style-loader','css-loader'] 12 | }, 13 | { 14 | test: /\.(gif|png|jpe?g|svg)$/i, 15 | use: [ 16 | 'file-loader', 17 | { 18 | loader: 'image-webpack-loader', 19 | options: { 20 | bypassOnDebug: true, // webpack@1.x 21 | disable: true, // webpack@2.x and newer 22 | } 23 | } 24 | ] 25 | }, 26 | { 27 | test: /\.(woff|woff2)(\?v=\d+\.\d+\.\d+)?$/, 28 | use: [ 29 | { 30 | loader: 'url-loader', 31 | options: { 32 | limit: 8192 33 | } 34 | } 35 | ] 36 | }, 37 | { 38 | test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, 39 | use: [ 40 | { 41 | loader: 'url-loader', 42 | options: { 43 | limit: 8192 44 | } 45 | } 46 | ] 47 | }, 48 | { 49 | test: /\.(eot)(\?v=\d+\.\d+\.\d+)?$/, 50 | use: [ 51 | { 52 | loader: 'url-loader', 53 | options: { 54 | limit: 8192 55 | } 56 | } 57 | ] 58 | } 59 | ] 60 | }, 61 | resolve: { 62 | extensions: ['.js', '.jsx'] 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/hooks/usePreloadImage.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 NearForm Ltd. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import { useLayoutEffect, useState } from 'react' 18 | 19 | export const eventWrapper = methodToCall => () => methodToCall(true) 20 | 21 | const usePreloadImage = imageToLoad => { 22 | const [loaded, setLoaded] = useState(true) // so that it renders on server 23 | const [errored, setErrored] = useState(false) 24 | useLayoutEffect( 25 | () => { 26 | if (typeof window === 'object' && imageToLoad) { 27 | setLoaded(false) 28 | setErrored(false) 29 | // Add a listener to wait until the preloadImage is ready 30 | const img = document.createElement('img') 31 | img.src = imageToLoad 32 | // On load, or on error, continue to show the component 33 | img.onload = eventWrapper(setLoaded) 34 | img.onerror = eventWrapper(setErrored) 35 | } 36 | }, 37 | [imageToLoad] 38 | ) 39 | return [errored, loaded] 40 | } 41 | 42 | export default usePreloadImage 43 | -------------------------------------------------------------------------------- /examples/src/components/Clock/index.js: -------------------------------------------------------------------------------- 1 | import React, { useEffect, useState } from 'react' 2 | import ReactDOM from 'react-dom' 3 | import AnimateGroup from '../../../../src/AnimateGroup' 4 | import digits from './digits' 5 | 6 | function ClockSegment({ value }) { 7 | 8 | const [ d0, d1 ] = (''+value).padStart( 2, '0') 9 | 10 | return ( 11 | 12 | 13 | ) 14 | } 15 | 16 | const ClockStyle = { 17 | color: 'red', 18 | backgroundColor: 'black', 19 | fontSize: '5em' 20 | } 21 | 22 | const pad = v => (''+v).padStart( 2, '0') 23 | 24 | function Clock( props ) { 25 | 26 | const { 27 | animation 28 | } = props 29 | 30 | const [ hour, setHour ] = useState( 0 ) 31 | const [ mins, setMins ] = useState( 0 ) 32 | const [ secs, setSecs ] = useState( 0 ) 33 | const [ started, setStarted ] = useState( false ) 34 | 35 | useEffect( () => { 36 | if( !started ) { 37 | const incTime = () => { 38 | const time = new Date() 39 | setHour( time.getHours() ) 40 | setMins( time.getMinutes() ) 41 | setSecs( time.getSeconds() ) 42 | } 43 | incTime() 44 | setInterval( incTime, 1000 ) 45 | setStarted( true ) 46 | } 47 | }) 48 | 49 | const [ h0, h1 ] = pad( hour ) 50 | const [ m0, m1 ] = pad( mins ) 51 | const [ s0, s1 ] = pad( secs ) 52 | 53 | return (
54 | 55 | 56 | 57 | : 58 | 59 | 60 | : 61 | 62 | 63 | 64 |
) 65 | } 66 | 67 | export default Clock 68 | -------------------------------------------------------------------------------- /src/hooks/usePreloadImage.test.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 NearForm Ltd. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import React from 'react' 18 | import PropTypes from 'prop-types' 19 | import { mount } from 'enzyme' 20 | import usePreloadImage, { eventWrapper } from './usePreloadImage' 21 | 22 | const TestComponent = ({ imageToLoad }) => { 23 | const [errored, loaded] = usePreloadImage(imageToLoad) 24 | return ( 25 |
26 | ) 27 | } 28 | 29 | TestComponent.propTypes = { 30 | imageToLoad: PropTypes.string 31 | } 32 | 33 | describe('usePreloadImage', () => { 34 | it('should return false by default for initial loaded states', () => { 35 | const component = mount() 36 | expect(component.find('.test-div').get(0).props.errored).toEqual('false') 37 | expect(component.find('.test-div').get(0).props.loaded).toEqual('false') 38 | }) 39 | 40 | it('should return the default loaded state when no image supplied', () => { 41 | const component = mount() 42 | expect(component.find('.test-div').get(0).props.errored).toEqual('false') 43 | expect(component.find('.test-div').get(0).props.loaded).toEqual('true') 44 | }) 45 | 46 | it('should call the setError and setLoaded methods as true in the wrapper', () => { 47 | const testFn = jest.fn() 48 | eventWrapper(testFn)() 49 | expect(testFn).toHaveBeenCalledWith(true) 50 | }) 51 | }) 52 | -------------------------------------------------------------------------------- /examples/src/styles/global.css: -------------------------------------------------------------------------------- 1 | /* Global style rules */ 2 | @font-face { 3 | font-family: 'Archia-Regular'; 4 | src: url('./fonts/Archia-Regular.eot'); 5 | src: url('./fonts/Archia-Regular.eot?#iefix') format('embedded-opentype'), 6 | url('./fonts/Archia-Regular.ttf') format('truetype'), 7 | url('./fonts/Archia-Regular.woff') format('woff'), 8 | url('./fonts/Archia-Regular.woff2') format('woff2'); 9 | font-weight: normal; 10 | font-style: normal; 11 | } 12 | 13 | @font-face { 14 | font-family: 'Archia-SemiBold'; 15 | src: url('./fonts/Archia-SemiBold.eot'); 16 | src: url('./fonts/Archia-SemiBold.eot?#iefix') format('embedded-opentype'), 17 | url('./fonts/Archia-SemiBold.ttf') format('truetype'), 18 | url('./fonts/Archia-SemiBold.woff') format('woff'), 19 | url('./fonts/Archia-SemiBold.woff2') format('woff2'); 20 | font-weight: normal; 21 | font-style: normal; 22 | } 23 | 24 | @font-face { 25 | font-family: 'Campton-Bold'; 26 | src: url('./fonts/Campton-Bold.eot'); 27 | src: url('./fonts/Campton-Bold.eot?#iefix') format('embedded-opentype'), 28 | url('./fonts/Campton-Bold.ttf') format('truetype'), 29 | url('./fonts/Campton-Bold.woff') format('woff'), 30 | url('./fonts/Campton-Bold.woff2') format('woff2'); 31 | font-weight: normal; 32 | font-style: normal; 33 | } 34 | 35 | body { 36 | background-image: linear-gradient( 37 | to left bottom, 38 | #d02018, 39 | #d5003f, 40 | #cd0064, 41 | #b70087, 42 | #901ca7, 43 | #7a29aa, 44 | #6232aa, 45 | #4637a9, 46 | #522f95, 47 | #572982, 48 | #582470, 49 | #552060 50 | ); 51 | background-attachment: fixed; 52 | background-size: cover; 53 | font-family: 'Archia-Regular', sans-serif; 54 | line-height: 1.75; 55 | min-height: 100vh; 56 | width: 100vw; 57 | } 58 | 59 | h1 { 60 | font-family: 'Campton-Bold', sans-serif; 61 | font-size: 42px; 62 | line-height: 52px; 63 | } 64 | 65 | p { 66 | font-size: 16px; 67 | margin-block-start: 0em; 68 | } 69 | 70 | h2, 71 | h3, 72 | h4 { 73 | font-family: 'Archia-SemiBold', sans-serif; 74 | margin-bottom: 0.5em; 75 | } 76 | -------------------------------------------------------------------------------- /src/theme/index.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 NearForm Ltd. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // Themed animations and easings to be used by the ui animation utilities 18 | 19 | // Timing functions a defined on easings.net 20 | export const easings = { 21 | linear: 'linear', 22 | easeInSine: 'cubic-bezier(0.47, 0, 0.745, 0.715)', 23 | easeOutSine: 'cubic-bezier(0.39, 0.575, 0.565, 1)', 24 | easeInOutSine: 'cubic-bezier(0.445, 0.05, 0.55, 0.95)', 25 | easeInQuad: 'cubic-bezier(0.55, 0.085, 0.68, 0.53)', 26 | easeOutQuad: 'cubic-bezier(0.25, 0.46, 0.45, 0.94)', 27 | easeInOutQuad: 'cubic-bezier(0.455, 0.03, 0.515, 0.955)', 28 | easeInCubic: 'cubic-bezier(0.55, 0.055, 0.675, 0.19)', 29 | easeOutCubic: 'cubic-bezier(0.215, 0.61, 0.355, 1)', 30 | easeInOutCubic: 'cubic-bezier(0.645, 0.045, 0.355, 1)', 31 | easeInQuart: 'cubic-bezier(0.895, 0.03, 0.685, 0.22)', 32 | easeOutQuart: 'cubic-bezier(0.165, 0.84, 0.44, 1)', 33 | easeInOutQuart: 'cubic-bezier(0.77, 0, 0.175, 1)', 34 | easeInQuint: 'cubic-bezier(0.755, 0.05, 0.855, 0.06)', 35 | easeOutQuint: 'cubic-bezier(0.23, 1, 0.32, 1)', 36 | easeInOutQuint: 'cubic-bezier(0.86, 0, 0.07, 1)', 37 | easeInExpo: 'cubic-bezier(0.95, 0.05, 0.795, 0.035)', 38 | easeOutExpo: 'cubic-bezier(0.19, 1, 0.22, 1)', 39 | easeInOutExpo: 'cubic-bezier(1, 0, 0, 1)', 40 | easeInBack: 'cubic-bezier(0.6, -0.28, 0.735, 0.045)', 41 | easeOutBack: 'cubic-bezier(0.175, 0.885, 0.32, 1.275)', 42 | easeInOutBack: 'cubic-bezier(0.68, -0.55, 0.265, 1.55)' 43 | } 44 | 45 | export const animations = { 46 | fadeIn: `fade-in 500ms ease-out forwards`, 47 | fadeOut: `fade-out 400ms ease-out forwards`, 48 | fadeInUp: `fade-in-up 800ms ${easings.easeOutExpo} forwards`, 49 | popIn: `pop-in 500ms ${easings.easeOutExpo} forwards`, 50 | popOut: `pop-out 400ms ${easings.easeOutBack} forwards`, 51 | bounceIn: `pop-in 300ms ${easings.easeOutBack} forwards`, 52 | bounceOut: `pop-out 300ms ${easings.easeInBack} forwards`, 53 | slideIn: `slide-in 500ms ${easings.easeInOutBack} forwards`, 54 | slideOut: `slide-out 350ms ${easings.easeOutBack} forwards` 55 | } 56 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-animation", 3 | "version": "1.2.2", 4 | "description": "Helpful animation components for adding movement to your React components", 5 | "main": "dist/react-animation.js", 6 | "files": [ 7 | "react-animation.js", 8 | "keyframes.css" 9 | ], 10 | "scripts": { 11 | "test": "jest", 12 | "test:watch": "jest --watch", 13 | "test:coverage": "jest --coverage", 14 | "start": "webpack-dev-server --mode development", 15 | "prebuild": "rm -rf ./dist", 16 | "build": "webpack --config webpack.dist.config.js", 17 | "dev": "npm start", 18 | "deploy": "gh-pages -d examples/dist", 19 | "build-demo": "webpack", 20 | "publish-demo": "npm run build && npm run build-demo && npm run deploy", 21 | "prepublishOnly": "npm run build" 22 | }, 23 | "repository": { 24 | "type": "git", 25 | "url": "git+https://github.com/nearform/react-animation.git" 26 | }, 27 | "keywords": [ 28 | "react", 29 | "animation", 30 | "keyframes", 31 | "timing-functions" 32 | ], 33 | "author": "NearForm", 34 | "license": "Apache-2.0", 35 | "peerDependencies": { 36 | "react": "^16.8.0", 37 | "react-dom": "^16.8.0" 38 | }, 39 | "bugs": { 40 | "url": "https://github.com/nearform/react-animation/issues" 41 | }, 42 | "homepage": "https://github.com/nearform/react-animation#readme", 43 | "devDependencies": { 44 | "babel-cli": "^6.26.0", 45 | "babel-core": "^6.26.3", 46 | "babel-eslint": "^10.0.1", 47 | "babel-jest": "^23.6.0", 48 | "babel-loader": "^7.1.4", 49 | "babel-plugin-transform-object-rest-spread": "^6.26.0", 50 | "babel-preset-env": "^1.7.0", 51 | "babel-preset-react": "^6.24.1", 52 | "copy-webpack-plugin": "^4.6.0", 53 | "css-loader": "^2.1.0", 54 | "enzyme": "^3.8.0", 55 | "enzyme-adapter-react-16": "^1.7.1", 56 | "eslint": "^5.12.0", 57 | "eslint-config-prettier": "^3.5.0", 58 | "eslint-config-standard": "^12.0.0", 59 | "eslint-config-standard-react": "^7.0.2", 60 | "eslint-plugin-import": "^2.14.0", 61 | "eslint-plugin-jest": "^22.1.3", 62 | "eslint-plugin-node": "^8.0.1", 63 | "eslint-plugin-promise": "^4.0.1", 64 | "eslint-plugin-react": "^7.12.4", 65 | "eslint-plugin-react-hooks": "0.0.0", 66 | "eslint-plugin-standard": "^4.0.0", 67 | "file-loader": "^3.0.1", 68 | "gh-pages": "^2.0.1", 69 | "html-webpack-plugin": "^3.2.0", 70 | "identity-obj-proxy": "^3.0.0", 71 | "image-webpack-loader": "^5.0.0", 72 | "jest": "^23.6.0", 73 | "react": "^16.8.0", 74 | "react-dom": "^16.8.0", 75 | "react-lazyload": "^2.3.0", 76 | "react-test-renderer": "^16.7.0", 77 | "style-loader": "^0.23.1", 78 | "styled-components": "^4.3.2", 79 | "url-loader": "^1.1.2", 80 | "webpack": "^4.28.4", 81 | "webpack-cli": "^3.2.1", 82 | "webpack-dev-server": "^3.1.14", 83 | "webpack-merge": "^4.2.1" 84 | }, 85 | "dependencies": { 86 | "regenerator-runtime": "^0.13.1", 87 | "react-transition-group": "^2.7.0" 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/HideUntilLoaded/index.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 NearForm Ltd. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import React from 'react' 18 | import PropTypes from 'prop-types' 19 | import usePreloadImage from '../hooks/usePreloadImage' 20 | import { animations, easings } from '../theme' 21 | import '../theme/keyframes.css' 22 | 23 | /** 24 | * Animates in a component once an image has loaded. 25 | * 26 | * Properties: 27 | * children 28 | * animationIn: {String} A named animation as defined in the theme animations 29 | * Spinner: {Component} An optional spinner component 30 | * imageToLoad: {String} A URL of the image being loaded 31 | * style: {Object} Custom style rules as required 32 | */ 33 | 34 | const HideUntilLoaded = ({ 35 | animationIn, 36 | children, 37 | Spinner, 38 | imageToLoad, 39 | style 40 | }) => { 41 | const [errored, loaded] = usePreloadImage(imageToLoad) 42 | 43 | const styles = { 44 | display: 'inline-block', 45 | position: 'relative', 46 | ...style 47 | } 48 | 49 | const contentStyles = { 50 | transition: 'none' 51 | } 52 | 53 | if (!loaded && !errored && imageToLoad) { 54 | contentStyles.opacity = 0 55 | contentStyles.visibility = 'hidden' 56 | } else { 57 | if (animationIn) { 58 | contentStyles.animation = animations[animationIn] || animationIn 59 | } else { 60 | contentStyles.opacity = 1 61 | contentStyles.transition = 'opacity 500ms ease-out' 62 | } 63 | } 64 | 65 | const spinnerStyles = { 66 | position: 'absolute', 67 | left: '50%', 68 | top: '50%', 69 | transition: `opacity 500ms ease-out, transform 0.5s ${ 70 | easings.easeInOutBack 71 | }` 72 | } 73 | 74 | if (!loaded && !errored) { 75 | spinnerStyles.opacity = 1 76 | spinnerStyles.transform = 'translate(-50%, -50%)' 77 | } else { 78 | spinnerStyles.opacity = 0 79 | spinnerStyles.transform = 'translate(-50%, -50%) scale(0.8)' 80 | } 81 | 82 | return ( 83 | 84 |
85 | {children} 86 |
87 | {Spinner && ( 88 |
89 | 90 |
91 | )} 92 |
93 | ) 94 | } 95 | 96 | HideUntilLoaded.propTypes = { 97 | animationIn: PropTypes.string, 98 | children: PropTypes.any.isRequired, 99 | imageToLoad: PropTypes.string.isRequired, 100 | Spinner: PropTypes.any, 101 | style: PropTypes.object 102 | } 103 | 104 | HideUntilLoaded.displayName = 'HideUntilLoaded' 105 | 106 | export default HideUntilLoaded 107 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | - Using welcoming and inclusive language 18 | - Being respectful of differing viewpoints and experiences 19 | - Gracefully accepting constructive criticism 20 | - Focusing on what is best for the community 21 | - Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | - The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | - Trolling, insulting/derogatory comments, and personal or political attacks 28 | - Public or private harassment 29 | - Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | - Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at [clinic@nearform.com][clinic]. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [clinic]: mailto:clinic@nearform.com 74 | [homepage]: https://www.contributor-covenant.org 75 | -------------------------------------------------------------------------------- /src/AnimateOnChange/index.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 NearForm Ltd. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import React, { useEffect, useState, useRef } from 'react' 18 | import PropTypes from 'prop-types' 19 | import { animations } from '../theme' 20 | import '../theme/keyframes.css' 21 | 22 | /** 23 | * Applies an animation to any changes of child content or components. 24 | * 25 | * Properties: 26 | * animate: { Boolean } Whether to perform the animation (optional) 27 | * children 28 | * animationIn: {String} A named animation as defined in the theme animations 29 | * animationOut: {String} A named animation as defined in the theme animations 30 | * animation: {String} Specify the animation base name; use in place of 31 | * animationIn and animationOut. 32 | * durationOut: {Number} Time in ms for the out animation (default is 200ms) 33 | * style: {Object} Custom style rules as required 34 | */ 35 | 36 | const AnimateOnChange = ({ 37 | animation: animationBaseName, 38 | animationIn = `${animationBaseName}In`, 39 | animationOut = `${animationBaseName}Out`, 40 | children, 41 | className, 42 | durationOut, 43 | style 44 | }) => { 45 | const [animation, setAnimation] = useState('') 46 | const [displayContent, setDisplayContent] = useState(children) 47 | const firstUpdate = useRef(true) 48 | 49 | useEffect(() => { 50 | // Don't run the effect the first time through 51 | if (firstUpdate.current) { 52 | firstUpdate.current = false 53 | return 54 | } 55 | setAnimation('out') 56 | }, [children]) 57 | 58 | const showDisplayContent = () => { 59 | if (animation === 'out') { 60 | setAnimation('in') 61 | setDisplayContent(children) 62 | } 63 | } 64 | 65 | const styles = { 66 | display: 'inline-block', 67 | transition: !className && `opacity ${durationOut}ms ease-out`, 68 | opacity: !className && animation === 'out' ? 0 : 1, 69 | animationDuration: durationOut + 'ms', 70 | ...style 71 | } 72 | 73 | switch (animation) { 74 | case 'in': 75 | styles.animation = animations[animationIn] || animationIn 76 | break 77 | case 'out': 78 | styles.animation = animations[animationOut] || animationOut 79 | break 80 | } 81 | 82 | const baseClassName = className || 'animate-on-change' 83 | return ( 84 | 90 | {displayContent} 91 | 92 | ) 93 | } 94 | 95 | AnimateOnChange.propTypes = { 96 | children: PropTypes.any.isRequired, 97 | className: PropTypes.string, 98 | durationOut: PropTypes.number, 99 | animation: PropTypes.string, 100 | animationIn: PropTypes.string, 101 | animationOut: PropTypes.string, 102 | style: PropTypes.object 103 | } 104 | 105 | AnimateOnChange.defaultProps = { 106 | durationOut: 200 107 | } 108 | 109 | AnimateOnChange.displayName = 'AnimateOnChange' 110 | 111 | export default AnimateOnChange 112 | -------------------------------------------------------------------------------- /src/AnimateGroup/index.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 NearForm Ltd. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import React from 'react' 18 | import ReactDOM from 'react-dom' 19 | import PropTypes from 'prop-types' 20 | import { 21 | Transition, 22 | TransitionGroup, 23 | } from 'react-transition-group' 24 | import { animations } from '../theme' 25 | import '../theme/keyframes.css' 26 | 27 | const cssPrefixes = ['','-webkit-'] 28 | const css = ( name, styles ) => cssPrefixes.reduce( ( s, p ) => s+ `${p}${name}: ${styles}; `, '') 29 | 30 | /** 31 | * Animate a group or list of components. 32 | * Animations are applied as components are added to or removed from the group. 33 | * NOTE: Child keys are necessary for animations to work correctly. 34 | */ 35 | function AnimateGroup( props ) { 36 | 37 | const { 38 | // Convenience property for setting animationIn and animationOut 39 | // at once; e.g. setting animation="slide" is equivalent to 40 | // animationIn="slideIn" animationOut="slideOut" 41 | animation, 42 | // Name of animation used when adding a new component. 43 | // See theme/index.js for animation names. 44 | animationIn = animation+'In', 45 | // Name of animation used when removing an existing component. 46 | animationOut = animation+'Out', 47 | // The CSS class name to apply to the group container. 48 | className, 49 | // Transition out duration. 50 | durationOut, 51 | // The group children. 52 | children 53 | } = props 54 | 55 | const transition = `transition: opacity ${durationOut}ms ease-out` 56 | const animationInCSS = animations[animationIn] || animationIn 57 | const animationOutCSS = animations[animationOut] || animationOut 58 | 59 | const childState = { 60 | entering: node => node.style = 'display: none', 61 | entered: node => node.style = `${transition}; opacity: 1; ${css('animation',animationInCSS)}`, 62 | exiting: node => node.style = `${transition}; opacity: 0; ${css('animation',animationOutCSS)}`, 63 | exited: node => node.style = `${transition}; opacity: 0; ${css('animation',animationOutCSS)}` 64 | } 65 | 66 | const groupChildren = React.Children.toArray(children).map(child => { 67 | const { key } = child 68 | return ( 76 | {child} 77 | ) 78 | }) 79 | 80 | return ( 81 | {groupChildren} 82 | ) 83 | } 84 | 85 | AnimateGroup.propTypes = { 86 | children: PropTypes.any.isRequired, 87 | className: PropTypes.string, 88 | durationOut: PropTypes.number, 89 | animation: PropTypes.string, 90 | animationIn: PropTypes.string, 91 | animationOut: PropTypes.string 92 | } 93 | 94 | AnimateGroup.defaultProps = { 95 | animation: 'fade', 96 | durationOut: 200 97 | } 98 | 99 | AnimateGroup.displayName = 'AnimateGroup' 100 | 101 | export default AnimateGroup 102 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Please take a second to read over this before opening an issue. Providing complete information upfront will help us address any issue (and ship new features!) faster. 4 | 5 | We greatly appreciate bug fixes, documentation improvements and new features, however when contributing a new major feature, it is a good idea to idea to first open an issue, to make sure the feature it fits with the goal of the project, so we don't waste your or our time. 6 | 7 | ## Code of Conduct 8 | 9 | The Node Clinic project has a [Code of Conduct][coc] that all contributors are 10 | expected to follow. 11 | 12 | ## Bug Reports 13 | 14 | A perfect bug report would have the following: 15 | 16 | 1. Summary of the issue you are experiencing. 17 | 2. Details on what versions of node and clinic you have (`node -v` and `clinic -v`). 18 | 3. A simple repeatable test case for us to run. Please try to run through it 2-3 times to ensure it is completely repeatable. 19 | 20 | We would like to avoid issues that require a follow up questions to identify the bug. These follow ups are difficult to do unless we have a repeatable test case. 21 | 22 | In addition, it is helpful if you upload your clinic data to help us diagnose your issues. 23 | Use the `clinic upload` tool to do this: 24 | 25 | ``` 26 | clinic upload 10000.clinic-doctor 27 | ``` 28 | 29 | After the upload has finished, add the printed upload id to your issue. 30 | 31 | ## For Developers 32 | 33 | All contributions should fit the [standard](https://github.com/standard/standard) linter, and pass the tests. 34 | You can test this by running: 35 | 36 | ``` 37 | npm test 38 | ``` 39 | 40 | ## For Collaborators 41 | 42 | Make sure to get a `:thumbsup:`, `+1` or `LGTM` from another collaborator before merging a PR. If you aren't sure if a release should happen, open an issue. 43 | 44 | Release process: 45 | 46 | - `npm test` 47 | - `npm version ` 48 | - `git push && git push --tags` 49 | - `npm publish` 50 | 51 | --- 52 | 53 | ## Licensing and Certification 54 | 55 | While the Node Clinic project as a whole is distributed under the GPL 3.0 56 | license, all contributions to the Node Clinic project are submitted _to_ the 57 | project under the MIT license. This gives the project maintainers flexibility 58 | while ensuring that your rights to your contributions do not change. 59 | 60 | The Node Clinic project uses a Contribution Certification that is derived from 61 | the [Developer Certificate of Origin][dco]. It is important to note that the 62 | Contribution Certification _is not the same as the standard DCO_ and we do not 63 | use the term "DCO" or "Developer Certificate of Origin" to describe it to avoid 64 | confusion. Nevertheless, the intent and purpose is effectively the same. 65 | 66 | Every contributor agrees to the Contribution Certification by including a 67 | `Signed-off-by` statement within each commit. The statement _must_ include 68 | the contributor's real full name and email address. 69 | 70 | ``` 71 | Signed-off-by: J. Random User 72 | ``` 73 | 74 | ### Certification 75 | 76 | By making a contribution to this project, I certify that: 77 | 78 | (a) The contribution was created in whole or in part by me and I have the right 79 | to and hereby submit it under the MIT license; or 80 | 81 | (b) The contribution is based upon previous work that, to the best of my 82 | knowledge, is covered under an appropriate open source license and I have the 83 | right under that license to submit that work with modifications, whether created 84 | in whole or in part by me, under the MIT License; or 85 | 86 | (c) The contribution was provided directly to me by some other person who 87 | certified (a), (b) or (c) and I have not modified it. 88 | 89 | (d) I understand and agree that this project and the contribution are public 90 | and that a record of the contribution (including all personal information I 91 | submit with it, including my sign-off) is maintained indefinitely and may be 92 | redistributed consistent with this project or license(s) involved. 93 | 94 | [coc]: CODE_OF_CONDUCT.md 95 | [dco]: https://developercertificate.org/ 96 | -------------------------------------------------------------------------------- /src/HideUntilLoaded/HideUntilLoaded.test.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 NearForm Ltd. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import React from 'react' 18 | import { mount } from 'enzyme' 19 | import usePreloadImage from '../hooks/usePreloadImage' 20 | import HideUntilLoaded from './' 21 | 22 | jest.mock('../hooks/usePreloadImage') 23 | 24 | describe('HideUntilLoaded', () => { 25 | beforeAll(() => { 26 | usePreloadImage.mockImplementation(() => [false, false]) 27 | }) 28 | 29 | it('should render child value', () => { 30 | const component = mount( 31 | 123 32 | ) 33 | expect(component.text()).toEqual('123') 34 | }) 35 | 36 | it('should render child components', () => { 37 | const component = mount( 38 | 39 |
40 |

41 |

42 |

43 |
44 | ) 45 | expect(component.find('.test-div').length).toEqual(1) 46 | expect(component.find('p').length).toEqual(2) 47 | }) 48 | 49 | it('should return children if no image specified', () => { 50 | const component = mount( 51 | 52 |
53 |

54 |

55 |

56 |
57 | ) 58 | expect(component.find('.test-div').length).toEqual(1) 59 | expect(component.find('p').length).toEqual(2) 60 | }) 61 | 62 | it('should begin with the contents hidden', () => { 63 | const component = mount( 64 | 123 65 | ) 66 | expect( 67 | component.find('.hide-until-loaded-content').get(0).props.style.opacity 68 | ).toEqual(0) 69 | expect( 70 | component.find('.hide-until-loaded-content').get(0).props.style.animation 71 | ).toEqual(undefined) 72 | }) 73 | 74 | it('should show content when loaded', () => { 75 | usePreloadImage.mockImplementation(() => [false, true]) 76 | const component = mount( 77 | 123 78 | ) 79 | expect( 80 | component.find('.hide-until-loaded-content').get(0).props.style.opacity 81 | ).toEqual(1) 82 | expect( 83 | component.find('.hide-until-loaded-content').get(0).props.style.transition 84 | ).toEqual(expect.stringContaining('opacity 500ms ease-out')) 85 | }) 86 | 87 | it('should apply animationIn when loaded', () => { 88 | usePreloadImage.mockImplementation(() => [false, true]) 89 | const component = mount( 90 | 91 | 123 92 | 93 | ) 94 | expect( 95 | component.find('.hide-until-loaded-content').get(0).props.style.animation 96 | ).toEqual(expect.stringContaining('pop-in')) 97 | }) 98 | 99 | it('should apply custom animationIn text when loaded', () => { 100 | usePreloadImage.mockImplementation(() => [false, true]) 101 | const component = mount( 102 | 103 | 123 104 | 105 | ) 106 | expect( 107 | component.find('.hide-until-loaded-content').get(0).props.style.animation 108 | ).toEqual(expect.stringContaining('test text')) 109 | }) 110 | 111 | it('should apply animationIn when errored (as a fallback)', () => { 112 | usePreloadImage.mockImplementation(() => [true, false]) 113 | const component = mount( 114 | 115 | 123 116 | 117 | ) 118 | expect( 119 | component.find('.hide-until-loaded-content').get(0).props.style.animation 120 | ).toEqual(expect.stringContaining('pop-in')) 121 | }) 122 | 123 | it('should show a given spinner', () => { 124 | const component = mount( 125 |
} 128 | > 129 | 123 130 | 131 | ) 132 | expect(component.find('.example-spinner').length).toEqual(1) 133 | }) 134 | 135 | it('should hide spinner when content loaded', () => { 136 | usePreloadImage.mockImplementation(() => [false, true]) 137 | const component = mount( 138 |
} 141 | > 142 | 123 143 | 144 | ) 145 | expect( 146 | component.find('.hide-until-loaded-spinner').get(0).props.style.opacity 147 | ).toEqual(0) 148 | }) 149 | 150 | it('should accept custom styles', () => { 151 | const component = mount( 152 | 153 | 123 154 | 155 | ) 156 | expect(component.find('span').get(0).props.style.background).toEqual('red') 157 | }) 158 | }) 159 | -------------------------------------------------------------------------------- /examples/src/styles/normalize.css: -------------------------------------------------------------------------------- 1 | /*! normalize.css v8.0.0 | MIT License | github.com/necolas/normalize.css */ 2 | 3 | html { 4 | line-height: 1.15; /* 1 */ 5 | -webkit-text-size-adjust: 100%; /* 2 */ 6 | } 7 | 8 | /* Sections 9 | ========================================================================== */ 10 | 11 | /** 12 | * Remove the margin in all browsers. 13 | */ 14 | 15 | body { 16 | margin: 0; 17 | } 18 | 19 | /** 20 | * Correct the font size and margin on h1 elements within section and 21 | * article contexts in Chrome, Firefox, and Safari. 22 | */ 23 | 24 | h1 { 25 | font-size: 2em; 26 | margin: 0.67em 0; 27 | } 28 | 29 | /* Grouping content 30 | ========================================================================== */ 31 | 32 | /** 33 | * 1. Add the correct box sizing in Firefox. 34 | * 2. Show the overflow in Edge and IE. 35 | */ 36 | 37 | hr { 38 | box-sizing: content-box; /* 1 */ 39 | height: 0; /* 1 */ 40 | overflow: visible; /* 2 */ 41 | } 42 | 43 | /** 44 | * 1. Correct the inheritance and scaling of font size in all browsers. 45 | * 2. Correct the odd em font sizing in all browsers. 46 | */ 47 | 48 | pre { 49 | font-family: monospace, monospace; /* 1 */ 50 | font-size: 1em; /* 2 */ 51 | } 52 | 53 | /* Text-level semantics 54 | ========================================================================== */ 55 | 56 | /** 57 | * Remove the gray background on active links in IE 10. 58 | */ 59 | 60 | a { 61 | background-color: transparent; 62 | } 63 | 64 | /** 65 | * 1. Remove the bottom border in Chrome 57- 66 | * 2. Add the correct text decoration in Chrome, Edge, IE, Opera, and Safari. 67 | */ 68 | 69 | abbr[title] { 70 | border-bottom: none; /* 1 */ 71 | text-decoration: underline; /* 2 */ 72 | text-decoration: underline dotted; /* 2 */ 73 | } 74 | 75 | /** 76 | * Add the correct font weight in Chrome, Edge, and Safari. 77 | */ 78 | 79 | b, 80 | strong { 81 | font-weight: bolder; 82 | } 83 | 84 | /** 85 | * 1. Correct the inheritance and scaling of font size in all browsers. 86 | * 2. Correct the odd em font sizing in all browsers. 87 | */ 88 | 89 | code, 90 | kbd, 91 | samp { 92 | font-family: monospace, monospace; /* 1 */ 93 | font-size: 1em; /* 2 */ 94 | } 95 | 96 | /** 97 | * Add the correct font size in all browsers. 98 | */ 99 | 100 | small { 101 | font-size: 80%; 102 | } 103 | 104 | /** 105 | * Prevent sub and sup elements from affecting the line height in 106 | * all browsers. 107 | */ 108 | 109 | sub, 110 | sup { 111 | font-size: 75%; 112 | line-height: 0; 113 | position: relative; 114 | vertical-align: baseline; 115 | } 116 | 117 | sub { 118 | bottom: -0.25em; 119 | } 120 | 121 | sup { 122 | top: -0.5em; 123 | } 124 | 125 | /* Embedded content 126 | ========================================================================== */ 127 | 128 | /** 129 | * Remove the border on images inside links in IE 10. 130 | */ 131 | 132 | img { 133 | border-style: none; 134 | } 135 | 136 | /* Forms 137 | ========================================================================== */ 138 | 139 | /** 140 | * 1. Change the font styles in all browsers. 141 | * 2. Remove the margin in Firefox and Safari. 142 | */ 143 | 144 | button, 145 | input, 146 | optgroup, 147 | select, 148 | textarea { 149 | font-family: inherit; /* 1 */ 150 | font-size: 100%; /* 1 */ 151 | line-height: 1.15; /* 1 */ 152 | margin: 0; /* 2 */ 153 | } 154 | 155 | /** 156 | * Show the overflow in IE. 157 | * 1. Show the overflow in Edge. 158 | */ 159 | 160 | button, 161 | input { /* 1 */ 162 | overflow: visible; 163 | } 164 | 165 | /** 166 | * Remove the inheritance of text transform in Edge, Firefox, and IE. 167 | * 1. Remove the inheritance of text transform in Firefox. 168 | */ 169 | 170 | button, 171 | select { /* 1 */ 172 | text-transform: none; 173 | } 174 | 175 | /** 176 | * Correct the inability to style clickable types in iOS and Safari. 177 | */ 178 | 179 | button, 180 | [type="button"], 181 | [type="reset"], 182 | [type="submit"] { 183 | -webkit-appearance: button; 184 | } 185 | 186 | /** 187 | * Remove the inner border and padding in Firefox. 188 | */ 189 | 190 | button::-moz-focus-inner, 191 | [type="button"]::-moz-focus-inner, 192 | [type="reset"]::-moz-focus-inner, 193 | [type="submit"]::-moz-focus-inner { 194 | border-style: none; 195 | padding: 0; 196 | } 197 | 198 | /** 199 | * Restore the focus styles unset by the previous rule. 200 | */ 201 | 202 | button:-moz-focusring, 203 | [type="button"]:-moz-focusring, 204 | [type="reset"]:-moz-focusring, 205 | [type="submit"]:-moz-focusring { 206 | outline: 1px dotted ButtonText; 207 | } 208 | 209 | /** 210 | * Correct the padding in Firefox. 211 | */ 212 | 213 | fieldset { 214 | padding: 0.35em 0.75em 0.625em; 215 | } 216 | 217 | /** 218 | * 1. Correct the text wrapping in Edge and IE. 219 | * 2. Correct the color inheritance from fieldset elements in IE. 220 | * 3. Remove the padding so developers are not caught out when they zero out 221 | * fieldset elements in all browsers. 222 | */ 223 | 224 | legend { 225 | box-sizing: border-box; /* 1 */ 226 | color: inherit; /* 2 */ 227 | display: table; /* 1 */ 228 | max-width: 100%; /* 1 */ 229 | padding: 0; /* 3 */ 230 | white-space: normal; /* 1 */ 231 | } 232 | 233 | /** 234 | * Add the correct vertical alignment in Chrome, Firefox, and Opera. 235 | */ 236 | 237 | progress { 238 | vertical-align: baseline; 239 | } 240 | 241 | /** 242 | * Remove the default vertical scrollbar in IE 10+. 243 | */ 244 | 245 | textarea { 246 | overflow: auto; 247 | } 248 | 249 | /** 250 | * 1. Add the correct box sizing in IE 10. 251 | * 2. Remove the padding in IE 10. 252 | */ 253 | 254 | [type="checkbox"], 255 | [type="radio"] { 256 | box-sizing: border-box; /* 1 */ 257 | padding: 0; /* 2 */ 258 | } 259 | 260 | /** 261 | * Correct the cursor style of increment and decrement buttons in Chrome. 262 | */ 263 | 264 | [type="number"]::-webkit-inner-spin-button, 265 | [type="number"]::-webkit-outer-spin-button { 266 | height: auto; 267 | } 268 | 269 | /** 270 | * 1. Correct the odd appearance in Chrome and Safari. 271 | * 2. Correct the outline style in Safari. 272 | */ 273 | 274 | [type="search"] { 275 | -webkit-appearance: textfield; /* 1 */ 276 | outline-offset: -2px; /* 2 */ 277 | } 278 | 279 | /** 280 | * Remove the inner padding in Chrome and Safari on macOS. 281 | */ 282 | 283 | [type="search"]::-webkit-search-decoration { 284 | -webkit-appearance: none; 285 | } 286 | 287 | /** 288 | * 1. Correct the inability to style clickable types in iOS and Safari. 289 | * 2. Change font properties to inherit in Safari. 290 | */ 291 | 292 | ::-webkit-file-upload-button { 293 | -webkit-appearance: button; /* 1 */ 294 | font: inherit; /* 2 */ 295 | } 296 | 297 | /* Interactive 298 | ========================================================================== */ 299 | 300 | /* 301 | * Add the correct display in Edge, IE 10+, and Firefox. 302 | */ 303 | 304 | details { 305 | display: block; 306 | } 307 | 308 | /* 309 | * Add the correct display in all browsers. 310 | */ 311 | 312 | summary { 313 | display: list-item; 314 | } 315 | 316 | /* Misc 317 | ========================================================================== */ 318 | 319 | /** 320 | * Add the correct display in IE 10+. 321 | */ 322 | 323 | template { 324 | display: none; 325 | } 326 | 327 | /** 328 | * Add the correct display in IE 10. 329 | */ 330 | 331 | [hidden] { 332 | display: none; 333 | } 334 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # React Animation 👌 2 | 3 | [![Coverage Status](https://coveralls.io/repos/github/nearform/react-animation/badge.svg?branch=master&v=1.0.6)](https://coveralls.io/github/nearform/react-animation?branch=master) 4 | [![NPM version](https://img.shields.io/npm/v/react-animation.svg)](https://www.npmjs.com/package/react-animation) 5 | 6 | Components and animations to easily add movement to your React project 7 | 8 | ## Demos 9 | 10 | You can see each of the components and animations that are included on [this demo page](https://nearform.github.com/react-animation/). 11 | 12 | ## Usage 13 | 14 | ``` 15 | npm install --save react-animation 16 | ``` 17 | 18 | ## Components 19 | 20 | The following components can be used to easily add animation to specific circumstances. 21 | 22 | ### AnimateOnChange 23 | 24 | It can be helpful to let users know when something has changed in our UI. This component aims to help by adding an animation to any content that changes. 25 | 26 | {value or child components} 27 | 28 | The `AnimateOnChange` component will automatically fade out old content and fade in the new content when the content changes. This could be a number or string or any child components. 29 | 30 | The following (optional) properties can be used: 31 | 32 | - animationIn 33 | - animationOut 34 | - className 35 | - durationOut 36 | - style 37 | 38 | By default, the animation used is a fade out and in. You can specify an animation for both the `out` (when the old content is removed) and `in` (when new content is shown) stages of the animation. 39 | 40 | You can reference these by name, for example: 41 | 42 | ... 43 | 44 | This will apply a `popOut` animation when removing the old content, and a `popIn` animation on the new content. 45 | 46 | #### Custom `animationIn`and `animationOut` values 47 | 48 | You can specify a built-in animation by name and it will call in the relevant animation property for you. If you prefer you can also supply a string here such as `my-animation 500ms ease-out forwards`. This way you can specify your own animation, as long as you've defined the `my-animation` keyframes somewhere. 49 | 50 | #### ClassName 51 | 52 | If you need more control, such as animating child components of pseudo-elements, you can supply a class name. By default, the component will add a `animate-on-change` class along with either `animate-on-change-in` and `animate-on-change-out` depending on the stage of the animation. 53 | 54 | You can supply a `className` of "foo" and it will apply `foo`, `foo-in` and `foo-out` as necessary. 55 | 56 | ### HideUntilLoaded 57 | 58 | Nobody likes a half-downloaded image appearing when rendering our UI. This component helps by hiding any children content until a specified image has finished downloading. 59 | 60 | Your content 61 | 62 | By default the component will apply an `opacity` of 0 and then a `transition` when loading has completed. This will fade in the fully downloaded content. 63 | 64 | You can supply `animationIn` an animation name to use that instead of the default fade. 65 | 66 | #### Spinners 67 | 68 | You can specify any React component as a "spinner" or loading indicator. This will be shown while the loading is taking place, and then removed once the content is ready to be shown. 69 | 70 | ``` 71 | import MySpinner from '../MySpinner' // This could be an animated SVG or any React component 72 | 73 | Your content 74 | 75 | ``` 76 | 77 | ### AnimateGroup 78 | 79 | This component makes it easy to animate additions and deletions to a group of components. 80 | 81 | {children} 82 | 83 | It is important for every child to have a unique key, in order for the component to detect changes to the group as children are added and removed. 84 | 85 | Animations and transitions can be specified in the same way as for `AnimateOnChange`, using `animationIn` and `animationOut` properties. Alternatively, `AnimateGroup` supports the convenience property `animation` which allows the base name of the animation to be specified; so for example, setting `animation="bounce"` is equivalent to setting the properties `animationIn="bounceIn" animationOut="bounceOut"`. 86 | 87 | Note that this component is different to `AnimateOnChange` as it _only_ animates addition of new elements and removal of existing ones; changes to existing elements are not animated. However, animation of updates to elements can be simulated by changing the child key when its value changes; this will be equivalent to removing the child's old value and replacing it with a new child with the new value. 88 | 89 | ## Animations 90 | 91 | This package includes some pre-built animations along with their associated keyframes. As well as supplying the animation names to the above components, you can also apply these animations directly to your components: 92 | 93 | ``` 94 | import { animations } from 'react-animation' 95 | 96 | const style = { 97 | animation: animations.popIn 98 | } 99 | 100 | 101 | 102 | ``` 103 | 104 | In this example, `popIn` evaluates to `pop-in 500ms cubic-bezier(0.19, 1, 0.22, 1) forwards`. 105 | 106 | The following animations are included: 107 | 108 | - fadeIn 109 | - fadeOut 110 | - fadeInUp 111 | - popIn 112 | - popOut 113 | - bounceIn 114 | - bounceOut 115 | - slideIn 116 | - slideOut 117 | 118 | ### Using your own styles 119 | 120 | You can pass any custom styles into the `AnimateOnChange` and `HideUntilLoaded` using the `style` property. 121 | 122 | ## Easings 123 | 124 | As well as pre-built animations, this package includes a range of timing functions you can use in animations and transitions. 125 | 126 | The full set can be seen in action on the [demo page](https://nearform.github.io/react-animation/). 127 | 128 | You can apply these to your component styles like so: 129 | 130 | ``` 131 | import { easings } from 'react-animation' 132 | 133 | const style = { 134 | animation: `pop-in ${easings.easeOutExpo} 500ms forwards` 135 | } 136 | 137 | 138 | 139 | ``` 140 | 141 | The full list includes: 142 | 143 | - linear 144 | - easeInSine 145 | - easeOutSine 146 | - easeInOutSine 147 | - easeInQuad 148 | - easeOutQuad 149 | - easeInOutQuad 150 | - easeInCubic 151 | - easeOutCubic 152 | - easeInOutCubic 153 | - easeInQuart 154 | - easeOutQuart 155 | - easeInOutQuart 156 | - easeInQuint 157 | - easeOutQuint 158 | - easeInOutQuint 159 | - easeInExpo 160 | - easeOutExpo 161 | - easeInOutExpo 162 | - easeInBack 163 | - easeOutBack 164 | - easeInOutBack 165 | 166 | ## FAQ 167 | 168 | ### What is this for? 169 | 170 | This package is for situations where UI needs to move or animate. These situations could include when data changes, or when an item takes a while to fully load a large background image. 171 | 172 | The package also contains pre-built animations that can be applied to components. 173 | 174 | ### Why not use `ReactTransitionGroup` for many of the use cases? 175 | 176 | `ReactTransitionGroup` is a useful and powerful approach to adding animation to elements, and you could certainly do most of what this package does with it. However you would need to define animation keyframes manually, and style each stage of each animation using classes. 177 | 178 | This package aims to help when adding UI movement by making common actions easier. So for situations such as when data changes, or when an element needs to wait until loading has completed before animating into place, this package offers an easier way. 179 | 180 | One benefit of this is ensuring that the animations you add to UI elements "feel" the same across a site or app. 181 | 182 | `ReactTransitionGroup` provides a good approach to timing and managing classes but doesn't bring any animations or timing functions. 183 | 184 | ## License 185 | 186 | Copyright 2019 NearForm 187 | 188 | Licensed under the Apache License, Version 2.0 (the "License"); 189 | you may not use this file except in compliance with the License. 190 | 191 | You may obtain a copy of the License at 192 | 193 | http://www.apache.org/licenses/LICENSE-2.0 194 | 195 | Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, 196 | 197 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 198 | 199 | See the License for the specific language governing permissions and limitations under the License. 200 | -------------------------------------------------------------------------------- /src/AnimateGroup/AnimateGroup.test.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 NearForm Ltd. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import React from 'react' 18 | import { act } from 'react-dom/test-utils' 19 | import { mount } from 'enzyme' 20 | import AnimateGroup from './' 21 | 22 | jest.useFakeTimers() 23 | 24 | describe('AnimateGroup', () => { 25 | 26 | it('should render child components', () => { 27 | const component = mount( 28 | 29 |
30 |
31 |
32 |
33 | ) 34 | expect(component.find('.child').length).toEqual(3) 35 | expect(component.find('.test-1').length).toEqual(1) 36 | expect(component.find('.test-2').length).toEqual(1) 37 | expect(component.find('.test-3').length).toEqual(1) 38 | }) 39 | 40 | it('should have a default duration out of 200', () => { 41 | const component = mount(
one
) 42 | expect(component.get(0).props.durationOut).toEqual(200) 43 | }) 44 | 45 | it('should render the correct transition out duration', () => { 46 | const durationOut = 250 47 | const component = mount( 48 |
old
49 | ) 50 | 51 | expect(component.get(0).props.durationOut).toEqual(durationOut) 52 | 53 | act(() => { 54 | component.setProps({ children: (
new
) }) 55 | component.update() 56 | }) 57 | 58 | expect(component.find('div.child').at(1).render().attr('style')).toEqual( 59 | expect.stringContaining(`${durationOut}ms`) 60 | ) 61 | }) 62 | 63 | it('should animate then change content when children changes', () => { 64 | const component = mount(
old
) 65 | 66 | let child = component.find('div.old').at(0) 67 | expect(child.text()).toEqual('old') 68 | 69 | // Update the children to trigger animation 70 | act(() => { 71 | component.setProps({ children: (
new
) }) 72 | component.update() 73 | }) 74 | jest.runAllTimers() 75 | 76 | // Expect old value while transitioning out 77 | child = component.find('div.old').at(0) 78 | expect(child.render().attr('style')).toEqual( expect.stringContaining('opacity: 0') ) 79 | expect(child.text()).toEqual('old') 80 | 81 | // Test opacity and new value after duration has passed 82 | act(() => { 83 | component.update() 84 | }) 85 | jest.runAllTimers() 86 | 87 | child = component.find('div.new').at(0) 88 | expect(child.render().attr('style')).toEqual( expect.stringContaining('opacity: 1') ) 89 | expect(child.text()).toEqual('new') 90 | }) 91 | 92 | it('should set named animation on in and out', () => { 93 | const component = mount( 94 | 95 |
old
96 |
97 | ) 98 | 99 | let child = component.find('div.old').at(0) 100 | expect(child.render().attr('style')).toEqual(undefined) 101 | expect(child.text()).toEqual('old') 102 | 103 | act(() => { 104 | component.setProps({ children: (
new
) }) 105 | component.update() 106 | }) 107 | jest.runAllTimers() 108 | 109 | child = component.find('div.old').at(0) 110 | expect(child.render().attr('style')).toEqual( 111 | expect.stringContaining('pop-out') 112 | ) 113 | 114 | act(() => { 115 | component.update() 116 | }) 117 | jest.runAllTimers() 118 | 119 | child = component.find('div.new').at(0) 120 | expect(child.render().attr('style')).toEqual( 121 | expect.stringContaining('pop-in') 122 | ) 123 | expect(child.text()).toEqual('new') 124 | }) 125 | 126 | it('should set custom animation properties on in and out', () => { 127 | const component = mount( 128 | 129 |
old
130 |
131 | ) 132 | let child = component.find('div.old').at(0) 133 | expect(child.render().attr('style')).toEqual(undefined) 134 | 135 | act(() => { 136 | component.setProps({ children: (
new
) }) 137 | component.update() 138 | }) 139 | jest.runAllTimers() 140 | 141 | child = component.find('div.old').at(0) 142 | expect(child.render().attr('style')).toEqual( 143 | expect.stringContaining('out test') 144 | ) 145 | 146 | act(() => { 147 | component.update() 148 | }) 149 | jest.runAllTimers() 150 | 151 | child = component.find('div.new').at(0) 152 | expect(child.render().attr('style')).toEqual( 153 | expect.stringContaining('in test') 154 | ) 155 | }) 156 | /* 157 | it('should set default class names on in and out', () => { 158 | const className = 'animate-on-change' 159 | const component = mount(old) 160 | component.simulate('transitionEnd') 161 | expect(component.find('span').get(0).props.className).toEqual( 162 | expect.stringContaining(className) 163 | ) 164 | expect(component.find('span').get(0).props.className).not.toEqual( 165 | expect.stringContaining(`${className}-in`) 166 | ) 167 | expect(component.find('span').get(0).props.className).not.toEqual( 168 | expect.stringContaining(`${className}-out`) 169 | ) 170 | 171 | act(() => { 172 | component.setProps({ children: 'new' }) 173 | }) 174 | 175 | act(() => { 176 | component.update() 177 | }) 178 | 179 | expect(component.find('span').get(0).props.className).toEqual( 180 | expect.stringContaining(`${className}-out`) 181 | ) 182 | expect(component.find('span').get(0).props.className).not.toEqual( 183 | expect.stringContaining(`${className}-in`) 184 | ) 185 | 186 | act(() => { 187 | component.update() 188 | }) 189 | component.simulate('transitionEnd') 190 | 191 | expect(component.find('span').get(0).props.className).not.toEqual( 192 | expect.stringContaining(`${className}-out`) 193 | ) 194 | expect(component.find('span').get(0).props.className).toEqual( 195 | expect.stringContaining(`${className}-in`) 196 | ) 197 | }) 198 | */ 199 | /* 200 | it('should set custom class names on in and out', () => { 201 | const className = 'test' 202 | const component = mount( 203 | old 204 | ) 205 | component.simulate('transitionEnd') 206 | expect(component.find('span').get(0).props.className).toEqual( 207 | expect.stringContaining(className) 208 | ) 209 | expect(component.find('span').get(0).props.className).not.toEqual( 210 | expect.stringContaining(`${className}-in`) 211 | ) 212 | expect(component.find('span').get(0).props.className).not.toEqual( 213 | expect.stringContaining(`${className}-out`) 214 | ) 215 | 216 | act(() => { 217 | component.setProps({ children: 'new' }) 218 | }) 219 | 220 | act(() => { 221 | component.update() 222 | }) 223 | 224 | expect(component.find('span').get(0).props.className).toEqual( 225 | expect.stringContaining(`${className}-out`) 226 | ) 227 | expect(component.find('span').get(0).props.className).not.toEqual( 228 | expect.stringContaining(`${className}-in`) 229 | ) 230 | 231 | act(() => { 232 | component.update() 233 | }) 234 | component.simulate('transitionEnd'); 235 | 236 | expect(component.find('span').get(0).props.className).not.toEqual( 237 | expect.stringContaining(`${className}-out`) 238 | ) 239 | expect(component.find('span').get(0).props.className).toEqual( 240 | expect.stringContaining(`${className}-in`) 241 | ) 242 | }) 243 | */ 244 | it('should set custom class name on container', () => { 245 | const className = 'test' 246 | const component = mount( 247 |
old
248 | ) 249 | expect(component.get(0).props.className).toEqual( 250 | expect.stringContaining(className) 251 | ) 252 | }) 253 | }) 254 | -------------------------------------------------------------------------------- /src/AnimateOnChange/AnimateOnChange.test.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 NearForm Ltd. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import React from 'react' 18 | import { act } from 'react-dom/test-utils' 19 | import { mount } from 'enzyme' 20 | import AnimateOnChange from './' 21 | 22 | describe('AnimateOnChange', () => { 23 | it('should render child value', () => { 24 | const component = mount(123) 25 | expect(component.text()).toEqual('123') 26 | }) 27 | 28 | it('should render child components', () => { 29 | const component = mount( 30 | 31 |
32 |

33 |

34 |

35 |
36 | ) 37 | expect(component.find('.test-1').length).toEqual(1) 38 | expect(component.find('p').length).toEqual(2) 39 | expect(component.find('.test-2').length).toEqual(1) 40 | expect(component.find('.test-3').length).toEqual(1) 41 | }) 42 | 43 | it('should have a default duration of 200', () => { 44 | const component = mount(123) 45 | expect(component.get(0).props.durationOut).toEqual(200) 46 | }) 47 | 48 | it('should render the correct transition duration', () => { 49 | const durationOut = 250 50 | const component = mount( 51 | 123 52 | ) 53 | expect(component.get(0).props.durationOut).toEqual(250) 54 | expect(component.find('span').get(0).props.style.transition).toEqual( 55 | expect.stringContaining('250ms') 56 | ) 57 | }) 58 | 59 | it('should animate then change content when children changes', () => { 60 | const component = mount(old) 61 | component.simulate('transitionEnd') 62 | expect(component.find('span').get(0).props.style.opacity).toEqual(1) 63 | expect(component.text()).toEqual('old') 64 | 65 | // Update the children to trigger animation 66 | act(() => { 67 | component.setProps({ children: 'new' }) 68 | }) 69 | 70 | act(() => { 71 | component.update() 72 | }) 73 | 74 | // Expect old value while transitioning out 75 | expect(component.find('span').get(0).props.style.opacity).toEqual(0) 76 | expect(component.text()).toEqual('old') 77 | 78 | // Test opacity and new value after duration has passed 79 | act(() => { 80 | component.update() 81 | }) 82 | component.simulate('transitionEnd') 83 | 84 | expect(component.find('span').get(0).props.style.opacity).toEqual(1) 85 | expect(component.text()).toEqual('new') 86 | }) 87 | 88 | it('should set named animation on in and out', () => { 89 | const component = mount( 90 | 91 | old 92 | 93 | ) 94 | component.simulate('transitionEnd') 95 | expect(component.find('span').get(0).props.style.animation).toEqual( 96 | undefined 97 | ) 98 | expect(component.text()).toEqual('old') 99 | 100 | act(() => { 101 | component.setProps({ children: 'new' }) 102 | }) 103 | 104 | act(() => { 105 | component.update() 106 | }) 107 | 108 | expect(component.find('span').get(0).props.style.animation).toEqual( 109 | expect.stringContaining('pop-out') 110 | ) 111 | expect(component.text()).toEqual('old') 112 | 113 | act(() => { 114 | component.update() 115 | }) 116 | component.simulate('transitionEnd') 117 | 118 | expect(component.find('span').get(0).props.style.animation).toEqual( 119 | expect.stringContaining('pop-in') 120 | ) 121 | expect(component.text()).toEqual('new') 122 | }) 123 | 124 | it('should set custom animation properties on in and out', () => { 125 | const component = mount( 126 | 127 | old 128 | 129 | ) 130 | component.simulate('transitionEnd') 131 | expect(component.find('span').get(0).props.style.animation).toEqual( 132 | undefined 133 | ) 134 | 135 | act(() => { 136 | component.setProps({ children: 'new' }) 137 | }) 138 | 139 | act(() => { 140 | component.update() 141 | }) 142 | 143 | expect(component.find('span').get(0).props.style.animation).toEqual( 144 | expect.stringContaining('out test') 145 | ) 146 | 147 | act(() => { 148 | component.update() 149 | }) 150 | component.simulate('transitionEnd') 151 | 152 | expect(component.find('span').get(0).props.style.animation).toEqual( 153 | expect.stringContaining('in test') 154 | ) 155 | }) 156 | 157 | it('should set default class names on in and out', () => { 158 | const className = 'animate-on-change' 159 | const component = mount(old) 160 | component.simulate('transitionEnd') 161 | expect(component.find('span').get(0).props.className).toEqual( 162 | expect.stringContaining(className) 163 | ) 164 | expect(component.find('span').get(0).props.className).not.toEqual( 165 | expect.stringContaining(`${className}-in`) 166 | ) 167 | expect(component.find('span').get(0).props.className).not.toEqual( 168 | expect.stringContaining(`${className}-out`) 169 | ) 170 | 171 | act(() => { 172 | component.setProps({ children: 'new' }) 173 | }) 174 | 175 | act(() => { 176 | component.update() 177 | }) 178 | 179 | expect(component.find('span').get(0).props.className).toEqual( 180 | expect.stringContaining(`${className}-out`) 181 | ) 182 | expect(component.find('span').get(0).props.className).not.toEqual( 183 | expect.stringContaining(`${className}-in`) 184 | ) 185 | 186 | act(() => { 187 | component.update() 188 | }) 189 | component.simulate('transitionEnd') 190 | 191 | expect(component.find('span').get(0).props.className).not.toEqual( 192 | expect.stringContaining(`${className}-out`) 193 | ) 194 | expect(component.find('span').get(0).props.className).toEqual( 195 | expect.stringContaining(`${className}-in`) 196 | ) 197 | }) 198 | 199 | it('should set custom class names on in and out', () => { 200 | const className = 'test' 201 | const component = mount( 202 | old 203 | ) 204 | component.simulate('transitionEnd') 205 | expect(component.find('span').get(0).props.className).toEqual( 206 | expect.stringContaining(className) 207 | ) 208 | expect(component.find('span').get(0).props.className).not.toEqual( 209 | expect.stringContaining(`${className}-in`) 210 | ) 211 | expect(component.find('span').get(0).props.className).not.toEqual( 212 | expect.stringContaining(`${className}-out`) 213 | ) 214 | 215 | act(() => { 216 | component.setProps({ children: 'new' }) 217 | }) 218 | 219 | act(() => { 220 | component.update() 221 | }) 222 | 223 | expect(component.find('span').get(0).props.className).toEqual( 224 | expect.stringContaining(`${className}-out`) 225 | ) 226 | expect(component.find('span').get(0).props.className).not.toEqual( 227 | expect.stringContaining(`${className}-in`) 228 | ) 229 | 230 | act(() => { 231 | component.update() 232 | }) 233 | component.simulate('transitionEnd') 234 | 235 | expect(component.find('span').get(0).props.className).not.toEqual( 236 | expect.stringContaining(`${className}-out`) 237 | ) 238 | expect(component.find('span').get(0).props.className).toEqual( 239 | expect.stringContaining(`${className}-in`) 240 | ) 241 | }) 242 | 243 | it('should accept custom styles', () => { 244 | const component = mount( 245 | 123 246 | ) 247 | expect(component.find('span').get(0).props.style.background).toEqual('red') 248 | }) 249 | 250 | it('should correctly apply durationOut', () => { 251 | const component = mount( 252 | DurationOut Test 253 | ) 254 | expect(component.find('span').get(0).props.style.animationDuration).toEqual( 255 | '5000ms' 256 | ) 257 | }) 258 | 259 | it('should should apply default durationOut', () => { 260 | const component = mount(DurationOut Test) 261 | expect(component.find('span').get(0).props.style.animationDuration).toEqual( 262 | '200ms' 263 | ) 264 | }) 265 | }) 266 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright 2019 NearForm Ltd. 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | 204 | -------------------------------------------------------------------------------- /examples/src/index.js: -------------------------------------------------------------------------------- 1 | import React, { useEffect, useState } from 'react' 2 | import { render } from 'react-dom' 3 | import PropTypes from 'prop-types' 4 | import styled from 'styled-components' 5 | import LazyLoad from 'react-lazyload' 6 | import GithubIcon from './components/GithubIcon' 7 | import Clock from './components/Clock' 8 | import { 9 | AnimateOnChange, 10 | HideUntilLoaded, 11 | AnimateGroup, 12 | animations, 13 | easings 14 | } from '../../src' 15 | import './styles/normalize.css' 16 | import './styles/global.css' 17 | 18 | const words = [ 19 | 'Awesome', 20 | 'Brilliant', 21 | 'Handy', 22 | 'Cool', 23 | 'Perfect', 24 | 'Effective', 25 | '👌', 26 | 'Sensational', 27 | 'Great', 28 | 'Party 🎉', 29 | 'Wow' 30 | ] 31 | 32 | const animationNames = Object.keys(animations) 33 | 34 | const easingNames = Object.keys(easings) 35 | 36 | const emojis = ['👌', '🎉', '😋', '🤩', '😻', '✨', '😍', '👍', '💥'] 37 | 38 | const getRandomFrom = array => array[Math.floor(Math.random() * array.length)] 39 | 40 | const HideUntilLoadedWrapper = props => { 41 | const generateRandomPictureURL = () => 42 | `https://loremflickr.com/1000/600?random&${Math.floor(Math.random() * 99) + 43 | 1}` 44 | 45 | const [randomPictureURL, setRandomPictureURL] = useState( 46 | generateRandomPictureURL() 47 | ) 48 | 49 | return ( 50 |
51 | 52 |
53 |
59 |
60 | 61 |

setRandomPictureURL(generateRandomPictureURL())} 64 | > 65 | Try again 66 |

67 |
68 | ) 69 | } 70 | 71 | const AnimatedBox = ({ animationName, className }) => ( 72 |
73 |
74 |

{animationName}

75 |
76 | ) 77 | 78 | AnimatedBox.propTypes = { 79 | animationName: PropTypes.string, 80 | className: PropTypes.string 81 | } 82 | 83 | const StyledAnimatedBox = styled(AnimatedBox)` 84 | &:hover { 85 | .example-animation-box { 86 | animation: ${props => animations[props.animationName]}; 87 | animation-delay: 200ms; 88 | } 89 | } 90 | 91 | .example-animation-box { 92 | animationDuration: '1000ms'; 93 | 94 | } 95 | } 96 | ` 97 | 98 | const DemoPage = ({ className }) => { 99 | const [randomWord, setRandomWord] = useState(getRandomFrom(words)) 100 | const [randomEmoji, setRandomEmoji] = useState(getRandomFrom(emojis)) 101 | const [randomWordGroup, setRandomWordGroup] = useState([getRandomFrom(words)]) 102 | 103 | useEffect(() => { 104 | const wordInterval = setInterval(() => { 105 | setRandomWord(getRandomFrom(words)) 106 | setRandomEmoji(getRandomFrom(emojis)) 107 | 108 | const word = getRandomFrom(words) 109 | if (randomWordGroup.includes(word)) { 110 | setRandomWordGroup(randomWordGroup.filter(w => w !== word)) 111 | } else { 112 | setRandomWordGroup(randomWordGroup.concat([word]).sort()) 113 | } 114 | }, 2000) 115 | return () => { 116 | clearInterval(wordInterval) 117 | } 118 | }) 119 | return ( 120 |
121 |
122 |

123 | React Animation{' '} 124 | 129 | {randomEmoji} 130 | 131 |

132 |
133 |

134 | Animation for your React projects. Includes helpful components and 135 | pre-built animations. 136 |

137 |
138 |
139 |

Installation

140 |

141 | Requires React and React DOM version ^16.8.0 or newer. 142 |

143 |

144 | npm install -s react-animation 145 |

146 |
147 |
148 |

AnimateOnChange

149 |

150 | import {`{ AnimateOnChange }`} from 'react-animation' 151 |

152 |

153 | The AnimateOnChange component waits for a change to any 154 | children and then creates a smooth transition between the old and 155 | new children states. 156 |

157 |

Default animation (fade)

158 |

159 | It will fade out old content and fade in the new content when the 160 | content changes. This could be a number or string or any child 161 | components. 162 |

163 | 164 |
165 |
166 |                 {`
167 |   ${randomWord}
168 | `}
169 |               
170 |
171 | {randomWord} 172 |
173 |
174 |
175 |

durationOut

176 |

177 | You can control how long the animation takes using the{' '} 178 | durationOut property. By default it is 200{' '} 179 | (milliseconds). 180 |

181 | 182 |
183 |
184 |                 {`
185 | 
188 |   ${randomWord}
189 | `}
190 |               
191 |
192 | 193 | {randomWord} 194 | 195 |
196 |
197 |
198 | 199 |

animationIn / animationOut

200 |

201 | By passing in animationIn and animationOut{' '} 202 | we can change the fade animation to any others defined in the{' '} 203 | animations object. 204 |

205 |

206 | Alternatively, a single animation property can be 207 | supplied with the base name of the animation to use; e.g. setting{' '} 208 | animation="fade" is equivalent to setting 209 | animationIn="fadeIn" animationOut="fadeOut". 210 |

211 | 212 |
213 |
214 |                 {`So very…
215 | 
220 |   ${randomWord}
221 | `}
222 |               
223 |
224 |
225 | So very…{' '} 226 | 227 | 232 | {randomWord} 233 | 234 | 235 |
236 |
237 |
238 |
239 |

240 | Content is styled as inline-block by default but you 241 | can overwrite that by passing an optional style object. 242 |

243 |

Find all the available animations listed under Animations.

244 | 245 |

Custom animations

246 |

247 | We can even pass out own string values for the{' '} 248 | animation property on the "in" and "out" stages. We do 249 | this by passing the string to animationIn and{' '} 250 | animationOut. 251 |

252 |

253 | Note: You will need to define custom-animation-in and{' '} 254 | custom-animation-out as keyframes yourself. 255 |

256 | 257 |
258 |
259 |                 {`This is
260 | 
265 | ${randomWord}
266 | `}
267 |               
268 |
269 |
270 | This is{' '} 271 | 272 | 277 | {randomWord} 278 | 279 | 280 |
281 |
282 |
283 |
284 | 285 |

Custom class name

286 |

287 | If we want to control things more precisely or animate children 288 | elements, we can pass in a class name instead. By default the class 289 | names are: 290 | animate-on-change, animate-on-change-in, 291 | and animate-on-change-out. The last two are 292 | automatically generated, so if you supply the className{' '} 293 | of "foo" it will apply foo, foo-in, and{' '} 294 | foo-out. 295 |

296 | 297 |
298 |
299 |                 {`This is
300 | 
304 | ${randomWord}
305 | `}
306 |               
307 |
308 |
309 | Class names are{' '} 310 | 311 | 312 |
{randomWord}
313 |
314 |
315 |
316 |
317 |
318 |
319 |

The code for the above animation looks like this:

320 |
321 |             {`.foo {
322 | .container {
323 |     display: inline-block;
324 |     position: relative;
325 |     padding: 0 0 0 10px;
326 |     width: 180px;
327 | 
328 |     &:after {
329 |       background-color: rgb(73, 41, 136);
330 |       content: '';
331 |       position: absolute;
332 |       top: 0;
333 |       right: 100%;
334 |       bottom: 0;
335 |       left: 0;
336 |       transition: all ${easings.easeOutExpo} 500ms 250ms;
337 |     }
338 |   }
339 | 
340 |   &.foo-out {
341 |     .container:after {
342 |       right: 0;
343 |       transition: all ${easings.easeInOutBack} 500ms;
344 |     }
345 |   }
346 | }`}
347 |           
348 |
349 |
350 |

HideUntilLoaded

351 |

352 | import {`{ HideUntilLoaded }`} from 'react-animation' 353 |

354 |

355 | Nobody likes a half-downloaded image appearing when rendering our 356 | UI. This component helps by hiding any children content until a 357 | specified image has finished downloading. 358 |

359 | 360 | 361 |
362 |
363 |                 {`
366 |   ... your content ...
367 | `}
368 |               
369 |
370 |
371 | 372 |
373 |
374 |
375 |
376 |

377 | By default this will fade-in the content once the image referenced 378 | by url imageToLoad has finished loading. 379 |

380 | 381 |

Spinner

382 |

383 | You can pass in your own component to act as a loading state - it 384 | will be shown until the image has loaded. 385 |

386 | 387 |
388 |
389 |                 {` 
Loading...
} 392 | > 393 | ... your content ... 394 |
`}
395 |
396 |
397 |
398 |
Loading...
} 400 | /> 401 |
402 |
403 |
404 |
405 |

406 | By default this will fade-in the content once the image referenced 407 | by url imageToLoad has finished loading. 408 |

409 | 410 |

animationIn

411 |

412 | Give the HideUntilLoaded component a named animation 413 | and it'll apply that. 414 |

415 | 416 |
417 |
418 |                 {` 
Loading...
} 422 | > 423 | ... your content ... 424 |
`}
425 |
426 |
427 |
428 |
Loading...
} 431 | /> 432 |
433 |
434 |
435 |
436 |
437 | 438 |
439 |

AnimateGroup

440 |

441 | import {`{ AnimateGroup }`} from 'react-animation' 442 |

443 |

444 | Use this component when you want to animate components being being 445 | added, removed or modified within a group of components. 446 |

447 | 448 | 449 |
450 | 451 |
452 |
453 | 454 |

455 | By default this will fade-in new components as they are added to the 456 | group, and fade-out components that are removed from the group. 457 |

458 | 459 | 460 |
461 |
462 |                 {`
    463 | 464 | {randomWordGroup.map(word => (
  • 465 | {word} 466 |
  • ))} 467 |
    468 |
`}
469 |
470 |
471 |
472 |
    473 | 474 | {randomWordGroup.map(word => ( 475 |
  • {word}
  • 476 | ))} 477 |
    478 |
479 |
480 |
481 |
482 |
483 | 484 |

485 | Components may be added of removed in any order. When using the 486 | component ensure that each child has a unique key within the group. 487 |

488 | 489 |

490 | The animation to use can be specified in the same way as{' '} 491 | AnimateOnChange, using animationIn and{' '} 492 | animationOut properties. Alternatively, a single{' '} 493 | animation property can be supplied with the base name 494 | of the animation to use; so animation="fade" is 495 | equivalent to 496 | animationIn="fadeIn" animationOut="fadeOut". 497 |

498 |
499 | 500 |
501 |

Animations

502 |

503 | import {`{ animations }`} from 'react-animation' 504 |

505 |

506 | Animations can be applied to your styling in animation properties 507 | such as {`style={{animation: animations.popIn}}`} 508 |

509 |

510 | Note: If you're using the animations by themselves you will also 511 | need to add in the bundle's keyframes using{' '} 512 | {`import 'react-animation/dist/keyframes.css'`}. 513 |

514 |

515 | If the animation isn't right you can override specific animation 516 | properties such as duration or timing function as needed. 517 |

518 |

519 | You could use them on components, or even use them on pages to have 520 | each page fade-in, for example. 521 |

522 |

Hover or tap each example to see it in action.

523 |
524 | {animationNames.map(animationName => ( 525 | 529 | ))} 530 |
531 |
532 |
533 |

Easings (timing functions)

534 |

535 | import {`{ easings }`} from 'react-animation' 536 |

537 |

538 | Similar to animations, you can use the built-in easings values in 539 | your projects. They are based on the timing functions set out on{' '} 540 | easings.net. 541 |

542 |
    543 | {easingNames.map(easingName => ( 544 |
  • {easingName}
  • 545 | ))} 546 |
547 |
548 |

549 | Made with{' '} 550 | 551 | {randomEmoji} 552 | {' '} 553 | by NearForm 554 |

555 |
556 |
557 | 558 | 559 | 560 |
561 |
562 | ) 563 | } 564 | 565 | DemoPage.propTypes = { 566 | className: PropTypes.string 567 | } 568 | 569 | const breakpoints = { 570 | desktop: '(min-width: 768px)' 571 | } 572 | 573 | const StyledDemoPage = styled(DemoPage)` 574 | align-items: center; 575 | animation-duration: 1000ms; 576 | animation: ${animations.fadeInUp}; 577 | display: flex; 578 | padding: 0 20px; 579 | flex-direction: column; 580 | justify-content: center; 581 | 582 | @media ${() => breakpoints.desktop} { 583 | padding: 0; 584 | } 585 | 586 | h1 { 587 | color: #fff; 588 | font-size: 32px; 589 | line-height: 40px; 590 | margin-bottom: 0; 591 | width: calc(100% - 40px); 592 | 593 | @media ${() => breakpoints.desktop} { 594 | font-size: 64px; 595 | line-height: 80px; 596 | max-width: 800px; 597 | } 598 | 599 | .title-emoji { 600 | @media ${() => breakpoints.desktop} { 601 | font-size: 52px; 602 | line-height: 72px; 603 | } 604 | } 605 | } 606 | 607 | .page-content { 608 | background: rgba(255, 255, 255, 0.9); 609 | border-radius: 12px; 610 | box-shadow: 10px 10px 160px rgba(0, 0, 0, 0.4); 611 | margin: 20px; 612 | padding: 20px; 613 | width: calc(100% - 40px); 614 | 615 | &:first-of-type { 616 | padding-top: 40px; 617 | } 618 | 619 | @media ${() => breakpoints.desktop} { 620 | max-width: 800px; 621 | width: 100%; 622 | } 623 | } 624 | 625 | p { 626 | font-size: 14px; 627 | 628 | @media ${() => breakpoints.desktop} { 629 | font-size: 16px; 630 | } 631 | } 632 | 633 | h2 { 634 | margin-top: 0.5em; 635 | line-height: 24px; 636 | margin-bottom: 1em; 637 | 638 | @media ${() => breakpoints.desktop} { 639 | font-size: 32px; 640 | margin-bottom: 0.75em; 641 | } 642 | } 643 | 644 | p > code { 645 | background: rgba(255, 255, 255, 0.5); 646 | border-radius: 6px; 647 | font-size: 14px; 648 | display: inline-block; 649 | margin: 0 2px; 650 | padding: 2px 6px; 651 | } 652 | 653 | .copyright { 654 | background: rgba(255, 255, 255, 0.9); 655 | border-radius: 6px; 656 | font-size: 22px; 657 | display: inline-block; 658 | margin: 20px 0 40px; 659 | padding: 2px 10px; 660 | 661 | a { 662 | color: rgb(29, 91, 225); 663 | font-weight: bold; 664 | text-decoration: none; 665 | } 666 | } 667 | 668 | .example { 669 | align-items: center; 670 | animation-delay: 500ms; 671 | animation-duration: 600ms; 672 | animation: ${animations.bounceIn}; 673 | background: #fff; 674 | border-radius: 12px; 675 | box-shadow: 10px 10px 60px rgba(0, 0, 0, 0.1); 676 | display: flex; 677 | flex-wrap: wrap; 678 | opacity: 0; 679 | padding: 10px 20px; 680 | margin-bottom: 1em; 681 | 682 | @media ${() => breakpoints.desktop} { 683 | flex-wrap: nowrap; 684 | margin: 0 -40px 30px; 685 | } 686 | 687 | > pre { 688 | align-items: center; 689 | background-color: rgba(23, 55, 175, 0.1); 690 | border-radius: 8px; 691 | display: flex; 692 | font-size: 14px; 693 | overflow: scroll; 694 | min-height: 150px; 695 | padding: 10px 20px; 696 | width: 100%; 697 | 698 | @media ${() => breakpoints.desktop} { 699 | width: 50%; 700 | } 701 | } 702 | 703 | > div { 704 | display: flex; 705 | justify-content: center; 706 | width: 100%; 707 | 708 | @media ${() => breakpoints.desktop} { 709 | width: 50%; 710 | } 711 | } 712 | 713 | > div.example-animate-group { 714 | height: ${words.length}em; 715 | ul { 716 | -moz-column-count: 2; 717 | -moz-column-gap: 2em; 718 | -webkit-column-count: 2; 719 | -webkit-column-gap: 2em; 720 | column-count: 2; 721 | column-gap: 2em; 722 | } 723 | } 724 | 725 | &-aoc { 726 | &-default { 727 | font-size: 40px; 728 | } 729 | &-animations { 730 | font-size: 18px; 731 | 732 | &-text { 733 | font-size: 24px; 734 | } 735 | } 736 | &-slow { 737 | font-size: 40px; 738 | } 739 | } 740 | 741 | &-hul { 742 | &-default { 743 | text-align: center; 744 | } 745 | &-spinner { 746 | text-align: center; 747 | } 748 | &-image { 749 | background-size: cover; 750 | box-shadow: 2px 2px 20px rgba(0, 0, 0, 0.2); 751 | border-radius: 6px; 752 | margin-top: 20px; 753 | height: 120px; 754 | width: 200px; 755 | } 756 | &-load { 757 | border: 2px solid #aaa; 758 | border-radius: 6px; 759 | background-image: linear-gradient( 760 | to left bottom, 761 | #d02018, 762 | #d5003f, 763 | #cd0064, 764 | #b70087, 765 | #901ca7 766 | ); 767 | cursor: pointer; 768 | font-size: 12px; 769 | padding: 4px 8px; 770 | margin: 14px 0; 771 | width: 100px; 772 | color: #fff; 773 | } 774 | } 775 | 776 | &-animation { 777 | align-items: center; 778 | cursor: help; 779 | display: flex; 780 | flex-direction: column; 781 | 782 | &-container { 783 | display: flex; 784 | justify-content: space-between; 785 | flex-wrap: wrap; 786 | } 787 | 788 | p { 789 | font-size: 14px; 790 | margin-top: 0.25em; 791 | } 792 | 793 | &-box { 794 | border-radius: 6px; 795 | height: 100px; 796 | margin: 10px 20px; 797 | width: 100px; 798 | border: 4px solid #522f95; 799 | background: #7a29aa; 800 | } 801 | } 802 | } 803 | 804 | .clock-example { 805 | text-align: center; 806 | padding: 10px 20px; 807 | margin-bottom: 1em; 808 | } 809 | 810 | @keyframes custom-animation-in { 811 | from { 812 | transform: scale(2); 813 | } 814 | to { 815 | transform: scale(1); 816 | } 817 | } 818 | 819 | @keyframes custom-animation-out { 820 | from { 821 | transform: scale(1); 822 | } 823 | to { 824 | transform: scale(2); 825 | } 826 | } 827 | 828 | .foo { 829 | .container { 830 | display: inline-block; 831 | position: relative; 832 | padding: 0 0 0 10px; 833 | width: 180px; 834 | 835 | &:after { 836 | background-color: rgb(73, 41, 136); 837 | content: ''; 838 | position: absolute; 839 | top: 0; 840 | right: 100%; 841 | bottom: 0; 842 | left: 0; 843 | transition: all ${easings.easeOutExpo} 500ms 250ms; 844 | } 845 | } 846 | 847 | &.foo-out { 848 | .container:after { 849 | right: 0; 850 | transition: all ${easings.easeInOutBack} 500ms; 851 | } 852 | } 853 | } 854 | ` 855 | 856 | render(, document.getElementById('root')) 857 | --------------------------------------------------------------------------------