bootstrap-vue-next
Version:
Seamless integration of Vue 3, Bootstrap 5, and TypeScript for modern, type-safe UI development
411 lines (410 loc) • 13 kB
JavaScript
import { computed, customRef, effectScope, getCurrentInstance, getCurrentScope, hasInjectionContext, inject, isRef, nextTick, onMounted, onScopeDispose, readonly, ref, shallowReadonly, shallowRef, toRef, toValue, watch } from "vue";
//#region ../../node_modules/.pnpm/@vueuse+shared@14.2.1_vue@3.5.41_typescript@5.9.3_/node_modules/@vueuse/shared/dist/index.js
/**
* Call onScopeDispose() if it's inside an effect scope lifecycle, if not, do nothing
*
* @param fn
*/
function tryOnScopeDispose(fn, failSilently) {
if (getCurrentScope()) {
onScopeDispose(fn, failSilently);
return true;
}
return false;
}
/**
* Utility for creating event hooks
*
* @see https://vueuse.org/createEventHook
*
* @__NO_SIDE_EFFECTS__
*/
function createEventHook() {
const fns = /* @__PURE__ */ new Set();
const off = (fn) => {
fns.delete(fn);
};
const clear = () => {
fns.clear();
};
const on = (fn) => {
fns.add(fn);
const offFn = () => off(fn);
tryOnScopeDispose(offFn);
return { off: offFn };
};
const trigger = (...args) => {
return Promise.all(Array.from(fns).map((fn) => fn(...args)));
};
return {
on,
off,
trigger,
clear
};
}
/**
* Keep states in the global scope to be reusable across Vue instances.
*
* @see https://vueuse.org/createGlobalState
* @param stateFactory A factory function to create the state
*
* @__NO_SIDE_EFFECTS__
*/
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;
});
}
var localProvidedStateMap = /* @__PURE__ */ new WeakMap();
/**
* On the basis of `inject`, it is allowed to directly call inject to obtain the value after call provide in the same component.
*
* @example
* ```ts
* injectLocal('MyInjectionKey', 1)
* const injectedValue = injectLocal('MyInjectionKey') // injectedValue === 1
* ```
*
* @__NO_SIDE_EFFECTS__
*/
var injectLocal = (...args) => {
var _getCurrentInstance;
const key = args[0];
const instance = (_getCurrentInstance = getCurrentInstance()) === null || _getCurrentInstance === void 0 ? void 0 : _getCurrentInstance.proxy;
const owner = instance !== null && instance !== void 0 ? instance : getCurrentScope();
if (owner == null && !hasInjectionContext()) throw new Error("injectLocal must be called in setup");
if (owner && localProvidedStateMap.has(owner) && key in localProvidedStateMap.get(owner)) return localProvidedStateMap.get(owner)[key];
return inject(...args);
};
var isClient = typeof window !== "undefined" && typeof document !== "undefined";
typeof WorkerGlobalScope !== "undefined" && globalThis instanceof WorkerGlobalScope;
var isDef = (val) => typeof val !== "undefined";
var notNullish = (val) => val != null;
var toString = Object.prototype.toString;
var isObject = (val) => toString.call(val) === "[object Object]";
var timestamp = () => +Date.now();
var noop = () => {};
var hasOwn = (val, key) => Object.prototype.hasOwnProperty.call(val, key);
var isIOS = /* @__PURE__ */ getIsIOS();
function getIsIOS() {
var _window, _window2, _window3;
return isClient && !!((_window = window) === null || _window === void 0 || (_window = _window.navigator) === null || _window === void 0 ? void 0 : _window.userAgent) && (/iP(?:ad|hone|od)/.test(window.navigator.userAgent) || ((_window2 = window) === null || _window2 === void 0 || (_window2 = _window2.navigator) === null || _window2 === void 0 ? void 0 : _window2.maxTouchPoints) > 2 && /iPad|Macintosh/.test((_window3 = window) === null || _window3 === void 0 ? void 0 : _window3.navigator.userAgent));
}
function toRef$1(...args) {
if (args.length !== 1) return toRef(...args);
const r = args[0];
return typeof r === "function" ? readonly(customRef(() => ({
get: r,
set: noop
}))) : ref(r);
}
/**
* @internal
*/
function createFilterWrapper(filter, fn) {
function wrapper(...args) {
return new Promise((resolve, reject) => {
Promise.resolve(filter(() => fn.apply(this, args), {
fn,
thisArg: this,
args
})).then(resolve).catch(reject);
});
}
return wrapper;
}
var bypassFilter = (invoke$1) => {
return invoke$1();
};
function throttleFilter(...args) {
let lastExec = 0;
let timer;
let isLeading = true;
let lastRejector = noop;
let lastValue;
let ms;
let trailing;
let leading;
let rejectOnCancel;
if (!isRef(args[0]) && typeof args[0] === "object") ({delay: ms, trailing = true, leading = true, rejectOnCancel = false} = args[0]);
else [ms, trailing = true, leading = true, rejectOnCancel = false] = args;
const clear = () => {
if (timer) {
clearTimeout(timer);
timer = void 0;
lastRejector();
lastRejector = noop;
}
};
const filter = (_invoke) => {
const duration = toValue(ms);
const elapsed = Date.now() - lastExec;
const invoke$1 = () => {
return lastValue = _invoke();
};
clear();
if (duration <= 0) {
lastExec = Date.now();
return invoke$1();
}
if (elapsed > duration) {
lastExec = Date.now();
if (leading || !isLeading) invoke$1();
} else if (trailing) lastValue = new Promise((resolve, reject) => {
lastRejector = rejectOnCancel ? reject : resolve;
timer = setTimeout(() => {
lastExec = Date.now();
isLeading = true;
resolve(invoke$1());
clear();
}, Math.max(0, duration - elapsed));
});
if (!leading && !timer) timer = setTimeout(() => isLeading = true, duration);
isLeading = false;
return lastValue;
};
return filter;
}
/**
* EventFilter that gives extra controls to pause and resume the filter
*
* @param extendFilter Extra filter to apply when the PausableFilter is active, default to none
* @param options Options to configure the filter
*/
function pausableFilter(extendFilter = bypassFilter, options = {}) {
const { initialState = "active" } = options;
const isActive = toRef$1(initialState === "active");
function pause() {
isActive.value = false;
}
function resume() {
isActive.value = true;
}
const eventFilter = (...args) => {
if (isActive.value) extendFilter(...args);
};
return {
isActive: readonly(isActive),
pause,
resume,
eventFilter
};
}
function increaseWithUnit(target, delta) {
var _target$match;
if (typeof target === "number") return target + delta;
const value = ((_target$match = target.match(/^-?\d+\.?\d*/)) === null || _target$match === void 0 ? void 0 : _target$match[0]) || "";
const unit = target.slice(value.length);
const result = Number.parseFloat(value) + delta;
if (Number.isNaN(result)) return target;
return result + unit;
}
/**
* Get a px value for SSR use, do not rely on this method outside of SSR as REM unit is assumed at 16px, which might not be the case on the client
*/
function pxValue(px) {
return px.endsWith("rem") ? Number.parseFloat(px) * 16 : Number.parseFloat(px);
}
function toArray(value) {
return Array.isArray(value) ? value : [value];
}
function getLifeCycleTarget(target) {
return target || getCurrentInstance();
}
/**
* Make a composable function usable with multiple Vue instances.
*
* @see https://vueuse.org/createSharedComposable
*
* @__NO_SIDE_EFFECTS__
*/
function createSharedComposable(composable) {
if (!isClient) return 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;
});
}
/**
* Throttle execution of a function. Especially useful for rate limiting
* execution of handlers on events like resize and scroll.
*
* @param fn A function to be executed after delay milliseconds. The `this` context and all arguments are passed through, as-is,
* to `callback` when the throttled-function is executed.
* @param ms A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.
* (default value: 200)
*
* @param [trailing] if true, call fn again after the time is up (default value: false)
*
* @param [leading] if true, call fn on the leading edge of the ms timeout (default value: true)
*
* @param [rejectOnCancel] if true, reject the last call if it's been cancel (default value: false)
*
* @return A new, throttled, function.
*
* @__NO_SIDE_EFFECTS__
*/
function useThrottleFn(fn, ms = 200, trailing = false, leading = true, rejectOnCancel = false) {
return createFilterWrapper(throttleFilter(ms, trailing, leading, rejectOnCancel), fn);
}
function watchWithFilter(source, cb, options = {}) {
const { eventFilter = bypassFilter, ...watchOptions } = options;
return watch(source, createFilterWrapper(eventFilter, cb), watchOptions);
}
/** @deprecated Use Vue's built-in `watch` instead. This function will be removed in future version. */
function watchPausable(source, cb, options = {}) {
const { eventFilter: filter, initialState = "active", ...watchOptions } = options;
const { eventFilter, pause, resume, isActive } = pausableFilter(filter, { initialState });
return {
stop: watchWithFilter(source, cb, {
...watchOptions,
eventFilter
}),
pause,
resume,
isActive
};
}
/**
* Two-way refs synchronization.
* From the set theory perspective to restrict the option's type
* Check in the following order:
* 1. L = R
* 2. L ∩ R ≠ ∅
* 3. L ⊆ R
* 4. L ∩ R = ∅
*/
function syncRef(left, right, ...[options]) {
const { flush = "sync", deep = false, immediate = true, direction = "both", transform = {} } = options || {};
const watchers = [];
const transformLTR = "ltr" in transform && transform.ltr || ((v) => v);
const transformRTL = "rtl" in transform && transform.rtl || ((v) => v);
if (direction === "both" || direction === "ltr") watchers.push(watchPausable(left, (newValue) => {
watchers.forEach((w) => w.pause());
right.value = transformLTR(newValue);
watchers.forEach((w) => w.resume());
}, {
flush,
deep,
immediate
}));
if (direction === "both" || direction === "rtl") watchers.push(watchPausable(right, (newValue) => {
watchers.forEach((w) => w.pause());
left.value = transformRTL(newValue);
watchers.forEach((w) => w.resume());
}, {
flush,
deep,
immediate
}));
const stop = () => {
watchers.forEach((w) => w.stop());
};
return stop;
}
/**
* Call onMounted() if it's inside a component lifecycle, if not, just call the function
*
* @param fn
* @param sync if set to false, it will run in the nextTick() of Vue
* @param target
*/
function tryOnMounted(fn, sync = true, target) {
if (getLifeCycleTarget(target)) onMounted(fn, target);
else if (sync) fn();
else nextTick(fn);
}
/**
* Wrapper for `setInterval` with controls
*
* @see https://vueuse.org/useIntervalFn
* @param cb
* @param interval
* @param options
*/
function useIntervalFn(cb, interval = 1e3, options = {}) {
const { immediate = true, immediateCallback = false } = options;
let timer = null;
const isActive = shallowRef(false);
function clean() {
if (timer) {
clearInterval(timer);
timer = null;
}
}
function pause() {
isActive.value = false;
clean();
}
function resume() {
const intervalValue = toValue(interval);
if (intervalValue <= 0) return;
isActive.value = true;
if (immediateCallback) cb();
clean();
if (isActive.value) timer = setInterval(cb, intervalValue);
}
if (immediate && isClient) resume();
if (isRef(interval) || typeof interval === "function") tryOnScopeDispose(watch(interval, () => {
if (isActive.value && isClient) resume();
}));
tryOnScopeDispose(pause);
return {
isActive: shallowReadonly(isActive),
pause,
resume
};
}
/**
* Reactively convert a string ref to number.
*
* @__NO_SIDE_EFFECTS__
*/
function useToNumber(value, options = {}) {
const { method = "parseFloat", radix, nanToZero } = options;
return computed(() => {
let resolved = toValue(value);
if (typeof method === "function") resolved = method(resolved);
else if (typeof resolved === "string") resolved = Number[method](resolved, radix);
if (nanToZero && Number.isNaN(resolved)) resolved = 0;
return resolved;
});
}
/**
* Shorthand for watching value with {immediate: true}
*
* @see https://vueuse.org/watchImmediate
*/
function watchImmediate(source, cb, options) {
return watch(source, cb, {
...options,
immediate: true
});
}
//#endregion
export { watchImmediate as C, useToNumber as S, toRef$1 as _, increaseWithUnit as a, useIntervalFn as b, isDef as c, noop as d, notNullish as f, toArray as g, timestamp as h, hasOwn as i, isIOS as l, syncRef as m, createGlobalState as n, injectLocal as o, pxValue as p, createSharedComposable as r, isClient as s, createEventHook as t, isObject as u, tryOnMounted as v, watchPausable as w, useThrottleFn as x, tryOnScopeDispose as y };
//# sourceMappingURL=dist-DKc2ivHS.js.map