@shopware-ag/meteor-component-library
Version:
The meteor component library is a Vue component library developed by Shopware. It is based on the [Meteor Design System](https://shopware.design/).
409 lines (408 loc) • 11.7 kB
JavaScript
import { Fragment, effectScope, getCurrentScope, onScopeDispose, customRef, toValue, onBeforeUnmount, shallowRef, watchEffect, readonly, getCurrentInstance, watch, ref, nextTick, computed, onMounted, unref, defineComponent, Comment, mergeProps, cloneVNode, h } from "vue";
function renderSlotFragments(children) {
if (!children)
return [];
return children.flatMap((child) => {
if (child.type === Fragment)
return renderSlotFragments(child.children);
return [child];
});
}
function computedEager(fn, options) {
var _a;
const result = shallowRef();
watchEffect(() => {
result.value = fn();
}, {
...options,
flush: (_a = options == null ? void 0 : options.flush) != null ? _a : "sync"
});
return readonly(result);
}
function tryOnScopeDispose(fn) {
if (getCurrentScope()) {
onScopeDispose(fn);
return true;
}
return false;
}
function createGlobalState(stateFactory) {
let initialized = false;
let state;
const scope = effectScope(true);
return (...args) => {
if (!initialized) {
state = scope.run(() => stateFactory(...args));
initialized = true;
}
return state;
};
}
function createSharedComposable(composable) {
let subscribers = 0;
let state;
let scope;
const dispose = () => {
subscribers -= 1;
if (scope && subscribers <= 0) {
scope.stop();
state = void 0;
scope = void 0;
}
};
return (...args) => {
subscribers += 1;
if (!scope) {
scope = effectScope(true);
state = scope.run(() => composable(...args));
}
tryOnScopeDispose(dispose);
return state;
};
}
const isClient = typeof window !== "undefined" && typeof document !== "undefined";
typeof WorkerGlobalScope !== "undefined" && globalThis instanceof WorkerGlobalScope;
const isDef = (val) => typeof val !== "undefined";
const toString = Object.prototype.toString;
const isObject = (val) => toString.call(val) === "[object Object]";
const isIOS = /* @__PURE__ */ getIsIOS();
function getIsIOS() {
var _a, _b;
return isClient && ((_a = window == null ? void 0 : window.navigator) == null ? void 0 : _a.userAgent) && (/iP(?:ad|hone|od)/.test(window.navigator.userAgent) || ((_b = window == null ? void 0 : window.navigator) == null ? void 0 : _b.maxTouchPoints) > 2 && /iPad|Macintosh/.test(window == null ? void 0 : window.navigator.userAgent));
}
function getLifeCycleTarget(target) {
return target || getCurrentInstance();
}
function toArray(value) {
return Array.isArray(value) ? value : [value];
}
function refAutoReset(defaultValue, afterMs = 1e4) {
return customRef((track, trigger) => {
let value = toValue(defaultValue);
let timer;
const resetAfter = () => setTimeout(() => {
value = toValue(defaultValue);
trigger();
}, toValue(afterMs));
tryOnScopeDispose(() => {
clearTimeout(timer);
});
return {
get() {
track();
return value;
},
set(newValue) {
value = newValue;
trigger();
clearTimeout(timer);
timer = resetAfter();
}
};
});
}
function tryOnBeforeUnmount(fn, target) {
const instance = getLifeCycleTarget(target);
if (instance)
onBeforeUnmount(fn, target);
}
function watchImmediate(source, cb, options) {
return watch(
source,
cb,
{
...options,
immediate: true
}
);
}
const defaultWindow = isClient ? window : void 0;
function unrefElement(elRef) {
var _a;
const plain = toValue(elRef);
return (_a = plain == null ? void 0 : plain.$el) != null ? _a : plain;
}
function useEventListener(...args) {
const cleanups = [];
const cleanup = () => {
cleanups.forEach((fn) => fn());
cleanups.length = 0;
};
const register = (el, event, listener, options) => {
el.addEventListener(event, listener, options);
return () => el.removeEventListener(event, listener, options);
};
const firstParamTargets = computed(() => {
const test = toArray(toValue(args[0])).filter((e) => e != null);
return test.every((e) => typeof e !== "string") ? test : void 0;
});
const stopWatch = watchImmediate(
() => {
var _a, _b;
return [
(_b = (_a = firstParamTargets.value) == null ? void 0 : _a.map((e) => unrefElement(e))) != null ? _b : [defaultWindow].filter((e) => e != null),
toArray(toValue(firstParamTargets.value ? args[1] : args[0])),
toArray(unref(firstParamTargets.value ? args[2] : args[1])),
// @ts-expect-error - TypeScript gets the correct types, but somehow still complains
toValue(firstParamTargets.value ? args[3] : args[2])
];
},
([raw_targets, raw_events, raw_listeners, raw_options]) => {
cleanup();
if (!(raw_targets == null ? void 0 : raw_targets.length) || !(raw_events == null ? void 0 : raw_events.length) || !(raw_listeners == null ? void 0 : raw_listeners.length))
return;
const optionsClone = isObject(raw_options) ? { ...raw_options } : raw_options;
cleanups.push(
...raw_targets.flatMap(
(el) => raw_events.flatMap(
(event) => raw_listeners.map((listener) => register(el, event, listener, optionsClone))
)
)
);
},
{ flush: "post" }
);
const stop = () => {
stopWatch();
cleanup();
};
tryOnScopeDispose(cleanup);
return stop;
}
function useMounted() {
const isMounted = shallowRef(false);
const instance = getCurrentInstance();
if (instance) {
onMounted(() => {
isMounted.value = true;
}, instance);
}
return isMounted;
}
function createKeyPredicate(keyFilter) {
if (typeof keyFilter === "function")
return keyFilter;
else if (typeof keyFilter === "string")
return (event) => event.key === keyFilter;
else if (Array.isArray(keyFilter))
return (event) => keyFilter.includes(event.key);
return () => true;
}
function onKeyStroke(...args) {
let key;
let handler;
let options = {};
if (args.length === 3) {
key = args[0];
handler = args[1];
options = args[2];
} else if (args.length === 2) {
if (typeof args[1] === "object") {
key = true;
handler = args[0];
options = args[1];
} else {
key = args[0];
handler = args[1];
}
} else {
key = true;
handler = args[0];
}
const {
target = defaultWindow,
eventName = "keydown",
passive = false,
dedupe = false
} = options;
const predicate = createKeyPredicate(key);
const listener = (e) => {
if (e.repeat && toValue(dedupe))
return;
if (predicate(e))
handler(e);
};
return useEventListener(target, eventName, listener, passive);
}
function cloneFnJSON(source) {
return JSON.parse(JSON.stringify(source));
}
function useVModel(props, key, emit, options = {}) {
var _a, _b, _c;
const {
clone = false,
passive = false,
eventName,
deep = false,
defaultValue,
shouldEmit
} = options;
const vm = getCurrentInstance();
const _emit = emit || (vm == null ? void 0 : vm.emit) || ((_a = vm == null ? void 0 : vm.$emit) == null ? void 0 : _a.bind(vm)) || ((_c = (_b = vm == null ? void 0 : vm.proxy) == null ? void 0 : _b.$emit) == null ? void 0 : _c.bind(vm == null ? void 0 : vm.proxy));
let event = eventName;
if (!key) {
key = "modelValue";
}
event = event || `update:${key.toString()}`;
const cloneFn = (val) => !clone ? val : typeof clone === "function" ? clone(val) : cloneFnJSON(val);
const getValue = () => isDef(props[key]) ? cloneFn(props[key]) : defaultValue;
const triggerEmit = (value) => {
if (shouldEmit) {
if (shouldEmit(value))
_emit(event, value);
} else {
_emit(event, value);
}
};
if (passive) {
const initialValue = getValue();
const proxy = ref(initialValue);
let isUpdating = false;
watch(
() => props[key],
(v) => {
if (!isUpdating) {
isUpdating = true;
proxy.value = cloneFn(v);
nextTick(() => isUpdating = false);
}
}
);
watch(
proxy,
(v) => {
if (!isUpdating && (v !== props[key] || deep))
triggerEmit(v);
},
{ deep }
);
return proxy;
} else {
return computed({
get() {
return getValue();
},
set(value) {
triggerEmit(value);
}
});
}
}
function useForwardExpose() {
const instance = getCurrentInstance();
const currentRef = ref();
const currentElement = computed(() => {
var _a, _b;
return ["#text", "#comment"].includes((_a = currentRef.value) == null ? void 0 : _a.$el.nodeName) ? (_b = currentRef.value) == null ? void 0 : _b.$el.nextElementSibling : unrefElement(currentRef);
});
const localExpose = Object.assign({}, instance.exposed);
const ret = {};
for (const key in instance.props)
Object.defineProperty(ret, key, {
enumerable: true,
configurable: true,
get: () => instance.props[key]
});
if (Object.keys(localExpose).length > 0)
for (const key in localExpose)
Object.defineProperty(ret, key, {
enumerable: true,
configurable: true,
get: () => localExpose[key]
});
Object.defineProperty(ret, "$el", {
enumerable: true,
configurable: true,
get: () => instance.vnode.el
});
instance.exposed = ret;
function forwardRef(ref$1) {
currentRef.value = ref$1;
if (!ref$1)
return;
Object.defineProperty(ret, "$el", {
enumerable: true,
configurable: true,
get: () => ref$1 instanceof Element ? ref$1 : ref$1.$el
});
instance.exposed = ret;
}
return {
forwardRef,
currentRef,
currentElement
};
}
const Slot = defineComponent({
name: "PrimitiveSlot",
inheritAttrs: false,
setup(_, { attrs, slots }) {
return () => {
var _a;
if (!slots.default)
return null;
const children = renderSlotFragments(slots.default());
const firstNonCommentChildrenIndex = children.findIndex((child) => child.type !== Comment);
if (firstNonCommentChildrenIndex === -1)
return children;
const firstNonCommentChildren = children[firstNonCommentChildrenIndex];
(_a = firstNonCommentChildren.props) == null ? true : delete _a.ref;
const mergedProps = firstNonCommentChildren.props ? mergeProps(attrs, firstNonCommentChildren.props) : attrs;
const cloned = cloneVNode({
...firstNonCommentChildren,
props: {}
}, mergedProps);
if (children.length === 1)
return cloned;
children[firstNonCommentChildrenIndex] = cloned;
return children;
};
}
});
const SELF_CLOSING_TAGS = [
"area",
"img",
"input"
];
const Primitive = defineComponent({
name: "Primitive",
inheritAttrs: false,
props: {
asChild: {
type: Boolean,
default: false
},
as: {
type: [String, Object],
default: "div"
}
},
setup(props, { attrs, slots }) {
const asTag = props.asChild ? "template" : props.as;
if (typeof asTag === "string" && SELF_CLOSING_TAGS.includes(asTag))
return () => h(asTag, attrs);
if (asTag !== "template")
return () => h(props.as, attrs, { default: slots.default });
return () => h(Slot, attrs, { default: slots.default });
}
});
export {
Primitive as P,
Slot as S,
useVModel as a,
useForwardExpose as b,
unrefElement as c,
defaultWindow as d,
createSharedComposable as e,
isIOS as f,
useEventListener as g,
refAutoReset as h,
isClient as i,
createGlobalState as j,
computedEager as k,
onKeyStroke as o,
renderSlotFragments as r,
tryOnBeforeUnmount as t,
useMounted as u
};
//# sourceMappingURL=Primitive-bb21d4ce.mjs.map