The styled-components in CSS are causing some issues with the color themes

Link to Image Showing the Issue. I have implemented themes and colors in my React application successfully, but I am encountering a peculiar problem with the labels. Whenever I switch the theme from green to blue and then back to green, focusing on the input causes the label color to revert to the opposite theme. This behavior is confusing me as initially, when I focus on the input, the label remains in blue color. https://i.sstatic.net/wy0ur.png

Below is the code snippet related to the theme implementation:

    {
        type: "light",
        primaryColor1: "#33b2ff", //lightest
        primaryColor2: themeColor === "blue" ? "#009fff" : "#00b05d", //ChangeColor()
        primaryColor3: "#007fcc", //darkest
        textColor1: "#111", //darkest
        textColor2: "#555",
        textColor3: "#888", //lightest
        backgroundColor1: "#fcfcfc", //lightest
        backgroundColor2: "#e3e3e3",
        backgroundColor3: "#cacaca", //darkest
        borderColor: "009fff",
        white: "#fff",
        black: "000",
    }

The above object defines all the colors for theming in the application, with the primary color dynamically changing based on the state of 'themeColor'. The entire application's theme is provided using useContext's Provider and passed via styled-components' ThemeProvider like so:

import { ThemeProvider } from "styled-components";

<ThemeContext.Provider value={{theme,themeToggler,themeColor,setThemeColor}}>
            <ThemeProvider theme={themeStyle}>
                {children}
            </ThemeProvider>
        </ThemeContext.Provider>

The CSS styling for Input and Label elements using styled-components is shown below:

export const Input = styled.input( ({theme}) => `
    border-radius: 10px;
    width: 100%;
    font-size: 1rem;
    background: ${theme.backgroundColor3};
    border: 0;
    color: ${theme.textColor1};
    line-height: 50px;
    padding: 5px 20px;
    box-sizing: border-box;
    font-family: Montserrat;

    &:focus
    {
        outline: none;
    }

    &[type=password]
    {
        font-size: 1.75rem;
    }
`);

export const Label = styled.label( ({theme}) => `
    font-size: 1rem;
    font-family: Montserrat;
    color: ${theme.textColor3};
    cursor: text;
    position: absolute;
    top: 30%;
    left: 5.5%;
    border: 1px solid ${theme.borderColor};
    transition: 0.2s ease all;
    background: none;

    ${Input}:focus + &,
    ${Input}:not(:placeholder-shown) + &
    {
        background: ${theme.primaryColor2};
        padding: 0.2rem 0.5rem 0.2rem;
        border-radius: 0.5rem;
        top: -15%;
        left: 3%;
        font-size: 0.9rem;
        color: #fff;
    }

`);

Answer №1

Perhaps using the !important rule could help address your issue. Consider adding !important after your CSS properties. For instance:

color: ${theme.secondaryColor} !important;

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

Tips for eliminating duplicate data entries within a row while building a table using ReactJS

Apologies for the not-so-great title, but I'm having trouble figuring out how to tackle this specific issue in reactjs. I'm struggling with creating a table where one column remains empty while the other continues to populate with data. This is ...

What is the best way to insert a <dv> element following a <Typography> component in the provided code?

After learning React, I have attempted in various ways to get this code snippet to work. I referred to a question on Stackoverflow as well as the HTML div documentation The goal is to have the text and link displayed in a horizontal line, not vertically s ...

Implementing a Class on Page Refresh in Next JS

I am fairly new to using react and nextjs. How can I insert a script in a component to add a class when the page reloads? The code below doesn't seem to work because the page is not yet rendered when I try to add the class for the body tag. const Mode ...

How can you add draggable functionality to a Bootstrap dropdown menu?

My custom bootstrap dropdown design <div class="dropdown"> <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"> Dropdown <span cla ...

Create a search preview in JavaScript that emphasizes key information and provides a condensed overview

I am currently working on incorporating a feature that generates a highlighted and condensed preview of a provided text. Below is the code snippet I have so far: <div> Search: <input value='vestibulum' disabled/> </div> < ...

The embedded CartoDB image header is set to have a style of "display:none"

I'm currently working on a CartoDB map showcasing my readers' summer fishing photos. Each row in the table has an image url, which I've placed at the top of the infowindow display list. I've enabled it and set the image window type to ...

Is it possible to pass data retrieved in getStaticProps to a component?

After setting up my getstaticProps: export async function getStaticProps({ params }) { const headerData = await fetch( `https://example.com/get/header-data` ).then((res) => res.json()); return { props: { headerData, }, reva ...

What is the best way to submit any form efficiently using just one jQuery/Ajax function? I'm uncertain about the

Here are a couple of things I need assistance with: 1) I'm looking to click on a link and have it load in a specified div on the page. Each link has a data attribute that indicates which div it should load into. This is how I achieve it: <div id= ...

Arrange items in a single parent Div in flex-column format, aligning them to the left and

In my Angular application, I am experimenting with stacking Bootstrap toast elements in a customized vertical layout. Specifically, I want certain toast elements to be positioned on the left side of the page (using the align-items-start style) and others o ...

What is the process for dynamically populating a select dropdown based on the selection made in another select dropdown?

I need to dynamically populate the second select box based on the option selected in the first select box. Here's what I have tried so far, but it doesn't seem to be working as expected. HTML: <form id="step1"> <p> Creat ...

The functionality of the Bootstrap carousel is not functioning properly in the Firefox browser

I attempted to customize the bootstrap carousel in order to create a simple content slider. After reviewing two events in the bootstrap documentation and utilizing animate.css for animation, I found that Chrome and Internet Explorer displayed the animation ...

Ways to execute multiple functions in synchronization within react js

I have a method called componentDidUpdate() in my code, where I have two functions implemented: componentDidUpdate(prevProps){ if (prevProps !== this.props){ this.getListHistory() this.forceUpdate() } } The this.getListH ...

Challenges encountered while attempting to animate the CSS clip feature in WebKit

I've been searching everywhere for a solution to my issue, but none of them seem to work. As someone new to CSS3 Animations, I appreciate your patience. I created a simple animation using CSS3 that functions perfectly in IE and Firefox, however, it ...

Creating a feature to display an error message when duplicate values are detected with Autocomplete in MUI React

I'm currently facing a challenge with managing and notifying the user about setting a duplicate value. By default, if a user tries to input a value that already exists in the list, the "tag" won't be added. While this functionality works for me, ...

Submitting POST data from a form to the current page

I am facing a rather complex issue. Currently, on a page, I have a form that collects information from my visitors. With the help of a script, I can grab this information and add it to a specific link. Initially, I used redirect.php to attach the data to ...

React Redux -> What causes the TypeError: Cannot read property 'item' of undefined?

Recently, I've been working on incorporating objects into my Redux store but it seems like something is off. Initially, I define my objects in the following manner: const products = [ { name: 'Blue Dress', price: 540, ...

What is the best way to define my background image using markup language?

I'm facing a challenge with some code that I didn't write which includes a background-image. While I want to maintain the identical appearance, I prefer to set the image in my markup rather than CSS. However, I'm unsure of how to do this. T ...

Overflow issues with flexbox design on mobile devices

Hello, I'm currently working on creating a sliding panel of images. While I have successfully implemented it, I am encountering some browser compatibility issues. The sliding panel functions perfectly on Chrome desktop, however, when viewed on mobil ...

Tips for creating a responsive div that contains both an image and description, including automatically resizing the image to fit the

#content { background-color: #ACAE4C; float: right; display: inline-block; padding: 0; } <div id="content"> <div class="pic"></div> <div class="desc"></div> </div> I need assistan ...

Issue encountered in React: Unable to access object value within ComponentDidUpdate method

I'm struggling to retrieve the value from an object key. componentDidUpdate(prevProps) { if (prevProps !== this.props) { console.log("component did update in top menu", this.props.topmenudata[0]) this.setState({ ...