Skip to content

Commit 71952d6

Browse files
authored
Adds DOM util for detecting mouse events in element bounding boxes (#6806)
## Motivation for features / changes This will be useful for supporting nested modals in the upcoming CDK Overlay based CustomModal revamp.
1 parent 0073643 commit 71952d6

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

tensorboard/webapp/util/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ tf_ts_library(
9797
name = "util_tests",
9898
testonly = True,
9999
srcs = [
100+
"dom_test.ts",
100101
"lang_test.ts",
101102
"matcher_test.ts",
102103
"memoize_test.ts",
@@ -107,6 +108,7 @@ tf_ts_library(
107108
],
108109
deps = [
109110
":colors",
111+
":dom",
110112
":lang",
111113
":matcher",
112114
":memoize",

tensorboard/webapp/util/dom.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,14 @@ export function nextElementId(): ElementId {
4040
currElementId++;
4141
return Symbol(currElementId);
4242
}
43+
44+
/** Checks whether a mouse event is within an element's bounding box. */
45+
export function isMouseEventInElement(event: MouseEvent, el: Element): boolean {
46+
const rect = el.getBoundingClientRect();
47+
return (
48+
rect.x <= event.clientX &&
49+
event.clientX <= rect.x + rect.width &&
50+
rect.y <= event.clientY &&
51+
event.clientY <= rect.y + rect.height
52+
);
53+
}

tensorboard/webapp/util/dom_test.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* Copyright 2024 The TensorFlow Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
==============================================================================*/
15+
16+
import {isMouseEventInElement} from './dom';
17+
18+
describe('dom utils', () => {
19+
describe('isMouseEventInElement', () => {
20+
[
21+
{
22+
testDesc: 'click is to the left of element',
23+
clientX: 99,
24+
clientY: 150,
25+
},
26+
{
27+
testDesc: 'click is to the right of element',
28+
clientX: 201,
29+
clientY: 150,
30+
},
31+
{
32+
testDesc: 'click is above element',
33+
clientX: 150,
34+
clientY: 99,
35+
},
36+
{
37+
testDesc: 'click is below element',
38+
clientX: 150,
39+
clientY: 201,
40+
},
41+
].forEach(({testDesc, clientX, clientY}) => {
42+
it(`returns false when ${testDesc}`, () => {
43+
const event = new MouseEvent('mouseup', {clientX, clientY});
44+
const element = document.createElement('div');
45+
spyOn(element, 'getBoundingClientRect').and.returnValue(
46+
new DOMRect(100, 100, 100, 100)
47+
);
48+
49+
const result = isMouseEventInElement(event, element);
50+
51+
expect(result).toBeFalse();
52+
});
53+
});
54+
55+
it('returns true when click is within element bounds', () => {
56+
const event = new MouseEvent('mouseup', {clientX: 150, clientY: 150});
57+
const element = document.createElement('div');
58+
spyOn(element, 'getBoundingClientRect').and.returnValue(
59+
new DOMRect(100, 100, 100, 100)
60+
);
61+
62+
const result = isMouseEventInElement(event, element);
63+
64+
expect(result).toBeTrue();
65+
});
66+
});
67+
});

0 commit comments

Comments
 (0)