UNPKG

5.88 kBJavaScriptView Raw
1import _regeneratorRuntime from "@babel/runtime/regenerator";
2import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
3import _extends from "@babel/runtime/helpers/extends";
4import _objectSpread from "@babel/runtime/helpers/objectSpread2";
5import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
6import React, { createRef, forwardRef, useCallback, useImperativeHandle, useMemo, useState } from 'react';
7import { render } from 'react-dom';
8import { getTooltipTheme } from '../../../es/_util/TooltipUtils';
9import { getProPrefixCls } from '../../../es/configure';
10import { getGlobalPopupContainer } from '../../../es/trigger/Popup';
11import Tooltip from './Tooltip';
12import TaskRunner from '../_util/TaskRunner';
13export var suffixCls = 'singleton-tooltip';
14var manager = {};
15var TooltipContainer = forwardRef(function (_, ref) {
16 var task = useMemo(function () {
17 return new TaskRunner();
18 }, []);
19
20 var _useState = useState(),
21 _useState2 = _slicedToArray(_useState, 2),
22 tooltipProps = _useState2[0],
23 setTooltipProps = _useState2[1];
24
25 var open = useCallback(function (target, args) {
26 var duration = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 100;
27 task.cancel();
28 task.delay(duration, function () {
29 setTooltipProps(_objectSpread({
30 theme: getTooltipTheme()
31 }, args, {
32 hidden: false,
33 getRootDomNode: function getRootDomNode() {
34 return target;
35 }
36 }));
37 });
38 return task;
39 }, [task]);
40 var close = useCallback(function () {
41 var duration = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 100;
42 task.cancel();
43 task.delay(duration, function () {
44 setTooltipProps(function (preState) {
45 return _objectSpread({}, preState, {
46 hidden: true
47 });
48 });
49 });
50 return task;
51 }, [task]);
52 var handlePopupMouseEnter = useCallback(function () {
53 task.cancel();
54 }, [task]);
55 var handlePopupMouseLeave = useCallback(function () {
56 close();
57 }, [close]);
58 useImperativeHandle(ref, function () {
59 return {
60 open: open,
61 close: close
62 };
63 }, [open, close]);
64 return React.createElement(Tooltip, _extends({}, tooltipProps, {
65 onPopupMouseEnter: handlePopupMouseEnter,
66 onPopupMouseLeave: handlePopupMouseLeave
67 }));
68});
69
70function getRoot() {
71 var root = manager.root;
72
73 if (typeof window !== 'undefined') {
74 var popupContainer = getGlobalPopupContainer();
75 var doc = window.document;
76
77 if (root) {
78 if (!root.parentNode) {
79 popupContainer.appendChild(root);
80 }
81 } else {
82 root = doc.createElement('div');
83 root.className = "".concat(getProPrefixCls(suffixCls), "-container");
84 popupContainer.appendChild(root);
85 manager.root = root;
86 }
87 }
88
89 return root;
90}
91
92function getContainer() {
93 return _getContainer.apply(this, arguments);
94}
95
96function _getContainer() {
97 _getContainer = _asyncToGenerator(
98 /*#__PURE__*/
99 _regeneratorRuntime.mark(function _callee() {
100 var container;
101 return _regeneratorRuntime.wrap(function _callee$(_context) {
102 while (1) {
103 switch (_context.prev = _context.next) {
104 case 0:
105 container = manager.container;
106
107 if (!container) {
108 _context.next = 3;
109 break;
110 }
111
112 return _context.abrupt("return", container);
113
114 case 3:
115 return _context.abrupt("return", new Promise(function (resolve) {
116 var root = getRoot();
117
118 if (root) {
119 var ref = createRef();
120 manager.container = ref;
121 render(React.createElement(TooltipContainer, {
122 ref: ref
123 }), root, function () {
124 return resolve(ref);
125 });
126 }
127 }));
128
129 case 4:
130 case "end":
131 return _context.stop();
132 }
133 }
134 }, _callee);
135 }));
136 return _getContainer.apply(this, arguments);
137}
138
139export function show(_x, _x2, _x3) {
140 return _show.apply(this, arguments);
141}
142
143function _show() {
144 _show = _asyncToGenerator(
145 /*#__PURE__*/
146 _regeneratorRuntime.mark(function _callee2(target, props, duration) {
147 var container, current;
148 return _regeneratorRuntime.wrap(function _callee2$(_context2) {
149 while (1) {
150 switch (_context2.prev = _context2.next) {
151 case 0:
152 _context2.next = 2;
153 return getContainer();
154
155 case 2:
156 container = _context2.sent;
157 current = container.current;
158
159 if (!current) {
160 _context2.next = 6;
161 break;
162 }
163
164 return _context2.abrupt("return", current.open(target, props, duration));
165
166 case 6:
167 case "end":
168 return _context2.stop();
169 }
170 }
171 }, _callee2);
172 }));
173 return _show.apply(this, arguments);
174}
175
176export function hide(_x4) {
177 return _hide.apply(this, arguments);
178}
179
180function _hide() {
181 _hide = _asyncToGenerator(
182 /*#__PURE__*/
183 _regeneratorRuntime.mark(function _callee3(duration) {
184 var container, current;
185 return _regeneratorRuntime.wrap(function _callee3$(_context3) {
186 while (1) {
187 switch (_context3.prev = _context3.next) {
188 case 0:
189 _context3.next = 2;
190 return getContainer();
191
192 case 2:
193 container = _context3.sent;
194 current = container.current;
195
196 if (!current) {
197 _context3.next = 6;
198 break;
199 }
200
201 return _context3.abrupt("return", current.close(duration));
202
203 case 6:
204 case "end":
205 return _context3.stop();
206 }
207 }
208 }, _callee3);
209 }));
210 return _hide.apply(this, arguments);
211}
212//# sourceMappingURL=singleton.js.map