DropdownMenu.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. DROPDOWN_KEY: () => DROPDOWN_KEY,
  21. default: () => stdin_default
  22. });
  23. module.exports = __toCommonJS(stdin_exports);
  24. var import_vue = require("vue");
  25. var import_vue2 = require("vue");
  26. var import_utils = require("../utils");
  27. var import_use_id = require("../composables/use-id");
  28. var import_use = require("@vant/use");
  29. const [name, bem] = (0, import_utils.createNamespace)("dropdown-menu");
  30. const dropdownMenuProps = {
  31. overlay: import_utils.truthProp,
  32. zIndex: import_utils.numericProp,
  33. duration: (0, import_utils.makeNumericProp)(0.2),
  34. direction: (0, import_utils.makeStringProp)("down"),
  35. activeColor: String,
  36. closeOnClickOutside: import_utils.truthProp,
  37. closeOnClickOverlay: import_utils.truthProp
  38. };
  39. const DROPDOWN_KEY = Symbol(name);
  40. var stdin_default = (0, import_vue2.defineComponent)({
  41. name,
  42. props: dropdownMenuProps,
  43. setup(props, {
  44. slots
  45. }) {
  46. const id = (0, import_use_id.useId)();
  47. const root = (0, import_vue2.ref)();
  48. const barRef = (0, import_vue2.ref)();
  49. const offset = (0, import_vue2.ref)(0);
  50. const {
  51. children,
  52. linkChildren
  53. } = (0, import_use.useChildren)(DROPDOWN_KEY);
  54. const scrollParent = (0, import_use.useScrollParent)(root);
  55. const opened = (0, import_vue2.computed)(() => children.some((item) => item.state.showWrapper));
  56. const barStyle = (0, import_vue2.computed)(() => {
  57. if (opened.value && (0, import_utils.isDef)(props.zIndex)) {
  58. return {
  59. zIndex: +props.zIndex + 1
  60. };
  61. }
  62. });
  63. const onClickAway = () => {
  64. if (props.closeOnClickOutside) {
  65. children.forEach((item) => {
  66. item.toggle(false);
  67. });
  68. }
  69. };
  70. const updateOffset = () => {
  71. if (barRef.value) {
  72. const rect = (0, import_use.useRect)(barRef);
  73. if (props.direction === "down") {
  74. offset.value = rect.bottom;
  75. } else {
  76. offset.value = import_utils.windowHeight.value - rect.top;
  77. }
  78. }
  79. };
  80. const onScroll = () => {
  81. if (opened.value) {
  82. updateOffset();
  83. }
  84. };
  85. const toggleItem = (active) => {
  86. children.forEach((item, index) => {
  87. if (index === active) {
  88. updateOffset();
  89. item.toggle();
  90. } else if (item.state.showPopup) {
  91. item.toggle(false, {
  92. immediate: true
  93. });
  94. }
  95. });
  96. };
  97. const renderTitle = (item, index) => {
  98. const {
  99. showPopup
  100. } = item.state;
  101. const {
  102. disabled,
  103. titleClass
  104. } = item;
  105. return (0, import_vue.createVNode)("div", {
  106. "id": `${id}-${index}`,
  107. "role": "button",
  108. "tabindex": disabled ? void 0 : 0,
  109. "class": [bem("item", {
  110. disabled
  111. }), {
  112. [import_utils.HAPTICS_FEEDBACK]: !disabled
  113. }],
  114. "onClick": () => {
  115. if (!disabled) {
  116. toggleItem(index);
  117. }
  118. }
  119. }, [(0, import_vue.createVNode)("span", {
  120. "class": [bem("title", {
  121. down: showPopup === (props.direction === "down"),
  122. active: showPopup
  123. }), titleClass],
  124. "style": {
  125. color: showPopup ? props.activeColor : ""
  126. }
  127. }, [(0, import_vue.createVNode)("div", {
  128. "class": "van-ellipsis"
  129. }, [item.renderTitle()])])]);
  130. };
  131. linkChildren({
  132. id,
  133. props,
  134. offset
  135. });
  136. (0, import_use.useClickAway)(root, onClickAway);
  137. (0, import_use.useEventListener)("scroll", onScroll, {
  138. target: scrollParent,
  139. passive: true
  140. });
  141. return () => {
  142. var _a;
  143. return (0, import_vue.createVNode)("div", {
  144. "ref": root,
  145. "class": bem()
  146. }, [(0, import_vue.createVNode)("div", {
  147. "ref": barRef,
  148. "style": barStyle.value,
  149. "class": bem("bar", {
  150. opened: opened.value
  151. })
  152. }, [children.map(renderTitle)]), (_a = slots.default) == null ? void 0 : _a.call(slots)]);
  153. };
  154. }
  155. });