Giter Club home page Giter Club logo

react-card-flip's Introduction

Build Status Coverage Status

ReactCardFlip

React Card Flip is allows you to use the card flipping animation. Credit for the CSS goes to David Walsh.

Demo & Examples

Live demo: Demo

To build the examples locally with npm, run:

npm install
npm run build-example && npm run start

To build the examples locally with yarn, run:

yarn install
yarn build-example && yarn start

Then localhost:8080 should open in a browser. If not you can go to that directly.

Installation

To use react-card-flip, install it from NPM with npm using the command:

npm install --save react-card-flip

To use react-card-flip, install it from NPM with yarn using the command:

yarn add react-card-flip

You can also use the standalone build by including lib/react-card-flip.js in your page. If you use this, make sure you have already included React, and it is available as a global variable.

Usage

To use this component, first import ReactCardFlip:

import ReactCardFlip from 'react-card-flip';

and then provide it with two child components with keys marked front and back so that the component can tell which component should be in the front and which component should be in the back.

This component only allows for manual card flip so make sure to include a tag that has an onClick handler for each side of the card.

The animation itself will be controlled by the prop isFlipped. Use this to control whether to show the front or the back of the card.

class App extends React.Component {
  constructor() {
    super();
      this.state = {
      isFlipped: false
    };
    this.handleClick = this.handleClick.bind(this);
  }

  handleClick(e) {
    e.preventDefault();
    this.setState(prevState => ({ isFlipped: !prevState.isFlipped }));
  }

  render() {
    return (
      <ReactCardFlip isFlipped={this.state.isFlipped} flipDirection="vertical">
        <YOUR_FRONT_CCOMPONENT>
          This is the front of the card.
          <button onClick={this.handleClick}>Click to flip</button>
        </YOUR_FRONT_CCOMPONENT>

        <YOUR_BACK_COMPONENT>
          This is the back of the card.
          <button onClick={this.handleClick}>Click to flip</button>
        </YOUR_BACK_COMPONENT>
      </ReactCardFlip>
    )
  }
}

YOUR_FRONT_CCOMPONENT and YOUR_BACK_COMPONENT here are meant to be the two components that you plan to use for the card, one for the front of the card and one for the back of the card.

Properties

Props Type Description Default
cardStyles object cardStyles.front styles are applied to the front face component container and cardStyles.back styles are applied to the back face component container undefined
cardZIndex string z-Index for the flip card. Used to help solve context stack issues while using multiple flip cards. auto
containerStyle object Extra css styling that can be applied to the container. {}
containerClassName string Custom class name to be applied to the container. undefined
isFlipped bool False to show the front of the card, true to show the back undefined
flipSpeedBackToFront number The speed of the flip animation when the card flips from back to front, the higher the number the slower the flip animation 0.6
flipSpeedFrontToBack number The speed of the flip animation when the card flips from front to back, the higher the number the slower the flip animation 0.6
infinite bool False to rotate in opposite directions on both sides of the card, true to rotate in the same direction false
flipDirection string Direction of the card flip (options are: 'horizontal' or 'vertical' ) horizontal

Development (src, lib and the build process)

NOTE: The source code for the component is in src. A transpiled CommonJS version (generated with Babel) is available in lib for use with node.js, browserify and webpack. A UMD bundle is also built to lib, which can be included without the need for any build system.

To build, watch and serve the examples (which will also watch the component source), run npm run build-example && npm run start.

Testing

To run tests for this project run one of the following commands:

  • npm run test - Runs tests then exits
  • npm run test:watch - Runs tests in watch mode
  • npm run test:coverage - Runs tests and creates a coverage report

Contributing

Fork this repo, add your proposed features and make a pull request. I will review as soon as possible.

License

This project is licensed under the terms of the MIT license. Check LICENSE.txt for more information.

react-card-flip's People

Contributors

aaronccwong avatar adamszeptycki avatar ammarlakis avatar blackfenix2 avatar chmac avatar claydiffrient avatar danielfrg avatar dependabot[bot] avatar dododevs avatar fwcd avatar gsasouza avatar igorkamyshev avatar marcusellison avatar petekeller2 avatar sbardian avatar thiagodebastos avatar yangchristina avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-card-flip's Issues

Absolute positioning

First things first, nice project.

My problem is that I do not understand why the "card" has an absolute position, I need it to be relative. Is there away to go around this or?

Latest update has broken our solution in Chrome

After the latest update, after flipping a tile, the back of the tile is no longer interactable. This only happens in Chrome as far as I can tell. It also seems to have slowed down all of the other animations in our solution. We have downgraded the package to fix, and will probably stay on the downgraded version for the foreseeable future, but I just thought that you should be aware. 👍

Could not resolve dependency

Any idea how to resolve this error? I get it when trying npm install:

npm ERR! Found: [email protected]
npm ERR! node_modules/react
npm ERR! react@"^16.14.0" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^17.0.1" from [email protected]
npm ERR! node_modules/react-card-flip
npm ERR! react-card-flip@"^1.0.11" from the root project

The package.json has: "react-card-flip": "^1.0.11",

I removed the node_modules directory and the package-lock.json file.

I'm not sure where the reference to 1.1.0 is coming from. I DID try to install it earlier when trying to upgrade React 17, but ran into limitations with blueprintjs/core 3.39.0 requiring React 16.14.0.

Thanks for any ideas or help you can provide.

Full content of the package.json below in case that helps.

{
"files": [
"./LICENSE.md"
],
"name": "myproject",
"version": "0.1.0",
"private": true,
"dependencies": {
"@blueprintjs/core": "^3.39.0",
"@ckeditor/ckeditor5-build-classic": "^12.2.0",
"@ckeditor/ckeditor5-react": "^1.1.3",
"apexcharts": "^3.19.3",
"axios": "^0.21.1",
"bootstrap": "^3.4.1",
"cleave.js": "^1.5.2",
"country-state-city": "^1.0.0",
"fomantic-ui": "^2.8.7",
"moment": "^2.24.0",
"plaid": "^8.1.6",
"react": "^16.14.0",
"react-apexcharts": "^1.3.7",
"react-bootstrap": "^1.4.3",
"react-burger-menu": "^2.6.11",
"react-card-flip": "^1.0.11",
"react-dom": "^16.8.6",
"react-dropzone": "^10.1.4",
"react-google-login": "^5.1.21",
"react-grid-system": "^4.4.5",
"react-intl-universal": "^2.4.1",
"react-redux": "^6.0.1",
"react-router-dom": "^5.2.0",
"react-scripts": "^3.4.4",
"react-scroll": "^1.7.14",
"react-select": "^2.4.4",
"react-share": "^3.0.0",
"react-signature-canvas": "^1.0.3",
"react-slick": "^0.24.0",
"react-star-ratings": "^2.3.0",
"react-stripe-elements": "^3.0.0",
"reactjs-popup": "^1.3.2",
"reactstrap": "^8.0.0",
"reaptcha": "^1.4.2",
"redux": "^4.0.1",
"redux-thunk": "^2.3.0",
"semantic-ui-react": "^0.86.0",
"socket.io-client": "^2.3.0",
"sweetalert2": "^10.14.0",
"sweetalert2-react": "^0.8.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
],

Text Overflow Impacting Visibility

I noticed that if I add a text with fixed height and overflowY:scroll, the back will show even if
it's not flipped.

screen shot 2018-03-19 at 3 29 50 pm

I'm trying to squeeze a long text in a small card so I'm using overflowY.

Thanks

mapping items

I have 3 cards and I display them via Array.map.
Each card has a button and when I click on it, all 3 cards turn over, not just one
image
image

Property cardStyles not documented.

It turns out ReactCardFlip accepts a cardStyles property, which can augment the styles passed to the front and back component wrappers.

This property isn't however documented. Maybe it could be added to the list on Readme.md as the property was benificial in solving a z-index issue I was having and could be benificial to others.

dependency tree includes [email protected] which breaks npm install for recent NodeJS versions

Trying rm -rf node_modules && rm package-lock.json && npm install in my project:

npm ERR! code ENOTSUP
npm ERR! notsup Unsupported engine for [email protected]: wanted: {"node":">=0.10 <=7"} (current: {"node":"9.5.0","npm":"5.6.0"})
npm ERR! notsup Not compatible with your version of node/npm: [email protected]
npm ERR! notsup Not compatible with your version of node/npm: [email protected]
npm ERR! notsup Required: {"node":">=0.10 <=7"}
npm ERR! notsup Actual:   {"npm":"5.6.0","node":"9.5.0"}

Ouch! It turns out react-card-flip currently depends on [email protected] (instead of a more recent version). See:

npm install --global npm-remote-ls && npm-remote-ls [email protected] | grep -i upath

...and indeed in your yarn.lock:

https://github.com/AaronCCWong/react-card-flip/blob/master/yarn.lock#L1499

It seems that chokidar needs to be updated to the latest version 2.0.1 to fix this problem:

https://github.com/paulmillr/chokidar/pull/679/files

Meanwhile, I am just going to revert back to a previous version of react-card-flip.

Thanks for looking into this!

Missing typescript types

When I import this library in my typescript project and try to build it I get a message: ReactCardFlip.d.ts (2,39): Cannot find name ReactFlipCardProps. I see that this name is defined in the github source but it is not in the node_modules folder.

Allow setting className for container

Hi,

I'm very glad this package exists! I would really appreciate it if was possible to add a className to the container, in addition to adding containerStyles. This would make things easier for me as I am using this component together with TailwindCSS.

This is the line that would need changing.

className="react-card-flip"

ReactCardFlip causing incorrect sizing

Here's what my first card looks like before implementing RCF:
screen shot 2017-05-22 at 11 23 31 am

And here's how it looks after implementing RCF:
screen shot 2017-05-22 at 11 23 49 am

Material-ui's Card components are supposed to take the height and width of the components inside. But for some reason, when implementing RCF, it's forcing the Card component to take the size of it's relative Cards in a GridList?

Multiple cards in a single row

I'm attempting to use react-card-flip using bootstrap rows and cols. Unfortunately the cards all stack on top of one another rather than the intended effect. Any insights on how I can fix this?

Jank with Safari

You demo works fine on Safari, but my code does not. I see rendering faults while the card is flipping, as if backface-visiblity:hidden isn't working.

P.S. I slowed the speed down and was surprised to see that as the card is flipping to the back side, the front side is upside down, but only on Safari.

no issues on Chrome.

I switched to a fork: https://github.com/kennethormandy/react-flipcard which works on Safari. Unfortunately, the animation style is missing some sweetness you have in yours, and I could not figure out what it was. So I really hope you get this working on Safari.

React 18 support - peerDependencies update request

Hello!

I'm currently using this package in an app I just upgraded to React 18. It seems to work fine, however it complains about its peerDependencies as it's set to React 17.

Could the peerDependencies be updated to be a bit more lax and allow newer versions of react & react-dom as well?

Thank you!

Cards wont go in rows

Tried plenty of things with Container, Row and Col from react-bootstrap but will not place more then one card in a row. Sizing of card changes like its trying to fit multiple cards. Any help would be great.

Dependency issues

getting errors trying to do an npm install

npm i react-card-flip
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: [email protected]
npm ERR! Found: [email protected]
npm ERR! node_modules/react
npm ERR! react@"^17.0.2" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^18.2.0" from [email protected]
npm ERR! node_modules/react-card-flip
npm ERR! react-card-flip@"*" from the root project

Typescript Support

Just a discussion thread for my eventual PR to add typescript support for this app. i can start by adding type definitions and go further by writing everything in typescript vs javascript. this would remove the need for the prop-types peer dependency.

preserve-3d is causing issues in Chrome v94.x

After upgrading Chrome to version 94, half of the content in react card flip is not interactive. I tried to debug it and found that disabling transformStyle preserve-3d works fine.

Material-UI components inside front/back components

Your docs say to simply fill the front and back components with anything. My app is using the react material-ui library; can I essentially use the Card component (material-ui) wrapped with your front and back components? My idea below:

<ReactCardFlip isFlipped={this.state.isFlipped}>
  <FrontComponent key="front">
     <Card>
     </Card>
  </FrontComponent>

  <BackComponent key="back">
     <Card>
     </Card>
  </BackComponent>
</ReactCardFlip>

Large bundle because of dependencies

The minified bundle is 14kB while the raw source is only 4kB, this is probably because some dependencies are included in the bundle. It should be up to the user of the package to install the required dependencies so that they are not duplicated in the requiring application. To resolve this the package.json should switch to peerDependencies instead of dependencies.

FrontComponent

I noticed you have a FrontComponent element in your code, When I use your code it gives an error that it is undefined. I thought it would be built in but doesn't seem like it. Where is this coming from? I didn't find it in your GitHub source either.

The transform: rotateY(180deg); is not rendering and I'm thinking it might be because of this. It is the only thing different between your code and mine.

Rebuild dist

Hello,

Having an issue with the default install but using the src file as is works in current React version. Please rebuild the dist.

upgrade react peer dependency to v17

Erro to install dependency in a new react app using npm 7

Screenshot
zsh

To Reproduce
Steps to reproduce the behavior:

  1. Create a new app with create-react-app typescript template
  2. Run command npm install react-card-flip --save
  3. Installation will be rejected.

Expected behavior
Be able to install dependency with success

Desktop:

OS: Mac OSX
Browser Chrome
Version

  • Node: v15.2.0
  • NPM: 7.0.10

Possible fix:
Upgrade react peer dependency to v17

filpped Animation bug

http://www.giphy.com/gifs/l0HUjHxrumnFNytJ6

this is my code:

		import React from 'react';
		import styled from 'styled-components';
		import ReactCardFlip from 'react-card-flip';

		const Card = styled.div`
			border-radius: 15px;
			cursor: pointer;
			height: 640px;
			position: absolute;
			width: 480px;
			
		`;
		const CardBackGround = styled.div`
			border-radius: 15px;
			background-repeat: no-repeat;
			background-size: cover;
			height: 640px;
			position: absolute;
			width: 480px;
		`;

		const CardContent = styled.div`
			bottom: 0px;
			background-color: rgba(0, 0, 0, 0.4);
			border-radius: 0px 0px 15px 15px;
			height: auto;
			position: absolute;
			width: 100%;
			z-index: 3;
		`;

		const Description = styled.p`
			clear: both;
			color: white;
			font-size: 20px;
			padding-left: 10px;
			padding-right: 10px;
		`;

		const TitleGroup = styled.div`
			padding-left: 10px;
			padding-right: 10px;
			padding-top: 20px;
			padding-bottom: 30px;
		`;

		const Title = styled.text`
			color: #ffffff;
			float: left;
			font-family: 'Times New Roman';
			font-size: 24px;
		`;

		const Tag = styled.text`
			color: #ffffff;
			float: right;
			font-family: 'Times New Roman';
			font-size: 24px;
		`;

		class PeopleCard extends React.Component {
			constructor() {
				super();
				this.state = {
					isFlipped: false
				};
				this.handleClick = this.handleClick.bind(this);
			}

			handleClick(e) {
				e.preventDefault();
				this.setState({ isFlipped: !this.state.isFlipped });
			}
			render() {
				return (
					<ReactCardFlip isFlipped={this.state.isFlipped} infinite>
						<Card onClick={this.handleClick} key="front">
							<CardBackGround style={{ backgroundImage: 'url(https://goo.gl/1m2DXS)' }}>
								<CardContent>
									<TitleGroup>
										<Title>Jofh grook</Title>
										<Tag>吟遊詩人</Tag>
									</TitleGroup>
									<Description>
										The border-radius property defines the radius of the element's corners. Tip: This property allows you to
										add rounded borders to elements!
									</Description>
								</CardContent>
							</CardBackGround>
						</Card>
						<Card onClick={this.handleClick} key="back">
							<CardBackGround>
								<CardContent>
									<TitleGroup>
										<Title>Jofh grook</Title>
										<Tag>吟人</Tag>
									</TitleGroup>
									<Description>
										The border-radius property defines the radius of the element's corners. Tip: This property allows you to
										add rounded borders to elements!
									</Description>
								</CardContent>
							</CardBackGround>
						</Card>
					</ReactCardFlip>
				);
			}
		}

		export default PeopleCard;

Issue with other components - causing overlap

I'm experiencing an issue with other components causing an overlap for example if I have a dropdown using bootstrap, it gets overlapped with a near by component. See screenshot below:

Overlap issue

The issue seems to be with the .react-card-flip and .react-card-flipper because when I disable the following in .react-card-flip:

perspective: 1000px;
transform-style: preserve-3d;

and disable the following in .react-card-flipper:

transform-style: preserve-3d;

It behaves fine. See screenshot below:

dropdown showing fine

Do you know how to resolve this?

Not working on react 16

Hello, I'm trying a super simple example and, nothing is rendered in the screen, also no errors are logged in the console
Here is the snippet, note that if I replace the "CardFlip" tag with a span, the element is rendered as expected:


...
import CardFlip from 'react-card-flip';
function Component(props){
return <CardFlip isFlipped={true}>
                    <div key="front">
                    <img
                            src="//img.buzzfeed.com/buzzfeed-static/static/2014-04/enhanced/webdr06/4/16/enhanced-11136-1396643149-13.jpg?no-auto"
                        />
                        <button>Click to flip</button>
                    </div>
                    <div key="back">
                    <img
                            src="//img.buzzfeed.com/buzzfeed-static/static/2014-04/enhanced/webdr06/4/16/enhanced-11136-1396643149-13.jpg?no-auto"
                        />
                        <button>Click to flip</button>
                    </div>
                </CardFlip>

}
...

I'm using react V16.2.0

Some parts of the back shows on the front

Thank you for making this. It works very well. I just found a little bug that I would like to report here.

  • Browser: Chrome Version 65.0.3325.162 (Official Build) (64-bit)
  • OS: macOS High Sierra Version 10.13.3

When the screen size goes lower than a specific size, some elements from the back of the card show up on the front! (mirrored). I have put them in red boxes below.

Any clue what's happening here?

Screen width <= 768px

Front
screenshot 2018-03-27 10 09 07

Back
screenshot 2018-03-27 10 09 07


Screen width > 768px

Front
screenshot 2018-03-27 10 09 41

back
screenshot 2018-03-27 10 09 41

Why the default to position absolute ?

Kinda messes with my layout. I feel this component should be not positioned from the intern of the library itself. I'll have to remove that position absolute with !important 📦

Other than that, great reusable component.

One thing would be cool would be to modify the transform speed and animation.

example using the State Hook no readme :)

import { useState } from 'react';
import ReactCardFlip from 'react-card-flip';

const Card = () => {
const [isFlipped, setIsFlipped] = useState(false)

function handleClick(e) {
e.preventDefault();
setIsFlipped(!isFlipped);
}

return (
<>

<YOUR_FRONT_CCOMPONENT>
This is the front of the card.
Click to flip
</YOUR_FRONT_CCOMPONENT>

    <YOUR_BACK_COMPONENT>
      This is the back of the card.
      <button onClick={handleClick}>Click to flip</button>
    </YOUR_BACK_COMPONENT>       
  </ReactCardFlip>
</>

)
}

export default Card;

How do I override the css?

I have an image inside the flipper. I want to change the style of the react-card-front and react-card-back. I want the position to be relative instead of absolute. Is there a way to do that?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.