Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 1 addition & 18 deletions packages/mjml-hero/src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { BodyComponent } from 'mjml-core'
import { flow, identity, join, filter } from 'lodash/fp'

import widthParser from 'mjml-core/lib/helpers/widthParser'

const makeBackgroundString = flow(filter(identity), join(' '))

export default class MjHero extends BodyComponent {
Expand Down Expand Up @@ -46,29 +44,14 @@ export default class MjHero extends BodyComponent {
}

getChildContext() {
// Refactor -- removePaddingFor(width, ['padding', 'inner-padding'])
const { containerWidth } = this.context
const paddingSize =
this.getShorthandAttrValue('padding', 'left') +
this.getShorthandAttrValue('padding', 'right')

let currentContainerWidth = `${parseFloat(containerWidth)}px`

const { unit, parsedWidth } = widthParser(currentContainerWidth, {
parseFloatToInt: false,
})

if (unit === '%') {
currentContainerWidth = `${
(parseFloat(containerWidth) * parsedWidth) / 100 - paddingSize
}px`
} else {
currentContainerWidth = `${parsedWidth - paddingSize}px`
}

return {
...this.context,
containerWidth: currentContainerWidth,
containerWidth: `${parseFloat(containerWidth) - paddingSize}px`,
}
}

Expand Down
37 changes: 37 additions & 0 deletions packages/mjml/test/hero-padding-inner-width.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const chai = require('chai')
const mjml = require('../lib')

describe('mj-hero inner container width', function () {
const cases = [
{ padding: '0', expected: 600 },
{ padding: '0 20px', expected: 560 },
{ padding: '0 10px 0 30px', expected: 560 },
{ padding: '20px', expected: 560 },
{ padding: '0 50px 0 50px', expected: 500 },
]

cases.forEach(function ({ padding, expected }) {
it(`reduces the inner outlook table width by horizontal padding (padding="${padding}")`, async function () {
const input = `
<mjml>
<mj-body>
<mj-hero
mode="fixed-height"
height="200px"
background-width="600px"
background-height="200px"
padding="${padding}">
<mj-text>hero content</mj-text>
</mj-hero>
</mj-body>
</mjml>
`

const { html } = await mjml(input)

chai
.expect(html)
.to.include(`style="width:${expected}px;" width="${expected}"`)
})
})
})