Content.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  2. import _extends from "@babel/runtime/helpers/esm/extends";
  3. import { withDirectives as _withDirectives, vShow as _vShow, createVNode as _createVNode } from "vue";
  4. import { computed, ref, defineComponent, nextTick } from 'vue';
  5. import Transition, { getTransitionProps } from '../_util/transition';
  6. import dialogPropTypes from './IDialogPropTypes';
  7. import { offset } from './util';
  8. const sentinelStyle = {
  9. width: 0,
  10. height: 0,
  11. overflow: 'hidden',
  12. outline: 'none'
  13. };
  14. export default defineComponent({
  15. compatConfig: {
  16. MODE: 3
  17. },
  18. name: 'DialogContent',
  19. inheritAttrs: false,
  20. props: _extends(_extends({}, dialogPropTypes()), {
  21. motionName: String,
  22. ariaId: String,
  23. onVisibleChanged: Function,
  24. onMousedown: Function,
  25. onMouseup: Function
  26. }),
  27. setup(props, _ref) {
  28. let {
  29. expose,
  30. slots,
  31. attrs
  32. } = _ref;
  33. const sentinelStartRef = ref();
  34. const sentinelEndRef = ref();
  35. const dialogRef = ref();
  36. expose({
  37. focus: () => {
  38. var _a;
  39. (_a = sentinelStartRef.value) === null || _a === void 0 ? void 0 : _a.focus();
  40. },
  41. changeActive: next => {
  42. const {
  43. activeElement
  44. } = document;
  45. if (next && activeElement === sentinelEndRef.value) {
  46. sentinelStartRef.value.focus();
  47. } else if (!next && activeElement === sentinelStartRef.value) {
  48. sentinelEndRef.value.focus();
  49. }
  50. }
  51. });
  52. const transformOrigin = ref();
  53. const contentStyleRef = computed(() => {
  54. const {
  55. width,
  56. height
  57. } = props;
  58. const contentStyle = {};
  59. if (width !== undefined) {
  60. contentStyle.width = typeof width === 'number' ? `${width}px` : width;
  61. }
  62. if (height !== undefined) {
  63. contentStyle.height = typeof height === 'number' ? `${height}px` : height;
  64. }
  65. if (transformOrigin.value) {
  66. contentStyle.transformOrigin = transformOrigin.value;
  67. }
  68. return contentStyle;
  69. });
  70. const onPrepare = () => {
  71. nextTick(() => {
  72. if (dialogRef.value) {
  73. const elementOffset = offset(dialogRef.value);
  74. transformOrigin.value = props.mousePosition ? `${props.mousePosition.x - elementOffset.left}px ${props.mousePosition.y - elementOffset.top}px` : '';
  75. }
  76. });
  77. };
  78. const onVisibleChanged = visible => {
  79. props.onVisibleChanged(visible);
  80. };
  81. return () => {
  82. var _a, _b, _c, _d;
  83. const {
  84. prefixCls,
  85. footer = (_a = slots.footer) === null || _a === void 0 ? void 0 : _a.call(slots),
  86. title = (_b = slots.title) === null || _b === void 0 ? void 0 : _b.call(slots),
  87. ariaId,
  88. closable,
  89. closeIcon = (_c = slots.closeIcon) === null || _c === void 0 ? void 0 : _c.call(slots),
  90. onClose,
  91. bodyStyle,
  92. bodyProps,
  93. onMousedown,
  94. onMouseup,
  95. visible,
  96. modalRender = slots.modalRender,
  97. destroyOnClose,
  98. motionName
  99. } = props;
  100. let footerNode;
  101. if (footer) {
  102. footerNode = _createVNode("div", {
  103. "class": `${prefixCls}-footer`
  104. }, [footer]);
  105. }
  106. let headerNode;
  107. if (title) {
  108. headerNode = _createVNode("div", {
  109. "class": `${prefixCls}-header`
  110. }, [_createVNode("div", {
  111. "class": `${prefixCls}-title`,
  112. "id": ariaId
  113. }, [title])]);
  114. }
  115. let closer;
  116. if (closable) {
  117. closer = _createVNode("button", {
  118. "type": "button",
  119. "onClick": onClose,
  120. "aria-label": "Close",
  121. "class": `${prefixCls}-close`
  122. }, [closeIcon || _createVNode("span", {
  123. "class": `${prefixCls}-close-x`
  124. }, null)]);
  125. }
  126. const content = _createVNode("div", {
  127. "class": `${prefixCls}-content`
  128. }, [closer, headerNode, _createVNode("div", _objectSpread({
  129. "class": `${prefixCls}-body`,
  130. "style": bodyStyle
  131. }, bodyProps), [(_d = slots.default) === null || _d === void 0 ? void 0 : _d.call(slots)]), footerNode]);
  132. const transitionProps = getTransitionProps(motionName);
  133. return _createVNode(Transition, _objectSpread(_objectSpread({}, transitionProps), {}, {
  134. "onBeforeEnter": onPrepare,
  135. "onAfterEnter": () => onVisibleChanged(true),
  136. "onAfterLeave": () => onVisibleChanged(false)
  137. }), {
  138. default: () => [visible || !destroyOnClose ? _withDirectives(_createVNode("div", _objectSpread(_objectSpread({}, attrs), {}, {
  139. "ref": dialogRef,
  140. "key": "dialog-element",
  141. "role": "document",
  142. "style": [contentStyleRef.value, attrs.style],
  143. "class": [prefixCls, attrs.class],
  144. "onMousedown": onMousedown,
  145. "onMouseup": onMouseup
  146. }), [_createVNode("div", {
  147. "tabindex": 0,
  148. "ref": sentinelStartRef,
  149. "style": sentinelStyle,
  150. "aria-hidden": "true"
  151. }, null), modalRender ? modalRender({
  152. originVNode: content
  153. }) : content, _createVNode("div", {
  154. "tabindex": 0,
  155. "ref": sentinelEndRef,
  156. "style": sentinelStyle,
  157. "aria-hidden": "true"
  158. }, null)]), [[_vShow, visible]]) : null]
  159. });
  160. };
  161. }
  162. });