NoticeBar.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. noticeBarProps: () => noticeBarProps
  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 = require("@vant/use");
  28. var import_use_expose = require("../composables/use-expose");
  29. var import_on_popup_reopen = require("../composables/on-popup-reopen");
  30. var import_icon = require("../icon");
  31. const [name, bem] = (0, import_utils.createNamespace)("notice-bar");
  32. const noticeBarProps = {
  33. text: String,
  34. mode: String,
  35. color: String,
  36. delay: (0, import_utils.makeNumericProp)(1),
  37. speed: (0, import_utils.makeNumericProp)(60),
  38. leftIcon: String,
  39. wrapable: Boolean,
  40. background: String,
  41. scrollable: {
  42. type: Boolean,
  43. default: null
  44. }
  45. };
  46. var stdin_default = (0, import_vue2.defineComponent)({
  47. name,
  48. props: noticeBarProps,
  49. emits: ["close", "replay"],
  50. setup(props, {
  51. emit,
  52. slots
  53. }) {
  54. let wrapWidth = 0;
  55. let contentWidth = 0;
  56. let startTimer;
  57. const wrapRef = (0, import_vue2.ref)();
  58. const contentRef = (0, import_vue2.ref)();
  59. const state = (0, import_vue2.reactive)({
  60. show: true,
  61. offset: 0,
  62. duration: 0
  63. });
  64. const renderLeftIcon = () => {
  65. if (slots["left-icon"]) {
  66. return slots["left-icon"]();
  67. }
  68. if (props.leftIcon) {
  69. return (0, import_vue.createVNode)(import_icon.Icon, {
  70. "class": bem("left-icon"),
  71. "name": props.leftIcon
  72. }, null);
  73. }
  74. };
  75. const getRightIconName = () => {
  76. if (props.mode === "closeable") {
  77. return "cross";
  78. }
  79. if (props.mode === "link") {
  80. return "arrow";
  81. }
  82. };
  83. const onClickRightIcon = (event) => {
  84. if (props.mode === "closeable") {
  85. state.show = false;
  86. emit("close", event);
  87. }
  88. };
  89. const renderRightIcon = () => {
  90. if (slots["right-icon"]) {
  91. return slots["right-icon"]();
  92. }
  93. const name2 = getRightIconName();
  94. if (name2) {
  95. return (0, import_vue.createVNode)(import_icon.Icon, {
  96. "name": name2,
  97. "class": bem("right-icon"),
  98. "onClick": onClickRightIcon
  99. }, null);
  100. }
  101. };
  102. const onTransitionEnd = () => {
  103. state.offset = wrapWidth;
  104. state.duration = 0;
  105. (0, import_use.raf)(() => {
  106. (0, import_use.doubleRaf)(() => {
  107. state.offset = -contentWidth;
  108. state.duration = (contentWidth + wrapWidth) / +props.speed;
  109. emit("replay");
  110. });
  111. });
  112. };
  113. const renderMarquee = () => {
  114. const ellipsis = props.scrollable === false && !props.wrapable;
  115. const style = {
  116. transform: state.offset ? `translateX(${state.offset}px)` : "",
  117. transitionDuration: `${state.duration}s`
  118. };
  119. return (0, import_vue.createVNode)("div", {
  120. "ref": wrapRef,
  121. "role": "marquee",
  122. "class": bem("wrap")
  123. }, [(0, import_vue.createVNode)("div", {
  124. "ref": contentRef,
  125. "style": style,
  126. "class": [bem("content"), {
  127. "van-ellipsis": ellipsis
  128. }],
  129. "onTransitionend": onTransitionEnd
  130. }, [slots.default ? slots.default() : props.text])]);
  131. };
  132. const reset = () => {
  133. const {
  134. delay,
  135. speed,
  136. scrollable
  137. } = props;
  138. const ms = (0, import_utils.isDef)(delay) ? +delay * 1e3 : 0;
  139. wrapWidth = 0;
  140. contentWidth = 0;
  141. state.offset = 0;
  142. state.duration = 0;
  143. clearTimeout(startTimer);
  144. startTimer = setTimeout(() => {
  145. if (!wrapRef.value || !contentRef.value || scrollable === false) {
  146. return;
  147. }
  148. const wrapRefWidth = (0, import_use.useRect)(wrapRef).width;
  149. const contentRefWidth = (0, import_use.useRect)(contentRef).width;
  150. if (scrollable || contentRefWidth > wrapRefWidth) {
  151. (0, import_use.doubleRaf)(() => {
  152. wrapWidth = wrapRefWidth;
  153. contentWidth = contentRefWidth;
  154. state.offset = -contentWidth;
  155. state.duration = contentWidth / +speed;
  156. });
  157. }
  158. }, ms);
  159. };
  160. (0, import_on_popup_reopen.onPopupReopen)(reset);
  161. (0, import_use.onMountedOrActivated)(reset);
  162. (0, import_use.useEventListener)("pageshow", reset);
  163. (0, import_use_expose.useExpose)({
  164. reset
  165. });
  166. (0, import_vue2.watch)(() => [props.text, props.scrollable], reset);
  167. return () => {
  168. const {
  169. color,
  170. wrapable,
  171. background
  172. } = props;
  173. return (0, import_vue.withDirectives)((0, import_vue.createVNode)("div", {
  174. "role": "alert",
  175. "class": bem({
  176. wrapable
  177. }),
  178. "style": {
  179. color,
  180. background
  181. }
  182. }, [renderLeftIcon(), renderMarquee(), renderRightIcon()]), [[import_vue.vShow, state.show]]);
  183. };
  184. }
  185. });