UNPKG

y-design-ssr

Version:

SSR component library of YUI with Vue3

947 lines (946 loc) 20.8 kB
import { ref as D, reactive as P, defineComponent as x, onMounted as fe, onUnmounted as pe, watch as ee, createVNode as r, Transition as O, withDirectives as N, vShow as R, computed as j, Teleport as me, Fragment as te, createApp as T, h as ne, mergeProps as ge, resolveDirective as H, isVNode as ye } from "vue"; const le = (e) => (t) => Object.prototype.toString.call(t) === `[object ${e}]`, be = le("Function"), Se = le("Promise"), A = (e, t) => t ? typeof t == "string" ? ` ${e}--${t}` : Array.isArray(t) ? t.reduce((o, a) => o + A(e, a), "") : Object.keys(t).reduce( (o, a) => o + (t[a] ? A(e, a) : ""), "" ) : "", ke = (e) => (t, o) => { let a = t, c = o; return a && typeof a != "string" && (c = a, a = ""), a = a ? `${e}__${a}` : e, `${a}${A(a, c)}`; }, ve = () => (e, t) => t ? `${A(`y-${e}`, t)}` : `y-${e}`, $e = { "pull-refresh": { pulling: "下拉刷新...", loosing: "释放刷新...", loading: "数据加载中...", success: "数据已更新", failed: "数据跟新失败,请稍后再试" }, "form-item": { validateMessage: "请输入正确内容" } }; D("zh-CN"); P({ "zh-CN": $e }); const w = (e) => { const t = `y-${e}`; return [t, ke(t), ve()]; }, y = (e) => isNaN(Number(e)) && typeof e == "string" ? e : `${e}px`, Ce = (e) => { switch (e) { case "top": return "column-reverse"; case "bottom": return "column"; case "left": return "row-reverse"; case "right": return "row"; default: return "column"; } }, [he, xe, we] = w("mask"), Be = { // 控制显示 modelValue: { type: Boolean, default: !1 }, // 层级 zIndex: { type: [Number, String], default: null }, // 动画时长 duration: { type: Number, default: 300 }, // 自定义类名 customClass: { type: String, default: "" }, // 自定义样式 customStyle: { type: Object, default: () => ({}) }, // 触摸滚动开关 lockScroll: { type: Boolean, default: !0 }, onAfterLeave: { type: Function, default: null } }, S = P([]); let J = 0; const _e = /* @__PURE__ */ x({ name: he, props: Be, emits: ["update:modelValue"], setup(e, { slots: t }) { const o = D(""); fe(() => { o.value = `mask${J}`, J += 1, a(e.modelValue, !0); }), pe(() => { const l = S.findIndex((u) => u._id === o.value); l >= 0 && S.splice(l, 1); }), ee(() => e.modelValue, (l) => { a(l); }); const a = (l, u) => { l ? (S.unshift({ _id: o.value, _state: l }), S.length > 1 && (S[1]._state = !1)) : u || (S.splice(0, 1), S.length > 0 && (S[0]._state = !0)); }, c = () => { let l = !1; for (let u = 0; u < S.length; u++) S[u]._id === o.value && (l = S[u]._state); return { _state: l }; }, d = (l) => { const { lockScroll: u } = e; u && (l.preventDefault(), l.stopPropagation()); }; return () => { const { customClass: l, duration: u, zIndex: m, customStyle: g, onAfterLeave: p } = e, { _state: i } = c(); return r(O, { name: we("animation", "fade"), onAfterLeave: p }, { default: () => { var n; return [N(r("div", { style: { animationDuration: `${u}ms`, zIndex: m, ...g }, class: [xe(), l], onTouchmove: d }, [(n = t.default) == null ? void 0 : n.call(t)]), [[R, i]])]; } }); }; } }); function B(e) { const t = e; return t.install = (o) => { const { name: a } = e; a && o.component(a, e); }, t; } const K = B(_e), [Ne, Q, X] = w("popup"), Ve = { // 控制显示 modelValue: { type: Boolean, default: !1 }, // 位置 position: { type: String, default: "center" }, // 圆角 borderRadius: { type: [Number, String], default: "" }, // 自定义类名 customClass: { type: String, default: "" }, // 自定义属性 customStyle: { type: Object, default: () => ({}) }, // 自定义类名 maskCustomClass: { type: String, default: "" }, // 自定义属性 maskCustomStyle: { type: Object, default: () => ({}) }, // 插入对象 teleport: { type: String, default: "body" }, // 动画时长 duration: { type: Number, default: 300 }, // 点击遮罩是否关闭 isCloseOnClickMask: { type: Boolean, default: !0 }, onAfterLeave: { type: Function, default: null }, lockScroll: { type: Boolean, default: !0 }, zIndex: { type: Number, default: 100 } }, Ie = /* @__PURE__ */ x({ name: Ne, props: Ve, emits: ["clickMask", "update:modelValue"], setup(e, { slots: t, emit: o }) { const a = j(() => { const { position: l, borderRadius: u, customStyle: m, duration: g, zIndex: p } = e, i = y(u); let n = "0"; switch (l) { case "top": n = ` 0 0 ${i} ${i}`; break; case "bottom": n = `${i} ${i} 0 0`; break; case "left": n = `0 ${i} ${i} 0`; break; case "right": n = `${i} 0 0 ${i}`; break; default: n = `${i}`; } return { borderRadius: n, animationDuration: `${g}ms`, ...m, zIndex: p + 1 }; }), c = (l) => { o("clickMask", l), e.isCloseOnClickMask && (d.value = !1); }, d = j({ get: () => e.modelValue, set: (l) => o("update:modelValue", l) }); return ee(() => d.value, (l, u) => { l !== u && (l && e.lockScroll ? document.body.style.overflow = "hidden" : document.body.style.overflow = ""); }), () => { const { teleport: l, position: u, customClass: m, duration: g, onAfterLeave: p, lockScroll: i, zIndex: n, maskCustomClass: f, maskCustomStyle: b } = e; return l ? r(me, { to: l }, { default: () => [r(K, { modelValue: d.value, "onUpdate:modelValue": (s) => d.value = s, duration: g, lockScroll: i, onClick: c, onAfterLeave: p, zIndex: n, customClass: f, customStyle: b }, null), r(O, { name: X("animation", `slide-${u}`) }, { default: () => { var s; return [N(r("div", { style: a.value, class: [Q("content", { [u]: u }), m] }, [((s = t.default) == null ? void 0 : s.call(t)) || null]), [[R, d.value]])]; } })] }) : r(te, null, [r(K, { modelValue: d.value, "onUpdate:modelValue": (s) => d.value = s, duration: g, lockScroll: i, onClick: c, onAfterLeave: p, zIndex: n, customClass: f, customStyle: b }, null), r(O, { name: X("animation", `slide-${u}`) }, { default: () => { var s; return [N(r("div", { style: a.value, class: [Q("content", { [u]: u }), m] }, [((s = t.default) == null ? void 0 : s.call(t)) || null]), [[R, d.value]])]; } })]); }; } }); const ze = B(Ie), [Ae, z] = w("icon"), Me = (e) => new RegExp(/\.(https|png|jpg|gif|jpeg|webp|apng)$/).test(e.toLowerCase()), Le = /* @__PURE__ */ x({ name: Ae, props: { dot: { type: Boolean, default: !1 }, name: { type: String, default: "" }, size: { type: [Number, String], default: "" }, info: { type: [Number, String], default: "" }, badge: { type: [Number, String], default: "" }, color: { type: String, default: "" }, subColor: { type: String, default: "" }, subBg: { type: String, default: "" }, classPrefix: { type: String, default: "y-icon" }, /** * @ignore */ tag: { type: String, default: "i" } }, emits: ["click"], setup(e, { slots: t, emit: o }) { const a = Me(e.name), c = () => ({ fontSize: y(e.size), color: e.color }), d = (l) => { o("click", l); }; return () => { const { name: l, classPrefix: u, badge: m, dot: g, subColor: p, subBg: i, tag: n } = e; return r(n, { class: `${u}${a ? "" : ` y-icon--${l}`}`, style: c(), onClick: d }, { default: () => { var f; return [a ? r("em", { class: z("node") }, [r("img", { class: z("image"), src: l, alt: "icon" }, null)]) : null, g ? r("em", { class: z("dot"), style: { backgroundColor: i } }, null) : null, m ? r("em", { class: z("badge"), style: { color: p, backgroundColor: i } }, [m]) : null, ((f = t.default) == null ? void 0 : f.call(t)) || null]; } }); }; } }); const Oe = B(Le), [Re, _, je] = w("button"), De = /* @__PURE__ */ x({ name: Re, props: { // 标签 tag: { type: String, default: "button" }, // 文案 text: { type: String, default: "" }, // 类型 type: { type: String, default: "primary" }, // 尺寸 size: { type: String, default: "middle" }, // 不可以状态 disabled: { type: Boolean, default: !1 }, // 阴影 shadow: { type: String, default: "" }, // 方形 square: { type: Boolean, default: !1 }, // 半圆弧 semicircle: { type: Boolean, default: !1 }, // 自定义圆角 radius: { type: [Number, String], default: "" }, // 补充文案,双行显示 supplementText: { type: String, default: "" }, // block 自动填满父布局 block: { type: Boolean, default: !1 }, // 危险按钮状态 danger: { type: Boolean, default: !1 } }, emits: ["click"], setup(e, { slots: t, emit: o }) { const a = (c) => { o("click", c); }; return () => { let { tag: c } = e; const { radius: d, type: l, size: u, disabled: m, shadow: g, block: p, square: i, semicircle: n, danger: f, text: b, supplementText: s } = e, v = t.default ? t.default() : b; return c = l === "link" ? "a" : c, r(c, { class: [_([l, u, { block: p, square: i, semicircle: n, disabled: m && c === "a" }, (l === "primary" || l === "secondary") && g ? `shadow_${g}` : "", f ? `${l}_danger` : "", m ? `${l}_disabled` : ""]), je("el", "clickable")], style: { borderRadius: y(d) }, disabled: m, onClick: a }, { default: () => { var $, V; return [r("div", { class: _("content") }, [(($ = t.leftIcon) == null ? void 0 : $.call(t)) || null, v && r(te, null, [s ? r("div", { class: _("text") }, [r("p", null, [v]), r("p", { class: _("subtext") }, [s])]) : r("span", { class: _("text") }, [v])]), ((V = t.rightIcon) == null ? void 0 : V.call(t)) || null])]; } }); }; } }); const Y = B(De), [Pe, h] = w("loading"), Te = { // Loading icon 的颜色 color: { type: String, default: "" }, // 背景色 bgColor: { type: String, default: "" }, bgBorderRadius: { type: [Number, String], default: "16px" }, // 粗细 thick: { type: [Number, String], default: 8 }, // 方向 direction: { type: String, default: "forward" }, // 大小 size: { type: [Number, String], default: "24px" }, padding: { type: [Number, String], default: "" }, // 文案 text: { type: String, default: "" }, // 文案位置 textLocation: { type: String, default: "bottom" }, // 文案大小 textSize: { type: [Number, String], default: "" }, // 文案颜色 textColor: { type: String, default: "" }, // 布局位置 position: { type: String, default: "relative" } }, ae = /* @__PURE__ */ x({ name: Pe, props: Te, setup(e, { slots: t }) { return () => { var s, v; const { position: o, textLocation: a, bgColor: c, bgBorderRadius: d, size: l, padding: u, color: m, thick: g, direction: p, text: i, textColor: n, textSize: f } = e, b = ((s = t.default) == null ? void 0 : s.call(t)) || i; return r("div", { class: h({ [o]: o !== "relative" }) }, [r("div", { class: h("bg", { [o]: o !== "relative" }), style: { flexDirection: Ce(a), backgroundColor: c, borderRadius: y(d), padding: y(u) } }, [r("span", { class: h("icon-wrapper"), style: { width: y(l), height: y(l) } }, [((v = t.icon) == null ? void 0 : v.call(t)) || r("svg", { class: h("icon", { [p]: p }), viewBox: "25 25 50 50" }, [r("circle", { class: h("icon-circle"), style: { stroke: m, strokeWidth: y(g) }, cx: "50", cy: "50", r: "20", fill: "none" }, null)])]), b && r("span", { class: h("text", { [a]: a }), style: { color: n, fontSize: y(f) } }, [b])])]); }; } }), Fe = (e, t) => { const a = T({ render: () => ne(ae, { ...e }, { ...t }) }), c = document.createElement("div"); return a.mount(c); }, Z = (e, t) => { t.value ? (e.classList.add("y-loading__wrap"), e.appendChild(e.instance.$el)) : (e.classList.remove("y-loading__wrap"), e.removeChild(e.instance.$el)); }, Ue = { getSSRProps: (e) => ({ ...e }), mounted: (e, t) => { e.instance = Fe({ ...e.customProps }, { ...e.customSlots }), t.value && Z(e, t); }, updated: (e, t) => { t.oldValue !== t.value && Z(e, t); }, unmounted: (e) => { e.instance && e.instance.$el.remove(); } }; B(ae); function Ee(e) { return typeof e == "function" || Object.prototype.toString.call(e) === "[object Object]" && !ye(e); } const [qe, k] = w("dialog"), We = { // 控制显示 modelValue: { type: Boolean, default: !1 }, width: { type: [Number, String], default: 288 }, title: { type: String, default: "" }, titleAlign: { type: String, default: "center" }, message: { type: String, default: "" }, messageAlign: { type: String, default: "center" }, isCloseOnClickMask: { type: Boolean, default: !0 }, showCloseIcon: { type: Boolean, default: !1 }, showCancelBtn: { type: Boolean, default: !1 }, cancelButtonText: { type: String, default: "取消" }, showConfirmBtn: { type: Boolean, default: !1 }, confirmButtonText: { type: String, default: "确认" }, beforeClose: { type: Function, default: null }, callback: { type: Function, default: null }, imgSrc: { type: String, default: null }, circleImg: { type: Boolean, default: !1 }, // 插入对象 teleport: { type: String, default: "body" }, // 动画时长 duration: { type: Number, default: 300 }, onClickMask: { type: Function, default: null } }, oe = /* @__PURE__ */ x({ name: qe, directives: { loading: Ue }, props: We, emits: ["update:modelValue", "cancel", "confirm"], setup(e, { emit: t, slots: o }) { const a = P({ cancel: !1, confirm: !1 }), c = (n) => { i.value = n; }, d = (n) => { var f; c(!1), (f = e.callback) == null || f.call(e, n); }, l = (n) => { t("cancel", n, m("cancel")); }, u = (n) => { t("confirm", n, m("confirm")); }, m = (n) => { const { modelValue: f, beforeClose: b } = e; if (f) if (be(b)) { a[n] = !0; const s = b(n); Se(s) ? s.then(($) => { g($, n); }).catch(($) => { throw new Error($); }) : g(s, n); } else d(n); }, g = (n, f) => { n && d(f), a[f] = !1; }, p = (n, f, b) => { let s = Number(n.substring(0, n.length - 2)); return s /= f || 2, b ? `${b(s)}px` : `${s}px`; }, i = j({ get: () => e.modelValue, set: (n) => t("update:modelValue", n) }); return () => { const { width: n, title: f, titleAlign: b, message: s, messageAlign: v, isCloseOnClickMask: $, showCloseIcon: V, showCancelBtn: F, cancelButtonText: M, showConfirmBtn: U, confirmButtonText: ue, imgSrc: E, circleImg: C, teleport: ce, duration: ie, onClickMask: se } = e; return r(ze, ge({ modelValue: i.value, "onUpdate:modelValue": (I) => i.value = I, customClass: k(), customStyle: { width: y(n) } }, { onClickMask: se, "onUpdate:modelValue": c }, { isCloseOnClickMask: $, teleport: ce, duration: ie }), { default: () => { var I, q, W, G; return [((I = o.img) == null ? void 0 : I.call(o)) || E && r("div", { class: k("img", { circle: C }), style: { top: C ? `-${p(y(n), 4)}` : "auto", left: C ? `calc(50% - ${p(y(n), 4)})` : "auto" } }, [r("div", { style: { width: C ? p(y(n)) : y(n), height: p(y(n)), background: `url(${E}) center`, borderRadius: C ? "50%" : "20px 20px 0 0" } }, null)]), r("div", { class: k("container") }, [r("div", { style: { height: C ? p(y(n), 4, (de) => de + 20) : "24px" } }, null), ((q = o.title) == null ? void 0 : q.call(o)) || r("div", { class: k("title"), style: { textAlign: b } }, [f]), r("div", { class: k("content") }, [r("div", { class: k("message"), style: { textAlign: v } }, [((W = o.default) == null ? void 0 : W.call(o)) || s]), ((G = o.footer) == null ? void 0 : G.call(o)) || r("div", { class: k("footer"), style: { paddingTop: F || U ? "20px" : "0px" } }, [F && N(r(Y, { type: "secondary", class: k("footer-btn"), onClick: l }, Ee(M) ? M : { default: () => [M] }), [[H("loading"), a.cancel]]), U && N(r(Y, { type: "primary", class: k("footer-btn"), onClick: u }, { default: () => [a.confirm ? "" : ue] }), [[H("loading"), a.confirm]])])]), V && r("div", { class: k("close"), onClick: () => d("close") }, [r(Oe, { name: "cross" }, null)])])]; } }); }; } }); let L = T({}), Ge = 0; const re = [], Ke = (e, t) => { const o = `dialog${Ge += 1}`; L = T({ setup() { const d = D(!0), l = (u) => { d.value = u; }; return () => ne( oe, { modelValue: d.value, "onUpdate:modelValue": l, ...e, onAfterLeave: () => { He(o); } }, { ...t } ); } }); const c = document.createElement("div"); re.push({ id: o, app: L, el: c }), L.mount(c); }, He = (e) => { re.forEach((t) => { e === t.id && t.app.unmount(); }); }; const Qe = B(oe); export { Ke as createDialog, Qe as default };