@maas/vue-primitive
Version:
Vue component for passing attributes to slot content
60 lines (59 loc) • 1.55 kB
JavaScript
import { defineComponent as f, Comment as d, mergeProps as c, cloneVNode as g, Fragment as h, h as u } from "vue";
function m(r) {
return r ? r.flatMap((e) => e.type === h ? m(e.children) : [e]) : [];
}
const C = f({
name: "PrimitiveSlot",
inheritAttrs: !1,
setup(r, { attrs: e, slots: s }) {
return () => {
var p;
if (!s.default) return null;
const t = m(s.default());
if (t.length === 0)
return null;
const n = t.findIndex(
(a) => a.type !== d
);
if (n === -1)
return t;
const o = t[n], l = o.props ? c(e, o.props) : e;
e.class && ((p = o.props) != null && p.class) && delete o.props.class;
const i = g(o, l);
for (const a in l)
a.startsWith("on") && (i.props || (i.props = {}), i.props[a] = l[a]);
return t.length === 1 ? i : (t[n] = i, t);
};
}
}), P = f({
name: "Primitive",
inheritAttrs: !1,
props: {
asChild: {
type: Boolean,
default: !1
},
as: {
type: [String, Object],
default: "div"
}
},
setup(r, { attrs: e, slots: s }) {
const t = ["area", "img", "input"], n = r.asChild ? "template" : r.as;
return () => {
switch (!0) {
// Self closing tags
case (typeof n == "string" && t.includes(n)):
return u(n, e);
// as prop
case n !== "template":
return u(r.as, e, { default: s.default });
default:
return u(C, e, { default: s.default });
}
};
}
});
export {
P as Primitive
};