1.0.6 • Published 11 months ago

react-blur-img v1.0.6

Weekly downloads
-
License
ISC
Repository
github
Last release
11 months ago

BlurImg Component

The BlurImg component is a React component that provides a blurred placeholder image until the main image finishes loading. It also supports an optional loading spinner for a better user experience.

Try the Demo

Demo

Features

  • Lazy-loads images with a blurred placeholder.
  • Displays a loading spinner while the image loads.
  • Fully customizable via props for aspectRatio, objectFit, and objectPosition.
  • Easy to use and integrate into any React project.

Installation

  1. Download the package via NPM.
npm i react-blur-img
  1. Import the component in your project
import { BlurImg } from "react-blur-img";
  1. Usage
import { BlurImg } from "react-blur-img";
import { img } from "https://example.com/real-image.jpg";
import { placeHolder } from "https://example.com/placeholder.jpg";

function App() {
  return (
    <div>
      <BlurImg img={img} placeHolder={placeHolder} alt="Descriptive Alt Text" />
    </div>
  );
}

Props

Required props

PropTypeDescription
imgStringThe image to render.
placeHolderStringPlaceHolder to render while image is loading.
altStringThe alt text

Optional props

PropTypeDefaultDescription
aspectRatioReact.CSSProperties["aspectRatio"]1/1Controls the aspect ratio of the image (e.g., "16/9")
objectFitReact.CSSProperties["objectFit"]containSpecifies how the image should fit within its container (e.g., "cover", "contain").
objectPositionReact.CSSProperties["objectPosition"]centerDetermines the position of the image within its container (e.g., "center", "top").
SpinnerSpinnerPropsCan change the default Spinner.

Spinner Props structure

PropTypeDefaultDescription
widthReact.CSSProperties["width"]40pxControls the width for the spinner.
heightReact.CSSProperties["height"]40pxControls the height for the spinner.
borderWidthReact.CSSProperties["borderWidth"]4pxControls the thicknes for the spinner.
colorReact.CSSProperties["borderColor"]blackControls the color for the spinner.
styleReact.CSSProperties["borderStyle"]dottedControls the style for the spinner.
borderTopReact.CSSProperties["borderTop"]noneControls Top border to create another rotating effect.

CSS classes

NameDescription
blur-load-containerThe main wrapper for the image and the loading placeholder.
blur-load-imgHandle img style
loadedAdded to the container once the image has fully loaded, removing the placeholder and making the image visible.
blur-load-spinnerDefines the spinner's appearance and animation.

Custom Spinner usage

import { BlurImg, SpinnerProps } from "react-blur-img";
import { img } from "https://example.com/real-image.jpg";
import { placeHolder } from "https://example.com/placeholder.jpg";

const spinner: SpinnerProps = {
  width: 40,
  height: 40,
  borderTop: "4px dashed red",
  color: "white",
  style: "dashed",
};

function App() {
  return (
    <div>
      <BlurImg
        img={img}
        placeHolder={placeHolder}
        alt="Descriptive Alt Text"
        spinner={spinner}
      />
    </div>
  );
}

Demo

Tip for Improving Placeholder Loading

To create a placeholder background similar to the original image, you can simply take the original image and resize it to a very small dimension, such as 20x20px or 30x30px. This way, the placeholder will visually resemble the final version, and its loading will be almost instantaneous due to the reduced size, providing a smoother user experience.

1.0.6

11 months ago

1.0.5

11 months ago

1.0.4

11 months ago

1.0.3

11 months ago

1.0.2

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago

0.0.7

11 months ago

0.0.6

11 months ago

0.0.5

11 months ago

0.0.4

11 months ago

0.0.3

12 months ago

0.0.2

12 months ago

0.0.1

12 months ago