@antv/component 1.0.0 → 1.0.1-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  import { substitute, createDOM } from '@antv/util';
2
2
  import { Component } from '../../core';
3
3
  import { Group } from '../../shapes';
4
- import { applyStyleSheet, throttle } from '../../util';
4
+ import { BBox, applyStyleSheet } from '../../util';
5
5
  import { getClassNames, getDefaultTooltipStyle } from './constant';
6
6
  import type { TooltipOptions, TooltipPosition, TooltipStyleProps } from './types';
7
7
 
@@ -10,6 +10,8 @@ export type { TooltipStyleProps, TooltipOptions };
10
10
  export class Tooltip extends Component<TooltipStyleProps> {
11
11
  public static tag = 'tooltip';
12
12
 
13
+ private timestamp = -1;
14
+
13
15
  public get HTMLTooltipElement() {
14
16
  return this.element;
15
17
  }
@@ -18,12 +20,6 @@ export class Tooltip extends Component<TooltipStyleProps> {
18
20
  return this.element;
19
21
  }
20
22
 
21
- public set position([x, y]: [number, number]) {
22
- this.attributes.x = x;
23
- this.attributes.y = y;
24
- this.updatePosition();
25
- }
26
-
27
23
  private get elementSize() {
28
24
  const width = this.element.offsetWidth;
29
25
  const height = this.element.offsetHeight;
@@ -89,19 +85,32 @@ export class Tooltip extends Component<TooltipStyleProps> {
89
85
  * 如果设置了坐标值,显示过程中会立即更新位置并关闭过渡动画
90
86
  */
91
87
  public show(x?: number, y?: number) {
92
- const disableTransition = x !== undefined && y !== undefined;
93
- if (disableTransition) {
94
- const transition = this.element.style.transition;
95
- this.element.style.transition = 'none';
96
- this.position = [x ?? +this.attributes.x, y ?? +this.attributes.y];
97
- setTimeout(() => {
98
- this.element.style.transition = transition;
99
- }, 10);
88
+ if (x !== undefined && y !== undefined) {
89
+ const isToggle = this.element.style.visibility === 'hidden';
90
+ const setPosition = () => {
91
+ this.attributes.x = x ?? this.attributes.x;
92
+ this.attributes.y = y ?? this.attributes.y;
93
+ this.updatePosition();
94
+ };
95
+ // 只有从 hidden 状态变为 visible 状态时才需要关闭过渡动画
96
+ isToggle ? this.closeTransition(setPosition) : setPosition();
100
97
  }
101
98
  this.element.style.visibility = 'visible';
102
99
  }
103
100
 
104
- public hide() {
101
+ /**
102
+ * 如果 hide 时传入了坐标值,那么只有当鼠标不在 tooltip 上时才会隐藏
103
+ * 对于 enterable = true 的时候,需要传入 x y,为了避免问题,建议上层在使用的时候,都传入 x y
104
+ * @param x
105
+ * @param y
106
+ * @returns
107
+ */
108
+ public hide(x = 0, y = 0) {
109
+ const { enterable } = this.attributes;
110
+
111
+ // 如果当前鼠标在 tooltip 上,则不隐藏
112
+ if (enterable && this.isCursorEntered(x, y)) return;
113
+
105
114
  this.element.style.visibility = 'hidden';
106
115
  }
107
116
 
@@ -186,10 +195,15 @@ export class Tooltip extends Component<TooltipStyleProps> {
186
195
  /**
187
196
  * 更新tooltip的位置
188
197
  */
189
- @throttle(100, true)
190
198
  private updatePosition() {
191
- // 尝试当前的位置使用默认position能否放下
192
- // 如果不能,则改变取溢出边的反向position
199
+ const { showDelay = 60 } = this.attributes;
200
+
201
+ const currentTimestamp = Date.now();
202
+ if (this.timestamp > 0 && currentTimestamp - this.timestamp < showDelay) return;
203
+
204
+ this.timestamp = currentTimestamp;
205
+ // 尝试当前的位置使用默认 position 能否放下
206
+ // 如果不能,则改变取溢出边的反向 position
193
207
  /**
194
208
  * 默认位置
195
209
  * ⬇️
@@ -241,4 +255,29 @@ export class Tooltip extends Component<TooltipStyleProps> {
241
255
  const correctedPositionString = correctivePosition.join('-');
242
256
  return this.getRelativeOffsetFromCursor(correctedPositionString as TooltipPosition);
243
257
  }
258
+
259
+ private isCursorEntered(clientX: number, clientY: number) {
260
+ // 是可捕获的,并且点在 tooltip dom 上
261
+ if (this.element) {
262
+ const { x, y, width, height } = this.element.getBoundingClientRect();
263
+ // const { container } = this.attributes;
264
+ // const { x: cx, y: cy } = container;
265
+
266
+ // console.log(1113, [clientX, clientY], [x, y, width, height], [cx, cy], new BBox(x - cx, y - cy, width, height).isPointIn(cursorX, cursorY));
267
+
268
+ return new BBox(x, y, width, height).isPointIn(clientX, clientY);
269
+ }
270
+ return false;
271
+ }
272
+
273
+ private closeTransition(callback: () => void) {
274
+ const transition = this.element.style.transition;
275
+ this.element.style.transition = 'none';
276
+
277
+ callback();
278
+
279
+ setTimeout(() => {
280
+ this.element.style.transition = transition;
281
+ }, 10);
282
+ }
244
283
  }
@@ -30,6 +30,8 @@ export type TooltipStyleProps = GroupStyleProps & {
30
30
  offset?: [number, number];
31
31
  /** 指针是否可进入 */
32
32
  enterable?: boolean;
33
+ /** 配合 enterable = true 使用,指定延迟显示的毫秒数,默认为 60ms */
34
+ showDelay?: number;
33
35
  /** 画布的左上角坐标 */
34
36
  container: {
35
37
  x: number;
package/src/util/bbox.ts CHANGED
@@ -48,6 +48,14 @@ export class BBox {
48
48
  left: this.left,
49
49
  };
50
50
  }
51
+
52
+ /**
53
+ * 点是否在 bbox 中
54
+ * @param p
55
+ */
56
+ public isPointIn(x: number, y: number) {
57
+ return x >= this.left && x <= this.right && y >= this.top && y <= this.bottom;
58
+ }
51
59
  }
52
60
 
53
61
  export function getRenderBBox(element: DisplayObject) {