Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional image / height props and documentation note #9

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,21 @@ Use to format links. Requires an `href` prop. Always sets `target="_blank"` and

### `<Image>`

An image, without any pesky borders, outlines, or underlines by default. Requires a `src` prop, and `width` and `height` to be set. You can override the default styles (such as adding a border) using the `style` prop.
An image, without any pesky borders, outlines, or underlines by default. Requires a `src` prop. Optional `width` and `height` props are available. You can override the default styles (such as adding a border) using the `style` prop.

`height` and `width` props default to `1px`. If you require a response image, for example, having `300px` of height and `auto` width. We recommend adding the number of pixels directly as a prop and having `auto` in your `headCSS`.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: "response" -> "responsive"


```js
const css = `
img {
width: auto !important;
}
`;

<Email headCSS={css}>
<Image src="myimg.png" height={300} />
</Email>
```

## Head CSS and Media Queries
You can pass a string prop `headCSS` to your `<Email>` component. You can see it in our [kitchenSink.js](https://github.com/chromakode/react-html-email/blob/master/examples/kitchenSink.js) example.
Expand Down
2 changes: 1 addition & 1 deletion examples/kitchenSink.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const email = (
<Box cellSpacing={20} width="100%" style={{ borderTop: '3px solid black' }}>
<Item>
<Span color="gray" lineHeight={20}>Generated by <A href="https://github.com/chromakode/react-html-email">react-html-email</A></Span>
<Image data-mc-bar="bar" data-mc-baz="baz" alt="react" src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/57/React.js_logo.svg/100px-React.js_logo.svg.png" width={100} height={100} />
<Image data-mc-bar="bar" data-mc-baz="baz" alt="react" src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/57/React.js_logo.svg/100px-React.js_logo.svg.png" height={100} />
</Item>
</Box>
</Item>
Expand Down
9 changes: 7 additions & 2 deletions src/components/Image.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ export default function Image(props) {
Image.propTypes = {
alt: PropTypes.string.isRequired,
src: PropTypes.string.isRequired,
width: PropTypes.number.isRequired,
height: PropTypes.number.isRequired,
width: PropTypes.number,
height: PropTypes.number,
style: EmailPropTypes.style,
}

Image.defaultProps = {
height: 1,
width: 1,
}
2 changes: 1 addition & 1 deletion test/renderEmail-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('renderEmail', () => {

it('produces expected output from a kitchen sink example', () => {
const actualOutput = renderEmail(kitchenSink)
const expectedOutput = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><title>Test Email</title><style type="text/css">@media only screen and (max-device-width: 480px) {\n font-size: 20px !important;\n}</style></head><body style="width:100%;margin:0;padding:0;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%;"><table width="100%" height="100%" cellpadding="0" cellspacing="0" align="left" valign="top"><tbody><tr><td align="center" valign="top"><table width="600" align="center" cellpadding="0" cellspacing="0" valign="top"><tbody><tr><td><span style="font-family:sans-serif;font-size:15px;line-height:15px;color:#000;">Hello, world!</span></td></tr><tr><td><table cellspacing="20" width="100%" style="border-top:3px solid black;" cellpadding="0" align="left" valign="top"><tbody><tr><td><span style="font-family:sans-serif;font-size:14px;line-height:20px;color:gray;">Generated by <a href="https://github.com/chromakode/react-html-email" target="_blank" style="text-decoration:underline;">react-html-email</a></span><img data-mc-bar="bar" data-mc-baz="baz" alt="react" src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/57/React.js_logo.svg/100px-React.js_logo.svg.png" width="100" height="100" style="display:block;outline:none;border:none;text-decoration:none;"/></td></tr></tbody></table></td></tr></tbody></table></td></tr></tbody></table></body></html>'
const expectedOutput = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><title>Test Email</title><style type="text/css">@media only screen and (max-device-width: 480px) {\n font-size: 20px !important;\n}</style></head><body style="width:100%;margin:0;padding:0;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%;"><table width="100%" height="100%" cellpadding="0" cellspacing="0" align="left" valign="top"><tbody><tr><td align="center" valign="top"><table width="600" align="center" cellpadding="0" cellspacing="0" valign="top"><tbody><tr><td><span style="font-family:sans-serif;font-size:15px;line-height:15px;color:#000;">Hello, world!</span></td></tr><tr><td><table cellspacing="20" width="100%" style="border-top:3px solid black;" cellpadding="0" align="left" valign="top"><tbody><tr><td><span style="font-family:sans-serif;font-size:14px;line-height:20px;color:gray;">Generated by <a href="https://github.com/chromakode/react-html-email" target="_blank" style="text-decoration:underline;">react-html-email</a></span><img data-mc-bar="bar" data-mc-baz="baz" alt="react" src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/57/React.js_logo.svg/100px-React.js_logo.svg.png" width="1" height="100" style="display:block;outline:none;border:none;text-decoration:none;"/></td></tr></tbody></table></td></tr></tbody></table></td></tr></tbody></table></body></html>'
expect(actualOutput).toBe(expectedOutput)
})

Expand Down