DropdownMenu.js 4.7 KB

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