The use of a <button> element in a React App within a Web Component with Shadow DOM in Chrome disables the ability to highlight text

An unusual problem has arisen, but I have a concise example that demonstrates the issue: https://codesandbox.io/s/falling-architecture-hvrsd?file=/src/index.js

https://i.stack.imgur.com/CkL4g.png

https://i.stack.imgur.com/nDjuD.png

By utilizing the divs at the top, you can toggle between displaying and hiding a button with the text WHY?, which will respectively prevent or enable the highlighting of the text at the bottom saying Try and Highlight This Text and

You only can if the button isn't shown
.

I am employing a Web Component and Shadow DOM for a chrome extension, aiming to avoid style conflicts.

If you comment out the web component section in index.js and directly render the demo with ReactDOM.render(), this behavior will not be present.

I am uncertain whether this issue is specific to only <button> elements.

EDIT: The functionality works in Firefox, but there are problems in Chrome or the latest version of Chromium Edge.

let shadow = document.getElementById("root").attachShadow({ mode: 'open', delegatesFocus: true });
const button = document.createElement('button');
button.innerText='Test';

const p = document.createElement('p');
p.innerText = 'Cannot highlight this in Chrome';
shadow.appendChild(button);
shadow.appendChild(p);
<div id="root"></div>

Answer №1

Although the exact mechanism is unclear to me, it seems to be related to the code snippet

const shadowRoot = this.attachShadow({... delegatesFocus: true})
. If you change delegatesFocus to false, the selection will function properly. According to MDN, this feature is experimental and only available in Chrome: https://developer.mozilla.org/en-US/docs/Web/API/ShadowRoot/delegatesFocus

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

The Splice function is malfunctioning due to issues with the object (the indexOf function is not recognized)

I am currently dealing with an object that looks like this: Object {val1: "Hello", val2: "", dt1: "pilo1", dt2: "pilo2", lo1: "log1"} My goal is to remove any keys within the object that have empty values (""). I attempted the following code snippet: ...

Show a notification if there are any errors in the form upon submission using react-final-form

I am currently working with react-final-form and I'm trying to figure out how to display an alert when a user tries to submit a form with errors. I have created a function for the onSubmit event like so: onSubmit={event => { event.preventDefault ...

Unable to retrieve the image

When trying to fetch an image, I encountered the following error: Failed to load resource: the server responded with a status of 404 (Not Found) TopBar.jsx import { useContext } from "react"; import { Link } from "react-router-dom"; ...

Issue when attempting to animate an SVG point using translateX transformation

I am attempting to create a basic animation using the translate X property on a section of my svg when hovering over the element. Here is the code I have so far: <html> <style> .big-dot:hover { transform: translateX(20px); animat ...

Attempting to create a JavaScript function that will show a JSON array when the next button is clicked

I am facing an issue with displaying a JSON array based on specific start and end indices. Even though I managed to display the entire array, the first button click does not seem to work as expected. Upon clicking the first button, an error message "this.d ...

The margins within the div of the new tab are not applying as expected

Greetings! Currently, I am in the process of generating a dynamic form within a new tab using JavaScript. For some reason, the margin effects on the div element are not being applied correctly. Despite my attempts to set the margins using both classes and ...

The unexpected end of input error is caused by an Ajax call that returns a SyntaxError

I recently developed a basic notepad application where users can retrieve a specific file (which essentially translates to a row from MySQL) in order to make edits. When the form is submitted directly to the PHP file without preventing the default behavi ...

Utilizing a React component within an Express server: A comprehensive guide

I need to create a way for users to upload files to my express backend, but the form is within a react component. How can I incorporate my react ConverterSec2 component into the get function? server.js: import ConverterSec2 from "./ihertz_website/src ...

Experiencing an issue where the canvas element fails to render on mobile Chrome browser,

I've encountered an issue with a script that draws a canvas based on the background color of an image. The image is loaded dynamically from a database using PHP. The responsive functionality works fine on mobile Safari, but not on Chrome. When the re ...

Troubleshooting a Laravel 5.2 modal popup issue for password recovery, experiencing a 500 internal server error with the Ajax function execution

Is there a way to check if an email exists in order to send a reset password link using an AJAX function? I keep encountering a 500 internal server error before the AJAX runs. I understand that a 500 error is usually due to a token mismatch, but do I actua ...

Font size in button labels in Material UI

Trying to find a solution on how to increase the font size of a label within a Material UI button in a React project. Here is the setup for the button: import React from 'react'; import MyButton from '../materialui/Button.js'; const ...

What is the method to display HTML code using JavaScript within a span element in a JSP page?

While working on a jsp file, I came across a span tag with the following code: <span id="divBasicSearchResults" style="height:100%;"></span> Below is the Javascript function that generates HTML content for this span: function renderBasicSear ...

What is the method for showcasing a single Observable from an Array of Observables in Angular?

I am facing a challenge with displaying questions from an Observable-Array one by one. Currently, I can only display all the questions at once using *ngFor in my HTML. Here is my component code snippet: import { Component, OnInit } from '@angula ...

How to efficiently fetch Redux state through reducers with an action-based approach

Utilizing Redux actions to manage a list of contacts, I am facing an issue where I am unable to retrieve the actual state contact. Despite setting the contact in the action, all I receive is the contact set within the action itself. Can someone guide me on ...

Having trouble accessing variables from the dotenv file in a NextJS project

Recently, I started using Next.js and ran into a bit of trouble. Here's the issue: When I'm in the connectToMongo function, my variable is coming through just fine. However, when I switch over to Main/index.tsx, my process.env appears as an emp ...

What is the best way to activate a JQ function with my submit button?

Is there a way to trigger a JQ function when clicking the submit button in a form? I managed to make Dreamweaver initiate an entire JS file, but not a particular function. ...

Issue with displaying the Bootstrap-select DropDownList

Within my web application, I have a bootstrap-select combobox that I populate with data using an ajax call. Html: <div class="row"> <select id="selectGemeente1" class="selectpicker" data-live-search="true"></select> </div> Ajax ca ...

Is it possible to submit a HTML5 form and have it displayed again on the same page?

Is it possible to simply reload the sidebar of a page containing an HTML5 form upon submission, or is it necessary to load a duplicate page with only the sidebar changed? I am unsure of how to tackle this situation; firstly, if it is achievable in this m ...

Is there a way to seamlessly inject a stylesheet into a React application without causing any flickering or reloading on the website?

In my React application built with next.js, I am fetching a stylesheet via a GET request and appending it to the webpage. However, whenever this stylesheet is loaded in, the elements impacted by it re-render unnecessarily, even if there are no actual chang ...

Tips for refreshing the direction route in google-maps-react

I have an array of coordinates, and when I add a new coordinate to the array, I want the route direction on the map to update accordingly. This is the code snippet from my googlemap.js file: /* global google */ import React, { Component } from "react ...