bootstrap-vue-next
Version:
Seamless integration of Vue 3, Bootstrap 5, and TypeScript for modern, type-safe UI development
92 lines (91 loc) • 2.64 kB
JavaScript
import { S as useTimestamp, f as useElementHover, u as useDocumentVisibility } from "./dist-B10a-gZ8.mjs";
import { computed, readonly, ref, toRef, toValue, watch } from "vue";
//#region src/composables/useCountdown.ts
/**
* A simple interval timer that counts down the remaining seconds
*
* @param {MaybeRefOrGetter<number>} length the total amount of time to loop through in ms
* @param {MaybeRefOrGetter<number>} interval how often the interval should refresh. Default 1000
* @param {Readonly<UseIntervalFnOptions>} intervalOpts opts to pass to the interval fn. Default {}
*/
var useCountdown = (length, interval, timestampOpts = {}) => {
const resolvedLength = readonly(toRef(length));
const isPaused = ref(false);
const target = ref(Date.now() + resolvedLength.value);
const { isActive, pause, resume, timestamp } = useTimestamp({
interval,
controls: true,
callback: (v) => {
if (v >= target.value) {
isPaused.value = false;
pause();
}
},
...timestampOpts
});
watch(useDocumentVisibility(), (newVisibility) => {
if (newVisibility === "visible" && isActive.value && !isPaused.value) {
if (Date.now() >= target.value) {
isPaused.value = false;
pause();
}
}
});
const value = computed(() => target.value - timestamp.value);
const restart = () => {
target.value = Date.now() + resolvedLength.value;
resume();
};
watch(resolvedLength, () => {
if (resolvedLength.value > 0) restart();
});
const myPause = () => {
isPaused.value = true;
pause();
};
const myResume = () => {
isPaused.value = false;
const remainingTime = target.value - timestamp.value;
target.value = Date.now() + remainingTime;
resume();
};
const stop = () => {
pause();
timestamp.value = target.value;
isPaused.value = false;
};
return {
isActive: readonly(isActive),
isPaused: readonly(isPaused),
stop,
pause: myPause,
resume: myResume,
restart,
value
};
};
//#endregion
//#region src/composables/useCountdownHover.ts
var useCountdownHover = (element, { modelValueIgnoresHover, noHoverPause, noResumeOnHoverLeave }, actions) => {
const isHovering = useElementHover(element);
const onMouseEnter = () => {
if (toValue(noHoverPause)) return;
actions.pause();
};
const onMouseLeave = () => {
if (toValue(noResumeOnHoverLeave)) return;
actions.resume();
};
watch(isHovering, (newValue) => {
if (toValue(modelValueIgnoresHover)) return;
if (newValue) {
onMouseEnter();
return;
}
onMouseLeave();
});
return { isHovering };
};
//#endregion
export { useCountdown as n, useCountdownHover as t };
//# sourceMappingURL=useCountdownHover-t9O7DHoK.mjs.map