| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- import { m as mountComponent, u as usePopupState } from "./mount-component-2b0f7b23.js";
- 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";
- import { P as Popup } from "./index-eef3af38.js";
- import { L as Loading } from "./index-217c49a0.js";
- 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";
- let lockCount = 0;
- function lockClick(lock) {
- if (lock) {
- if (!lockCount) {
- document.body.classList.add("van-toast--unclickable");
- }
- lockCount++;
- } else if (lockCount) {
- lockCount--;
- if (!lockCount) {
- document.body.classList.remove("van-toast--unclickable");
- }
- }
- }
- const [name, bem] = createNamespace("toast");
- const popupInheritProps = ["show", "overlay", "teleport", "transition", "overlayClass", "overlayStyle", "closeOnClickOverlay", "zIndex"];
- const toastProps = {
- icon: String,
- show: Boolean,
- type: makeStringProp("text"),
- overlay: Boolean,
- message: numericProp,
- iconSize: numericProp,
- duration: makeNumberProp(2e3),
- position: makeStringProp("middle"),
- teleport: [String, Object],
- wordBreak: String,
- className: unknownProp,
- iconPrefix: String,
- transition: makeStringProp("van-fade"),
- loadingType: String,
- forbidClick: Boolean,
- overlayClass: unknownProp,
- overlayStyle: Object,
- closeOnClick: Boolean,
- closeOnClickOverlay: Boolean,
- zIndex: numericProp
- };
- var stdin_default = defineComponent({
- name,
- props: toastProps,
- emits: ["update:show"],
- setup(props, {
- emit,
- slots
- }) {
- let timer;
- let clickable = false;
- const toggleClickable = () => {
- const newValue = props.show && props.forbidClick;
- if (clickable !== newValue) {
- clickable = newValue;
- lockClick(clickable);
- }
- };
- const updateShow = (show) => emit("update:show", show);
- const onClick = () => {
- if (props.closeOnClick) {
- updateShow(false);
- }
- };
- const clearTimer = () => clearTimeout(timer);
- const renderIcon = () => {
- const {
- icon,
- type,
- iconSize,
- iconPrefix,
- loadingType
- } = props;
- const hasIcon = icon || type === "success" || type === "fail";
- if (hasIcon) {
- return createVNode(Icon, {
- "name": icon || type,
- "size": iconSize,
- "class": bem("icon"),
- "classPrefix": iconPrefix
- }, null);
- }
- if (type === "loading") {
- return createVNode(Loading, {
- "class": bem("loading"),
- "size": iconSize,
- "type": loadingType
- }, null);
- }
- };
- const renderMessage = () => {
- const {
- type,
- message
- } = props;
- if (slots.message) {
- return createVNode("div", {
- "class": bem("text")
- }, [slots.message()]);
- }
- if (isDef(message) && message !== "") {
- return type === "html" ? createVNode("div", {
- "key": 0,
- "class": bem("text"),
- "innerHTML": String(message)
- }, null) : createVNode("div", {
- "class": bem("text")
- }, [message]);
- }
- };
- watch(() => [props.show, props.forbidClick], toggleClickable);
- watch(() => [props.show, props.type, props.message, props.duration], () => {
- clearTimer();
- if (props.show && props.duration > 0) {
- timer = setTimeout(() => {
- updateShow(false);
- }, props.duration);
- }
- });
- onMounted(toggleClickable);
- onUnmounted(toggleClickable);
- return () => createVNode(Popup, mergeProps({
- "class": [bem([props.position, props.wordBreak === "normal" ? "break-normal" : props.wordBreak, {
- [props.type]: !props.icon
- }]), props.className],
- "lockScroll": false,
- "onClick": onClick,
- "onClosed": clearTimer,
- "onUpdate:show": updateShow
- }, pick(props, popupInheritProps)), {
- default: () => [renderIcon(), renderMessage()]
- });
- }
- });
- const defaultOptions = {
- icon: "",
- type: "text",
- message: "",
- className: "",
- overlay: false,
- onClose: void 0,
- onOpened: void 0,
- duration: 2e3,
- teleport: "body",
- iconSize: void 0,
- iconPrefix: void 0,
- position: "middle",
- transition: "van-fade",
- forbidClick: false,
- loadingType: void 0,
- overlayClass: "",
- overlayStyle: void 0,
- closeOnClick: false,
- closeOnClickOverlay: false
- };
- let queue = [];
- let allowMultiple = false;
- let currentOptions = extend({}, defaultOptions);
- const defaultOptionsMap = /* @__PURE__ */ new Map();
- function parseOptions(message) {
- if (isObject(message)) {
- return message;
- }
- return {
- message
- };
- }
- function createInstance() {
- const {
- instance,
- unmount
- } = mountComponent({
- setup() {
- const message = ref("");
- const {
- open,
- state,
- close,
- toggle
- } = usePopupState();
- const onClosed = () => {
- };
- const render = () => {
- const attrs = {
- onClosed,
- "onUpdate:show": toggle
- };
- return createVNode(stdin_default, mergeProps(state, attrs), null);
- };
- watch(message, (val) => {
- state.message = val;
- });
- getCurrentInstance().render = render;
- return {
- open,
- close,
- message
- };
- }
- });
- return instance;
- }
- function getInstance() {
- if (!queue.length || allowMultiple) {
- const instance = createInstance();
- queue.push(instance);
- }
- return queue[queue.length - 1];
- }
- function showToast(options = {}) {
- if (!inBrowser) {
- return {};
- }
- const toast = getInstance();
- const parsedOptions = parseOptions(options);
- toast.open(extend({}, currentOptions, defaultOptionsMap.get(parsedOptions.type || currentOptions.type), parsedOptions));
- return toast;
- }
- export {
- stdin_default as a,
- showToast as s
- };
|