Tooltip.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import _extends from "@babel/runtime/helpers/esm/extends";
  2. import { resolveDirective as _resolveDirective, createVNode as _createVNode } from "vue";
  3. import { computed, watch, defineComponent, ref } from 'vue';
  4. import VcTooltip from '../vc-tooltip';
  5. import classNames from '../_util/classNames';
  6. import PropTypes from '../_util/vue-types';
  7. import warning from '../_util/warning';
  8. import { getStyle, filterEmpty, isValidElement, initDefaultProps, isFragment } from '../_util/props-util';
  9. import { cloneElement } from '../_util/vnode';
  10. import abstractTooltipProps from './abstractTooltipProps';
  11. import useConfigInject from '../config-provider/hooks/useConfigInject';
  12. import getPlacements from '../_util/placements';
  13. import firstNotUndefined from '../_util/firstNotUndefined';
  14. import raf from '../_util/raf';
  15. import { parseColor } from './util';
  16. import useStyle from './style';
  17. import { getTransitionName } from '../_util/transition';
  18. const splitObject = (obj, keys) => {
  19. const picked = {};
  20. const omitted = _extends({}, obj);
  21. keys.forEach(key => {
  22. if (obj && key in obj) {
  23. picked[key] = obj[key];
  24. delete omitted[key];
  25. }
  26. });
  27. return {
  28. picked,
  29. omitted
  30. };
  31. };
  32. export const tooltipProps = () => _extends(_extends({}, abstractTooltipProps()), {
  33. title: PropTypes.any
  34. });
  35. export const tooltipDefaultProps = () => ({
  36. trigger: 'hover',
  37. align: {},
  38. placement: 'top',
  39. mouseEnterDelay: 0.1,
  40. mouseLeaveDelay: 0.1,
  41. arrowPointAtCenter: false,
  42. autoAdjustOverflow: true
  43. });
  44. export default defineComponent({
  45. compatConfig: {
  46. MODE: 3
  47. },
  48. name: 'ATooltip',
  49. inheritAttrs: false,
  50. props: initDefaultProps(tooltipProps(), {
  51. trigger: 'hover',
  52. align: {},
  53. placement: 'top',
  54. mouseEnterDelay: 0.1,
  55. mouseLeaveDelay: 0.1,
  56. arrowPointAtCenter: false,
  57. autoAdjustOverflow: true
  58. }),
  59. slots: Object,
  60. // emits: ['update:visible', 'visibleChange'],
  61. setup(props, _ref) {
  62. let {
  63. slots,
  64. emit,
  65. attrs,
  66. expose
  67. } = _ref;
  68. if (process.env.NODE_ENV !== 'production') {
  69. [['visible', 'open'], ['onVisibleChange', 'onOpenChange']].forEach(_ref2 => {
  70. let [deprecatedName, newName] = _ref2;
  71. warning(props[deprecatedName] === undefined, 'Tooltip', `\`${deprecatedName}\` is deprecated, please use \`${newName}\` instead.`);
  72. });
  73. }
  74. const {
  75. prefixCls,
  76. getPopupContainer,
  77. direction,
  78. rootPrefixCls
  79. } = useConfigInject('tooltip', props);
  80. const mergedOpen = computed(() => {
  81. var _a;
  82. return (_a = props.open) !== null && _a !== void 0 ? _a : props.visible;
  83. });
  84. const innerOpen = ref(firstNotUndefined([props.open, props.visible]));
  85. const tooltip = ref();
  86. let rafId;
  87. watch(mergedOpen, val => {
  88. raf.cancel(rafId);
  89. rafId = raf(() => {
  90. innerOpen.value = !!val;
  91. });
  92. });
  93. const isNoTitle = () => {
  94. var _a;
  95. const title = (_a = props.title) !== null && _a !== void 0 ? _a : slots.title;
  96. return !title && title !== 0;
  97. };
  98. const handleVisibleChange = val => {
  99. const noTitle = isNoTitle();
  100. if (mergedOpen.value === undefined) {
  101. innerOpen.value = noTitle ? false : val;
  102. }
  103. if (!noTitle) {
  104. emit('update:visible', val);
  105. emit('visibleChange', val);
  106. emit('update:open', val);
  107. emit('openChange', val);
  108. }
  109. };
  110. const getPopupDomNode = () => {
  111. return tooltip.value.getPopupDomNode();
  112. };
  113. expose({
  114. getPopupDomNode,
  115. open: innerOpen,
  116. forcePopupAlign: () => {
  117. var _a;
  118. return (_a = tooltip.value) === null || _a === void 0 ? void 0 : _a.forcePopupAlign();
  119. }
  120. });
  121. const tooltipPlacements = computed(() => {
  122. var _a;
  123. const {
  124. builtinPlacements,
  125. autoAdjustOverflow,
  126. arrow,
  127. arrowPointAtCenter
  128. } = props;
  129. let mergedArrowPointAtCenter = arrowPointAtCenter;
  130. if (typeof arrow === 'object') {
  131. mergedArrowPointAtCenter = (_a = arrow.pointAtCenter) !== null && _a !== void 0 ? _a : arrowPointAtCenter;
  132. }
  133. return builtinPlacements || getPlacements({
  134. arrowPointAtCenter: mergedArrowPointAtCenter,
  135. autoAdjustOverflow
  136. });
  137. });
  138. const isTrueProps = val => {
  139. return val || val === '';
  140. };
  141. const getDisabledCompatibleChildren = ele => {
  142. const elementType = ele.type;
  143. if (typeof elementType === 'object' && ele.props) {
  144. if ((elementType.__ANT_BUTTON === true || elementType === 'button') && isTrueProps(ele.props.disabled) || elementType.__ANT_SWITCH === true && (isTrueProps(ele.props.disabled) || isTrueProps(ele.props.loading)) || elementType.__ANT_RADIO === true && isTrueProps(ele.props.disabled)) {
  145. // Pick some layout related style properties up to span
  146. // Prevent layout bugs like https://github.com/ant-design/ant-design/issues/5254
  147. const {
  148. picked,
  149. omitted
  150. } = splitObject(getStyle(ele), ['position', 'left', 'right', 'top', 'bottom', 'float', 'display', 'zIndex']);
  151. const spanStyle = _extends(_extends({
  152. display: 'inline-block'
  153. }, picked), {
  154. cursor: 'not-allowed',
  155. lineHeight: 1,
  156. width: ele.props && ele.props.block ? '100%' : undefined
  157. });
  158. const buttonStyle = _extends(_extends({}, omitted), {
  159. pointerEvents: 'none'
  160. });
  161. const child = cloneElement(ele, {
  162. style: buttonStyle
  163. }, true);
  164. return _createVNode("span", {
  165. "style": spanStyle,
  166. "class": `${prefixCls.value}-disabled-compatible-wrapper`
  167. }, [child]);
  168. }
  169. }
  170. return ele;
  171. };
  172. const getOverlay = () => {
  173. var _a, _b;
  174. return (_a = props.title) !== null && _a !== void 0 ? _a : (_b = slots.title) === null || _b === void 0 ? void 0 : _b.call(slots);
  175. };
  176. const onPopupAlign = (domNode, align) => {
  177. const placements = tooltipPlacements.value;
  178. // 当前返回的位置
  179. const placement = Object.keys(placements).find(key => {
  180. var _a, _b;
  181. return placements[key].points[0] === ((_a = align.points) === null || _a === void 0 ? void 0 : _a[0]) && placements[key].points[1] === ((_b = align.points) === null || _b === void 0 ? void 0 : _b[1]);
  182. });
  183. if (placement) {
  184. // 根据当前坐标设置动画点
  185. const rect = domNode.getBoundingClientRect();
  186. const transformOrigin = {
  187. top: '50%',
  188. left: '50%'
  189. };
  190. if (placement.indexOf('top') >= 0 || placement.indexOf('Bottom') >= 0) {
  191. transformOrigin.top = `${rect.height - align.offset[1]}px`;
  192. } else if (placement.indexOf('Top') >= 0 || placement.indexOf('bottom') >= 0) {
  193. transformOrigin.top = `${-align.offset[1]}px`;
  194. }
  195. if (placement.indexOf('left') >= 0 || placement.indexOf('Right') >= 0) {
  196. transformOrigin.left = `${rect.width - align.offset[0]}px`;
  197. } else if (placement.indexOf('right') >= 0 || placement.indexOf('Left') >= 0) {
  198. transformOrigin.left = `${-align.offset[0]}px`;
  199. }
  200. domNode.style.transformOrigin = `${transformOrigin.left} ${transformOrigin.top}`;
  201. }
  202. };
  203. const colorInfo = computed(() => parseColor(prefixCls.value, props.color));
  204. const injectFromPopover = computed(() => attrs['data-popover-inject']);
  205. const [wrapSSR, hashId] = useStyle(prefixCls, computed(() => !injectFromPopover.value));
  206. return () => {
  207. var _a, _b;
  208. const {
  209. openClassName,
  210. overlayClassName,
  211. overlayStyle,
  212. overlayInnerStyle
  213. } = props;
  214. let children = (_b = filterEmpty((_a = slots.default) === null || _a === void 0 ? void 0 : _a.call(slots))) !== null && _b !== void 0 ? _b : null;
  215. children = children.length === 1 ? children[0] : children;
  216. let tempVisible = innerOpen.value;
  217. // Hide tooltip when there is no title
  218. if (mergedOpen.value === undefined && isNoTitle()) {
  219. tempVisible = false;
  220. }
  221. if (!children) {
  222. return null;
  223. }
  224. const child = getDisabledCompatibleChildren(isValidElement(children) && !isFragment(children) ? children : _createVNode("span", null, [children]));
  225. const childCls = classNames({
  226. [openClassName || `${prefixCls.value}-open`]: true,
  227. [child.props && child.props.class]: child.props && child.props.class
  228. });
  229. const customOverlayClassName = classNames(overlayClassName, {
  230. [`${prefixCls.value}-rtl`]: direction.value === 'rtl'
  231. }, colorInfo.value.className, hashId.value);
  232. const formattedOverlayInnerStyle = _extends(_extends({}, colorInfo.value.overlayStyle), overlayInnerStyle);
  233. const arrowContentStyle = colorInfo.value.arrowStyle;
  234. const vcTooltipProps = _extends(_extends(_extends({}, attrs), props), {
  235. prefixCls: prefixCls.value,
  236. arrow: !!props.arrow,
  237. getPopupContainer: getPopupContainer === null || getPopupContainer === void 0 ? void 0 : getPopupContainer.value,
  238. builtinPlacements: tooltipPlacements.value,
  239. visible: tempVisible,
  240. ref: tooltip,
  241. overlayClassName: customOverlayClassName,
  242. overlayStyle: _extends(_extends({}, arrowContentStyle), overlayStyle),
  243. overlayInnerStyle: formattedOverlayInnerStyle,
  244. onVisibleChange: handleVisibleChange,
  245. onPopupAlign,
  246. transitionName: getTransitionName(rootPrefixCls.value, 'zoom-big-fast', props.transitionName)
  247. });
  248. return wrapSSR(_createVNode(VcTooltip, vcTooltipProps, {
  249. default: () => [innerOpen.value ? cloneElement(child, {
  250. class: childCls
  251. }) : child],
  252. arrowContent: () => _createVNode("span", {
  253. "class": `${prefixCls.value}-arrow-content`
  254. }, null),
  255. overlay: getOverlay
  256. }));
  257. };
  258. }
  259. });