Popover.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name2 in all)
  7. __defProp(target, name2, { get: all[name2], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var stdin_exports = {};
  19. __export(stdin_exports, {
  20. default: () => stdin_default,
  21. popoverProps: () => popoverProps
  22. });
  23. module.exports = __toCommonJS(stdin_exports);
  24. var import_vue = require("vue");
  25. var import_vue2 = require("vue");
  26. var import_popperjs = require("@vant/popperjs");
  27. var import_utils = require("../utils");
  28. var import_use = require("@vant/use");
  29. var import_use_sync_prop_ref = require("../composables/use-sync-prop-ref");
  30. var import_icon = require("../icon");
  31. var import_popup = require("../popup");
  32. const [name, bem] = (0, import_utils.createNamespace)("popover");
  33. const popupProps = ["overlay", "duration", "teleport", "overlayStyle", "overlayClass", "closeOnClickOverlay"];
  34. const popoverProps = {
  35. show: Boolean,
  36. theme: (0, import_utils.makeStringProp)("light"),
  37. overlay: Boolean,
  38. actions: (0, import_utils.makeArrayProp)(),
  39. trigger: (0, import_utils.makeStringProp)("click"),
  40. duration: import_utils.numericProp,
  41. showArrow: import_utils.truthProp,
  42. placement: (0, import_utils.makeStringProp)("bottom"),
  43. iconPrefix: String,
  44. overlayClass: import_utils.unknownProp,
  45. overlayStyle: Object,
  46. closeOnClickAction: import_utils.truthProp,
  47. closeOnClickOverlay: import_utils.truthProp,
  48. closeOnClickOutside: import_utils.truthProp,
  49. offset: {
  50. type: Array,
  51. default: () => [0, 8]
  52. },
  53. teleport: {
  54. type: [String, Object],
  55. default: "body"
  56. }
  57. };
  58. var stdin_default = (0, import_vue2.defineComponent)({
  59. name,
  60. props: popoverProps,
  61. emits: ["select", "touchstart", "update:show"],
  62. setup(props, {
  63. emit,
  64. slots,
  65. attrs
  66. }) {
  67. let popper;
  68. const popupRef = (0, import_vue2.ref)();
  69. const wrapperRef = (0, import_vue2.ref)();
  70. const popoverRef = (0, import_vue2.ref)();
  71. const show = (0, import_use_sync_prop_ref.useSyncPropRef)(() => props.show, (value) => emit("update:show", value));
  72. const getPopoverOptions = () => ({
  73. placement: props.placement,
  74. modifiers: [{
  75. name: "computeStyles",
  76. options: {
  77. adaptive: false,
  78. gpuAcceleration: false
  79. }
  80. }, (0, import_utils.extend)({}, import_popperjs.offsetModifier, {
  81. options: {
  82. offset: props.offset
  83. }
  84. })]
  85. });
  86. const createPopperInstance = () => {
  87. if (wrapperRef.value && popoverRef.value) {
  88. return (0, import_popperjs.createPopper)(wrapperRef.value, popoverRef.value.popupRef.value, getPopoverOptions());
  89. }
  90. return null;
  91. };
  92. const updateLocation = () => {
  93. (0, import_vue2.nextTick)(() => {
  94. if (!show.value) {
  95. return;
  96. }
  97. if (!popper) {
  98. popper = createPopperInstance();
  99. } else {
  100. popper.setOptions(getPopoverOptions());
  101. }
  102. });
  103. };
  104. const updateShow = (value) => {
  105. show.value = value;
  106. };
  107. const onClickWrapper = () => {
  108. if (props.trigger === "click") {
  109. show.value = !show.value;
  110. }
  111. };
  112. const onClickAction = (action, index) => {
  113. if (action.disabled) {
  114. return;
  115. }
  116. emit("select", action, index);
  117. if (props.closeOnClickAction) {
  118. show.value = false;
  119. }
  120. };
  121. const onClickAway = () => {
  122. if (show.value && props.closeOnClickOutside && (!props.overlay || props.closeOnClickOverlay)) {
  123. show.value = false;
  124. }
  125. };
  126. const renderActionContent = (action, index) => {
  127. if (slots.action) {
  128. return slots.action({
  129. action,
  130. index
  131. });
  132. }
  133. return [action.icon && (0, import_vue.createVNode)(import_icon.Icon, {
  134. "name": action.icon,
  135. "classPrefix": props.iconPrefix,
  136. "class": bem("action-icon")
  137. }, null), (0, import_vue.createVNode)("div", {
  138. "class": [bem("action-text"), import_utils.BORDER_BOTTOM]
  139. }, [action.text])];
  140. };
  141. const renderAction = (action, index) => {
  142. const {
  143. icon,
  144. color,
  145. disabled,
  146. className
  147. } = action;
  148. return (0, import_vue.createVNode)("div", {
  149. "role": "menuitem",
  150. "class": [bem("action", {
  151. disabled,
  152. "with-icon": icon
  153. }), className],
  154. "style": {
  155. color
  156. },
  157. "tabindex": disabled ? void 0 : 0,
  158. "aria-disabled": disabled || void 0,
  159. "onClick": () => onClickAction(action, index)
  160. }, [renderActionContent(action, index)]);
  161. };
  162. (0, import_vue2.onMounted)(() => {
  163. updateLocation();
  164. (0, import_vue2.watchEffect)(() => {
  165. var _a;
  166. popupRef.value = (_a = popoverRef.value) == null ? void 0 : _a.popupRef.value;
  167. });
  168. });
  169. (0, import_vue2.onBeforeUnmount)(() => {
  170. if (popper) {
  171. popper.destroy();
  172. popper = null;
  173. }
  174. });
  175. (0, import_vue2.watch)(() => [show.value, props.offset, props.placement], updateLocation);
  176. (0, import_use.useClickAway)([wrapperRef, popupRef], onClickAway, {
  177. eventName: "touchstart"
  178. });
  179. return () => {
  180. var _a;
  181. return (0, import_vue.createVNode)(import_vue.Fragment, null, [(0, import_vue.createVNode)("span", {
  182. "ref": wrapperRef,
  183. "class": bem("wrapper"),
  184. "onClick": onClickWrapper
  185. }, [(_a = slots.reference) == null ? void 0 : _a.call(slots)]), (0, import_vue.createVNode)(import_popup.Popup, (0, import_vue.mergeProps)({
  186. "ref": popoverRef,
  187. "show": show.value,
  188. "class": bem([props.theme]),
  189. "position": "",
  190. "transition": "van-popover-zoom",
  191. "lockScroll": false,
  192. "onUpdate:show": updateShow
  193. }, attrs, (0, import_utils.pick)(props, popupProps)), {
  194. default: () => [props.showArrow && (0, import_vue.createVNode)("div", {
  195. "class": bem("arrow")
  196. }, null), (0, import_vue.createVNode)("div", {
  197. "role": "menu",
  198. "class": bem("content")
  199. }, [slots.default ? slots.default() : props.actions.map(renderAction)])]
  200. })]);
  201. };
  202. }
  203. });