1.0.7 • Published 2 years ago

mui-image v1.0.7

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

If you're already using Material-UI v5, why not display your images according to the Material guidelines too?

Illustrations and photographs may load and transition in three phases at staggered durations, rather than relying on opacity changes alone.

Visualize the image fading in, like a print during the photo development process.

- Material guidelines

1. Install

npm i mui-image

or

yarn add mui-image

Using TypeScript? Also add @types/mui-image 🥳

2. Use

import Image from 'mui-image'

// or

import { Image } from 'mui-image'

// then

<Image src="my-image.png" />

3. Profit 💰

Note: Profits not guaranteed and MUI v5 is a peer dependency. If you need to support legacy versions of Material-UI, use material-ui-image instead. See the comparison chart below for more.

Usage Examples

You can use mui-image like a regular image.

<Image src="my-image.png" />

Except... it will fade and animate in as the Material guidelines recommend. 🤯

Add a height and/or width to reserve space on the page for the image and avoid uncomforable content shifts as your picture loads. They both default to 100% of the parent you place them in and accept any valid CSS property. Numbers are converted to pixels.

<Image src="my-image.png" width={500} />
<Image src="my-image.png" height="90vh" />

Apply the showLoading prop to add a progress indicator to let your fans know something amazing is coming. You can use the default MUI indicator or bring your own. 😎

<Image src="my-image.png" showLoading />
<Image src="my-image.bmp" showLoading={<MyCustomSpinner />} />

If you want the image to fail silently you can disable the errorIcon, or you can add your own to suit your brand.

<Image src="my-cats.png" errorIcon={null} />
<Image src="my-dogs.png" errorIcon={<MyErrorIcon />} />

If you want to disobey Google 😵 then you can customise the animation and speed via the duration and easing props to any valid CSS property. Duration is always milliseconds.

<Image src="my-fish.png" duration={325} />
<Image src="my-bird.jpg" easing="ease-in-out" />

To add that extra bit of spice 🌶 you can do exactly what Google suggests and apply a small position shift to images as they appear. The direction, distance, and duration (in milliseconds) are up to you.

<Image src="my-lawd.png" shift="left" />
<Image src="my-gawd.jpg" shift="bottom" distance={300} />
<Image src="my-gosh.gif" shift="top" distance="2rem" shiftDuration={320} />

And of course, you can style mui-image like you would a regular image... but with the addition of the MUI v5 sx prop and all the benefits it brings. 😏

<Image src="my-self.jpeg" style={{ borderRadius: 16 }} />
<Image src="my-wife.webp" className="custom-class" />
<Image src="my-exgf.tiff" sx={{ display: { sm: 'none', lg: 'inline' }}} />

If you want to get fancy 💃 you can also add inline styles and additional className's to the root wrapper div and loading/error icon wrapper div, or just target their default className's. This allows for complete customisation of every aspect of the component.

Like and subscribe below for more. ⏬

Props

NameTypeDefaultDescription
altstring""image alt tag value
bgColorstring"inherit"the color the image transitions in from
classNamestring"mui-image-img"CSS class for the image
distancestring / number100any valid CSS length value (for the shift)
durationnumber3000sets the CSS transition-duration in milliseconds
easingstringcubic-bezier(0.7, 0, 0.6, 1)sets the CSS transition-timing-function
errorIconboolean / nodetruedisplay default error icon, or your own
fitstring"contain"any valid CSS object-fit value
heightnumber / string"100%"any valid CSS height value
iconWrapperClassNamestring"mui-image-iconWrapper"CSS class for the icon wrapper div
iconWrapperStyleobjectinline styles for the icon wrapper div
positionstring"relative"any valid CSS position value
shiftboolean / stringfalseeither "left", "right", "top", "bottom", null, or false
shiftDurationnumberduration * 0.3duration of shift in milliseconds
showLoadingboolean / nodefalsedisplay default loading spinner, or your own
src *stringimage src tag... required
styleobjectinline styles for the image
widthnumber / string"100%"any valid CSS width value
wrapperClassNamestring"mui-image-wrapper"CSS class for the root wrapper div
wrapperStyleobjectinline styles for the root wrapper div

* required prop

Any other props (eg. sx, onLoad) are passed directly to the native img element.

Material guidelines for loading images

✅ Fade-in

Visualize the image fading in, like a print during the photo development process.

✅ Opacity, exposure, and saturation recommendations

Images should begin loading with low contrast levels and desaturated color. Once image opacity reaches 100%, display the image with full-color saturation.

✅ Duration

A longer duration is recommended for loading images, and a shorter duration is recommended for transitions.

✅ Animation

Add a small position shift to loading images.

(Source)

Comparison with similar components

Featuremui-imagematerial-ui-image
Size (minzipped)npm bundle sizenpm bundle size
Supports MUI v5
Fade-in
Progressive level adjustments
Suggested duration
Optional shift animation
Supports legacy MUI versions

License

© benmneb

ISC License

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.