@ntohq/buefy-next
Version:
Lightweight UI components for Vue.js (v3) based on Bulma
135 lines (130 loc) • 3.9 kB
JavaScript
'use strict';
var vue = require('vue');
var helpers = require('./helpers.js');
!!(process.env.NODE_ENV !== "production") ? Object.freeze({}) : {};
!!(process.env.NODE_ENV !== "production") ? Object.freeze([]) : [];
const cacheStringFunction = (fn) => {
const cache = /* @__PURE__ */ Object.create(null);
return (str) => {
const hit = cache[str];
return hit || (cache[str] = fn(str));
};
};
const camelizeRE = /-(\w)/g;
const camelize = cacheStringFunction((str) => {
return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : "");
});
const hyphenateRE = /\B([A-Z])/g;
const hyphenate = cacheStringFunction(
(str) => str.replace(hyphenateRE, "-$1").toLowerCase()
);
const capitalize = cacheStringFunction((str) => {
return str.charAt(0).toUpperCase() + str.slice(1);
});
const toHandlerKey = cacheStringFunction((str) => {
const s = str ? `on${capitalize(str)}` : ``;
return s;
});
var BSlotComponent = vue.defineComponent({
name: "BSlotComponent",
props: {
component: {
type: Object,
required: true
},
name: {
type: String,
default: "default"
},
scoped: {
type: Boolean
},
props: {
type: Object
},
tag: {
type: String,
default: "div"
},
event: {
type: String,
default: "vue:updated"
}
},
data: () => ({
// see: https://github.com/vuejs/core/blob/7976f7044e66b3b7adac4c72a392935704658b10/packages/runtime-core/src/apiLifecycle.ts#L69-L74
// eslint-disable-next-line @typescript-eslint/ban-types
updatedHook: void 0,
handlerKey: void 0
}),
methods: {
refresh() {
this.$forceUpdate();
}
},
created() {
if (helpers.isVueComponent(this.component)) {
if (this.event === "vue:updated") {
this.updatedHook = vue.onUpdated(this.refresh, this.component.$);
} else {
const { vnode } = this.component.$;
let handlerKey = toHandlerKey(this.event);
if (vnode.props == null) {
vnode.props = { [handlerKey]: this.refresh };
} else {
const { props } = vnode;
if (props[this.handlerKey] == null) {
handlerKey = toHandlerKey(camelize(this.event));
if (props[handlerKey] == null) {
handlerKey = toHandlerKey(hyphenate(this.event));
}
}
if (props[handlerKey] == null) {
handlerKey = toHandlerKey(this.event);
props[handlerKey] = this.refresh;
} else {
if (Array.isArray(props[handlerKey])) {
props[handlerKey].push(this.refresh);
} else {
props[handlerKey] = [props[handlerKey], this.refresh];
}
}
}
this.handlerKey = handlerKey;
}
}
},
beforeUnmount() {
if (helpers.isVueComponent(this.component)) {
if (this.updatedHook != null) {
const index = this.component.$.u.indexOf(this.updatedHook);
if (index !== -1) {
this.component.$.u.splice(index, 1);
}
} else if (this.handlerKey != null) {
const { props } = this.component.$.vnode;
if (props != null) {
if (Array.isArray(props[this.handlerKey])) {
const index = props[this.handlerKey].indexOf(this.refresh);
if (index > -1) {
props[this.handlerKey].splice(index, 1);
if (props[this.handlerKey].length === 1) {
props[this.handlerKey] = props[this.handlerKey][0];
}
}
} else {
delete props[this.handlerKey];
}
}
}
}
},
render() {
return vue.h(
this.tag,
{},
this.component.$slots ? this.scoped ? this.component.$slots[this.name](this.props) : this.component.$slots[this.name]() : void 0
);
}
});
exports.BSlotComponent = BSlotComponent;