Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle keyboard-triggered click events correctly #225

Merged
merged 1 commit into from
Jan 18, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/utils/dom.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
/**
* Finds overlay element that is visible under the cursor
* Uses the center of the target element if triggered by keyboard
*
* @param e Mouse event containing cursor position
* @returns Element if one is visible under the cursor, null otherwise
*/
export function visibleUnderCursor(e: MouseEvent) {
// Get all the elements under the mouse
const elements = document.elementsFromPoint(e.clientX, e.clientY);
let { clientX: x, clientY: y } = e;

// If the click event was triggered by a keyboard (e.g. 'Enter')
// Then use the center of the target element as the position of the click
if (e.detail === 0 && e.target instanceof HTMLElement) {
const box = e.target.getBoundingClientRect();
x = box.left + box.width / 2;
y = box.top + box.height / 2;
}

// Get all the elements under the click
const elements = document.elementsFromPoint(x, y);

// For each element, if it has a background then assume it is part of the overlay and return it
for (const element of elements) {
Expand Down
Loading