Skip to content

Commit 4d41b9d

Browse files
authored
Merge pull request #3000 from mjmlio/feature/MJML-49-mj-carousel_thumbnails_functionality
fix(mjml-carousel): thumbnail functionality #2999
2 parents 9bac7c6 + d21f3b2 commit 4d41b9d

File tree

4 files changed

+74
-4
lines changed

4 files changed

+74
-4
lines changed

packages/mjml-carousel/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ tb-border-radius | px | border-radius of the thumbnails | none
4949
tb-hover-border-color | string | css border color of the hovered thumbnail | none
5050
tb-selected-border-color | string | css border color of the selected thumbnail | none
5151
tb-width | px | thumbnail width | null
52-
thumbnails | String | display or not the thumbnails (visible | hidden)
52+
thumbnails | String | visible / hidden / supported ('supported' shows only for clients that support full carousel functionality) | visible
5353

5454
### mj-carousel-image
5555

packages/mjml-carousel/src/Carousel.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default class MjCarousel extends BodyComponent {
1919
'padding-left': 'unit(px,%)',
2020
'padding-right': 'unit(px,%)',
2121
'right-icon': 'string',
22-
thumbnails: 'enum(visible,hidden)',
22+
thumbnails: 'enum(visible,hidden,supported)',
2323
'tb-border': 'string',
2424
'tb-border-radius': 'unit(px,%)',
2525
'tb-hover-border-color': 'color',
@@ -136,6 +136,19 @@ export default class MjCarousel extends BodyComponent {
136136
border-color: ${this.getAttribute('tb-selected-border-color')} !important;
137137
}
138138
139+
${range(0, length)
140+
.map(
141+
(i) =>
142+
`.mj-carousel-${carouselId}-radio-${i + 1}:checked ${repeat(
143+
'+ * ',
144+
length - i - 1,
145+
)}+ .mj-carousel-content .mj-carousel-${carouselId}-thumbnail
146+
`,
147+
)
148+
.join(',')} {
149+
display: inline-block !important;
150+
}
151+
139152
.mj-carousel-image img + div,
140153
.mj-carousel-thumbnail img + div {
141154
display: none !important;
@@ -261,7 +274,8 @@ export default class MjCarousel extends BodyComponent {
261274
}
262275

263276
generateThumbnails() {
264-
if (this.getAttribute('thumbnails') !== 'visible') return ''
277+
if (!['visible', 'supported'].includes(this.getAttribute('thumbnails')))
278+
return ''
265279

266280
return this.renderChildren(this.props.children, {
267281
attributes: {
@@ -375,6 +389,13 @@ export default class MjCarousel extends BodyComponent {
375389
)
376390
}
377391

392+
getChildContext() {
393+
return {
394+
...this.context,
395+
thumbnails: this.getAttribute('thumbnails'),
396+
}
397+
}
398+
378399
render() {
379400
return `
380401
${msoConditionalTag(

packages/mjml-carousel/src/CarouselImage.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export default class MjCarouselImage extends BodyComponent {
2424
}
2525

2626
getStyles() {
27+
const hasThumbnailsSupported = this.hasThumbnailsSupported()
2728
return {
2829
images: {
2930
img: {
@@ -49,7 +50,7 @@ export default class MjCarouselImage extends BodyComponent {
4950
a: {
5051
border: this.getAttribute('tb-border'),
5152
'border-radius': this.getAttribute('tb-border-radius'),
52-
display: 'inline-block',
53+
display: hasThumbnailsSupported ? 'none' : 'inline-block',
5354
overflow: 'hidden',
5455
width: this.getAttribute('tb-width'),
5556
},
@@ -62,6 +63,12 @@ export default class MjCarouselImage extends BodyComponent {
6263
}
6364
}
6465

66+
hasThumbnailsSupported() {
67+
const thumbnails =
68+
this.getAttribute('thumbnails') || this.context.thumbnails
69+
return thumbnails === 'supported'
70+
}
71+
6572
renderThumbnail() {
6673
const { carouselId, src, alt, 'tb-width': width, target } = this.attributes
6774
const imgIndex = this.props.index + 1
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const chai = require('chai')
2+
const { load } = require('cheerio')
3+
const mjml = require('../lib')
4+
5+
describe('mj-carousel-thumbnail thumbnails supported', function () {
6+
it('should render correct display in CSS style values on mj-carousel-thumbnail', function () {
7+
const input = `
8+
<mjml>
9+
<mj-body>
10+
<mj-section>
11+
<mj-column>
12+
<mj-carousel thumbnails="supported">
13+
<mj-carousel-image src="https://placehold.co/450x300/333/ccc/png" />
14+
<mj-carousel-image src="https://placehold.co/450x300/ccc/000/png" />
15+
<mj-carousel-image src="https://placehold.co/450x300/f45e43/fff/png" />
16+
</mj-carousel>
17+
</mj-column>
18+
</mj-section>
19+
</mj-body>
20+
</mjml>
21+
`
22+
23+
const { html } = mjml(input)
24+
25+
const $ = load(html)
26+
27+
// style values should be correct
28+
chai
29+
.expect(
30+
$('.mj-carousel-thumbnail')
31+
.map(function getAttr() {
32+
const start = $(this).attr('style').indexOf('display:') + 8
33+
const end = $(this).attr('style').indexOf(';', start)
34+
const result = $(this).attr('style').substring(start, end)
35+
return result
36+
})
37+
.get(),
38+
'Display CSS style values on mj-carousel-thumbnail',
39+
)
40+
.to.eql(['none', 'none', 'none'])
41+
})
42+
})

0 commit comments

Comments
 (0)