function-call-dc072a29.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. import { m as mountComponent, u as usePopupState } from "./mount-component-2b0f7b23.js";
  2. import { c as createNamespace, y as pick, z as makeStringProp, n as numericProp, a1 as makeNumberProp, G as unknownProp, I as Icon, k as isDef, e as extend, N as inBrowser, i as isObject } from "./index-487cde8c.js";
  3. import { P as Popup } from "./index-eef3af38.js";
  4. import { L as Loading } from "./index-217c49a0.js";
  5. import { d as defineComponent, w as watch, o as onMounted, G as onUnmounted, c as createVNode, m as mergeProps, r as ref, g as getCurrentInstance } from "./index-5e4623ce.js";
  6. let lockCount = 0;
  7. function lockClick(lock) {
  8. if (lock) {
  9. if (!lockCount) {
  10. document.body.classList.add("van-toast--unclickable");
  11. }
  12. lockCount++;
  13. } else if (lockCount) {
  14. lockCount--;
  15. if (!lockCount) {
  16. document.body.classList.remove("van-toast--unclickable");
  17. }
  18. }
  19. }
  20. const [name, bem] = createNamespace("toast");
  21. const popupInheritProps = ["show", "overlay", "teleport", "transition", "overlayClass", "overlayStyle", "closeOnClickOverlay", "zIndex"];
  22. const toastProps = {
  23. icon: String,
  24. show: Boolean,
  25. type: makeStringProp("text"),
  26. overlay: Boolean,
  27. message: numericProp,
  28. iconSize: numericProp,
  29. duration: makeNumberProp(2e3),
  30. position: makeStringProp("middle"),
  31. teleport: [String, Object],
  32. wordBreak: String,
  33. className: unknownProp,
  34. iconPrefix: String,
  35. transition: makeStringProp("van-fade"),
  36. loadingType: String,
  37. forbidClick: Boolean,
  38. overlayClass: unknownProp,
  39. overlayStyle: Object,
  40. closeOnClick: Boolean,
  41. closeOnClickOverlay: Boolean,
  42. zIndex: numericProp
  43. };
  44. var stdin_default = defineComponent({
  45. name,
  46. props: toastProps,
  47. emits: ["update:show"],
  48. setup(props, {
  49. emit,
  50. slots
  51. }) {
  52. let timer;
  53. let clickable = false;
  54. const toggleClickable = () => {
  55. const newValue = props.show && props.forbidClick;
  56. if (clickable !== newValue) {
  57. clickable = newValue;
  58. lockClick(clickable);
  59. }
  60. };
  61. const updateShow = (show) => emit("update:show", show);
  62. const onClick = () => {
  63. if (props.closeOnClick) {
  64. updateShow(false);
  65. }
  66. };
  67. const clearTimer = () => clearTimeout(timer);
  68. const renderIcon = () => {
  69. const {
  70. icon,
  71. type,
  72. iconSize,
  73. iconPrefix,
  74. loadingType
  75. } = props;
  76. const hasIcon = icon || type === "success" || type === "fail";
  77. if (hasIcon) {
  78. return createVNode(Icon, {
  79. "name": icon || type,
  80. "size": iconSize,
  81. "class": bem("icon"),
  82. "classPrefix": iconPrefix
  83. }, null);
  84. }
  85. if (type === "loading") {
  86. return createVNode(Loading, {
  87. "class": bem("loading"),
  88. "size": iconSize,
  89. "type": loadingType
  90. }, null);
  91. }
  92. };
  93. const renderMessage = () => {
  94. const {
  95. type,
  96. message
  97. } = props;
  98. if (slots.message) {
  99. return createVNode("div", {
  100. "class": bem("text")
  101. }, [slots.message()]);
  102. }
  103. if (isDef(message) && message !== "") {
  104. return type === "html" ? createVNode("div", {
  105. "key": 0,
  106. "class": bem("text"),
  107. "innerHTML": String(message)
  108. }, null) : createVNode("div", {
  109. "class": bem("text")
  110. }, [message]);
  111. }
  112. };
  113. watch(() => [props.show, props.forbidClick], toggleClickable);
  114. watch(() => [props.show, props.type, props.message, props.duration], () => {
  115. clearTimer();
  116. if (props.show && props.duration > 0) {
  117. timer = setTimeout(() => {
  118. updateShow(false);
  119. }, props.duration);
  120. }
  121. });
  122. onMounted(toggleClickable);
  123. onUnmounted(toggleClickable);
  124. return () => createVNode(Popup, mergeProps({
  125. "class": [bem([props.position, props.wordBreak === "normal" ? "break-normal" : props.wordBreak, {
  126. [props.type]: !props.icon
  127. }]), props.className],
  128. "lockScroll": false,
  129. "onClick": onClick,
  130. "onClosed": clearTimer,
  131. "onUpdate:show": updateShow
  132. }, pick(props, popupInheritProps)), {
  133. default: () => [renderIcon(), renderMessage()]
  134. });
  135. }
  136. });
  137. const defaultOptions = {
  138. icon: "",
  139. type: "text",
  140. message: "",
  141. className: "",
  142. overlay: false,
  143. onClose: void 0,
  144. onOpened: void 0,
  145. duration: 2e3,
  146. teleport: "body",
  147. iconSize: void 0,
  148. iconPrefix: void 0,
  149. position: "middle",
  150. transition: "van-fade",
  151. forbidClick: false,
  152. loadingType: void 0,
  153. overlayClass: "",
  154. overlayStyle: void 0,
  155. closeOnClick: false,
  156. closeOnClickOverlay: false
  157. };
  158. let queue = [];
  159. let allowMultiple = false;
  160. let currentOptions = extend({}, defaultOptions);
  161. const defaultOptionsMap = /* @__PURE__ */ new Map();
  162. function parseOptions(message) {
  163. if (isObject(message)) {
  164. return message;
  165. }
  166. return {
  167. message
  168. };
  169. }
  170. function createInstance() {
  171. const {
  172. instance,
  173. unmount
  174. } = mountComponent({
  175. setup() {
  176. const message = ref("");
  177. const {
  178. open,
  179. state,
  180. close,
  181. toggle
  182. } = usePopupState();
  183. const onClosed = () => {
  184. };
  185. const render = () => {
  186. const attrs = {
  187. onClosed,
  188. "onUpdate:show": toggle
  189. };
  190. return createVNode(stdin_default, mergeProps(state, attrs), null);
  191. };
  192. watch(message, (val) => {
  193. state.message = val;
  194. });
  195. getCurrentInstance().render = render;
  196. return {
  197. open,
  198. close,
  199. message
  200. };
  201. }
  202. });
  203. return instance;
  204. }
  205. function getInstance() {
  206. if (!queue.length || allowMultiple) {
  207. const instance = createInstance();
  208. queue.push(instance);
  209. }
  210. return queue[queue.length - 1];
  211. }
  212. function showToast(options = {}) {
  213. if (!inBrowser) {
  214. return {};
  215. }
  216. const toast = getInstance();
  217. const parsedOptions = parseOptions(options);
  218. toast.open(extend({}, currentOptions, defaultOptionsMap.get(parsedOptions.type || currentOptions.type), parsedOptions));
  219. return toast;
  220. }
  221. export {
  222. stdin_default as a,
  223. showToast as s
  224. };