Is the focus styling malfunctioning within a React component?

Issue:

I'm facing a challenge with my custom search box in a React application. Despite my styling efforts, I can't seem to get the desired outline effect when someone types in the textbox.

<div className="search-box">
  <Row>
    <div className="search-box-icon">
      <i className="fas fa-search" />
    </div>
    <input
      className="search-input"
      placeholder="search in listbox"
    />
  </Row>
</div>

The CSS styles I've applied so far are as follows:

.search-box-icon {
  margin-left: 1%;
}

.search-input {
  border: none;
}

.search-input:focus {
  outline: none;
}

.search-box:focus {
  outline-color: #53c9fc;
  outline-width: 2px;
}

If anyone has a solution to help me achieve the desired outlined effect on the search-box div when typing in the textbox, I would greatly appreciate it. Thank you.

Answer â„–1

If you're looking to make non-interactive content focusable using CSS/HTML, it's best to avoid using the tabindex attribute, as advised by MDN:

Avoid using the tabindex attribute in conjunction with non-interactive content to make something intended to be interactive focusable by keyboard input. An example of this would be using a div element to describe a button, instead of the button element.

W3 also suggests semantically describing content using interactive elements like <a>, <button>, <details>, <input>, <select>, <textarea>, etc.

The recommended practice for this scenario is to use addEventListener() in JavaScript. However, if you still prefer using tabindex, ensure that you include tabindex in the inner HTML content.

Alternatively,

If your aim is simply to change the border of a div without the need for tabindex, you can utilize :focus-within and adjust the border accordingly.

.search-box {
  margin-left: 1%;
  outline: red;
  border: 1px solid #fc3;
}

.search-input {
  border: none;
}

.search-input:focus {
  outline: none;
}

.search-box:focus-within {
  border: 2px solid #53c9fc;
}
<div class="search-box">
  <Row>
    <div class="search-box-icon">
    </div>
    <input
      class="search-input"
      placeholder="search in listbox"
    />
  </Row>
</div>

Answer â„–2

In order to focus on a div element, you need to include the attribute tabindex.

<div 
  className="search-box" 
  tabIndex="1" // don't forget to add this line
>
  ...
</div>

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

Utilizing the If statement within AlertifyJS

Whenever I attempt to use an if statement in conjunction with Alertify, it doesn't seem to function properly. Could there be an error in my approach? var question = alertify.prompt("What is your name?", "Lenovo"); if (question === "Lenovo") { fu ...

Using enzyme mock function prior to componentDidMount

When it comes to mocking a function of a component using Jest, Enzyme, and React, the process typically involves creating a shallow wrapper of the component and then overloading the function as needed. However, there seems to be an issue where the componen ...

Step-by-step guide to tweeting with Codebird PHP from a pop-up window

I want to enhance the visitor experience on my website by allowing them to easily post a tweet with an image directly from the site. To achieve this, I have implemented the Codebird PHP library. While everything is functioning correctly at the moment, ther ...

AngularJS: Encountering issues sending POST requests with correct CORS headers

I am currently in the process of developing a web application using AngularJS. To test the application, I have set it up on a NodeJS server and utilized the angular-seed template. One of the features of this application requires me to send a JSON message ...

Prioritize the Menu display

Is there a way to bring the menu's black background in front of the body text in the image below? Here is the JSFiddle link for reference: https://jsfiddle.net/johnking/j58cubux/6/ The issue occurs when scrolling down and the menu is not clearly vi ...

Tips for refreshing an html table without affecting the scroll location?

HTML: <div class="html_table"></div> # Within the html body tag. Utilizing Ajax function to retrieve table data. var $html_table= $('.html_table'); function ajaxCallFunction() { $.ajax({ type: 'POST', ...

Struggling to grasp the concept within this particular Array.map callback

Within my React component, I am exploring the concept of the this keyword and its context with the following code snippet. While this example may seem simple, it is part of my efforts to enhance my comprehension. const NAVBAR_ITEM_TITLES = ["Home" ...

Implementing HTTPS redirection in Express 4.0 on Heroku: A Step-by-Step Guide

I'm having trouble understanding why my current code isn't achieving the desired result. Can someone please point out where I may be making a mistake? My goal is to redirect all HTTP requests to HTTPS on Heroku, but not on localhost. Any guidance ...

Tips for managing documents from mongoDB with _bsontype attributes

Hey there, I'm currently working with a JSON array that I retrieved from querying documents in mongoDB. However, I've encountered a strange behavior that I can't quite figure out: in key: _bsontype |value: ObjectID in key: id |value: S÷¯à ...

What can be done to ensure smooth functionality of my nested navigation menu on mobile devices?

I'm utilizing the incredible features of Base Web for my website. One of the components I am using is a menu with a child menu, based on this example. The menu works perfectly fine on desktop - when I hover over an option, the child menu appears. Howe ...

Tips on eliminating certain text from a hyperlink

I need assistance with removing the text  Title from my link while preserving the image. <tr id="group0"> <td colspan="100" nowrap="" class="ms-gb"> <a href="javascript:" onclick="javascript:ExpCollGroup('28-1_', ...

Vue.js seems to be leading me down a long and steady path of progress at a snail

I've exhausted all efforts to resolve the file paths for Home and App. I even turned to AI to help me out, but still no luck. Code snippet from main.js in the src folder: import Home from '@views/Home.vue'; // Using alias import App from ...

PHP is functioning properly, but no content is being displayed

I recently delved into coding and had to tackle a form that sends data through email using PHP. While the functionality of my form is working perfectly and I'm able to receive emails, I encountered an issue where the PHP page appears blank. Despite tr ...

ReactJS cannot retrieve the API data

I need help with my to-do list application. Whenever I try to pass data from an API as a prop, I encounter the error message: TypeError: Cannot read property 'length' of undefined. How can I ensure that my data is successfully fetched before pass ...

Navigating the Angular Controller life cycle

I have set up my application states using ui-router: $stateProvider .state('app', { abstract: true, views: { 'nav@': { templateUrl: 'app/navbar.html', controller: 'NavbarController' ...

The ultimate guide to loading multiple YAML files simultaneously in JavaScript

A Ruby script was created to split a large YAML file named travel.yaml, which includes a list of country keys and information, into individual files for each country. data = YAML.load(File.read('./src/constants/travel.yaml')) data.fetch('co ...

Having difficulty organizing JavaScript Chart.JS charts into two rows, instead of having everything piled up in a single column

As I delve into JavaScript and Chart.JS for data visualization, I encountered a challenge with the layout. Despite resizing the charts in my template (linked below), all the charts are still displaying in a single column format. My goal is to have two rows ...

Receiving information from a promise within an if statement in Vue

Recently, I encountered an issue with my login function. I needed to store some data in my session for future use but faced a problem where the data was being pushed into the session before I could retrieve the result from a promise. Here's the snipp ...

What are the steps to provide code so that others can easily incorporate your content into their HTML pages?

Is the solution to this problem simpler than I realize? In my Zend Framework PHP web application, I plan to create a straightforward API that generates a user account report. The report's content will be basic, like so: <ul> <li>Item ...

Ways to update the value of an object in typescript

When working with an array of objects, I encountered an issue while trying to change the object value. The error message "Type 'string | boolean' is not assignable to type 'never'. Type 'string' is not assignable to type &apos ...