├── .nvmrc
├── index.js
├── .eslintignore
├── demo.gif
├── .babelrc
├── .gitattributes
├── src
├── enzymeSetup.js
├── item.js
├── expectUtils.js
├── __snapshots__
│ └── index.spec.jsx.snap
├── Hint.css
├── touch.svg
├── generateStyles.js
├── index.jsx
└── index.spec.jsx
├── .gitignore
├── .eslintrc.json
├── webpack.config.js
├── LICENSE
├── .travis.yml
├── package.json
├── CHANGELOG.md
└── README.md
/.nvmrc:
--------------------------------------------------------------------------------
1 | lts/*
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | module.exports = require('./dist/index');
--------------------------------------------------------------------------------
/.eslintignore:
--------------------------------------------------------------------------------
1 | dist/index.js
2 | src/enzymeSetup.js
3 | __snapshots__/
--------------------------------------------------------------------------------
/demo.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/darenju/react-flip-page/HEAD/demo.gif
--------------------------------------------------------------------------------
/.babelrc:
--------------------------------------------------------------------------------
1 | {
2 | "presets": ["@babel/preset-env", "@babel/preset-react"]
3 | }
4 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | * text=lf
2 | *.js text eol=lf
3 | *.json text eol=lf
4 | *.sh text eol=lf
5 | *.env text eol=lf
6 | *.md text eol=lf
--------------------------------------------------------------------------------
/src/enzymeSetup.js:
--------------------------------------------------------------------------------
1 | import { configure } from 'enzyme';
2 | import Adapter from 'enzyme-adapter-react-16';
3 |
4 | configure({ adapter: new Adapter() });
5 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.swp
2 | *~
3 | *.iml
4 | .*.haste_cache.*
5 | .DS_Store
6 | .idea
7 | npm-debug.log
8 | node_modules
9 | dist/
10 | coverage/
11 | __snapshots__
12 |
--------------------------------------------------------------------------------
/.eslintrc.json:
--------------------------------------------------------------------------------
1 | {
2 | "extends": "airbnb",
3 | "parser": "babel-eslint",
4 | "env": {
5 | "browser": true,
6 | "jest": true
7 | },
8 | "rules": {
9 | "react/destructuring-assignment": 0
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/src/item.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import PropTypes from 'prop-types';
3 |
4 | class FlipPageItem extends React.PureComponent {
5 | render() {
6 | const { component } = this.props;
7 | return component;
8 | }
9 | }
10 |
11 | FlipPageItem.propTypes = {
12 | component: PropTypes.node.isRequired,
13 | };
14 |
15 | export default FlipPageItem;
16 |
--------------------------------------------------------------------------------
/webpack.config.js:
--------------------------------------------------------------------------------
1 | const path = require('path');
2 | const TerserPlugin = require('terser-webpack-plugin');
3 |
4 | module.exports = {
5 | context: path.resolve('src'),
6 | entry: {
7 | app: './index.jsx'
8 | },
9 | mode: 'production',
10 | output: {
11 | path: path.resolve('dist'),
12 | filename: 'index.js',
13 | libraryTarget: 'umd',
14 | library: 'ReactFlipPage'
15 | },
16 | optimization: {
17 | minimize: true,
18 | minimizer: [new TerserPlugin()],
19 | },
20 | module: {
21 | rules: [
22 | {
23 | test: /\.jsx?$/,
24 | include: [/src/],
25 | exclude: /node_modules/,
26 | loader: 'babel-loader'
27 | },
28 | {
29 | test: /\.css$/,
30 | use: ['style-loader', 'css-loader']
31 | },
32 | {
33 | test: /\.svg$/,
34 | use: {
35 | loader: 'svg-url-loader',
36 | options: {
37 | stripdeclarations: true
38 | }
39 | }
40 | }
41 | ]
42 | },
43 | externals: {
44 | 'react': 'react'
45 | }
46 | };
47 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2018 Julien Fradin
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in
13 | all copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 | THE SOFTWARE.
22 |
--------------------------------------------------------------------------------
/src/expectUtils.js:
--------------------------------------------------------------------------------
1 | module.exports.expectTurnRight = (wrapper) => {
2 | let state = wrapper.state();
3 | expect(state.secondHalfStyle.transform).toMatch(/rotateY\(-180deg\)$/);
4 |
5 | jest.runOnlyPendingTimers();
6 | state = wrapper.state();
7 |
8 | expect(state.secondHalfStyle).toEqual({});
9 | };
10 |
11 | module.exports.expectTurnLeft = (wrapper) => {
12 | let state = wrapper.state();
13 | expect(state.firstHalfStyle.transform).toMatch(/rotateY\(180deg\)$/);
14 |
15 | jest.runOnlyPendingTimers();
16 | state = wrapper.state();
17 |
18 | expect(state.firstHalfStyle).toEqual({});
19 | };
20 |
21 |
22 | module.exports.expectTurnTop = (wrapper) => {
23 | let state = wrapper.state();
24 | expect(state.firstHalfStyle.transform).toMatch(/rotateX\(-180deg\)$/);
25 |
26 | jest.runOnlyPendingTimers();
27 | state = wrapper.state();
28 |
29 | expect(state.firstHalfStyle).toEqual({});
30 | };
31 | module.exports.expectTurnBottom = (wrapper) => {
32 | let state = wrapper.state();
33 | expect(state.secondHalfStyle.transform).toMatch(/rotateX\(180deg\)$/);
34 |
35 | jest.runOnlyPendingTimers();
36 | state = wrapper.state();
37 |
38 | expect(state.secondHalfStyle).toEqual({});
39 | };
40 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | sudo: false
2 | language: node_js
3 | os: linux
4 | cache:
5 | directories:
6 | - node_modules
7 | before_install:
8 | - "[[ $(node -v) =~ ^v9.*$ ]] || npm install -g npm@latest"
9 | - npm install -g greenkeeper-lockfile@1
10 | - npm install
11 | before_script: greenkeeper-lockfile-update
12 | after_script: greenkeeper-lockfile-upload
13 | script:
14 | - npm run lint
15 | - npm test -- --coverage
16 | - bash <(curl -s https://codecov.io/bash) || echo 'Codecov failed to upload'
17 | - npm run build
18 | deploy:
19 | provider: npm
20 | skip_cleanup: true
21 | email: julien@frad.in
22 | api_key:
23 | secure: MkPT9UrpFHHQGuacivNcaiPsRaButLLuA3TemtvGpkz7vc4vhJaJQZLx/uFqmxogiVr1We5RcGC4KPQsLQd1T9mLXgDTgnhhDdmdlJ8AxOeYMGWWg5MKOdO6yWbdQVyWk3Ae3+GZvDUVlIYZi+595Qczgq5fWJb8HM9cOGqRuBgoK3O/S6VZ0tNxrTytDLvIr78eWuu9oiyNHz08PYSx8hkHlcoZpZgWUA6N0HJQcwwhRysgU1BkQw0B8pVAhdXfvRolgtCyui/cZOmVPhMtpnnOrQ3XRbqT0dMVllwxf0YGz3sg9eTwlSbx3zMaB0IZsfY/rlnzxJKSV1S2ZEqAGXlDnrFigtU27xLsHZwzlb0cTDeK+iKc/Or4qE5UKVFptHC0l0d7S00Sj9q/rKWjgRncBqJzPflB6EPT7kJbq0sXloitWZz6Ns8W7xSBxtXUbhZQx/1cVPadn6rVvRgu9Yo0wf/Eni9ySSuQmTeaTpQ8IXsmhzHlIwAP7eerjJZeo7nkf2ctTR+o+mDp9h6xtEG3mszTF6T2dJt7XHGz97f21Ybdg9pFlOJwyz8uG5k5f1Afe4FinaYXCuX3p5YY68J4waLf2hukX/HDi+yldjPsC3mxnLGESi7bm+A6Z/Qd5dg/YSs/0/7DkoP4of2NBNDLSz41AGWdXGMCIcVAf9M=
24 | on:
25 | tags: true
26 | notifications:
27 | email: false
28 |
--------------------------------------------------------------------------------
/src/__snapshots__/index.spec.jsx.snap:
--------------------------------------------------------------------------------
1 | // Jest Snapshot v1, https://goo.gl/fbAQLP
2 |
3 | exports[` renders without crashing 1`] = `"
"`;
4 |
5 | exports[` should include two touch zones 1`] = `"
"`;
6 |
7 | exports[` should include a touch hint element 1`] = `"
"`;
8 |
9 | exports[` should not have gradient elements 1`] = `""`;
10 |
11 | exports[` should allow "horizontal" as an orientation entry 1`] = `""`;
12 |
13 | exports[` should allow "vertical" as an orientation entry 1`] = `""`;
14 |
15 | exports[` should include a swipe hint element 1`] = `"
"`;
16 |
17 | exports[` should not include a touch hint element 1`] = `""`;
18 |
--------------------------------------------------------------------------------
/src/Hint.css:
--------------------------------------------------------------------------------
1 | .rfp-swipeHint {
2 | animation: 2s ease-in-out rfp-swipeHint-vertical 2;
3 | background-color: rgba(255,255,255,0.5);
4 | border-radius: 15px;
5 | box-sizing: border-box;
6 | height: 30px;
7 | opacity: 0;
8 | pointer-events: none;
9 | position: absolute;
10 | width: 30px;
11 | }
12 | .rfp-swipeHint--vertical {
13 | animation-name: rfp-swipeHint-vertical;
14 | bottom: 20%;
15 | left: 50%;
16 | margin-left: -15px;
17 | }
18 | .rfp-swipeHint--horizontal {
19 | animation-name: rfp-swipeHint-horizontal;
20 | margin-top: -15px;
21 | right: 20%;
22 | top: 50%;
23 | }
24 | .rfp-swipeHint:before {
25 | border-radius: 15px;
26 | border: 3px double rgba(255,255,255,0.5);
27 | box-sizing: border-box;
28 | content: '';
29 | height: 28px;
30 | position: absolute;
31 | width: 28px;
32 | }
33 | .rfp-swipeHint--vertical:before {
34 | left: 50%;
35 | margin-left: -14px;
36 | top: 1px;
37 | }
38 | .rfp-swipeHint--horizontal:before {
39 | left: 1px;
40 | top: 1px;
41 | }
42 | .rfp-touchHint {
43 | animation: 2s ease-in-out rfp-touchHint 2;
44 | opacity: 0;
45 | pointer-events: none;
46 | position: absolute;
47 | z-index: 5;
48 | }
49 | .rfp-touchHint--vertical {
50 | bottom: 5px;
51 | left: 50%;
52 | margin-left: -12px;
53 | }
54 | .rfp-touchHint--horizontal {
55 | margin-top: -14px;
56 | right: 5px;
57 | top: 50%;
58 | }
59 | .rfp-touchHint:after, .rfp-swipeHint:after {
60 | content: url(./touch.svg);
61 | }
62 | .rfp-swipeHint:after {
63 | position: absolute;
64 | top: 11px;
65 | left: 6px;
66 | }
67 | @keyframes rfp-swipeHint-vertical {
68 | 0% {
69 | opacity: 0;
70 | height: 30px;
71 | }
72 | 20%, 40% {
73 | opacity: 1;
74 | height: 30px;
75 | }
76 | 60%, 80% {
77 | opacity: 1;
78 | height: 60%;
79 | }
80 | 100% {
81 | opacity: 0;
82 | height: 60%;
83 | }
84 | }
85 | @keyframes rfp-swipeHint-horizontal {
86 | 0% {
87 | opacity: 0;
88 | width: 30px;
89 | }
90 | 20%, 40% {
91 | opacity: 1;
92 | width: 30px;
93 | }
94 | 60%, 80% {
95 | opacity: 1;
96 | width: 60%;
97 | }
98 | 100% {
99 | opacity: 0;
100 | width: 60%;
101 | }
102 | }
103 | @keyframes rfp-touchHint {
104 | 0% {
105 | opacity: 0;
106 | }
107 | 20% {
108 | opacity: 1;
109 | }
110 | 40%,60% {
111 | transform: scale(0.75);
112 | }
113 | 80% {
114 | opacity: 1;
115 | }
116 | 100% {
117 | opacity: 0;
118 | }
119 | }
120 |
--------------------------------------------------------------------------------
/src/touch.svg:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "react-flip-page",
3 | "version": "1.6.4",
4 | "description": "A React.js implementation of the Flipboard page swipe.",
5 | "main": "dist/index.js",
6 | "files": [
7 | "dist/"
8 | ],
9 | "repository": {
10 | "type": "git",
11 | "url": "https://github.com/darenju/react-flip-page.git"
12 | },
13 | "author": "Julien Fradin",
14 | "contributors": [
15 | "Everton Nikolas de Oliveira (https://br.linkedin.com/in/enikolas/en)",
16 | "Dmitry Buravtsov (https://github.com/DimaBur)",
17 | "slevy85 (https://github.com/slevy85)",
18 | "Konstantin Komelin (https://komelin.com/)"
19 | ],
20 | "license": "MIT",
21 | "bugs": {
22 | "url": "https://github.com/darenju/react-flip-page/issues"
23 | },
24 | "homepage": "https://github.com/darenju/react-flip-page",
25 | "keywords": [
26 | "react-component",
27 | "react",
28 | "flip",
29 | "flip-page",
30 | "page",
31 | "flipboard"
32 | ],
33 | "scripts": {
34 | "prebuild": "check-dependencies",
35 | "pretest": "check-dependencies",
36 | "build": "webpack",
37 | "lint": "eslint --ext .jsx --ext .js --ignore-pattern *spec* --quiet src/",
38 | "test": "jest -u",
39 | "test:coverage": "jest --coverage"
40 | },
41 | "precommit": [
42 | "lint",
43 | "test"
44 | ],
45 | "jest": {
46 | "setupTestFrameworkScriptFile": "/src/enzymeSetup.js",
47 | "moduleNameMapper": {
48 | "\\.(css|svg)$": "identity-obj-proxy"
49 | },
50 | "coverageDirectory": "coverage",
51 | "coveragePathIgnorePatterns": [
52 | "/dist/",
53 | "/src/enzymeSetup.js"
54 | ],
55 | "testURL": "http://localhost"
56 | },
57 | "dependencies": {
58 | "prop-types": "^15.6.2",
59 | "react": "^16.8.4",
60 | "react-dom": "^16.8.4"
61 | },
62 | "devDependencies": {
63 | "@babel/core": "^7.9.0",
64 | "@babel/preset-env": "^7.9.5",
65 | "@babel/preset-react": "^7.9.4",
66 | "@babel/register": "^7.9.0",
67 | "babel-core": "^7.0.0-bridge.0",
68 | "babel-eslint": "^10.0.1",
69 | "babel-jest": "^23.6.0",
70 | "babel-loader": "8.1.0",
71 | "check-dependencies": "^1.1.0",
72 | "css-loader": "^3.0.0",
73 | "enzyme": "^3.6.0",
74 | "enzyme-adapter-react-16": "^1.5.0",
75 | "eslint": "^6.0.0",
76 | "eslint-config-airbnb": "^18.0.1",
77 | "eslint-plugin-import": "^2.13.0",
78 | "eslint-plugin-jsx-a11y": "^6.1.0",
79 | "eslint-plugin-react": "^7.10.0",
80 | "identity-obj-proxy": "^3.0.0",
81 | "jest": "^23.5.0",
82 | "path": "^0.12.7",
83 | "pre-commit": "^1.2.2",
84 | "style-loader": "^1.0.0",
85 | "svg-url-loader": "^5.0.0",
86 | "terser-webpack-plugin": "^3.0.2",
87 | "webpack": "^4.15.1",
88 | "webpack-cli": "^3.0.0"
89 | },
90 | "peerDependencies": {
91 | "react-dom": "^16.2.0"
92 | }
93 | }
94 |
--------------------------------------------------------------------------------
/src/generateStyles.js:
--------------------------------------------------------------------------------
1 | const makeGradient = (direction) => `linear-gradient(to ${direction}, rgba(0,0,0,0.25) 0%,rgba(0,0,0,0) 50px)`;
2 |
3 | export default (
4 | currentPage,
5 | key,
6 | direction,
7 | rotate,
8 | uncutPages,
9 | orientation,
10 | maskOpacity,
11 | pageBackground,
12 | animationDuration,
13 | ) => ({
14 | container: {
15 | display: currentPage === key ? 'block' : 'none',
16 | height: '100%',
17 | overflow: uncutPages === false ? 'hidden' : '',
18 | position: 'relative',
19 | touchAction: 'none',
20 | width: '100%',
21 | },
22 | part: {
23 | position: 'absolute',
24 | },
25 | visiblePart: {
26 | transformStyle: 'preserve-3d',
27 | },
28 | firstHalf: {
29 | bottom: orientation === 'vertical' ? '50%' : 0,
30 | left: 0,
31 | right: orientation === 'vertical' ? 0 : '50%',
32 | top: 0,
33 | transformOrigin: orientation === 'vertical' ? 'bottom center' : 'right center',
34 | },
35 | secondHalf: {
36 | bottom: 0,
37 | left: orientation === 'vertical' ? 0 : '50%',
38 | right: 0,
39 | top: orientation === 'vertical' ? '50%' : 0,
40 | transformOrigin: orientation === 'vertical' ? 'top center' : 'left center',
41 | },
42 | face: {
43 | backfaceVisibility: 'hidden',
44 | WebkitBackfaceVisibility: 'hidden',
45 | bottom: 0,
46 | left: 0,
47 | overflow: 'hidden',
48 | position: 'absolute',
49 | right: 0,
50 | top: 0,
51 | transformStyle: 'preserve-3d',
52 | },
53 | back: {
54 | transform: orientation === 'vertical' ? 'rotateX(180deg)' : 'rotateY(180deg)',
55 | },
56 | before: {
57 | bottom: 0,
58 | left: 0,
59 | right: 0,
60 | top: 0,
61 | },
62 | after: {
63 | bottom: 0,
64 | left: orientation === 'vertical' ? 0 : '50%',
65 | right: 0,
66 | top: orientation === 'vertical' ? '50%' : 0,
67 | },
68 | cut: {
69 | background: pageBackground,
70 | bottom: 0,
71 | left: 0,
72 | overflow: 'hidden',
73 | position: 'absolute',
74 | right: 0,
75 | top: 0,
76 | },
77 | firstCut: {
78 | right: orientation === 'vertical' ? 0 : '-100%',
79 | },
80 | pull: {
81 | left: orientation === 'vertical' ? 0 : '-100%',
82 | position: 'absolute',
83 | height: '100%',
84 | right: 0,
85 | top: orientation === 'vertical' ? '-100%' : 0,
86 | },
87 | gradient: {
88 | bottom: 0,
89 | left: 0,
90 | pointerEvents: 'none',
91 | position: 'absolute',
92 | right: 0,
93 | top: 0,
94 | transition: `opacity ${animationDuration / 1000}s ease-in-out`,
95 | opacity: (() => {
96 | if (direction) {
97 | return 1;
98 | }
99 | return 0;
100 | })(),
101 | },
102 | gradientBefore: {
103 | background: (() => {
104 | if (direction === 'right') {
105 | return makeGradient('left');
106 | }
107 | if (direction === 'down') {
108 | return makeGradient('top');
109 | }
110 |
111 | return '';
112 | })(),
113 | width: (() => {
114 | if (orientation === 'horizontal') {
115 | return '50%';
116 | }
117 | return '100%';
118 | })(),
119 | height: (() => {
120 | if (orientation === 'vertical') {
121 | return '50%';
122 | }
123 | return '100%';
124 | })(),
125 | },
126 | gradientAfter: {
127 | background: (() => {
128 | if (direction === 'left') {
129 | return makeGradient('right');
130 | }
131 | if (direction === 'up') {
132 | return makeGradient('bottom');
133 | }
134 |
135 | return '';
136 | })(),
137 | },
138 | gradientFirstHalf: {
139 | background: (() => {
140 | if (direction === 'left') {
141 | return makeGradient('left');
142 | } if (direction === 'up') {
143 | return makeGradient('top');
144 | }
145 |
146 | return '';
147 | })(),
148 | },
149 | gradientSecondHalf: {
150 | background: (() => {
151 | if (direction === 'right') {
152 | return makeGradient('right');
153 | } if (direction === 'down') {
154 | return makeGradient('bottom');
155 | }
156 |
157 | return '';
158 | })(),
159 | },
160 | mask: {
161 | backgroundColor: '#000',
162 | bottom: 0,
163 | left: 0,
164 | opacity: direction !== '' ? Math.max(maskOpacity - ((Math.abs(rotate) / 90) * maskOpacity), 0) : 0,
165 | pointerEvents: 'none',
166 | position: 'absolute',
167 | right: 0,
168 | top: 0,
169 | },
170 | maskReverse: {
171 | opacity: direction !== '' ? Math.max(((Math.abs(rotate) / 90) * maskOpacity) - maskOpacity, 0) : 0,
172 | },
173 | });
174 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 |
2 | # Changelog
3 |
4 | All notable changes to this project will be documented in this file.
5 |
6 | The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
7 | and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
8 |
9 | ## 1.6.0
10 |
11 | ### Added
12 |
13 | - Added a `noShadow` property that disables the inset drop shadow (which is in fact a gradient) on the inside of the flipping pages.
14 |
15 | ## 1.5.3
16 |
17 | ### Fixed
18 |
19 | - Fixed the method to get the mouse/touch position.
20 |
21 | ## 1.5.2
22 |
23 | ### Fixed
24 |
25 | - Fixed an event confusion in the `swipeImmune` feature.
26 |
27 | ## 1.5.1
28 |
29 | ### Fixed
30 |
31 | - Fixed the `swipeImmune` detection condition.
32 |
33 | ## 1.5.0
34 |
35 | ### Added
36 |
37 | - Added a `swipeImmune` property thanks to **@slevy85**. This property is an arary of CSS classes that makes the elements targeted by those classes immune to the swipe. This means the user can not initiate a swipe gesture from those elements.
38 |
39 | ### Fixed
40 |
41 | - The `flipOnTouch` buttons were not working anymore due to some asynchronous refactoring.
42 |
43 | ## 1.4.0
44 |
45 | ### Added
46 |
47 | - Added a `reverse` property thanks to **@slevy85**. The property allows the user to swipe pages in reverse order: the first page is visible, to see the second page, he has to swipe down instead of up.
48 |
49 | ### Fixed
50 |
51 | - Tests were not working anymore. Added `babel-core@^7.0.0-bridge.0` and `babel-jest` in `devDependencies`.
52 |
53 | ## 1.3.0
54 |
55 | ### Fixed
56 |
57 | - Fixed the swiping not being correctly registered.
58 |
59 | ## 1.2.2
60 |
61 | ### Fixed
62 |
63 | - Fixed `gotoPreviousPage` & `gotoNextPage` method spam.
64 | - Fixed right side of component being cut off.
65 |
66 | ## 1.2.1
67 |
68 | ### Fixed
69 |
70 | - Condition in `gotoPage` method for checking inbounds of index was wrong, did not allow to go to first page (index `0`).
71 |
72 | ## 1.2.0
73 |
74 | ### Changed
75 |
76 | - Call `onStartSwiping` after treshold is reached.
77 |
78 | ## 1.1.0
79 |
80 | ### Added
81 |
82 | - Added `onStartPageChange` event. This event is triggered when the move gesture begins, in opposition to `onPageChange` event which is triggered after the gesture is complete.
83 |
84 | ## 1.0.0
85 |
86 | ### Added
87 |
88 | - Added the list of contributors to the project, finalizing v1.0.0.
89 |
90 | ## 0.18.0
91 |
92 | ### Added
93 |
94 | - Added a `gotoPage` method that takes an index of page in parameter. Places the component to the correct page.
95 |
96 | ## 0.17.0
97 |
98 | ### Changed
99 |
100 | - Changed the way page components are rendered. To limit number of renders, the component now uses `PureComponent`s wrapper (higher-order components) for actual pages.
101 |
102 | ## 0.16.0
103 |
104 | ### Added
105 |
106 | - New property `startAt`.
107 |
108 | ## 0.15.1
109 |
110 | ### Fixed
111 |
112 | - Fixed `onPageChange` not passing the correct page number.
113 |
114 | ## 0.15.0
115 |
116 | ### Added
117 |
118 | - Improve code coverage for `` from `74%` to `99%`.
119 | - `onStartSwiping` and `onEndSwiping` callback.
120 |
121 | ## 0.14.2
122 |
123 | ### Added
124 |
125 | - Improve code coverage for `` from `59%` to `74%`:
126 | - `mouseLeave()`: 100% covered
127 | - `reset()`: 100% covered
128 | - `props.orientation`: 100% covered
129 | - `moveGesture()`: 90% covered (WIP)
130 |
131 | ### Changed
132 |
133 | - Change `eslintrc` run config, as it was given different error outputs based on the OS of the developer. It was basically change `eslint src/**/*.js* --quiet` to `eslint --ext .jsx --ext .js --quiet src/`.
134 |
135 | ## 0.14.1
136 |
137 | ### Fixed
138 |
139 | - `button`s and `a` were not clickable.
140 |
141 | ## 0.14.0
142 |
143 | ### Added
144 |
145 | - `responsive` property (default to `false`) to enable responsive mode.
146 | - Improve code coverage for ``:
147 | - `getHeight()`: 100% covered
148 | - `getHalfHeight()`: 100% covered
149 | - `getWidth()`: 100% covered
150 | - `getHalfWidth()`: 100% covered
151 | - `isLastPage()`: 100% covered
152 | - `isFirstPage()`: 100% covered
153 | - `incrementPage()`: 100% covered
154 | - `decrementPage()`: 100% covered
155 | - `doNotMove()`: 100% covered
156 | - `startMoving()`: 100% covered
157 | - Fix some lint issues on `index.jsx`
158 | - Add codecov to the project (@darenju still needs setup it to his github repository)
159 |
160 | ### Fixed
161 |
162 | - Replace the code style badge url fron `standardjs` to `airbnb`.
163 |
164 | ## 0.13.1
165 | ### Added
166 |
167 | - Added mask on previous/next parts. This way if you go to the drop area, you will see the shadow of the page. This only happened when lifting the page.
168 |
169 | ### Fixed
170 |
171 | - Shadows were not displayed on some parts when swiping. The fix for non-clickable links had caused this.
172 |
173 | ## 0.13.0
174 | ### Added
175 |
176 | - `showTouchHint` property that shows a pointer indicating where to click or touch to switch pages.
177 |
178 | ### Changed
179 |
180 | - `flipOnTouchAllowDrag` has been renamed to `disableSwipe`. By default, the swipe is always allowed with `flipOnTouch`, unless `disableSwipe` is specified.
181 | - `showTouchHint` has been renamed to `showSwipeHint` to avoid confusion.
182 | - Changed default `flipOnTouchZone` from `20` to `10`.
183 |
184 | ### Removed
185 |
186 | - Removed `touchHintTimeout`, `touchHintTimeoutTimer`, `showTouchHint`, `hintVisible` as they were useless.
187 |
188 | ## 0.12.2
189 | ### Added
190 |
191 | - `flipOnTouchAllowDrag` propery allowing the user to keep the dragging feature even though `flipOnTouch`
192 | is active.
193 |
194 | ## 0.12.1
195 | ### Added
196 |
197 | - Class names on touch zones.
198 |
199 | ## 0.12.0
200 | ### Added
201 |
202 | - Add `flipOnTouch` feature. Zones to touch/click are defined by the `flipOnTouchZone` property.
203 |
204 | ## 0.11.8
205 | ### Added
206 |
207 | - Add `.gitattributes` for windows developers
208 | - Add Jest setup
209 | - Add Enzyme setup
210 | - Add `code coverage` report file
211 | - Add `check-dependencies` to `prebuild` and `pretest` lifecycle. This lib checks if the developer has the up-to-date dependencies inside its node_modules directory, as described in the `package.json` file.
212 | - Add TravisCI and Greenkeeper initial setup (@darenju still needs to setup each of these in his repository)
213 | - Add `pre-commit` which runs lint before each commit
214 |
215 | ### Removed
216 |
217 | - Remove generated code from VCS, as a good practice. The generated code should not be versioned, as it can be checked at [Best Practices for Node.js Development](https://devcenter.heroku.com/articles/node-best-practices#only-git-the-important-bits)
218 |
219 | ### Fixed
220 |
221 | - Temporaly disables lint for a specific line: `src/index.jsx:577:3`. Style is a pretty generic prop, receiving any style the user needs. Ideally, this eslint should be disable if you still have the intent of using this props. Or, the code should be refactored, to match the proper [eslint rule](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/forbid-prop-types.md)
222 |
223 | ## 0.10.3 - 2018-01-29
224 | ### Fixed
225 | - Issue causing the package to switch back to first page all the time. Issue was because package was not updated to react@16.
226 |
227 | ## 0.10.1 - 2017-09-24
228 | ### Fixed
229 | - Adjusted properties types.
230 |
231 | ## 0.10.0 - 2017-09-14
232 | ### Changed
233 | - Now using eslint as code style.
234 |
235 | ## 0.9.1 - 2017-09-04
236 | ### Fixed
237 | - Fixed `showTouchHint` always displaying despite property being defined to `false`.
238 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | [](https://badge.fury.io/js/react-flip-page)
2 | [](https://travis-ci.org/darenju/react-flip-page)
3 | [](https://codecov.io/gh/darenju/react-flip-page)
4 | [](https://greenkeeper.io/)
5 | [](https://github.com/airbnb/javascript)
6 |
7 | # react-flip-page
8 |
9 | > **DISCLAIMER**: This package is in no way related to nor endorsed by Flipboard, Inc. nor [flipboard.com](http://www.flipboard.com). This is just a showcase of HTML5 & CSS3 effect implemented with React.
10 |
11 | This package allows you to use the cool Flipboard page swipe effect in your React.js apps.
12 |
13 | > Please keep in mind that static content will work best with this component. Content like videos and related can produce undesired effects.
14 |
15 | It has a `responsive` option, so you can possibly cover your entire page with it!
16 |
17 | 
18 |
19 | You can play with [this demo](http://darenju.me/react-flip-page/).
20 |
21 | ## Install
22 |
23 | Installation is pretty straight-forward, as you just have to `npm install` this package:
24 |
25 | ```
26 | npm install --save react-flip-page
27 | ```
28 |
29 | Then, you can require the module with any way you like, let it be webpack or something else.
30 |
31 | ## Usage
32 |
33 | This package consists of one single component that does all the work. Simply throw a `FlipPage` component with some children that will be the content.
34 |
35 | ```html
36 |
37 |
38 |
My awesome first article
39 |
My awesome first content
40 |
41 |
42 |
My wonderful second article
43 |
My wonderful second content
44 |
45 |
46 |
My excellent third article
47 |
My excellent third content
48 |
49 |
50 | ```
51 |
52 | ### Props
53 |
54 | There are a few properties that define the behaviour of the component, here they are:
55 |
56 | | Prop | Type | Default | Role |
57 | |------|------|---------|------|
58 | | `orientation` | `string` | `vertical` | Orientation of swipes. `vertical` or `horizontal` for respectively up/down swipes and left/right swipes |
59 | | `uncutPages` | `boolean` | `false` | If `true`, the pages will be allowed to overflow through the container. The original effect is to keep everything inside the container, but you can set this to `true` to have a more "bookish" effect. |
60 | | `animationDuration` | `number` | `200` | Duration in ms of the fold/unfold animation |
61 | | `treshold` | `number` | `10` | Distance in px to swipe before the gesture is activated |
62 | | `maxAngle` | `number` | `45` | Angle of the page when there's nothing to display before/after |
63 | | `maskOpacity` | `number` | `0.4` | Opacity of the masks that covers the underneath content |
64 | | `perspective` | `string` | `130em` | Perspective value of the page fold effect. The bigger, the less noticeable |
65 | | `pageBackground` | `string` | `#fff` | Background of the pages. This can be overriden in individual pages by styling the component |
66 | | `firstComponent` | `element` | `null` | Component that will be displayed under the first page |
67 | | `lastComponent` | `element` | `null` | Component that will be displayed under the last page |
68 | | `showHint` | `bool` | `false` | Indicates if the component must hint the user on how it works. Setting this to `true` will lift the bottom of the page 1s after the component is mounted, for 1s |
69 | | `showSwipeHint` | `bool` | `false` | Indicates if the component must hint the user on how it works. Setting this to `true` will show an example of gesture to switch pages |
70 | | `showTouchHint` | `bool` | `false` | Indicates if the component must hint the user on how it works. Setting this to `true` will show a pointer indicating where to click to switch pages. Works with
71 | `flipOnTouch` |
72 | | `style` | `object` | `{}` | Additional style for the flipboard |
73 | | `height` | `number` | `480` | Height for the flipboard |
74 | | `width` | `number` | `320` | Width for the flipboard |
75 | | `onPageChange` | `function` | | Callback when the page has been changed. Parameters: `pageIndex`, `direction` |
76 | | `onStartPageChange` | `function` | | Callback when the page starts to change. Parameters: `oldPageIndex`, `direction` |
77 | | `onStartSwiping` | `function` | | Callback when the user starts swiping |
78 | | `onStopSwiping` | `function` | | Callback when the user stops swiping |
79 | | `className` | `string` | `''` | Optional CSS class to be applied on the container |
80 | | `loopForever` | `boolean` | `false` | If `true` flipping after the last page will return to the first (and visa-versa) |
81 | | `flipOnTouch` | `boolean` | `false` | If `true`, the user can flip pages by touching/clicking a top/bottom or left/right zone. These zones have CSS classes: `rfp-touchZone`, `rfp-touchZone-previous` and `rfp-touchZone-next` so that you can style them |
82 | | `flipOnTouchZone` | `number` | `210` | Percentage of dimensions of the zone to touch/click to flip pages |
83 | | `disableSwipe` | `boolean` | `false` | If `true`, users can't use the swipe feature to switch pages while `flipOnTouch` is enabled. Make sure you enable `flipOnTouch` so they can switch pages, or provide buttons binded to Methods |
84 | | `responsive` | `boolean` | `false` | If `true`, the component will be responsive, meaning it will take all the available space. Place the component in a container before to make sure it is visible |
85 | | `startAt` | `number` | `0` | Default start position of the component |
86 | | `reverse` | `boolean` | `false` | If `true`, the user must swip in reverse order: he must swipe down/right to see the next page, and up/left to see the previous page. |
87 | | `swipeImmune` | `array` | `[]` | This array holds the CSS class names that the user can not initiate a swipe gesture from. |
88 | | `noShadow` | `boolean` | `false` | This disables the inset drop shadow on the inside of the flipping pages. |
89 |
90 | ## Methods
91 |
92 | There are currently three methods that can be called on the component. To call them, you can use the
93 | `ref` attribute in React:
94 |
95 | ```javascript
96 | { this.flipPage = component; }}>
97 | ...
98 |
99 |
100 | this.flipPage.gotoPreviousPage();
101 | ```
102 |
103 | ### `gotoPreviousPage()`
104 |
105 | This method triggers the effect and switches to the previous page, if possible.
106 |
107 | ### `gotoNextPage()`
108 |
109 | This method triggers the effect and switches to the next page, if possible.
110 |
111 | ### `gotoPage(page)`
112 |
113 | This methods positions the component to the wanted page index. The `page` argument should be between `0` and the number of pages. If not, a `RangeError` will be thrown. Also note that this does **not** call the `onPageChange` nor the `onStartPageChange` callback.
114 |
115 | ## Contribute
116 |
117 | Since this is an open source project and it's far from perfect, contribution is welcome. Fork the repository and start working on your fix or new feature. Remember, it's good practice to work in your own branch, to avoid painful merge conflicts.
118 |
119 | Once you think your work is ready, fire a [pull request](https://github.com/darenju/react-flip-page/pulls) with an understandable description of what you're bringing to the project. If it's alright, chances are high your work will be merged!
120 |
121 | ## Donate
122 |
123 | This project takes some of my time, and I do it for free. If you're kind enough, you can show your support for my work by making a small donation here:
124 |
125 | [](darenju@live.com)
126 |
127 | I would very much appreciate it!
128 |
--------------------------------------------------------------------------------
/src/index.jsx:
--------------------------------------------------------------------------------
1 | import React, { Component, Children } from 'react';
2 | import PropTypes from 'prop-types';
3 | import FlipPageItem from './item';
4 | import './Hint.css';
5 | import generateStyles from './generateStyles';
6 |
7 | const m = (...objs) => Object.assign({}, ...objs);
8 |
9 | const doNotMove = (e) => {
10 | e.preventDefault();
11 | };
12 |
13 | const getPosition = (e) => ({
14 | posX: e.pageX || e.clientX || (e.touches && e.touches[0].pageX),
15 | posY: e.pageY || e.clientY || (e.touches && e.touches[0].pageY),
16 | });
17 |
18 | class FlipPage extends Component {
19 | constructor(props) {
20 | super(props);
21 |
22 | this.state = {
23 | page: props.startAt, // current index of page
24 | startY: -1, // start position of swipe
25 | diffY: 0, // diffYerence between last swipe position and current position
26 | timestamp: 0, // time elapsed between two swipes
27 | angle: 0, // rotate angle of half page
28 | rotate: 0, // absolute value of above, limited to 45° if necessary
29 | direction: '', // original swipe direction
30 | lastDirection: '', // last registered swipe direction
31 | secondHalfStyle: {}, // transform style of bottom half
32 | firstHalfStyle: {}, // transform style of top half
33 | canAnimate: true, // should the component animate with the methods?
34 | };
35 |
36 | // binding events
37 | this.startMoving = this.startMoving.bind(this);
38 | this.moveGesture = this.moveGesture.bind(this);
39 | this.stopMoving = this.stopMoving.bind(this);
40 | this.reset = this.reset.bind(this);
41 | this.mouseLeave = this.mouseLeave.bind(this);
42 | this.incrementPage = this.incrementPage.bind(this);
43 | this.decrementPage = this.decrementPage.bind(this);
44 | this.hasNextPage = this.hasNextPage.bind(this);
45 | this.hasPreviousPage = this.hasPreviousPage.bind(this);
46 | this.turnRightOrDown = this.turnRightOrDown.bind(this);
47 | this.turnLeftOrUp = this.turnLeftOrUp.bind(this);
48 |
49 | this.transition = `transform ${this.props.animationDuration / 1000}s ease-in-out`;
50 | this.onStartSwipingCalled = false;
51 | }
52 |
53 | componentDidMount() {
54 | const { showHint, showSwipeHint } = this.props;
55 |
56 | if (showHint) {
57 | this.hintTimeout = setTimeout(() => this.showHint(), showSwipeHint ? 1800 : 1000);
58 | }
59 | }
60 |
61 | componentWillUnmount() {
62 | clearTimeout(this.hintTimeout);
63 | clearTimeout(this.hintHideTimeout);
64 | }
65 |
66 | getHeight() {
67 | const { responsive } = this.props;
68 | return !responsive ? `${this.props.height}px` : '100%';
69 | }
70 |
71 | getWidth() {
72 | const { responsive } = this.props;
73 | return !responsive ? `${this.props.width}px` : '100%';
74 | }
75 |
76 | flatChildren() {
77 | return Array.prototype.flat.call(this.props.children);
78 | }
79 |
80 | isLastPage() {
81 | return this.state.page + 1 === Children.count(this.flatChildren());
82 | }
83 |
84 | isFirstPage() {
85 | return this.state.page === 0;
86 | }
87 |
88 | showHint() {
89 | const { orientation, perspective } = this.props;
90 | const { transition } = this;
91 |
92 | this.setState({ secondHalfStyle: { transition } }, () => {
93 | this.setState({
94 | secondHalfStyle: {
95 | transition,
96 | transform: orientation === 'vertical' ? `perspective(${perspective}) rotateX(30deg)` : `perspective(${perspective}) rotateY(-30deg)`,
97 | },
98 | });
99 |
100 | const callback = () => this.setState({ secondHalfStyle: { transition } });
101 | this.hintHideTimeout = setTimeout(callback, 1000);
102 | });
103 | }
104 |
105 | incrementPage() {
106 | const lastPage = Children.count(this.flatChildren());
107 | const { page } = this.state;
108 | this.setState({
109 | page: (page + 1) % lastPage,
110 | }, () => this.props.onPageChange(this.state.page, 'next'));
111 | }
112 |
113 | decrementPage() {
114 | const lastPage = Children.count(this.flatChildren());
115 | const { page } = this.state;
116 | let nextPage;
117 |
118 | if (this.isFirstPage()) {
119 | nextPage = lastPage - 1;
120 | } else {
121 | nextPage = page - 1;
122 | }
123 | this.setState({
124 | page: nextPage,
125 | }, () => this.props.onPageChange(this.state.page, 'prev'));
126 | }
127 |
128 | hasNextPage() {
129 | const { loopForever } = this.props;
130 | return !this.isLastPage() || loopForever;
131 | }
132 |
133 | hasPreviousPage() {
134 | const { loopForever } = this.props;
135 | return !this.isFirstPage() || loopForever;
136 | }
137 |
138 | startMoving(e) {
139 | // prevent the button's and a's to not be clickable.
140 | const { tagName, className } = e.target;
141 |
142 | if (tagName === 'BUTTON' || tagName === 'A') {
143 | return;
144 | }
145 |
146 | doNotMove(e);
147 |
148 | const { swipeImmune } = this.props;
149 | const cancel = swipeImmune.some((testedClassName) => className.indexOf(testedClassName) !== -1);
150 |
151 | if (cancel) {
152 | return;
153 | }
154 |
155 | const { posX, posY } = getPosition(e);
156 |
157 | this.setState({
158 | startX: posX,
159 | startY: posY,
160 | canAnimate: false,
161 | });
162 | }
163 |
164 | moveGesture(e) {
165 | e.preventDefault();
166 |
167 | const { posX, posY } = getPosition(e);
168 |
169 | const {
170 | orientation, treshold, maxAngle, perspective, reverse,
171 | } = this.props;
172 | const {
173 | startX, startY, diffX, diffY, direction, lastDirection,
174 | } = this.state;
175 |
176 | if (startY !== -1) {
177 | const newDiffY = posY - startY;
178 | const newDiffX = posX - startX;
179 | const diffToUse = (direction === 'up' || direction === 'down') ? newDiffY : newDiffX;
180 | const angle = (diffToUse / 250) * 180;
181 | let useMaxAngle = false;
182 | if (direction === 'up' || direction === 'left') {
183 | useMaxAngle = reverse ? !this.hasPreviousPage() : !this.hasNextPage();
184 | } else if (direction === 'down' || direction === 'right') {
185 | useMaxAngle = reverse ? !this.hasNextPage() : !this.hasPreviousPage();
186 | }
187 |
188 | const rotate = Math.min(Math.abs(angle), useMaxAngle ? maxAngle : 180);
189 | const aboveTreshold = (Math.abs(newDiffX) > treshold || Math.abs(newDiffY) > treshold);
190 |
191 | let nextDirection = '';
192 |
193 | if (!this.onStartSwipingCalled && aboveTreshold) {
194 | this.props.onStartSwiping();
195 | this.onStartSwipingCalled = true;
196 | }
197 |
198 | // determine direction to prevent two-directions swipe
199 | if (direction === '' && aboveTreshold) {
200 | if (newDiffY < 0 && orientation === 'vertical') {
201 | nextDirection = 'up';
202 | } else if (newDiffY > 0 && orientation === 'vertical') {
203 | nextDirection = 'down';
204 | } else if (newDiffX < 0 && orientation === 'horizontal') {
205 | nextDirection = 'left';
206 | } else if (newDiffX > 0 && orientation === 'horizontal') {
207 | nextDirection = 'right';
208 | }
209 |
210 | this.setState({ direction: nextDirection });
211 | }
212 |
213 | // set the last direction
214 | let nextLastDirection = lastDirection;
215 | if (diffY > newDiffY) {
216 | nextLastDirection = 'up';
217 | } else if (diffY < newDiffY) {
218 | nextLastDirection = 'down';
219 | } else if (diffX > newDiffX) {
220 | nextLastDirection = 'right';
221 | } else if (diffX < newDiffX) {
222 | nextLastDirection = 'left';
223 | }
224 |
225 | this.setState({
226 | angle,
227 | rotate,
228 | timestamp: Date.now(),
229 | diffY: newDiffY,
230 | diffX: newDiffX,
231 | lastDirection: nextLastDirection,
232 | });
233 |
234 | // flip bottom
235 | if (newDiffY < 0 && this.state.direction === 'up') {
236 | this.setState({
237 | angle,
238 | secondHalfStyle: {
239 | transform: `perspective(${perspective}) rotateX(${rotate}deg)`,
240 | },
241 | });
242 | } else if (newDiffY > 0 && this.state.direction === 'down') {
243 | this.setState({
244 | angle,
245 | firstHalfStyle: {
246 | transform: `perspective(${perspective}) rotateX(-${rotate}deg)`,
247 | zIndex: 2, // apply a z-index to pop over the back face
248 | },
249 | });
250 | } else if (newDiffX < 0 && this.state.direction === 'left') {
251 | this.setState({
252 | angle,
253 | secondHalfStyle: {
254 | transform: `perspective(${perspective}) rotateY(-${rotate}deg)`,
255 | },
256 | });
257 | } else if (newDiffX > 0 && this.state.direction === 'right') {
258 | this.setState({
259 | angle,
260 | firstHalfStyle: {
261 | transform: `perspective(${perspective}) rotateY(${rotate}deg)`,
262 | zIndex: 2, // apply a z-index to pop over the back face
263 | },
264 | });
265 | }
266 | }
267 | }
268 |
269 | turnRightOrDown(callback) {
270 | const {
271 | perspective, orientation, animationDuration,
272 | } = this.props;
273 | const { transition } = this;
274 | let secondHalfTransform = `perspective(${perspective}) `;
275 |
276 | if (orientation === 'vertical') {
277 | secondHalfTransform += 'rotateX(180deg)';
278 | } else {
279 | secondHalfTransform += 'rotateY(-180deg)';
280 | }
281 |
282 | this.setState({
283 | firstHalfStyle: {
284 | transition,
285 | transform: '',
286 | zIndex: 'auto',
287 | },
288 |
289 | secondHalfStyle: {
290 | transition,
291 | transform: secondHalfTransform,
292 | },
293 |
294 | canAnimate: false,
295 | }, () => {
296 | setTimeout(() => {
297 | callback();
298 | this.setState({
299 | secondHalfStyle: {},
300 | canAnimate: true,
301 | });
302 | }, animationDuration);
303 | });
304 | }
305 |
306 | turnLeftOrUp(callback) {
307 | const {
308 | perspective, orientation, animationDuration,
309 | } = this.props;
310 | const { transition } = this;
311 | let firstHalfTransform = `perspective(${perspective}) `;
312 |
313 | if (orientation === 'vertical') {
314 | firstHalfTransform += 'rotateX(-180deg)';
315 | } else {
316 | firstHalfTransform += 'rotateY(180deg)';
317 | }
318 |
319 | this.setState({
320 | firstHalfStyle: {
321 | transition,
322 | transform: firstHalfTransform,
323 | zIndex: 2,
324 | },
325 |
326 | secondHalfStyle: {
327 | transition,
328 | transform: '',
329 | },
330 |
331 | canAnimate: false,
332 | }, () => {
333 | setTimeout(() => {
334 | callback();
335 | this.setState({
336 | firstHalfStyle: {},
337 | canAnimate: true,
338 | });
339 | }, animationDuration);
340 | });
341 | }
342 |
343 | gotoNextPage() {
344 | if (!this.hasNextPage() || !this.state.canAnimate) return;
345 |
346 | const { onStartPageChange, reverse } = this.props;
347 | // Send an event before the end of the change page animation
348 | onStartPageChange(this.state.page, 'next');
349 | // We separe the next/previous logic to the right/left logic
350 | if (!reverse) this.turnRightOrDown(this.incrementPage);
351 | else this.turnLeftOrUp(this.incrementPage);
352 | }
353 |
354 | gotoPreviousPage() {
355 | if (!this.hasPreviousPage() || !this.state.canAnimate) return;
356 |
357 | const { onStartPageChange, reverse } = this.props;
358 |
359 | // Send an event before the end of the change page animation
360 | onStartPageChange(this.state.page, 'prev');
361 | // We separe the next/previous logic to the right/left logic
362 | if (!reverse) this.turnLeftOrUp(this.decrementPage);
363 | else this.turnRightOrDown(this.decrementPage);
364 | }
365 |
366 | gotoPage(page) {
367 | const children = this.flatChildren();
368 | const { onPageChange } = this.props;
369 |
370 | if (page >= 0 && page < children.length) {
371 | this.setState({ page }, () => {
372 | onPageChange(page, 'set');
373 | });
374 | } else {
375 | throw new RangeError('`page` argument is out of bounds.');
376 | }
377 | }
378 |
379 | stopMoving(cb) {
380 | const { reverse, onStopSwiping } = this.props;
381 | const {
382 | timestamp, angle, direction, lastDirection,
383 | } = this.state;
384 | const delay = Date.now() - timestamp;
385 |
386 | // We don't check hasNextPage because it is done in gotoNextPage
387 | const goUpOrRight = angle <= -90
388 | || (delay <= 20 && direction === 'up' && lastDirection === 'up')
389 | || (delay <= 20 && direction === 'right' && lastDirection === 'right');
390 |
391 | const goDownOrLeft = angle >= 90
392 | || (delay <= 20 && direction === 'down' && lastDirection === 'down')
393 | || (delay <= 20 && direction === 'left' && lastDirection === 'left');
394 |
395 | // reset everything
396 | this.reset(() => {
397 | onStopSwiping();
398 |
399 | if (goUpOrRight) {
400 | if (!reverse) this.gotoNextPage();
401 | else this.gotoPreviousPage();
402 | }
403 |
404 | if (goDownOrLeft) {
405 | if (!reverse) this.gotoPreviousPage();
406 | else this.gotoNextPage();
407 | }
408 |
409 | if (typeof cb === 'function') {
410 | cb();
411 | }
412 | });
413 | }
414 |
415 | beforeItem() {
416 | const lastPage = Children.count(this.props.children);
417 | const { firstComponent, loopForever } = this.props;
418 | const children = this.flatChildren();
419 |
420 | if (!this.isFirstPage()) {
421 | return children[this.state.page - 1];
422 | }
423 |
424 | return loopForever ? children[lastPage - 1] : firstComponent;
425 | }
426 |
427 | afterItem() {
428 | const { lastComponent, loopForever } = this.props;
429 | const children = this.flatChildren();
430 |
431 | if (!this.isLastPage()) {
432 | return children[this.state.page + 1];
433 | }
434 |
435 | return loopForever ? children[0] : lastComponent;
436 | }
437 |
438 | mouseLeave() {
439 | if (this.props.flipOnLeave) {
440 | this.stopMoving();
441 | } else {
442 | this.reset();
443 | }
444 | }
445 |
446 | reset(callback) {
447 | const { transition } = this;
448 | this.onStartSwipingCalled = false;
449 |
450 | this.setState({
451 | startY: -1,
452 | startX: -1,
453 | angle: 0,
454 | rotate: 0,
455 | direction: '',
456 | lastDirection: '',
457 | secondHalfStyle: { transition },
458 | firstHalfStyle: { transition },
459 | canAnimate: true,
460 | }, callback);
461 | }
462 |
463 | renderPage(_page, key) {
464 | const activeItem = key === this.state.page;
465 |
466 | const { page, direction, rotate } = this.state;
467 | const {
468 | orientation,
469 | uncutPages,
470 | maskOpacity,
471 | pageBackground,
472 | animationDuration,
473 | flipOnTouch,
474 | disableSwipe,
475 | reverse,
476 | noShadow,
477 | } = this.props;
478 |
479 | const style = generateStyles(
480 | page,
481 | key,
482 | direction,
483 | rotate,
484 | uncutPages,
485 | orientation,
486 | maskOpacity,
487 | pageBackground,
488 | animationDuration,
489 | );
490 |
491 | const {
492 | container,
493 | part,
494 | visiblePart,
495 | firstHalf,
496 | secondHalf,
497 | face,
498 | back,
499 | before,
500 | after,
501 | cut,
502 | firstCut,
503 | pull,
504 | gradient,
505 | gradientBefore,
506 | gradientAfter,
507 | gradientSecondHalf,
508 | gradientFirstHalf,
509 | mask,
510 | maskReverse,
511 | } = style;
512 |
513 | const pageItem = (
514 |
518 | );
519 |
520 | const beforeItem = reverse ? this.afterItem() : this.beforeItem();
521 | const afterItem = reverse ? this.beforeItem() : this.afterItem();
522 |
523 | const clonedBeforeItem = beforeItem ? (
524 |
528 | ) : null;
529 | const clonedAfterItem = afterItem ? (
530 |
534 | ) : null;
535 |
536 | const allowSwipe = (flipOnTouch && !disableSwipe) || !flipOnTouch;
537 | const onStartTouching = allowSwipe ? this.startMoving : doNotMove;
538 |
539 | return (
540 |