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

How come the setter of React useState does not update the state within a function?

I need to retrieve and set data from React useState within the same function in order to perform validation before calling an API. Each time the submit button is pressed, the onSubmit function is called. However, there seems to be an issue where on the fir ...

The handler provided to Boost::asio::async_read_until is never triggered

I am attempting to establish a connection between two computers on a local network. One is using a slightly modified version of the Boost Asio C++ TCP asynchronous server sample, while the other is running NodeJS. tcp_client.js : var net = require(' ...

Adjust the background color of the react-alert component

After successfully integrating react-alert into my dashboard, I found that the alert's background color clashes with the overall theme of my UI. View Alert Image I attempted to modify the background color in the template file where the property is l ...

Sparse planeBufferGeometry in THREE.js is a specialized type of geometry that

I am currently working with a file that contains sparse elevation data derived from GPS information. I have been utilizing this data to fill a PlaneBuffer array with elevations. var vertices = new Float32Array( (grid.NCOL*grid.NROW) * 4 ); for (var i = 0, ...

Is it really necessary to still think poorly of JavaScript in 2011?

Here's an intriguing question for you. I've tested out a variety of popular websites, including Facebook, and I've noticed that many features still work perfectly fine even when JavaScript is disabled. People always used to say that JavaScr ...

Error: The method `push` within the `useHistory` function is causing an issue and is currently undefined

Whenever the home button is clicked, I need it to redirect to the homepage '/ '. However, I keep encountering this error. Any suggestions on what steps I should take to resolve this? : import { Route, useHistory } from 'react-router-dom/cjs/ ...

Steps to trigger an alert when the entered quantity exceeds the current stock levels

After developing an IMS System, I encountered a problem where my stock is going into negative figures. To resolve this issue, my primary goal is to trigger an alert whenever the quantity entered by a user exceeds the available stock. For example, if a us ...

I am experiencing difficulties in installing JavaScript libraries

PS D:\React> npm i -g expo-cli npm WARN EBADENGINE Unsupported engine { npm WARN EBADENGINE package: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2d48555d42004e41446d68c7b6d717268805a6369786964 ...

Is there a way to customize the icon in the material-ui date picker component for React?

Is it possible to customize the icon for the material UI Date picker? I've searched through the code and API documentation but can't find any mention of how to change the icon. You can check out their official documentation here: https://materi ...

Utilize load-grunt-configs and run bower.install() with grunt

I have been attempting to break down my current Gruntfile into smaller parts for better clarity. However, I have encountered a hurdle while using bower.install to manage project dependencies. My directory structure appears as follows: package.json bower.j ...

React setupProxy failing to proxy request

Recently, I attempted to establish a React web application integrated with an asp.net core web API housed in two separate projects. Adhering to Microsoft's instructions for configuring the templates within two distinct Visual Studio projects proved su ...

Exploring the power of JavaScript template literals within the Document Object Model

I am curious as to why this particular template literal is not functioning correctly when I try to implement it using JavaScript DOM for styling CSS. Any assistance in helping me understand this issue would be greatly appreciated! let weatherColors = [& ...

Is there a way to duplicate a GLTF model that has been imported into the Autodesk Viewer?

I encountered an issue while trying to dynamically clone a loaded GLB model and allow the user to position it in the scene. Despite using the model.clone() method, the cloned model ends up appearing at the same position as the original, causing changes in ...

Troubleshooting problems with data rendering in jQuery

Currently, my goal is to use JQuery to display a menu of checkboxes based on a specific template in a div. To enhance user experience, I have included a search box that will filter the menu items. However, there is an unusual issue occurring. When the men ...

Is it possible to change the src attribute after the page has loaded and execute it

A. I'm trying to figure out how to dynamically change the src attribute of an image using JavaScript after the page has loaded. <img src="http://example.com/image.png" /> to <img src="http://domain.com/different.jpg" /> B. Another questi ...

Is there a way to populate fields on a hidden input using Mechanize?

When I try to use the form to navigate to a different page, I realize that the input field is hidden. Below is the HTML code for reference: <form id="form_pager" method="post" action=""> <input type="hidden" id="txtPage" name="page"> ...

Pressing the tab key makes all placeholders vanish

case 'input': echo '<div class="col-md-3"> <div class="placeholder"> <img src="images/person.png" /> &l ...

What is the reason for not being able to locate the controller of the necessary directive within AngularJS?

A couple of angularjs directives were written, with one being nested inside the other. Here are the scripts for the directives: module.directive('foo', [ '$log', function($log) { return { restrict: 'E', r ...

"By implementing an event listener, we ensure that the same action cannot be

function addEventListenerToElement(element, event, handlerFunction) { if(element.addEventListener) { element.addEventListener(event, function(){ handlerFunction(this.getAttribute("src")); }, false); } } //initialize the function addEve ...

Having trouble parsing an XML received from AJAX request

I am encountering an issue with the code below: $.ajax({ type: "POST", dataType: "xml", url: getUrl('/GetPeriodicStats/'), data: XML.innerHTML,//some xml, success: function(c) { After receiving the XML data in the clie ...