Skip to content
Merged
Next Next commit
[DevTools] Use Popover API for TraceUpdates highlighting
  This change ensures that component highlighting in React DevTools appears above toplayer elements by using
   the Popover API. This fixes an issue where highlights would appear behind elements that use the browser's
   toplayer (such as dialogs, popovers, etc).

  The implementation gracefully falls back to the previous behavior in browsers that don't support the
  Popover API.
  • Loading branch information
yongsk0066 committed Mar 14, 2025
commit ecb403ff4cd59991cefde3e754cb76f6dd1d680a
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ function drawNative(nodeToData: Map<HostInstance, Data>, agent: Agent) {
function drawWeb(nodeToData: Map<HostInstance, Data>) {
if (canvas === null) {
initialize();
} else {
try {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, you don't need it to wrap it in try / catch, since we bumped minimum versions for the extension

Please also check other calls below

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing this out! I've removed all try/catch blocks around Popover API calls.

if (canvas.hasAttribute('popover') && !canvas.matches(':popover-open')) {
canvas.showPopover();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you gating it with try/catch because for feature checking?

We might bump the minimum required version for Chrome / other browsers here:
https://github.com/facebook/react/blob/main/packages/react-devtools-extensions/chrome/manifest.json#L7

So you won't need to worry about if its supported or not

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's right. I've handled it in case popover isn't supported. Would it be okay to raise the minimum version? If we do raise it, I plan to increase it to version 114 (https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/showPopover#browser_compatibility) which supports the popover API.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do it for Edge browser as well. I believe Firefox should already list version that is higher than needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added Edge as well! Yes, Firefox requires 125 or higher, but it's already set to 128, so we're good.

} catch (e) {}
}

const dpr = window.devicePixelRatio || 1;
Expand Down Expand Up @@ -191,6 +197,12 @@ function destroyNative(agent: Agent) {

function destroyWeb() {
if (canvas !== null) {
try {
if (canvas.hasAttribute('popover')) {
canvas.hidePopover();
}
} catch (e) {}

if (canvas.parentNode != null) {
canvas.parentNode.removeChild(canvas);
}
Expand All @@ -204,6 +216,7 @@ export function destroy(agent: Agent): void {

function initialize(): void {
canvas = window.document.createElement('canvas');
canvas.setAttribute('popover', 'auto');
canvas.style.cssText = `
xx-background-color: red;
xx-opacity: 0.5;
Expand All @@ -214,8 +227,13 @@ function initialize(): void {
right: 0;
top: 0;
z-index: 1000000000;
background-color: transparent;
`;

const root = window.document.documentElement;
root.insertBefore(canvas, root.firstChild);

try {
canvas.showPopover();
} catch (e) {}
}
Loading