Skip to content

Commit

Permalink
Adds Image and ResponsiveImage className prop (#82)
Browse files Browse the repository at this point in the history
* add className prop to images to override default styling

* image docs elaboration

* docs tweaks, fix indentation

* amp-img doesn't get className; only concat classNames if prop is defined
  • Loading branch information
James Shakespeare authored Dec 4, 2020
1 parent f599c34 commit 27cc9c8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/components/Image/Image.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ ImageAmp.propTypes = {
function Image ( {
alt,
amp,
className,
fallbackHeight: height,
fallbackWidth: width,
loading,
Expand All @@ -63,7 +64,7 @@ function Image ( {
return (
<img
alt={alt}
className={styles.image}
className={className ? `${styles.image} ${className}` : styles.image}
decoding="async"
height={height}
loading={loading}
Expand All @@ -90,6 +91,11 @@ Image.propTypes = {
*/
amp: PropTypes.bool.isRequired,

/**
* Passed verbatim to element as class attribute.
*/
className: PropTypes.string,

/**
* The rendered height of the image when CSS cannot be loaded or in very old
* browsers. With `fallbackWidth`, it sets the aspect ratio for the image.
Expand Down
4 changes: 3 additions & 1 deletion src/components/Image/Image.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import Image from './Image';

Displays an image. The `Image` component requires `fallbackHeight` and `fallbackWidth` to set aspect ratio and `sizes` and `srcSet` to provide [responsive rendering](https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images). They will not be calculated for you and there are no default values. Future components may provide a simpler implementation for specific use cases.

The image element (`img` or `amp-img`) is rendered with some lightly opinionated but commonly needed styles: `display: block` and `width: 100%`. These styles may be overridden using the `className` prop, but whenever possible you should use a parent element to constrain the dimensions of the image.

This component does not provide [art direction](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/picture), which is also a task for a future component.

## Usage guidelines

- **Do** use a container to constrain the size or shape of the image. An `Image` will always expand or contract to fit the width of its container. The `Image` component purposefully does not accept a `className` prop.
- **Do** use a container to constrain the size or shape of the image whenever possible. By default, an `Image` will expand or contract to fit the width of its container. This can be overridden using styles applied via the `className` prop. You may (rarely) want to do this in order to have an image fill its parent element by height rather than width, or in order to use the `object-fit` property.
- **Do** understand that the `fallbackHeight` and `fallbackWidth` props are [used to set aspect ratio and help the browser reserve space](https://blog.logrocket.com/jank-free-page-loading-with-media-aspect-ratios/) for the image during initial layout.
- **Do** [provide useful alt text](https://www.w3.org/WAI/tutorials/images/decision-tree/) for your images. Sometimes, an empty string is the most useful!
- **Do** learn why and how to use [responsive rendering](https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images). It’s important to understand how `sizes` and `srcSet` work.
Expand Down
7 changes: 7 additions & 0 deletions src/components/ResponsiveImage/ResponsiveImage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { arrayFromRange, resizeWPImage } from '@quartz/js-utils';
function ResponsiveImage( {
alt,
amp,
className,
fallbackHeight,
fallbackWidth,
sizes,
Expand Down Expand Up @@ -33,6 +34,7 @@ function ResponsiveImage( {
<Image
alt={alt}
amp={amp}
className={className}
sizes={sizes || sizesDefault}
src={resizeWPImage( src, fallbackWidth, fallbackHeight )}
srcSet={srcSet}
Expand All @@ -56,6 +58,11 @@ ResponsiveImage.propTypes = {
*/
amp: PropTypes.bool.isRequired,

/**
* Passed verbatim to img or amp-img element as class attribute.
*/
className: PropTypes.string,

/**
* The rendered height of the image when CSS cannot be loaded or in very old
* browsers. With `fallbackWidth`, it sets the aspect ratio for the image.
Expand Down

0 comments on commit 27cc9c8

Please sign in to comment.