Tips for updating border color when focused with styled-components

How can I change the border color of an input on focus using styled-components and React? Here is the code snippet I am currently using:

import React from "react";
import PropTypes from "prop-types";
import styled from "styled-components";

const StringInput = styled.input `
  border: 1px solid black;
  border-radius: 4px;
  padding: 6px 6px;
  box-sizing: border-box;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  &:focus {
    border-color: red;
    transition: border-color 0.3s ease-in-out;
  }
`;

class Tst extends React.Component {
  render = () => {

    return ( <
      StringInput / >

    );
  };
}

export default Tst;

I'm having trouble getting the border color to change when clicking or focusing on the input. Any help would be greatly appreciated!

Answer №2

An issue has been identified with the outline property.

import React from "react";
import styled from "styled-components";

const TextInput = styled.input`
  border: 1px solid black;
  border-radius: 4px;
  padding: 6px 6px;
  box-sizing: border-box;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  &:focus {
    border-color: red;
    transition: border-color 0.3s ease-in-out;
    outline: 0;
  }
`;

class Example extends React.Component {
  render = () => {
    return <TextInput />;
  };
}

export default Example;

You can view the code on codesandbox at this link

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

unable to respond when clicking an angularjs link

I'm facing an issue where I can't get a link to respond to click events in AngularJS. When I click on the anchor link, nothing happens. Here is a snippet of the AngularJS script: <script data-require="<a href="/cdn-cgi/l/email-protection" ...

RxJS: Transforming an Observable array prior to subscribing

I am retrieving data (students through getStudents()) from an API that returns an Observable. Within this result, I need to obtain data from two different tables and merge the information. Below are my simplified interfaces: export interface student Stude ...

Issue with Next.js useRouter() returning undefined value in useEffect()

I am facing an issue where I am trying to access the useRouter() variable inside useEffect() but keep getting an undefined error in the console. My goal is to fetch data from an API based on the slug. import { useRouter } from 'next/router'; ...

Aligning multiple items to the right using Flexbox

While it's common knowledge how to align one item to the right in flexbox, what about aligning multiple items, like the last two elements, to the right and the rest to the left? Take a look at this example where I want elements with the class .r to be ...

Is it mandatory for the npm install command to only be compatible with Node.js for the script

I've encountered an API that provides a JS SDK, and the instructions on its GitHub page suggest using npm install. My question is whether I need to have Node.js in order to use this SDK since it seems to be designed for it, or if I can simply work wit ...

How can a regular number be used to create an instance of BigInteger in jsbn.js?

I attempted both methods: const num1 = new BigNumber(5); And const num2 = new BigNumber(7, 10); However, I encountered the following error: Error: 'undefined' is not an object (evaluating 'num2.nextBytes') bnpFromNumberjsbn2.js:126 ...

Troubleshooting PHP and jQuery AJAX: Why is $_POST always empty?

I'm struggling to display the $_POST value in an alert box using jQuery AJAX function. However, the $_POST value from my PHP controller always turns out to be empty, resulting in an empty array in the response. My setup includes PHP version 5.5 and j ...

Can someone guide me on the proper implementation of the 'useEffect' hook in React version 18?

I'm currently working on a project following a YouTube tutorial that uses React 17, while I'm using React 18. Everything has been going smoothly so far, but I've hit a roadblock with formatting animated text. The specific task I'm stuck ...

What is the best way to customize the icon-bar in react-bootstrap's Navbar?

I am trying to customize the icon-bar in a react-bootstrap's Navbar component by setting its background color to white. However, my webpack scss loader is preventing me from styling CSS classes with dashes, as it only allows camel case. This restricti ...

Managing the state of a flag: communicating between Angular2 header and sidebar components

In my Angular2 project, I was working on a layout folder where I divided common layouts of certain pages into three components: header, sidebar, and footer. There was an anchor tag in the header that, when clicked, needed to extend the header to the left e ...

Create a customized menu with jQuery that disappears when hovered over

Check out this menu I've created: http://jsbin.com/useqa4/3 The hover effect seems to be working fine, but I'm looking to hide the div #submenuSolutions when the user's cursor is not on the "Solution" item or the submenu. Is there a way to ...

Tips for getting flex-end to function properly in IE11

My attempt to implement justify-content: flex-end; for a DIV with hidden overflow content in IE11 was unsuccessful. Despite trying various approaches, I managed to create a basic code snippet that functions properly in Chrome but fails in IE11: .token- ...

PHP variable not receiving Ajax variable

As a beginner in Ajax and jQuery, I am learning through Stack Overflow and online tutorials. Please be considerate of my novice level as you read and potentially offer advice on my post. I have successfully created a form that displays a message upon subm ...

Two interactive dropdown menus featuring custom data attributes, each influencing the options available in the other

Looking to create interactive select boxes based on the data attribute values? This is the code I currently have: HTML <select id="hours" onchange="giveSelection()"> <option value="somethingA" data-option="1">optionA</option> <o ...

The geocoder-autocomplete feature in ReactJS is now experiencing a sudden Network Error in Firefox

I followed the tutorial provided by HERE and successfully implemented the solution for street address validation using ReactJS and HERE geocoder autocomplete. Everything was working fine on Firefox and Chrome until a few weeks ago when it suddenly stopped ...

Exploring the Implementation of Multiple Form Validations in SharePoint using PreSaveAction

My knowledge of Javascript is limited to what I can gather from online resources. Currently, I'm facing a challenge with a SharePoint form where I need to set up specific validations that trigger when a user hits the "Save" button. The validations I& ...

Extracting Data from JSON Structures

I am struggling with extracting data from a JSON string that looks like this: var txt= '{“group”: [ {“family1”: { “firstname”:”child1”, “secondname”:”chlid2” }}, {“family2”: { ...

What is the best way to center CSS slider switches without causing any damage to them?

Does anyone have ideas on how to align CSS Slider Switches? I tried using position: absolute; margin-left: 15em, but the sliders need to remain non-absolute for visual effects. A flex grid was considered, but I don't want the switches to wrap. Usin ...

When debugging in ASP MVC4, JavaScript will only evaluate HiddenFor once you've paused to inspect it

Struggling with evaluating a hidden field in JavaScript (ASP MVC4). I've set up a model in my View with a hidden input for one of the properties. @Html.HiddenFor(mdl => mdl.FilterByUser, new { @id = "filterByUserId" }) Within my Helper, I have a ...

I'm facing a CORS dilemma and I'm seeking assistance to resolve it

I've been struggling with CORS issues and have tried every method possible to resolve it, but without success. Here is the screenshot of my code: https://i.stack.imgur.com/2gTF4.png Below is the request from my React app: https://i.stack.imgur.com/W ...