How to place the loading component in the centre of the image using React?

Check out the code and demo below:

https://codesandbox.io/s/sparkling-silence-7cv3l

I recently implemented the react-image-enlarger component to enable zoom functionality for images, similar to Medium. This component offers an API called renderLoading which allows you to specify a loading component to display while the enlarged image is being downloaded. To achieve this, I included the react-loading library.

The snippet of relevant code looks like:

<div style={{ margin: "0.25rem" }}>
      <Image
        style={{ width: "200px", height: "auto" }}
        zoomed={zoomed}
        src={src}
        enlargedSrc={{}}
        onClick={() => setZoomed(true)}
        onRequestClose={() => setZoomed(false)}
        renderLoading={<ReactLoading type={"cylon"} color="black" />}
      />
    </div>

Currently, the loading animation appears under the image.

https://i.sstatic.net/jdhkL.png

However, my goal is to have the loading animation centered on the image with a darkening filter applied (possibly using brightness(0.7);)

I understand that achieving this may not be straightforward due to varying image sizes and my limited CSS skills. Any assistance in accomplishing this would be greatly appreciated.

Answer №1

Here is a CSS code snippet that you can use to position the loader in the center of each image:

img.EnlargedImage__Image + div {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;
    background-color: rgba(255, 255, 255, 0.75);
    height: 100% !important;
    width: 100% !important;
}
img.EnlargedImage__Image + div svg {
    height: 64px;
    width: 64px;
    margin: auto;
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    right: 0;
}

Answer №2

Make sure to include the classname="loader" prop in your loader component as shown below. Also, add this CSS code snippet to your file:

Edit: updated with transition: all 0.5s; for .loader

renderLoading={<ReactLoading type={"cylon"} color="black" className="loader"/>

.EnlargedImage__container .loader {
    position: absolute;
    top: 0;
    background-color: rgba(0, 0, 0, 0.1);
    height: 100% !important;
    width: 100% !important;
    transition: all 0.5s;

}

.EnlargedImage__container .loader svg {
    height: 64px;
    width: 64px;
    margin: auto;
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    right: 0;
    }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Essential Guide to Binding Events with JQuery

Why is the handler for an event on an element triggering the wrong response? I was expecting the click event of Div1 to display a dialog stating 'div1', but it's showing 'div2' instead. I am new to this and trying to figure out wh ...

Value of type 'string' cannot be assigned to type '{ model: { nodes: []; links: []; }; }'

I am a beginner in TypeScript and I have added types to my project. However, I am encountering an error with one of the types related to the graph: Type 'string' is not assignable to type '{ model: { nodes: []; links: []; }; }'.ts(2322) ...

Encountering a timeout error while using selenium-webdriver and phantomJS

Currently, I am developing my automated test scripts using a combination of selenium-webdriver, phantomJS, and mocha. The script I am working on is written in JavaScript. One requirement is to wait until a specific element becomes completely visible befo ...

Iterate through a JavaScript object while preserving the key along with its corresponding value

var info = { 2016-09-24: { 0: {amount: 200, id: 2}, 1: {...} }, 2016-09-25: { 0: {amount: 500, id: 8}, 1: {...} } } In order to display the data stored in the object above, I want to create a view that looks like this: "**" will r ...

The style in App.js was rejected and the script was not executed as intended

element, I have encountered a common issue that many others seem to face. The typical solution involves correcting a line of code like this one: app.use(express.static(__dirname + '/public')); Unfortunately, despite my efforts to follow the ad ...

JQuery implementation of Axis webservice call

Trying to invoke my Axis webservice with the code below var wsUrl = "http://localhost:8080/TestServ/services/TestCls?wsdl"; var soapreq = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:cod=\"http:// ...

"Resolving an inconsistency problem with the Vary header in Htaccess

Could someone help me identify the issues in my htaccess code? I'm encountering errors: This response is negotiated, but doesn't have an appropriate Vary header. The resource doesn't send Vary consistently. ## add vary header <IfModule ...

Eliminating date and time formatting in HTML emails on iPhones

I've designed an HTML email that is responsive and displays well on various platforms, but I'm encountering an issue specifically when viewing it on an iPhone. It appears that iOS is identifying a date and time in the content and applying a blue ...

Getting the Request Body Content in Express Middleware

Currently, I am in the process of developing a small API logger to use as an Express middleware. This logger is designed to gather data from both the request and response objects, then store this information in a JSON file on disk for later reference. Her ...

Connecting Node.js Post script to an HTML form: A step-by-step guide

After successfully creating a RESTful API, I tested it using Postman and everything worked as expected. The test is executed from an index.js file where the routes are saved as shown below. const config = require('config'); const mongoose = requi ...

Utilizing Angular JS to Manage Controller Events

I am currently working on an application that requires saving a large amount of data in cascade, similar to a typical master-detail view. Within this view, there is a "Save All" Button which saves each row in an iteration. This process triggers jQuery cus ...

Is it possible to define a data type from an external package using TypeScript and Node.js?

I'm currently in the process of reorganizing some code to utilize a list of signals and connect `.once` handlers to each one individually. const terminationSignals = ["SIGINT", "SIGUSR2", "SIGTERM"]; terminationSignals.f ...

Learn how to bring in images using props within React and incorporate the 'import' pathway

These are the props I am passing: const people=['Eliana','Stefania','Ahmed'] { people.map(function(name, index){ return <Person item={index} name={name}/>; }) } import Eliana from '../assets/imgs/people ...

Developing a universally accessible variable for utilization in various functions

I'm having trouble understanding why 'currentPos.LatLng' is undefined when trying to access it outside the function even though it's part of an object. I want to be able to retrieve the current position values to use them in another fun ...

AngularJS is throwing a $injector:modulerr error and I've exhausted all possible solutions

I recently started learning Angular and I've been following the tutorial on the official AngularJS website. Here's what I've tried so far: Installed angular-route and included the script below angular.min.js Utilized ngRoute in my mod ...

When pressed, the button changes color to a vibrant shade of blue, and not just a simple outline

I'm experiencing an issue with a button that turns blue when the user holds onto it for a while on mobile. You can see an example in the image below: https://i.sstatic.net/VmnZ8.png Adding ::selected or outline did not resolve the problem as the ent ...

Utilizing JFlex for efficient brace matching

Currently, I am in the process of developing an IntelliJ plugin. One of the key features that I am working on is a brace matcher. After completing the JetBrains plugin tutorial, I was able to get the brace matcher functioning using this regular expression ...

html2canvas bottom margin

I coded this in react function download(element) { html2canvas(element).then((canvas) => { window.open(canvas.toDataURL('image/png')); }); } return ( <div className='box' onClick={( ...

Unveiling Hidden Passwords with Selenium and Python: A Guide to Sending Keys to Password Fields with Various IDs

Currently, I am utilizing selenium with python 2.7 and facing an issue with inserting a password into the form below: <tr id="mockpass"> <td> <input type="text" value="something1" onfocus="document.getElementById('mockpass& ...

How come TypeScript remains silent when it comes to interface violations caused by Object.create?

type Foo = { x: number; }; function g(): Foo { return {}; // Fails type-check // Property 'x' is missing in type '{}' but required in type 'Foo'. } function f(): Foo { return Object.create({}); // Passes! } functio ...