Create a moving background gradient with styled-components

Currently, I am working on setting the background of the <Paper /> component using Material-UI version 1.0.0-beta.25 to display a gradient of colors. The colors are dynamically added by clicking the Add button and selecting one from the color picker. Here is my current code:

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

I aim to animate the background gradient of the component to achieve an effect similar to this example. By modifying the pen's code, removing unnecessary sections, and incorporating the use of styled-components, I came up with the following implementation:

import styled, { keyframes } from 'styled-components';

{/* rest of the code */}

render() {

  const gradient = keyframes`
    0% {
      background-position: 0% 50%
    }
    50% {
      background-position: 100% 50%
    }
    100% {
      background-position: 0% 50%
    }
  `;

  const MyPaper = styled(Paper)`
    && {
      background: ${ colors.length == 0 ? `linear-gradient(to right, ${ "#FFFFFF" }, ${ "#FFFFFF" })` 
        : `linear-gradient(to right, ${ colors.map((color, key) => color.name ) })` };

      animation: ${gradient} 15s ease infinite;
    }
  `;

  return (
    <div>
      <div className = { classes.root }>

        {/* Selected Color */}
        <div>
          <MyPaper className = { classes.paperStyle } elevation = { 4 }></MyPaper>
        </div>
        <br /><br /><br />

        {/* rest of the code */}

Unfortunately, my background gradient animation is not functioning as intended.

If there are any errors in my approach or alternative methods to achieve this animation effect, I would greatly appreciate any guidance. Thank you in advance for your assistance!

Answer №1

It's important to note the array structure of your colors may need adjustment for proper functionality. Additionally, ensure that the CSS for your MyPaper component includes a background-size value greater than 100% and 100% for the animation to function correctly.

@keyframes gradient {
    0% {
      background-position: 0% 50%;
    }
    50% {
      background-position: 100% 50%;
    }
    100% {
      background-position: 0% 50%;
    }
}

.gradient {
 height: 100px;
 width: 100px;
 background: linear-gradient(to right, red, yellow, blue);
 background-size: 110% 110%;
 animation: gradient 15s ease infinite;
}

.cool {
 background: linear-gradient(to right, green, red, black);
 background-size: 200% 200%;
}
<div class="gradient"></div>
<div class="gradient cool"></div>

Consider setting dimensions (height and width) for your Material Paper component as well to ensure proper functionality.

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

What is the process for changing proxy settings through the command line when using Create React App?

I recently created a React project using Create React App and set up the development server to proxy API requests through the proxy setting in my package.json: ... "proxy": "https://dev-backend.example.com" ... However, I am looking ...

CSS - text featuring a gentle shadow hue and a deeper foreground color

I'm encountering an aesthetic issue where my list of texts have random light/bright colors, making them hard to read on white backgrounds. I've tried setting the text color to shadow, but now I want the text itself to be a darker, more readable c ...

The Vue 3 page does not allow scrolling unless the page is refreshed

I am encountering an issue with my vue3 web app. Whenever I attempt to navigate to a page using <router-link to ="/Dashboard"/> Here is the code for Dashboard.vue: <template> <div class="enquiry"> <div class= ...

The ideal login page design

I apologize for my lack of knowledge in React, but I am quite new to it and have been struggling with this issue for days. I am having trouble creating a login page in ReactJS. Here is the code in my app.js: import React from 'react'; import {Ha ...

Ensuring maximum length validation on input

I am having an issue with the function "checkNumbers()". I am attempting to validate if my input does not exceed 10 numbers; while "isAllowedSymbol()" is functioning correctly, the other one is not. What could be causing this problem? function isAllowe ...

Displaying random divs and subsequently animating them downwards using JavaScript

I am in the process of creating a random appearing div that then falls down. Here is the code I have so far: $(document).ready(function(){ $('#test').animate({top: 80 + '%'},900); }); <div id="test" style="background:#98bf21;heigh ...

Modify a particular row in a table by referencing its distinct identifier

Currently, I have two components within my ReactJS application - the parent component is named Layout and the child component is called Report. In the Report component, there is a table that loops through and displays a list of car accidents. There is a ha ...

Can I fetch the visible rows (along with their order) from a React Material-table?

I've recently started working with React and I'm utilizing a special component known for its unique filtering functionality that I couldn't find elsewhere. This component typically shows 10 rows of data by default. Whenever I apply filters ...

Creating uniform height for Definition Terms (DT) and Definition Descriptions (

Looking to creatively style a description list <dl> in a way that showcases two columns, even when there are multiple <dd>s following the <dt>. The challenge lies in wanting this design to be dynamic without resorting to floating the < ...

What is the best way to increase the font size without resizing the parent input element?

Is there a way to increase the font size in this text box without enlarging the box itself? Any suggestions or advice would be appreciated! https://i.sstatic.net/91pon.jpg. Below is my CSS code for reference (the class of the form is 'box'). .bo ...

Make sure the parent component is able to access the final state of the child component through a

React optimizes the useState behind the scenes using various techniques such as batching, scheduling, and discarding some useState calls if the value remains unchanged. I encountered a situation where I am unsure if the parent component will eventually re ...

an all-encompassing loading animation designed specifically for updates to location.href

We have a situation where a customer's website is displaying extremely slowly within an iFrame, taking about 7 seconds to load. All we can do is provide the customer with a JavaScript file for inclusion on their site, as they are not willing to make a ...

Leveraging Bootstrap grid system within AngularJS elements

I am currently working on wrapping grid element divs into Angular components in order to streamline the input process and establish a standard: <bootstrap-row> <bootstrap-input-text col=6 ng-model="$ctrl.model" label="hey!"& ...

Choose the CSS class based on the content provided

Help needed with CSS issue. I want to target the <tr> element's class using Bootstrap classes like .success or .danger. In my Vue.js project, the status name in my object does not align with the .success or .danger classes, but I still need to ...

setting state with object argument does not succeed

Recently, I built my own web-app for creating and managing posts. However, I've encountered an issue while trying to add a new post and update the state using 'setState'. It seems like there's an error in my approach. Can someone provid ...

Acquire parameters for the specific category and page

Utilizing this code snippet on my blog homepage enables pagination functionality. The pages are structured like url/?page=2 etc. export default async function HomePage({ searchParams, }: { searchParams: {[key: string]: string | string[] | undefined ...

Chrome isn't displaying the text color of the link

I'm having an issue with a link that is styled like a button. It looks fine in Internet Explorer and Firefox, but not on Chrome. For example: The problem lies with the teal button in the center of the page that reads "follow issue." I intended for ...

Issues with the animation of letters bouncing in css3

I wanted to implement an animated effect in CSS, specifically the "jumping letters" effect. However, it seems that the current implementation is not working as intended. The entire word moves upward instead of each letter bouncing individually. Can you h ...

Updating contact list to a database table structure

Currently, my code displays the data in address books, but I would like it to be shown in a table instead. I attempted to use document.write to create the table, but I'm unsure how to populate the table with the data rather than the address book forma ...

Mysterious gap found between image and table

Hey there! I've been struggling with an unusual gap between a top image and a table on my website (www.tradecaptaincoaching.com). The table was created using . Despite spending hours on it, I can't seem to get rid of the huge 200 pixel gap that& ...