NoticeBar.js 5.6 KB

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