Skip to content

Commit 1186067

Browse files
authored
fix(carousel): update carousel design (#308)
* fix(carousel): update carousel design #287 * feat(carousel): add pauseOnHover
1 parent 77c5014 commit 1186067

File tree

7 files changed

+19094
-153
lines changed

7 files changed

+19094
-153
lines changed

doc/package-lock.json

Lines changed: 14282 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

doc/src/components/common/componentData/Carousel/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ export const componentData = {
3333
type: 'Boolean',
3434
defaultValue: 'true',
3535
description: 'Set boolean for showing / hiding left and right controls.',
36+
},
37+
{
38+
prop: 'pauseOnHover',
39+
type: 'Boolean',
40+
defaultValue: 'true',
41+
description: 'Set boolean to pause the carousel animation on mouse hover.',
3642
}
3743
],
3844
themesData: [

lib/carousel/index.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,25 @@ class Carousel extends React.Component {
1818
}
1919

2020
setActiveItem = (active) => {
21-
const { data } = this.props;
21+
const { data, pauseOnHover } = this.props;
2222
clearInterval(this.animation);
2323
this.setState({
2424
active: (data.length + active) % data.length,
25-
}, this.animate);
25+
}, () => {
26+
if (!pauseOnHover) {
27+
this.animate();
28+
}
29+
});
2630
};
2731

32+
pauseAnimation = () => {
33+
clearInterval(this.animation);
34+
}
35+
36+
resumeAnimation = () => {
37+
this.animate();
38+
}
39+
2840
animate = () => {
2941
const { data, interval } = this.props;
3042
this.animation = setInterval(() => {
@@ -35,10 +47,16 @@ class Carousel extends React.Component {
3547
}
3648

3749
render() {
38-
const { theme, data, controls } = this.props;
50+
const {
51+
theme, data, controls, pauseOnHover,
52+
} = this.props;
3953
const { active } = this.state;
4054
return (
41-
<div className={theme['carousel-container']}>
55+
<div
56+
className={theme['carousel-container']}
57+
onMouseEnter={pauseOnHover ? this.pauseAnimation : undefined}
58+
onMouseLeave={pauseOnHover ? this.resumeAnimation : undefined}
59+
>
4260
{/* eslint-disable jsx-a11y/click-events-have-key-events */}
4361
{/* eslint-disable jsx-a11y/no-static-element-interactions */}
4462
{
@@ -78,12 +96,14 @@ Carousel.propTypes = {
7896
theme: PropTypes.oneOfType([PropTypes.object]),
7997
interval: PropTypes.number,
8098
controls: PropTypes.bool,
99+
pauseOnHover: PropTypes.bool,
81100
};
82101

83102
Carousel.defaultProps = {
84103
interval: 4000,
85104
theme: defaultTheme,
86105
controls: true,
106+
pauseOnHover: true,
87107
};
88108

89109
export default themr('CBCarousel', defaultTheme)(Carousel);

lib/carousel/readMe.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ A basic Carousel component.
88
| `data` | `Array` | &nbsp; | An array of image urls or custom children. ( `Required` ) |
99
| `interval` | `Number` | `4000` | Set an interval ( in ms ) for switching between carousel items. |
1010
| `controls` | `Boolean` | `true` | Set boolean for showing / hiding left and right controls. |
11+
| `pauseOnHover` | `Boolean` | `true` | Set boolean to pause the carousel animation on mouse hover. |
1112

1213
### Theme
1314

lib/carousel/tests/render.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,23 @@ describe('Carousel Render tests', () => {
6565
done();
6666
}, 4000 * data.length);
6767
});
68+
69+
it('Successfully pauses carousel animation on hover and resume after leaving the carousel area.', () => {
70+
const expectedValueBeforeChange = data[0];
71+
const expectedValueAfterChange = data[1];
72+
73+
const simulatedValue = () => wrappedComponent.find('.current').props().children.props.src;
74+
75+
expect(simulatedValue()).to.equal(expectedValueBeforeChange);
76+
wrappedComponent.children().childAt(0).simulate('mouseEnter');
77+
setTimeout(() => {
78+
expect(simulatedValue()).to.equal(expectedValueBeforeChange);
79+
wrappedComponent.children().childAt(0).simulate('mouseLeave');
80+
setTimeout(() => {
81+
expect(simulatedValue()).to.equal(expectedValueAfterChange);
82+
done();
83+
}, 4000);
84+
}, 6000);
85+
});
86+
6887
});

lib/carousel/theme.module.scss

Lines changed: 78 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,95 @@
11
:local(.carousel-container) {
2-
position: relative;
3-
width: 100%;
4-
height: 250px;
5-
display: flex;
6-
justify-content: center;
7-
overflow: hidden;
2+
position: relative;
3+
width: 100%;
4+
height: 250px;
5+
display: flex;
6+
justify-content: center;
7+
overflow: hidden;
8+
&:hover {
9+
&::before,
10+
&::after {
11+
opacity: 1;
12+
}
13+
}
14+
&::before,
15+
&::after {
16+
content: '';
17+
display: block;
18+
height: 100%;
19+
width: 9%;
20+
position: absolute;
21+
transition: opacity 1s ease;
22+
opacity: 0;
23+
}
24+
&::before {
25+
left: 0;
26+
background-image: linear-gradient(to right, rgba(0,0,0,0.5) 0, 70%, transparent);
27+
z-index: 1;
28+
}
29+
&::after {
30+
right: 0;
31+
background-image: linear-gradient(to left, rgba(0,0,0,0.5) 0, 70%, transparent);
32+
}
833
}
934

1035
:local(.carousel-item) {
11-
display: flex;
12-
width: 100%;
13-
height: 100%;
14-
justify-content: center;
15-
align-items: center;
16-
&.active {
17-
opacity: 0;
18-
transition: 1.5s ease-in-out;
19-
position: absolute;
20-
left: 0;
21-
}
22-
&.current {
23-
opacity: 1;
24-
transition: 1.5s ease-in-out;
25-
}
26-
& > img {
27-
height: 100%;
36+
display: flex;
2837
width: 100%;
29-
}
38+
height: 100%;
39+
justify-content: center;
40+
align-items: center;
41+
&.active {
42+
opacity: 0;
43+
transition: 1.5s ease-in-out;
44+
position: absolute;
45+
left: 0;
46+
}
47+
&.current {
48+
opacity: 1;
49+
transition: 1.5s ease-in-out;
50+
}
51+
&>img {
52+
height: 100%;
53+
width: 100%;
54+
}
3055
}
3156

3257
:local(.dot) {
33-
position: absolute;
34-
bottom: 10%;
35-
display: flex;
36-
flex-wrap: wrap;
37-
justify-content: center;
38-
& > span {
39-
cursor: pointer;
40-
height: 5px;
41-
width: 5px;
42-
margin: 2px;
43-
border: 1px solid white;
44-
border-radius: 50%;
45-
background-color: transparent;
46-
transition: background-color 1.5s ease-in-out;
47-
&.active {
48-
background-color: white;
58+
position: absolute;
59+
bottom: 10%;
60+
display: flex;
61+
flex-wrap: wrap;
62+
justify-content: center;
63+
&>span {
64+
cursor: pointer;
65+
height: 5px;
66+
width: 5px;
67+
margin: 2px;
68+
border: 1px solid white;
69+
border-radius: 50%;
70+
background-color: transparent;
71+
transition: background-color 1.5s ease-in-out;
72+
&.active {
73+
background-color: white;
74+
}
4975
}
50-
}
5176
}
5277

5378
:local(.left-control),
5479
:local(.right-control) {
55-
z-index: 1;
56-
font-size: 2rem;
57-
color: white;
58-
position: absolute;
59-
top: 45%;
60-
align-self: center;
61-
cursor: pointer;
80+
z-index: 1;
81+
font-size: 2rem;
82+
color: white;
83+
position: absolute;
84+
top: 45%;
85+
align-self: center;
86+
cursor: pointer;
6287
}
88+
6389
:local(.left-control) {
64-
left: 2%;
90+
left: 2%;
6591
}
92+
6693
:local(.right-control) {
67-
right: 2%;
68-
}
94+
right: 2%;
95+
}

0 commit comments

Comments
 (0)