refactor: simplify escapekeydown handler logic for tooltips and dropdown menus

This commit is contained in:
Dustin Healy 2025-12-12 15:19:55 -08:00
parent df1765c48e
commit d4327173c7

View file

@ -84,39 +84,18 @@ const DialogContent = React.forwardRef<
const handleEscapeKeyDown = React.useCallback(
(event: KeyboardEvent) => {
const tooltips = document.querySelectorAll('.tooltip');
for (const tooltip of Array.from(tooltips)) {
const computedStyle = window.getComputedStyle(tooltip);
const opacity = parseFloat(computedStyle.opacity);
if (
tooltip.parentElement &&
computedStyle.display !== 'none' &&
computedStyle.visibility !== 'hidden' &&
opacity > 0
) {
event.preventDefault();
return;
}
}
// Check if a dropdown menu is open
const dropdownMenus = document.querySelectorAll('[role="menu"]');
for (const dropdown of Array.from(dropdownMenus)) {
const computedStyle = window.getComputedStyle(dropdown);
const opacity = parseFloat(computedStyle.opacity);
if (
computedStyle.display !== 'none' &&
computedStyle.visibility !== 'hidden' &&
opacity > 0
) {
if (tooltips.length > 0) {
event.preventDefault();
return;
}
if (dropdownMenus.length > 0) {
event.preventDefault();
return;
}
// Call the original handler if it exists
propsOnEscapeKeyDown?.(event);
},
[propsOnEscapeKeyDown],