-
Notifications
You must be signed in to change notification settings - Fork 2
/
ElementTooltip.tsx
230 lines (208 loc) · 6.73 KB
/
ElementTooltip.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Notification
*/
import { UiEvent } from "@itwin/appui-abstract";
import type { ToolTipOptions } from "@itwin/core-frontend";
import type { XAndY } from "@itwin/core-geometry";
import type { CommonProps, Point, SizeProps } from "@itwin/core-react";
import { MessageRenderer, Rectangle } from "@itwin/core-react";
import classnames from "classnames";
import * as React from "react";
import { offsetAndContainInContainer, Tooltip } from "../layout/popup/Tooltip";
import type { NotifyMessageType } from "../messages/ReactNotifyMessageDetails";
import { BeUiEvent } from "@itwin/core-bentley";
/** [[ElementTooltip]] State.
* @internal
*/
interface ElementTooltipState {
isVisible: boolean;
message: NotifyMessageType;
position: XAndY;
options?: ToolTipOptions;
}
/** [[ElementTooltipChangedEvent]] arguments.
* @public
* @deprecated in 4.13.x. Event args are inferred from a listener. If explicit type is needed use a type helper.
*/
export interface ElementTooltipChangedEventArgs {
isTooltipVisible: boolean;
message: NotifyMessageType;
el?: HTMLElement;
pt?: XAndY;
options?: ToolTipOptions;
}
/** ElementTooltip Changed Event class.
* @public
* @deprecated in 4.13.x. This class should not be used by applications to instantiate objects.
*/
// eslint-disable-next-line deprecation/deprecation
export class ElementTooltipChangedEvent extends UiEvent<ElementTooltipChangedEventArgs> {}
/** ElementTooltip React component.
* @public
*/
export class ElementTooltip extends React.Component<
CommonProps,
ElementTooltipState
> {
private static _elementTooltipChangedEvent =
new BeUiEvent<ElementTooltipChangedEventArgs>(); // eslint-disable-line deprecation/deprecation
private static _isTooltipVisible: boolean;
private static _isTooltipHalted: boolean;
// eslint-disable-next-line deprecation/deprecation
public static get onElementTooltipChangedEvent(): ElementTooltipChangedEvent {
return ElementTooltip._elementTooltipChangedEvent;
}
public static get isTooltipVisible(): boolean {
return ElementTooltip._isTooltipVisible;
}
public static showTooltip(
el: HTMLElement,
message: NotifyMessageType,
pt?: XAndY,
options?: ToolTipOptions
): void {
// istanbul ignore if
if (ElementTooltip._isTooltipHalted) return;
ElementTooltip._isTooltipVisible = true;
ElementTooltip.onElementTooltipChangedEvent.emit({
isTooltipVisible: true,
el,
message,
pt,
options,
});
el.ownerDocument.addEventListener(
"mousemove",
ElementTooltip._handleMouseMove
);
}
public static hideTooltip(): void {
ElementTooltip._isTooltipVisible = false;
ElementTooltip.onElementTooltipChangedEvent.emit({
isTooltipVisible: false,
message: "",
});
}
// istanbul ignore next
public static get isTooltipHalted(): boolean {
return ElementTooltip._isTooltipHalted;
}
// istanbul ignore next
public static set isTooltipHalted(halt: boolean) {
ElementTooltip._isTooltipHalted = halt;
if (halt && ElementTooltip._isTooltipVisible) ElementTooltip.hideTooltip();
}
private _size: SizeProps = {
height: 0,
width: 0,
};
private _element?: HTMLElement;
private _position?: XAndY;
/** @internal */
public override readonly state: Readonly<ElementTooltipState> = {
message: "",
isVisible: false,
position: {
x: 0,
y: 0,
},
};
constructor(props: CommonProps) {
super(props);
}
public override render() {
if (!this.state.isVisible) return null;
const className = classnames("uifw-element-tooltip", this.props.className);
return (
<div className="uifw-element-tooltip-container">
<Tooltip
className={className}
style={this.props.style}
position={this.state.position}
onSizeChanged={this._handleSizeChanged}
>
<MessageRenderer message={this.state.message} />
</Tooltip>
</div>
);
}
public override componentDidMount(): void {
ElementTooltip.onElementTooltipChangedEvent.addListener(
this._handleElementTooltipChangedEvent
);
}
public override componentWillUnmount(): void {
ElementTooltip.onElementTooltipChangedEvent.removeListener(
this._handleElementTooltipChangedEvent
);
}
private static _handleMouseMove(event: MouseEvent) {
const el = event.currentTarget as Document;
/* Only monitor mouse movement when an ElementTooltip is open. */
if (
(el && ElementTooltip._isTooltipHalted) ||
!ElementTooltip._isTooltipVisible
) {
el.removeEventListener("mousemove", ElementTooltip._handleMouseMove);
return;
}
const hoveredElement = el
? el.elementFromPoint(event.clientX, event.clientY)
: undefined;
/* If the mouse has moved to an element that is not the view canvas, close the ElementTooltip. */
// istanbul ignore next.
if (hoveredElement && hoveredElement.localName !== "canvas") {
ElementTooltip.onElementTooltipChangedEvent.emit({
isTooltipVisible: false,
message: "",
});
el.removeEventListener("mousemove", ElementTooltip._handleMouseMove);
return;
}
}
private _handleElementTooltipChangedEvent = (
args: ElementTooltipChangedEventArgs // eslint-disable-line deprecation/deprecation
) => {
this._element = args.el;
this._position = args.pt;
this.setState({
isVisible: args.isTooltipVisible,
message: args.message,
});
this.updatePosition();
};
// istanbul ignore next
private _handleSizeChanged = (size: SizeProps) => {
this._size = size;
this.updatePosition();
};
private updatePosition() {
this.setState((prevState) => {
if (!this._element) return null;
// istanbul ignore next
if (!this._position) return null;
const containerBounds = Rectangle.create(
this._element.getBoundingClientRect()
);
const relativeBounds = Rectangle.createFromSize(this._size).offset(
this._position
);
const adjustedPosition: Point = offsetAndContainInContainer(
relativeBounds,
containerBounds.getSize(),
{ x: 8, y: 8 }
);
const position = adjustedPosition.offset(containerBounds.topLeft());
// istanbul ignore else
if (position.equals(prevState.position)) return null;
// istanbul ignore next
return {
position,
};
});
}
}