foundation.ts 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. /* eslint-disable no-param-reassign */
  2. /* eslint-disable prefer-destructuring, max-lines-per-function, one-var, max-len, @typescript-eslint/restrict-plus-operands */
  3. /* argus-disable unPkgSensitiveInfo */
  4. import { get, isEmpty } from 'lodash-es';
  5. import { DOMRectLikeType } from '../utils/dom';
  6. import BaseFoundation, { DefaultAdapter } from '../base/foundation';
  7. import { ArrayElement } from '../utils/type';
  8. import { strings } from './constants';
  9. const REGS = {
  10. TOP: /top/i,
  11. RIGHT: /right/i,
  12. BOTTOM: /bottom/i,
  13. LEFT: /left/i,
  14. };
  15. const defaultRect = {
  16. left: 0,
  17. top: 0,
  18. height: 0,
  19. width: 0,
  20. scrollLeft: 0,
  21. scrollTop: 0,
  22. };
  23. export interface TooltipAdapter<P = Record<string, any>, S = Record<string, any>> extends DefaultAdapter<P, S> {
  24. registerPortalEvent(portalEventSet: any): void;
  25. unregisterPortalEvent(): void;
  26. registerResizeHandler(onResize: () => void): void;
  27. unregisterResizeHandler(onResize?: () => void): void;
  28. on(arg0: string, arg1: () => void): void;
  29. notifyVisibleChange(isVisible: any): void;
  30. getPopupContainerRect(): PopupContainerDOMRect;
  31. containerIsBody(): boolean;
  32. off(arg0: string): void;
  33. canMotion(): boolean;
  34. registerScrollHandler(arg: () => Record<string, any>): void;
  35. unregisterScrollHandler(): void;
  36. insertPortal(...args: any[]): void;
  37. removePortal(...args: any[]): void;
  38. getEventName(): {
  39. mouseEnter: string;
  40. mouseLeave: string;
  41. mouseOut: string;
  42. mouseOver: string;
  43. click: string;
  44. focus: string;
  45. blur: string;
  46. };
  47. registerTriggerEvent(...args: any[]): void;
  48. getTriggerBounding(...args: any[]): DOMRect;
  49. getWrapperBounding(...args: any[]): DOMRect;
  50. setPosition(...args: any[]): void;
  51. togglePortalVisible(...args: any[]): void;
  52. registerClickOutsideHandler(...args: any[]): void;
  53. unregisterClickOutsideHandler(...args: any[]): void;
  54. unregisterTriggerEvent(): void;
  55. containerIsRelative(): boolean;
  56. containerIsRelativeOrAbsolute(): boolean;
  57. getDocumentElementBounding(): DOMRect;
  58. updateContainerPosition(): void;
  59. updatePlacementAttr(placement: Position): void;
  60. getContainerPosition(): string;
  61. }
  62. export type Position = ArrayElement<typeof strings.POSITION_SET>;
  63. export interface PopupContainerDOMRect extends DOMRectLikeType {
  64. scrollLeft?: number;
  65. scrollTop?: number;
  66. }
  67. export default class Tooltip<P = Record<string, any>, S = Record<string, any>> extends BaseFoundation<TooltipAdapter<P, S>, P, S> {
  68. _timer: ReturnType<typeof setTimeout>;
  69. _mounted: boolean;
  70. constructor(adapter: TooltipAdapter<P, S>) {
  71. super({ ...adapter });
  72. this._timer = null;
  73. }
  74. init() {
  75. this._mounted = true;
  76. this._bindEvent();
  77. this._shouldShow();
  78. this._initContainerPosition();
  79. }
  80. destroy() {
  81. this._mounted = false;
  82. this._unBindEvent();
  83. }
  84. _bindEvent() {
  85. const types = this.getProp('trigger'); // get trigger type
  86. const { triggerEventSet, portalEventSet } = this._generateEvent(types);
  87. this._bindTriggerEvent(triggerEventSet);
  88. this._bindPortalEvent(portalEventSet);
  89. this._bindResizeEvent();
  90. }
  91. _unBindEvent() {
  92. this._unBindTriggerEvent();
  93. this._unBindPortalEvent();
  94. this._unBindResizeEvent();
  95. this._unBindScrollEvent();
  96. }
  97. _bindTriggerEvent(triggerEventSet: Record<string, any>) {
  98. this._adapter.registerTriggerEvent(triggerEventSet);
  99. }
  100. _unBindTriggerEvent() {
  101. this._adapter.unregisterTriggerEvent();
  102. }
  103. _bindPortalEvent(portalEventSet: Record<string, any>) {
  104. this._adapter.registerPortalEvent(portalEventSet);
  105. }
  106. _unBindPortalEvent() {
  107. this._adapter.unregisterPortalEvent();
  108. }
  109. _bindResizeEvent() {
  110. this._adapter.registerResizeHandler(this.onResize);
  111. }
  112. _unBindResizeEvent() {
  113. this._adapter.unregisterResizeHandler(this.onResize);
  114. }
  115. _reversePos(position = '', isVertical = false) {
  116. if (isVertical) {
  117. if (REGS.TOP.test(position)) {
  118. return position.replace('top', 'bottom').replace('Top', 'Bottom');
  119. } else if (REGS.BOTTOM.test(position)) {
  120. return position.replace('bottom', 'top').replace('Bottom', 'Top');
  121. }
  122. } else if (REGS.LEFT.test(position)) {
  123. return position.replace('left', 'right').replace('Left', 'Right');
  124. } else if (REGS.RIGHT.test(position)) {
  125. return position.replace('right', 'left').replace('Right', 'Left');
  126. }
  127. return position;
  128. }
  129. clearDelayTimer() {
  130. if (this._timer) {
  131. clearTimeout(this._timer);
  132. this._timer = null;
  133. }
  134. }
  135. _generateEvent(types: ArrayElement<typeof strings.TRIGGER_SET>) {
  136. const eventNames = this._adapter.getEventName();
  137. const triggerEventSet = {};
  138. let portalEventSet = {};
  139. switch (types) {
  140. case 'focus':
  141. triggerEventSet[eventNames.focus] = () => {
  142. this.delayShow();
  143. };
  144. triggerEventSet[eventNames.blur] = () => {
  145. this.delayHide();
  146. };
  147. portalEventSet = triggerEventSet;
  148. break;
  149. case 'click':
  150. triggerEventSet[eventNames.click] = () => {
  151. // this.delayShow();
  152. this.show();
  153. };
  154. portalEventSet = {};
  155. // Click outside needs special treatment, can not be directly tied to the trigger Element, need to be bound to the document
  156. break;
  157. case 'hover':
  158. triggerEventSet[eventNames.mouseEnter] = () => {
  159. // console.log(e);
  160. this.setCache('isClickToHide', false);
  161. this.delayShow();
  162. // this.show('trigger');
  163. };
  164. triggerEventSet[eventNames.mouseLeave] = () => {
  165. // console.log(e);
  166. this.delayHide();
  167. // this.hide('trigger');
  168. };
  169. portalEventSet = { ...triggerEventSet };
  170. if (this.getProp('clickToHide')) {
  171. portalEventSet[eventNames.click] = () => {
  172. this.setCache('isClickToHide', true);
  173. this.hide();
  174. };
  175. portalEventSet[eventNames.mouseEnter] = () => {
  176. if (this.getCache('isClickToHide')) {
  177. return;
  178. }
  179. this.delayShow();
  180. };
  181. }
  182. break;
  183. case 'custom':
  184. // when trigger type is 'custom', no need to bind eventHandler
  185. // show/hide completely depond on props.visible which change by user
  186. break;
  187. default:
  188. break;
  189. }
  190. return { triggerEventSet, portalEventSet };
  191. }
  192. onResize = () => {
  193. // this.log('resize');
  194. // rePosition when window resize
  195. this.calcPosition();
  196. };
  197. _shouldShow() {
  198. const visible = this.getProp('visible');
  199. if (visible) {
  200. this.show();
  201. } else {
  202. // this.hide();
  203. }
  204. }
  205. delayShow = () => {
  206. const mouseEnterDelay: number = this.getProp('mouseEnterDelay');
  207. this.clearDelayTimer();
  208. if (mouseEnterDelay > 0) {
  209. this._timer = setTimeout(() => {
  210. this.show();
  211. this.clearDelayTimer();
  212. }, mouseEnterDelay);
  213. } else {
  214. this.show();
  215. }
  216. };
  217. show = () => {
  218. const content = this.getProp('content');
  219. const trigger = this.getProp('trigger');
  220. const clickTriggerToHide = this.getProp('clickTriggerToHide');
  221. this.clearDelayTimer();
  222. /**
  223. * If you emit an event in setState callback, you need to place the event listener function before setState to execute.
  224. * This is to avoid event registration being executed later than setState callback when setState is executed in setTimeout.
  225. * internal-issues:1402#note_38969412
  226. */
  227. this._adapter.on('portalInserted', () => {
  228. this.calcPosition();
  229. });
  230. this._adapter.on('positionUpdated', () => {
  231. this._togglePortalVisible(true);
  232. });
  233. const position = this.calcPosition(null, null, null, false);
  234. this._adapter.insertPortal(content, position);
  235. if (trigger === 'custom') {
  236. this._togglePortalVisible(true);
  237. }
  238. /**
  239. * trigger类型是click时,仅当portal被插入显示后,才绑定clickOutsideHandler
  240. * 因为handler需要绑定在document上。如果在constructor阶段绑定
  241. * 当一个页面中有多个容器实例时,一次click会触发多个容器的handler
  242. *
  243. * When the trigger type is click, clickOutsideHandler is bound only after the portal is inserted and displayed
  244. * Because the handler needs to be bound to the document. If you bind during the constructor phase
  245. * When there are multiple container instances in a page, one click triggers the handler of multiple containers
  246. */
  247. if (trigger === 'click' || clickTriggerToHide) {
  248. this._adapter.registerClickOutsideHandler(this.hide);
  249. }
  250. this._bindScrollEvent();
  251. this._bindResizeEvent();
  252. };
  253. _togglePortalVisible(isVisible: boolean) {
  254. const nowVisible = this.getState('visible');
  255. if (nowVisible !== isVisible) {
  256. this._adapter.togglePortalVisible(isVisible, () => this._adapter.notifyVisibleChange(isVisible));
  257. }
  258. }
  259. _roundPixel(pixel: number) {
  260. if (typeof pixel === 'number') {
  261. return Math.round(pixel);
  262. }
  263. return pixel;
  264. }
  265. calcTransformOrigin(position: Position, triggerRect: DOMRect, translateX: number, translateY: number) {
  266. // eslint-disable-next-line
  267. if (position && triggerRect && translateX != null && translateY != null) {
  268. if (this.getProp('transformFromCenter')) {
  269. if (['topLeft', 'bottomLeft'].includes(position)) {
  270. return `${this._roundPixel(triggerRect.width / 2)}px ${-translateY * 100}%`;
  271. }
  272. if (['topRight', 'bottomRight'].includes(position)) {
  273. return `calc(100% - ${this._roundPixel(triggerRect.width / 2)}px) ${-translateY * 100}%`;
  274. }
  275. if (['leftTop', 'rightTop'].includes(position)) {
  276. return `${-translateX * 100}% ${this._roundPixel(triggerRect.height / 2)}px`;
  277. }
  278. if (['leftBottom', 'rightBottom'].includes(position)) {
  279. return `${-translateX * 100}% calc(100% - ${this._roundPixel(triggerRect.height / 2)}px)`;
  280. }
  281. }
  282. return `${-translateX * 100}% ${-translateY * 100}%`;
  283. }
  284. return null;
  285. }
  286. calcPosStyle(triggerRect: DOMRect, wrapperRect: DOMRect, containerRect: PopupContainerDOMRect, position?: Position, spacing?: number) {
  287. triggerRect = (isEmpty(triggerRect) ? triggerRect : this._adapter.getTriggerBounding()) || { ...defaultRect as any };
  288. containerRect = (isEmpty(containerRect) ? containerRect : this._adapter.getPopupContainerRect()) || {
  289. ...defaultRect,
  290. };
  291. wrapperRect = (isEmpty(wrapperRect) ? wrapperRect : this._adapter.getWrapperBounding()) || { ...defaultRect as any };
  292. // eslint-disable-next-line
  293. position = position != null ? position : this.getProp('position');
  294. // eslint-disable-next-line
  295. const SPACING = spacing != null ? spacing : this.getProp('spacing');
  296. const { arrowPointAtCenter, showArrow, arrowBounding } = this.getProps();
  297. const pointAtCenter = showArrow && arrowPointAtCenter;
  298. const horizontalArrowWidth = get(arrowBounding, 'width', 24);
  299. const verticalArrowHeight = get(arrowBounding, 'width', 24);
  300. const arrowOffsetY = get(arrowBounding, 'offsetY', 0);
  301. const positionOffsetX = 6;
  302. const positionOffsetY = 6;
  303. // You must use left/top when rendering, using right/bottom does not render the element position correctly
  304. // Use left/top + translate to achieve tooltip positioning perfectly without knowing the size of the tooltip expansion layer
  305. let left;
  306. let top;
  307. let translateX = 0; // Container x-direction translation distance
  308. let translateY = 0; // Container y-direction translation distance
  309. const middleX = triggerRect.left + triggerRect.width / 2;
  310. const middleY = triggerRect.top + triggerRect.height / 2;
  311. const offsetXWithArrow = positionOffsetX + horizontalArrowWidth / 2;
  312. const offsetYWithArrow = positionOffsetY + verticalArrowHeight / 2;
  313. switch (position) {
  314. case 'top':
  315. left = middleX;
  316. top = triggerRect.top - SPACING;
  317. translateX = -0.5;
  318. translateY = -1;
  319. break;
  320. case 'topLeft':
  321. left = pointAtCenter ? middleX - offsetXWithArrow : triggerRect.left;
  322. top = triggerRect.top - SPACING;
  323. translateY = -1;
  324. break;
  325. case 'topRight':
  326. left = pointAtCenter ? middleX + offsetXWithArrow : triggerRect.right;
  327. top = triggerRect.top - SPACING;
  328. translateY = -1;
  329. translateX = -1;
  330. break;
  331. case 'left':
  332. left = triggerRect.left - SPACING;
  333. top = middleY;
  334. translateX = -1;
  335. translateY = -0.5;
  336. break;
  337. case 'leftTop':
  338. left = triggerRect.left - SPACING;
  339. top = pointAtCenter ? middleY - offsetYWithArrow : triggerRect.top;
  340. translateX = -1;
  341. break;
  342. case 'leftBottom':
  343. left = triggerRect.left - SPACING;
  344. top = pointAtCenter ? middleY + offsetYWithArrow : triggerRect.bottom;
  345. translateX = -1;
  346. translateY = -1;
  347. break;
  348. case 'bottom':
  349. left = middleX;
  350. top = triggerRect.top + triggerRect.height + SPACING;
  351. translateX = -0.5;
  352. break;
  353. case 'bottomLeft':
  354. left = pointAtCenter ? middleX - offsetXWithArrow : triggerRect.left;
  355. top = triggerRect.bottom + SPACING;
  356. break;
  357. case 'bottomRight':
  358. left = pointAtCenter ? middleX + offsetXWithArrow : triggerRect.right;
  359. top = triggerRect.bottom + SPACING;
  360. translateX = -1;
  361. break;
  362. case 'right':
  363. left = triggerRect.right + SPACING;
  364. top = middleY;
  365. translateY = -0.5;
  366. break;
  367. case 'rightTop':
  368. left = triggerRect.right + SPACING;
  369. top = pointAtCenter ? middleY - offsetYWithArrow : triggerRect.top;
  370. break;
  371. case 'rightBottom':
  372. left = triggerRect.right + SPACING;
  373. top = pointAtCenter ? middleY + offsetYWithArrow : triggerRect.bottom;
  374. translateY = -1;
  375. break;
  376. case 'leftTopOver':
  377. left = triggerRect.left;
  378. top = triggerRect.top;
  379. break;
  380. case 'rightTopOver':
  381. left = triggerRect.right;
  382. top = triggerRect.top;
  383. translateX = -1;
  384. break;
  385. default:
  386. break;
  387. }
  388. const transformOrigin = this.calcTransformOrigin(position, triggerRect, translateX, translateY); // Transform origin
  389. const _containerIsBody = this._adapter.containerIsBody();
  390. // Calculate container positioning relative to window
  391. left = left - containerRect.left;
  392. top = top - containerRect.top;
  393. /**
  394. * container为body时,如果position不为relative或absolute,这时trigger计算出的top/left会根据html定位(initial containing block)
  395. * 此时如果body有margin,则计算出的位置相对于body会有问题 fix issue #1368
  396. *
  397. * When container is body, if position is not relative or absolute, then the top/left calculated by trigger will be positioned according to html
  398. * At this time, if the body has a margin, the calculated position will have a problem relative to the body fix issue #1368
  399. */
  400. if (_containerIsBody && !this._adapter.containerIsRelativeOrAbsolute()) {
  401. const documentEleRect = this._adapter.getDocumentElementBounding();
  402. // Represents the left of the body relative to html
  403. left += containerRect.left - documentEleRect.left;
  404. // Represents the top of the body relative to html
  405. top += containerRect.top - documentEleRect.top;
  406. }
  407. // ContainerRect.scrollLeft to solve the inner scrolling of the container
  408. left = _containerIsBody ? left : left + containerRect.scrollLeft;
  409. top = _containerIsBody ? top : top + containerRect.scrollTop;
  410. const triggerHeight = triggerRect.height;
  411. if (
  412. this.getProp('showArrow') &&
  413. !arrowPointAtCenter &&
  414. triggerHeight <= (verticalArrowHeight / 2 + arrowOffsetY) * 2
  415. ) {
  416. const offsetY = triggerHeight / 2 - (arrowOffsetY + verticalArrowHeight / 2);
  417. if ((position.includes('Top') || position.includes('Bottom')) && !position.includes('Over')) {
  418. top = position.includes('Top') ? top + offsetY : top - offsetY;
  419. }
  420. }
  421. // The left/top value here must be rounded, otherwise it will cause the small triangle to shake
  422. const style: Record<string, string | number> = {
  423. left: this._roundPixel(left),
  424. top: this._roundPixel(top),
  425. };
  426. let transform = '';
  427. // eslint-disable-next-line
  428. if (translateX != null) {
  429. transform += `translateX(${translateX * 100}%) `;
  430. Object.defineProperty(style, 'translateX', {
  431. enumerable: false,
  432. value: translateX,
  433. });
  434. }
  435. // eslint-disable-next-line
  436. if (translateY != null) {
  437. transform += `translateY(${translateY * 100}%) `;
  438. Object.defineProperty(style, 'translateY', {
  439. enumerable: false,
  440. value: translateY,
  441. });
  442. }
  443. // eslint-disable-next-line
  444. if (transformOrigin != null) {
  445. style.transformOrigin = transformOrigin;
  446. }
  447. if (transform) {
  448. style.transform = transform;
  449. }
  450. return style;
  451. }
  452. /**
  453. * 耦合的东西比较多,稍微罗列一下:
  454. *
  455. * - 根据 trigger 和 wrapper 的 boundingClient 计算当前的 left、top、transform-origin
  456. * - 根据当前的 position 和 wrapper 的 boundingClient 决定是否需要自动调整位置
  457. * - 根据当前的 position、trigger 的 boundingClient 以及 motion.handleStyle 调整当前的 style
  458. *
  459. * There are many coupling things, a little list:
  460. *
  461. * - calculate the current left, top, and transfer-origin according to the boundingClient of trigger and wrapper
  462. * - decide whether to automatically adjust the position according to the current position and the boundingClient of wrapper
  463. * - adjust the current style according to the current position, the boundingClient of trigger and motion.handle Style
  464. */
  465. calcPosition = (triggerRect?: DOMRect, wrapperRect?: DOMRect, containerRect?: PopupContainerDOMRect, shouldUpdatePos = true) => {
  466. triggerRect = (isEmpty(triggerRect) ? this._adapter.getTriggerBounding() : triggerRect) || { ...defaultRect as any };
  467. containerRect = (isEmpty(containerRect) ? this._adapter.getPopupContainerRect() : containerRect) || {
  468. ...defaultRect,
  469. };
  470. wrapperRect = (isEmpty(wrapperRect) ? this._adapter.getWrapperBounding() : wrapperRect) || { ...defaultRect as any };
  471. // console.log('containerRect: ', containerRect, 'triggerRect: ', triggerRect, 'wrapperRect: ', wrapperRect);
  472. let style = this.calcPosStyle(triggerRect, wrapperRect, containerRect);
  473. let position = this.getProp('position');
  474. if (this.getProp('autoAdjustOverflow')) {
  475. // console.log('style: ', style, '\ntriggerRect: ', triggerRect, '\nwrapperRect: ', wrapperRect);
  476. const adjustedPos = this.adjustPosIfNeed(position, style, triggerRect, wrapperRect, containerRect);
  477. if (position !== adjustedPos) {
  478. position = adjustedPos;
  479. style = this.calcPosStyle(triggerRect, wrapperRect, containerRect, position);
  480. }
  481. }
  482. if (shouldUpdatePos && this._mounted) {
  483. // this._adapter.updatePlacementAttr(style.position);
  484. this._adapter.setPosition({ ...style, position });
  485. }
  486. return style;
  487. };
  488. isLR(position = '') {
  489. return position.indexOf('left') === 0 || position.indexOf('right') === 0;
  490. }
  491. isTB(position = '') {
  492. return position.indexOf('top') === 0 || position.indexOf('bottom') === 0;
  493. }
  494. // place the dom correctly
  495. adjustPosIfNeed(position: Position | string, style: Record<string, any>, triggerRect: DOMRect, wrapperRect: DOMRect, containerRect: PopupContainerDOMRect) {
  496. const { innerWidth, innerHeight } = window;
  497. if (wrapperRect.width > 0 && wrapperRect.height > 0) {
  498. // let clientLeft = left + translateX * wrapperRect.width - containerRect.scrollLeft;
  499. // let clientTop = top + translateY * wrapperRect.height - containerRect.scrollTop;
  500. // if (this._adapter.containerIsBody() || this._adapter.containerIsRelative()) {
  501. // clientLeft += containerRect.left;
  502. // clientTop += containerRect.top;
  503. // }
  504. // const clientRight = clientLeft + wrapperRect.width;
  505. // const clientBottom = clientTop + wrapperRect.height;
  506. // The relative position of the elements on the screen
  507. // https://lf3-static.bytednsdoc.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/tooltip-pic.svg
  508. const clientLeft = triggerRect.left;
  509. const clientRight = triggerRect.right;
  510. const clientTop = triggerRect.top;
  511. const clientBottom = triggerRect.bottom;
  512. const restClientLeft = innerWidth - clientLeft;
  513. const restClientTop = innerHeight - clientTop;
  514. const restClientRight = innerWidth - clientRight;
  515. const restClientBottom = innerHeight - clientBottom;
  516. const widthIsBigger = wrapperRect.width > triggerRect.width;
  517. const heightIsBigger = wrapperRect.height > triggerRect.height;
  518. // The wrapperR ect.top|bottom equivalent cannot be directly used here for comparison, which is easy to cause jitter
  519. const shouldReverseTop = clientTop < wrapperRect.height && restClientBottom > wrapperRect.height;
  520. const shouldReverseLeft = clientLeft < wrapperRect.width && restClientRight > wrapperRect.width;
  521. const sholdReverseBottom = restClientBottom < wrapperRect.height && clientTop > wrapperRect.height;
  522. const shouldReverseRight = restClientRight < wrapperRect.width && clientLeft > wrapperRect.width;
  523. const shouldReverseTopSide = restClientTop < wrapperRect.height && clientBottom > wrapperRect.height;
  524. const shouldReverseBottomSide = clientBottom < wrapperRect.height && restClientTop > wrapperRect.height;
  525. const shouldReverseLeftSide = restClientLeft < wrapperRect.width && clientRight > wrapperRect.width;
  526. const shouldReverseRightSide = clientRight < wrapperRect.width && restClientLeft > wrapperRect.width;
  527. switch (position) {
  528. case 'top':
  529. if (shouldReverseTop) {
  530. position = this._reversePos(position, true);
  531. }
  532. break;
  533. case 'topLeft':
  534. if (shouldReverseTop) {
  535. position = this._reversePos(position, true);
  536. }
  537. if (shouldReverseLeftSide && widthIsBigger) {
  538. position = this._reversePos(position);
  539. }
  540. break;
  541. case 'topRight':
  542. if (shouldReverseTop) {
  543. position = this._reversePos(position, true);
  544. }
  545. if (shouldReverseRightSide && widthIsBigger) {
  546. position = this._reversePos(position);
  547. }
  548. break;
  549. case 'left':
  550. if (shouldReverseLeft) {
  551. position = this._reversePos(position);
  552. }
  553. break;
  554. case 'leftTop':
  555. if (shouldReverseLeft) {
  556. position = this._reversePos(position);
  557. }
  558. if (shouldReverseTopSide && heightIsBigger) {
  559. position = this._reversePos(position, true);
  560. }
  561. break;
  562. case 'leftBottom':
  563. if (shouldReverseLeft) {
  564. position = this._reversePos(position);
  565. }
  566. if (shouldReverseBottomSide && heightIsBigger) {
  567. position = this._reversePos(position, true);
  568. }
  569. break;
  570. case 'bottom':
  571. if (sholdReverseBottom) {
  572. position = this._reversePos(position, true);
  573. }
  574. break;
  575. case 'bottomLeft':
  576. if (sholdReverseBottom) {
  577. position = this._reversePos(position, true);
  578. }
  579. if (shouldReverseLeftSide && widthIsBigger) {
  580. position = this._reversePos(position);
  581. }
  582. break;
  583. case 'bottomRight':
  584. if (sholdReverseBottom) {
  585. position = this._reversePos(position, true);
  586. }
  587. if (shouldReverseRightSide && widthIsBigger) {
  588. position = this._reversePos(position);
  589. }
  590. break;
  591. case 'right':
  592. if (shouldReverseRight) {
  593. position = this._reversePos(position);
  594. }
  595. break;
  596. case 'rightTop':
  597. if (shouldReverseRight) {
  598. position = this._reversePos(position);
  599. }
  600. if (shouldReverseTopSide && heightIsBigger) {
  601. position = this._reversePos(position, true);
  602. }
  603. break;
  604. case 'rightBottom':
  605. if (shouldReverseRight) {
  606. position = this._reversePos(position);
  607. }
  608. if (shouldReverseBottomSide && heightIsBigger) {
  609. position = this._reversePos(position, true);
  610. }
  611. break;
  612. default:
  613. break;
  614. }
  615. }
  616. return position;
  617. }
  618. delayHide = () => {
  619. const mouseLeaveDelay = this.getProp('mouseLeaveDelay');
  620. this.clearDelayTimer();
  621. if (mouseLeaveDelay > 0) {
  622. this._timer = setTimeout(() => {
  623. // console.log('delayHide for ', mouseLeaveDelay, ' ms, ', ...args);
  624. this.hide();
  625. this.clearDelayTimer();
  626. }, mouseLeaveDelay);
  627. } else {
  628. this.hide();
  629. }
  630. };
  631. hide = () => {
  632. this.clearDelayTimer();
  633. this._togglePortalVisible(false);
  634. this._adapter.off('portalInserted');
  635. this._adapter.off('positionUpdated');
  636. if (!this._adapter.canMotion()) {
  637. this._adapter.removePortal();
  638. // When the portal is removed, the global click outside event binding is also removed
  639. this._adapter.unregisterClickOutsideHandler();
  640. this._unBindScrollEvent();
  641. this._unBindResizeEvent();
  642. }
  643. };
  644. _bindScrollEvent() {
  645. this._adapter.registerScrollHandler(() => this.calcPosition());
  646. // Capture scroll events on the window to determine whether the current scrolling area (e.target) will affect the positioning of the pop-up layer relative to the viewport when scrolling
  647. // (By determining whether the e.target contains the triggerDom of the current tooltip) If so, the pop-up layer will also be affected and needs to be repositioned
  648. }
  649. _unBindScrollEvent() {
  650. this._adapter.unregisterScrollHandler();
  651. }
  652. _initContainerPosition() {
  653. this._adapter.updateContainerPosition();
  654. }
  655. }