show tab focus outline only

In search of a straightforward and effective method for focusable elements to display an outline only when the tab key is pressed, without showing it when using a mouse in React applications. (looking for something similar to :focus-visible that functions correctly on various browsers) It's frustrating how there are almost no clear, general explanations online for creating an accessible website. Appreciate any assistance!

Answer №1

One effective approach is to incorporate event listeners for both keyboard and mouse presses. When the tab key is pressed, you can apply a CSS class that highlights the intended elements.

function handleFirstTab(e) {
  if (e.keyCode === 9) {
    document.body.classList.add('user-is-tabbing');

    window.removeEventListener('keydown', handleFirstTab);
    window.addEventListener('mousedown', handleMouseDownOnce);
  }
}

function handleMouseDownOnce() {
  document.body.classList.remove('user-is-tabbing');

  window.removeEventListener('mousedown', handleMouseDownOnce);
  window.addEventListener('keydown', handleFirstTab);
}

window.addEventListener('keydown', handleFirstTab);

Answer №2

This is a general solution for focusing elements. In case there are other styles taking precedence over focus, you might need to add specificity or use "!important" to make these properties effective.

*:focus {
  outline-width: 2px;
  outline-style: solid;
  outline-color: Highlight;
}

/* WebKit uses its default focus styles.
 */
@media (-webkit-min-device-pixel-ratio:0) {
  *:focus {
    outline-color: -webkit-focus-ring-color;
    outline-style: auto;
  }
}

source:

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

arranging fashionable divs side by side

I am currently working on a website and I want to include four circles that stand next to each other with a margin of approximately 50px between them. I managed to position one circle correctly, but when I try to add more circles, they do not display prope ...

Transferring data to a Component as properties in Next.js

I've encountered an issue while trying to pass editvalues as props to my EditForm component, resulting in the error 'editvalues is not defined'. Important note: editvalues is an object Upon using console.log(editvalues), I can see the valu ...

Guide on implementing various styles for a class in Tailwind CSS when transitioning to dark mode with the help of Next.js 13.4 and next-theme

Is it possible to apply unique styles for a class in Tailwind CSS when transitioning to dark mode using Next.js 13.4 and next-theme? I am currently setting up a dark theme in Tailwind CSS with Next.js 13.4 utilizing next-theme. Within my globals.css file, ...

Changing the color of Material UI pagination: a step-by-step guide

I have integrated Material UI to create a pagination bar for my website. While everything is functioning properly, I am looking to personalize the appearance. After reviewing their documentation, I tried implementing a theme but encountered an issue with c ...

Can the sidebar be positioned after the div on an HTML page?

Is it possible for the sidebar to remain static until it is reached by scrolling on the page? I want it to be positioned below the Jumbotron and then stick to the left and top when scrolling down. Currently, it's overlapping everything and adjusting z ...

Having trouble rendering the response text from the API fetched in Next.js on a webpage

I've written some code to back up a session border controller (SBC) and it seems to be working well based on the output from console.log. The issue I'm facing is that the response comes in as a text/ini file object and I'm unable to display ...

How can you establish an environmental variable in node.js and subsequently utilize it in the terminal?

Is there a way to dynamically set an environmental variable within a Node.js file execution? I am looking for something like this: process.env['VARIABLE'] = 'value'; Currently, I am running the JS file in terminal using a module whe ...

Transferring .jsx files between React applications

Recently, I attempted to create separate .jsx files in React. I successfully made a couple of them using export default name / import name from ./name.jsx'. However, I encountered an issue. Initially, when I imported Route and Links from react-router, ...

Differences between Angular's $injector and Angular's dependency injectionAngular

As a newcomer to Angular, I am exploring the use of $injector and its get function to retrieve specific services. For instance: app.factory('$myService', function($injector) { return { ... var http = $injector.get('$http&apos ...

Vue.js does not display HTML properly within the vue-swal component

I'm currently working on integrating HTML content into a Sweet Alert popup using this code snippet Link: https://www.npmjs.com/package/vue-swal this.$swal({ title: '<i>Custom HTML</i>', html:`This is an <em> em ...

Adding an onClick event to a React MUI Fab: A Step-by-Step Guide

I'm attempting to incorporate the Floating Action Button from MUI5. Visit: https://mui.com/material-ui/react-floating-action-button/ for more details. Upon adding an onClick event to the button, no action is triggered. Below is my code snippet: <G ...

Converting a string into an array of JSON objects

I am currently attempting to send data to my NodeJS server using the HTTP protocol (vue-resource). The goal is to send an array of JSON objects structured like this : [{"name":"Charlotte","surname":"Chacha","birth":"2000-04-02"},{"name":"Michael","surname ...

Create unique identifiers for the TD elements of Viz.js that will be displayed within the SVG elements

Below is the DOT code snippet in Viz.js that I am working with: digraph G { node [fontname = "font-awesome"]; 17 [id=17, shape="hexagon", label=<<TABLE BORDER="0"> <TR><TD>undefined</TD></TR> <TR><TD>[47-56]< ...

The tab content in VerticalTab Material React UI is spilling out and overflowing

Having some trouble with this particular component. Whenever I input content larger than my Grid size, it overflows the VerticalTab section. You can see an example of this in this codesandbox. Here's what I've attempted: return ( <div cla ...

When attempting to capture an element screenshot as PNG, a TypeError occurs indicating that the 'bytes' object is not callable

Can someone please help me figure out how to save a screenshot of a specific element in Selenium using Python3? Here is the code I am trying: from selenium import webdriver import pyautogui as pog import time options = webdriver.ChromeOptions() options ...

Is there a way to target a child element for hover effect in CSS without affecting the parent element?

Is it feasible to hover over a nested child element without activating a hover effect on the parent element? In the demonstration below, you'll notice that even when hovering over the child, the hover effect on the parent is still in place. I am int ...

A guide on adjusting the height of UI elements in Semantic: steps for modifying

One of my current challenges involves using Semantic UI and embedding it within an iFrame tag to display a video. However, I am struggling to adjust the height of the Semantic UI element. <div className="video_box ui embed"> ...

Trying out the componentWillUnmount() function to erase a class from the body element

In my React component, I have the following code: componentWillUnmount () { document.body.classList.remove('isPreloaded') } Initially, in my index.html file, I have a body tag with the class isPreloaded, along with a Preloader HTML and ...

What is the process for generating SDF-Icons (Mapbox's specialized icons) from PNG files?

I am currently working on changing the icon color of an icon image in Mapbox. According to Mapbox documentation, the only way to do this is by using sdf-icons (https://docs.mapbox.com/mapbox-gl-js/style-spec/layers/#paint-symbol-icon-color). After hours o ...

Converting hexadecimal to binary using Javascript or Typescript before writing a file on an Android or iOS device

Hey everyone! I'm facing a puzzling issue and I can't seem to figure out why it's happening. I need to download a file that is stored in hex format, so I have to first read it as hex, convert it to binary, and then write it onto an Android/ ...