Skip to content

Commit 382fded

Browse files
committed
Merge branch 'feature/levitate-on-hover' into develop
2 parents fe3761d + c661a72 commit 382fded

File tree

5 files changed

+49
-15
lines changed

5 files changed

+49
-15
lines changed

README.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,19 @@ component.
2424
| Chrome >= 38 || |
2525
| Edge >= 14 || |
2626
| Firefox >= 16 | ✅‍ | |
27-
| IE 11-10 | ⚠️ | Animations are disabled for compatibility |
27+
| IE 11-10 | ⚠️ | Card flips have no animation |
2828
| IE 9.0 || No toggling of cards |
2929
| Opera >= 30 || |
3030
| Safari >= 6.2.8 || |
31-
| Safari 6.0.5 | ⚠️ | Card flip has no animation |
31+
| Safari 6.0.5 | ⚠️ | Card flips have no animation |
3232

3333

3434
## Getting Started
3535
You can import react-card-flipper into your React app. The following is a bare
3636
bones example.
3737

38+
> **Important:** The `<ReactCardFlipper>` component must have two `<div>` elements, one for the front and one for the back.
39+
3840
```js
3941
import React from 'react';
4042
import ReactDOM from 'react-dom';
@@ -56,26 +58,27 @@ ReactDOM.render(
5658
```
5759

5860
## Props and Options
59-
The `ReactCardFlipper` component has 3 props it accepts that you can use to adjust
61+
The `ReactCardFlipper` component has 4 props it accepts that you can use to adjust
6062
how your card behaves.
6163

62-
| Prop / Option | Accepted Prop(s) | Default |
63-
| ------------- |:---------------------------:|--------:|
64-
| `width` | String (ex: `300px`) | `auto` |
65-
| `height` | String (ex: `600px`) | `auto` |
66-
| `behavior` | String (`click` or `hover`) | `click` |
64+
| Prop / Option | Accepted Prop(s) | Default | Description |
65+
| ------------- |:---------------------------:|:-------:| ----------- |
66+
| `width` | String (ex: `300px`) | `auto` | Card width. |
67+
| `height` | String (ex: `600px`) | `auto` | Card height. |
68+
| `behavior` | String (`click` or `hover`) | `click` | If the card should click to flip, or hover to flip. |
69+
| `levitate` | Boolean | `false` | If the card should "levitate" up on hover. Only applied when `behavior` is `click`. |
6770

6871
#### Example:
6972
```js
7073
render() {
7174
return(
7275
<div>
73-
<ReactCardFlipper width="300px" height="550px" behavior="hover">
76+
<ReactCardFlipper width="300px" height="550px" behavior="click" levitate={true}>
7477
<div>
75-
<h3>Hover me to learn more</h3>
78+
<h3>Click me to learn more</h3>
7679
</div>
7780
<div>
78-
<p>You hovered! Keep that knowledge growing...</p>
81+
<p>You Clicked!</p>
7982
</div>
8083
</ReactCardFlipper>
8184
</div>

ReactCardFlipper.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
transform-style: preserve-3d;
99
width: auto;
1010
height: auto;
11+
transition: transform 500ms cubic-bezier(.18,.45,.11,.91);
1112
}
1213

1314
.rcf-front, .rcf-back {
@@ -38,6 +39,11 @@
3839
transform: rotateY(180deg);
3940
}
4041

42+
.rcf-levitate {
43+
transform: translateY(-15px);
44+
transition: transform 500ms cubic-bezier(.18,.45,.11,.91);
45+
}
46+
4147
/* IE Support for 11-10 */
4248
@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {
4349
.rcf-active .rcf-back {

ReactCardFlipper.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import './ReactCardFlipper.css';
1010
* Width: string (default: auto)
1111
* Height: string (default: auto)
1212
* behavior: click, hover (default: click)
13+
* levitate: boolean (default: false, only works when behavior is set to `click`)
1314
**/
1415

1516
class ReactCardFlipper extends Component {
@@ -20,11 +21,13 @@ class ReactCardFlipper extends Component {
2021
isFlipped: false,
2122
width: this.props.width,
2223
height: this.props.height,
23-
behavior: this.props.behavior ? this.props.behavior : 'click'
24+
behavior: this.props.behavior ? this.props.behavior : 'click',
25+
levitate: this.props.levitate ? this.props.levitate : false
2426
}
2527

2628
// bind this 🤙
2729
this.handleFlip = this.handleFlip.bind(this);
30+
this.handleMouseEvent = this.handleMouseEvent.bind(this);
2831
}
2932

3033
handleFlip(e) {
@@ -34,15 +37,29 @@ class ReactCardFlipper extends Component {
3437
})
3538
}
3639

40+
handleLevitate(e) {
41+
e.currentTarget.classList.toggle('rcf-levitate');
42+
}
43+
44+
handleMouseEvent(e) {
45+
if( this.state.behavior === 'hover' ) {
46+
return this.handleFlip(e);
47+
} else if ( this.state.behavior === 'click' && this.state.levitate ) {
48+
return this.handleLevitate(e);
49+
} else if ( this.state.behavior === 'click' ) {
50+
return
51+
}
52+
}
53+
3754
render(){
3855
const containerStyles = {
3956
width: this.state.width,
4057
height: this.state.height,
4158
}
4259

4360
return(
44-
<div className="rcf-container" style={containerStyles} onClick={ (e) => { if(this.state.behavior === 'click'){ this.handleFlip(e) }}} onMouseEnter={ (e) => { if(this.state.behavior === 'hover'){ this.handleFlip(e) }}}
45-
onMouseLeave={ (e) => { if(this.state.behavior === 'hover'){ this.handleFlip(e) }}}>
61+
<div className="rcf-container" style={containerStyles} onClick={ (e) => { if(this.state.behavior === 'click'){ this.handleFlip(e) }}} onMouseEnter={ (e) => { this.handleMouseEvent(e) }}
62+
onMouseLeave={ (e) => { this.handleMouseEvent(e) }}>
4663
<div className="rcf-flipper">
4764
<div className="rcf-front" style={containerStyles}>
4865
{this.props.children[0]}

Test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ ReactDOM.render(
2222
Great job! You win person of the day.
2323
</div>
2424
</ReactCardFlipper>
25+
<ReactCardFlipper width="300px" height="400px" behavior="click" levitate={true}>
26+
<div className="text-center">
27+
You can click me, and I levitate
28+
</div>
29+
<div className="text-center">
30+
Great job! You win person of the minute.
31+
</div>
32+
</ReactCardFlipper>
2533
</div>
2634
</div>
2735
</div>,

tests/tests.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
.rcf-container {
66
display: inline-block;
7-
margin: 0 20px;
7+
margin: 10px 20px;
88
}
99

1010
.rcf-front, .rcf-back{

0 commit comments

Comments
 (0)