@stacksjs/composables 0.70.87 → 0.70.90
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/_shared.d.ts +49 -0
- package/dist/_shared.js +23 -0
- package/dist/cloneFnJSON.d.ts +5 -0
- package/dist/cloneFnJSON.js +3 -0
- package/dist/computedAsync.d.ts +28 -0
- package/dist/computedAsync.js +20 -0
- package/dist/computedEager.d.ts +27 -0
- package/dist/computedEager.js +10 -0
- package/dist/createEventHook.d.ts +16 -0
- package/dist/createEventHook.js +15 -0
- package/dist/createFetch.d.ts +14 -0
- package/dist/createFetch.js +8 -0
- package/dist/createGlobalState.d.ts +17 -0
- package/dist/createGlobalState.js +10 -0
- package/dist/createSharedComposable.d.ts +8 -0
- package/dist/createSharedComposable.js +9 -0
- package/dist/extendRef.d.ts +9 -0
- package/dist/extendRef.js +6 -0
- package/dist/formatTimeAgo.d.ts +23 -0
- package/dist/formatTimeAgo.js +33 -0
- package/dist/index.d.ts +271 -0
- package/dist/index.js +210 -2
- package/dist/isDefined.d.ts +5 -0
- package/dist/isDefined.js +3 -0
- package/dist/makeDestructurable.d.ts +18 -0
- package/dist/makeDestructurable.js +16 -0
- package/dist/onClickOutside.d.ts +14 -0
- package/dist/onClickOutside.js +25 -0
- package/dist/onKeyStroke.d.ts +26 -0
- package/dist/onKeyStroke.js +31 -0
- package/dist/onLongPress.d.ts +14 -0
- package/dist/onLongPress.js +43 -0
- package/dist/onStartTyping.d.ts +8 -0
- package/dist/onStartTyping.js +23 -0
- package/dist/refAutoReset.d.ts +11 -0
- package/dist/refAutoReset.js +15 -0
- package/dist/refDefault.d.ts +9 -0
- package/dist/refDefault.js +8 -0
- package/dist/resolveRef.d.ts +7 -0
- package/dist/resolveRef.js +7 -0
- package/dist/ssrHandlers.d.ts +13 -0
- package/dist/ssrHandlers.js +8 -0
- package/dist/syncRef.d.ts +21 -0
- package/dist/syncRef.js +35 -0
- package/dist/tryOnMounted.d.ts +22 -0
- package/dist/tryOnMounted.js +22 -0
- package/dist/unrefElement.d.ts +6 -0
- package/dist/unrefElement.js +9 -0
- package/dist/until.d.ts +34 -0
- package/dist/until.js +62 -0
- package/dist/useActiveElement.d.ts +6 -0
- package/dist/useActiveElement.js +19 -0
- package/dist/useAnimate.d.ts +18 -0
- package/dist/useAnimate.js +48 -0
- package/dist/useArrayUtils.d.ts +42 -0
- package/dist/useArrayUtils.js +114 -0
- package/dist/useAsyncQueue.d.ts +18 -0
- package/dist/useAsyncQueue.js +22 -0
- package/dist/useAsyncState.d.ts +23 -0
- package/dist/useAsyncState.js +35 -0
- package/dist/useBase64.d.ts +12 -0
- package/dist/useBase64.js +45 -0
- package/dist/useBattery.d.ts +15 -0
- package/dist/useBattery.js +38 -0
- package/dist/useBreakpoints.d.ts +96 -0
- package/dist/useBreakpoints.js +97 -0
- package/dist/useBroadcastChannel.d.ts +14 -0
- package/dist/useBroadcastChannel.js +31 -0
- package/dist/useBrowserLocation.d.ts +18 -0
- package/dist/useBrowserLocation.js +64 -0
- package/dist/useClipboard.d.ts +17 -0
- package/dist/useClipboard.js +32 -0
- package/dist/useClipboardItems.d.ts +11 -0
- package/dist/useClipboardItems.js +15 -0
- package/dist/useCloned.d.ts +15 -0
- package/dist/useCloned.js +16 -0
- package/dist/useColorMode.d.ts +18 -0
- package/dist/useColorMode.js +60 -0
- package/dist/useConfirmDialog.d.ts +14 -0
- package/dist/useConfirmDialog.js +34 -0
- package/dist/useCounter.d.ts +20 -0
- package/dist/useCounter.js +28 -0
- package/dist/useCssVar.d.ts +10 -0
- package/dist/useCssVar.js +23 -0
- package/dist/useCycleList.d.ts +18 -0
- package/dist/useCycleList.js +27 -0
- package/dist/useDark.d.ts +11 -0
- package/dist/useDark.js +38 -0
- package/dist/useDateFormat.d.ts +10 -0
- package/dist/useDateFormat.js +11 -0
- package/dist/useDebounce.d.ts +10 -0
- package/dist/useDebounce.js +16 -0
- package/dist/useDebounceFn.d.ts +10 -0
- package/dist/useDebounceFn.js +18 -0
- package/dist/useDebouncedRef.d.ts +11 -0
- package/dist/useDebouncedRef.js +22 -0
- package/dist/useDebouncedRefHistory.d.ts +5 -0
- package/dist/useDebouncedRefHistory.js +36 -0
- package/dist/useDeviceMotion.d.ts +27 -0
- package/dist/useDeviceMotion.js +53 -0
- package/dist/useDeviceOrientation.d.ts +15 -0
- package/dist/useDeviceOrientation.js +23 -0
- package/dist/useDevicePixelRatio.d.ts +5 -0
- package/dist/useDevicePixelRatio.js +24 -0
- package/dist/useDocumentVisibility.d.ts +5 -0
- package/dist/useDocumentVisibility.js +16 -0
- package/dist/useDraggable.d.ts +23 -0
- package/dist/useDraggable.js +58 -0
- package/dist/useDropZone.d.ts +15 -0
- package/dist/useDropZone.js +52 -0
- package/dist/useElementBounding.d.ts +21 -0
- package/dist/useElementBounding.js +59 -0
- package/dist/useElementByPoint.d.ts +10 -0
- package/dist/useElementByPoint.js +18 -0
- package/dist/useElementHover.d.ts +9 -0
- package/dist/useElementHover.js +28 -0
- package/dist/useElementSize.d.ts +19 -0
- package/dist/useElementSize.js +34 -0
- package/dist/useElementVisibility.d.ts +10 -0
- package/dist/useElementVisibility.js +10 -0
- package/dist/useEventBus.d.ts +14 -0
- package/dist/useEventBus.js +41 -0
- package/dist/useEventListener.d.ts +14 -0
- package/dist/useEventListener.js +29 -0
- package/dist/useEventSource.d.ts +32 -0
- package/dist/useEventSource.js +72 -0
- package/dist/useEyeDropper.d.ts +10 -0
- package/dist/useEyeDropper.js +17 -0
- package/dist/useFavicon.d.ts +8 -0
- package/dist/useFavicon.js +22 -0
- package/dist/useFetch.d.ts +35 -0
- package/dist/useFetch.js +61 -0
- package/dist/useFileDialog.d.ts +15 -0
- package/dist/useFileDialog.js +24 -0
- package/dist/useFocus.d.ts +13 -0
- package/dist/useFocus.js +42 -0
- package/dist/useFocusWithin.d.ts +9 -0
- package/dist/useFocusWithin.js +29 -0
- package/dist/useForm.d.ts +97 -0
- package/dist/useForm.js +182 -0
- package/dist/useFps.d.ts +7 -0
- package/dist/useFps.js +22 -0
- package/dist/useFullscreen.d.ts +16 -0
- package/dist/useFullscreen.js +67 -0
- package/dist/useGeolocation.d.ts +26 -0
- package/dist/useGeolocation.js +55 -0
- package/dist/useIdle.d.ts +18 -0
- package/dist/useIdle.js +46 -0
- package/dist/useImage.d.ts +18 -0
- package/dist/useImage.js +32 -0
- package/dist/useInfiniteScroll.d.ts +11 -0
- package/dist/useInfiniteScroll.js +50 -0
- package/dist/useIntersectionObserver.d.ts +16 -0
- package/dist/useIntersectionObserver.js +27 -0
- package/dist/useInterval.d.ts +13 -0
- package/dist/useInterval.js +32 -0
- package/dist/useIntervalFn.d.ts +16 -0
- package/dist/useIntervalFn.js +24 -0
- package/dist/useKeyModifier.d.ts +6 -0
- package/dist/useKeyModifier.js +22 -0
- package/dist/useLastChanged.d.ts +11 -0
- package/dist/useLastChanged.js +8 -0
- package/dist/useLocalStorage.d.ts +17 -0
- package/dist/useLocalStorage.js +4 -0
- package/dist/useMagicKeys.d.ts +16 -0
- package/dist/useMagicKeys.js +81 -0
- package/dist/useManualRefHistory.d.ts +27 -0
- package/dist/useManualRefHistory.js +64 -0
- package/dist/useMath.d.ts +17 -0
- package/dist/useMath.js +52 -0
- package/dist/useMediaQuery.d.ts +9 -0
- package/dist/useMediaQuery.js +19 -0
- package/dist/useMemoize.d.ts +13 -0
- package/dist/useMemoize.js +24 -0
- package/dist/useMounted.d.ts +6 -0
- package/dist/useMounted.js +8 -0
- package/dist/useMouse.d.ts +19 -0
- package/dist/useMouse.js +67 -0
- package/dist/useMouseInElement.d.ts +17 -0
- package/dist/useMouseInElement.js +31 -0
- package/dist/useMousePressed.d.ts +10 -0
- package/dist/useMousePressed.js +31 -0
- package/dist/useMutationObserver.d.ts +16 -0
- package/dist/useMutationObserver.js +27 -0
- package/dist/useNavigatorLanguage.d.ts +5 -0
- package/dist/useNavigatorLanguage.js +17 -0
- package/dist/useNetwork.d.ts +18 -0
- package/dist/useNetwork.js +58 -0
- package/dist/useNow.d.ts +8 -0
- package/dist/useNow.js +10 -0
- package/dist/useObjectUrl.d.ts +8 -0
- package/dist/useObjectUrl.js +23 -0
- package/dist/useOffsetPagination.d.ts +21 -0
- package/dist/useOffsetPagination.js +45 -0
- package/dist/useOnline.d.ts +7 -0
- package/dist/useOnline.js +20 -0
- package/dist/usePageLeave.d.ts +9 -0
- package/dist/usePageLeave.js +27 -0
- package/dist/useParallax.d.ts +15 -0
- package/dist/useParallax.js +33 -0
- package/dist/useParentElement.d.ts +6 -0
- package/dist/useParentElement.js +8 -0
- package/dist/usePermission.d.ts +7 -0
- package/dist/usePermission.js +21 -0
- package/dist/usePointer.d.ts +15 -0
- package/dist/usePointer.js +32 -0
- package/dist/usePointerLock.d.ts +13 -0
- package/dist/usePointerLock.js +33 -0
- package/dist/usePointerSwipe.d.ts +20 -0
- package/dist/usePointerSwipe.js +52 -0
- package/dist/usePreferredColorScheme.d.ts +6 -0
- package/dist/usePreferredColorScheme.js +25 -0
- package/dist/usePreferredContrast.d.ts +6 -0
- package/dist/usePreferredContrast.js +29 -0
- package/dist/usePreferredLanguages.d.ts +5 -0
- package/dist/usePreferredLanguages.js +16 -0
- package/dist/usePreferredReducedMotion.d.ts +6 -0
- package/dist/usePreferredReducedMotion.js +18 -0
- package/dist/usePrevious.d.ts +9 -0
- package/dist/usePrevious.js +8 -0
- package/dist/useQuery.d.ts +106 -0
- package/dist/useQuery.js +265 -0
- package/dist/useRafFn.d.ts +19 -0
- package/dist/useRafFn.js +37 -0
- package/dist/useRefHistory.d.ts +28 -0
- package/dist/useRefHistory.js +72 -0
- package/dist/useResizeObserver.d.ts +16 -0
- package/dist/useResizeObserver.js +27 -0
- package/dist/useScreenOrientation.d.ts +13 -0
- package/dist/useScreenOrientation.js +27 -0
- package/dist/useScreenSafeArea.d.ts +12 -0
- package/dist/useScreenSafeArea.js +25 -0
- package/dist/useScriptTag.d.ts +20 -0
- package/dist/useScriptTag.js +52 -0
- package/dist/useScroll.d.ts +36 -0
- package/dist/useScroll.js +116 -0
- package/dist/useScrollLock.d.ts +10 -0
- package/dist/useScrollLock.js +39 -0
- package/dist/useSessionStorage.d.ts +17 -0
- package/dist/useSessionStorage.js +4 -0
- package/dist/useShare.d.ts +15 -0
- package/dist/useShare.js +11 -0
- package/dist/useSorted.d.ts +10 -0
- package/dist/useSorted.js +10 -0
- package/dist/useStepper.d.ts +23 -0
- package/dist/useStepper.js +46 -0
- package/dist/useStorage.d.ts +18 -0
- package/dist/useStorage.js +54 -0
- package/dist/useStyleTag.d.ts +17 -0
- package/dist/useStyleTag.js +39 -0
- package/dist/useSupported.d.ts +8 -0
- package/dist/useSupported.js +10 -0
- package/dist/useSwipe.d.ts +20 -0
- package/dist/useSwipe.js +58 -0
- package/dist/useTextDirection.d.ts +9 -0
- package/dist/useTextDirection.js +16 -0
- package/dist/useTextSelection.d.ts +10 -0
- package/dist/useTextSelection.js +27 -0
- package/dist/useTextareaAutosize.d.ts +10 -0
- package/dist/useTextareaAutosize.js +20 -0
- package/dist/useThrottle.d.ts +9 -0
- package/dist/useThrottle.js +23 -0
- package/dist/useThrottleFn.d.ts +11 -0
- package/dist/useThrottleFn.js +36 -0
- package/dist/useThrottledRef.d.ts +11 -0
- package/dist/useThrottledRef.js +30 -0
- package/dist/useThrottledRefHistory.d.ts +5 -0
- package/dist/useThrottledRefHistory.js +43 -0
- package/dist/useTimeAgo.d.ts +16 -0
- package/dist/useTimeAgo.js +58 -0
- package/dist/useTimeout.d.ts +12 -0
- package/dist/useTimeout.js +32 -0
- package/dist/useTimeoutFn.d.ts +16 -0
- package/dist/useTimeoutFn.js +27 -0
- package/dist/useTimeoutPoll.d.ts +10 -0
- package/dist/useTimeoutPoll.js +33 -0
- package/dist/useTimestamp.d.ts +15 -0
- package/dist/useTimestamp.js +43 -0
- package/dist/useTitle.d.ts +10 -0
- package/dist/useTitle.js +12 -0
- package/dist/useToNumber.d.ts +11 -0
- package/dist/useToNumber.js +25 -0
- package/dist/useToString.d.ts +6 -0
- package/dist/useToString.js +10 -0
- package/dist/useToggle.d.ts +6 -0
- package/dist/useToggle.js +12 -0
- package/dist/useTransition.d.ts +28 -0
- package/dist/useTransition.js +89 -0
- package/dist/useUrlSearchParams.d.ts +18 -0
- package/dist/useUrlSearchParams.js +72 -0
- package/dist/useVibrate.d.ts +13 -0
- package/dist/useVibrate.js +14 -0
- package/dist/useVirtualList.d.ts +23 -0
- package/dist/useVirtualList.js +49 -0
- package/dist/useWakeLock.d.ts +12 -0
- package/dist/useWakeLock.js +32 -0
- package/dist/useWebNotification.d.ts +25 -0
- package/dist/useWebNotification.js +39 -0
- package/dist/useWebSocket.d.ts +38 -0
- package/dist/useWebSocket.js +103 -0
- package/dist/useWebWorker.d.ts +18 -0
- package/dist/useWebWorker.js +56 -0
- package/dist/useWindowFocus.d.ts +5 -0
- package/dist/useWindowFocus.js +20 -0
- package/dist/useWindowScroll.d.ts +9 -0
- package/dist/useWindowScroll.js +17 -0
- package/dist/useWindowSize.d.ts +15 -0
- package/dist/useWindowSize.js +28 -0
- package/dist/watchArray.d.ts +5 -0
- package/dist/watchArray.js +9 -0
- package/dist/watchAtMost.d.ts +5 -0
- package/dist/watchAtMost.js +12 -0
- package/dist/watchDebounced.d.ts +23 -0
- package/dist/watchDebounced.js +20 -0
- package/dist/watchDeep.d.ts +7 -0
- package/dist/watchDeep.js +13 -0
- package/dist/watchIgnorable.d.ts +12 -0
- package/dist/watchIgnorable.js +19 -0
- package/dist/watchImmediate.d.ts +5 -0
- package/dist/watchImmediate.js +7 -0
- package/dist/watchOnce.d.ts +19 -0
- package/dist/watchOnce.js +17 -0
- package/dist/watchPausable.d.ts +13 -0
- package/dist/watchPausable.js +23 -0
- package/dist/watchThrottled.d.ts +23 -0
- package/dist/watchThrottled.js +37 -0
- package/dist/watchTriggerable.d.ts +5 -0
- package/dist/watchTriggerable.js +10 -0
- package/dist/watchWithFilter.d.ts +7 -0
- package/dist/watchWithFilter.js +6 -0
- package/dist/whenever.d.ts +18 -0
- package/dist/whenever.js +7 -0
- package/package.json +2 -2
package/dist/useSwipe.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { onUnmounted, ref } from "@stacksjs/stx";
|
|
2
|
+
import { unref } from "./_shared";
|
|
3
|
+
export function useSwipe(target, options = {}) {
|
|
4
|
+
const { threshold = 50, onSwipe, onSwipeStart, onSwipeEnd, passive = !0 } = options, isSwiping = ref(!1), direction = ref("none"), lengthX = ref(0), lengthY = ref(0);
|
|
5
|
+
let startX = 0, startY = 0;
|
|
6
|
+
if (typeof window > "u")
|
|
7
|
+
return { isSwiping, direction, lengthX, lengthY };
|
|
8
|
+
const el = unref(target) ?? window, onTouchStart = (e) => {
|
|
9
|
+
const touch = e.touches[0];
|
|
10
|
+
if (!touch)
|
|
11
|
+
return;
|
|
12
|
+
startX = touch.clientX;
|
|
13
|
+
startY = touch.clientY;
|
|
14
|
+
isSwiping.value = !0;
|
|
15
|
+
direction.value = "none";
|
|
16
|
+
lengthX.value = 0;
|
|
17
|
+
lengthY.value = 0;
|
|
18
|
+
onSwipeStart?.(e);
|
|
19
|
+
}, onTouchMove = (e) => {
|
|
20
|
+
if (!isSwiping.value)
|
|
21
|
+
return;
|
|
22
|
+
const touch = e.touches[0];
|
|
23
|
+
if (!touch)
|
|
24
|
+
return;
|
|
25
|
+
lengthX.value = touch.clientX - startX;
|
|
26
|
+
lengthY.value = touch.clientY - startY;
|
|
27
|
+
const absX = Math.abs(lengthX.value), absY = Math.abs(lengthY.value);
|
|
28
|
+
if (absX > absY)
|
|
29
|
+
direction.value = lengthX.value > 0 ? "right" : "left";
|
|
30
|
+
else if (absY > absX)
|
|
31
|
+
direction.value = lengthY.value > 0 ? "down" : "up";
|
|
32
|
+
}, onTouchEnd = (e) => {
|
|
33
|
+
if (!isSwiping.value)
|
|
34
|
+
return;
|
|
35
|
+
isSwiping.value = !1;
|
|
36
|
+
const absX = Math.abs(lengthX.value), absY = Math.abs(lengthY.value);
|
|
37
|
+
if (Math.max(absX, absY) >= threshold) {
|
|
38
|
+
onSwipe?.(direction.value);
|
|
39
|
+
onSwipeEnd?.(e, direction.value);
|
|
40
|
+
} else {
|
|
41
|
+
direction.value = "none";
|
|
42
|
+
onSwipeEnd?.(e, "none");
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
el.addEventListener("touchstart", onTouchStart, { passive });
|
|
46
|
+
el.addEventListener("touchmove", onTouchMove, { passive });
|
|
47
|
+
el.addEventListener("touchend", onTouchEnd, { passive });
|
|
48
|
+
el.addEventListener("touchcancel", onTouchEnd, { passive });
|
|
49
|
+
try {
|
|
50
|
+
onUnmounted(() => {
|
|
51
|
+
el.removeEventListener("touchstart", onTouchStart);
|
|
52
|
+
el.removeEventListener("touchmove", onTouchMove);
|
|
53
|
+
el.removeEventListener("touchend", onTouchEnd);
|
|
54
|
+
el.removeEventListener("touchcancel", onTouchEnd);
|
|
55
|
+
});
|
|
56
|
+
} catch {}
|
|
57
|
+
return { isSwiping, direction, lengthX, lengthY };
|
|
58
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Ref } from '@stacksjs/stx';
|
|
2
|
+
/**
|
|
3
|
+
* Reactive text direction of the document or element.
|
|
4
|
+
*
|
|
5
|
+
* @param target - Target element (defaults to document.documentElement)
|
|
6
|
+
* @param initialValue - Initial direction value
|
|
7
|
+
*/
|
|
8
|
+
export declare function useTextDirection(target?: HTMLElement, initialValue?: TextDirection): Ref<TextDirection>;
|
|
9
|
+
export type TextDirection = 'ltr' | 'rtl' | 'auto';
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { ref, watch } from "@stacksjs/stx";
|
|
2
|
+
export function useTextDirection(target, initialValue) {
|
|
3
|
+
const el = target ?? (typeof document < "u" ? document.documentElement : null);
|
|
4
|
+
let initial = initialValue ?? "ltr";
|
|
5
|
+
if (el) {
|
|
6
|
+
const dir = el.getAttribute("dir");
|
|
7
|
+
if (dir === "rtl" || dir === "ltr" || dir === "auto")
|
|
8
|
+
initial = dir;
|
|
9
|
+
}
|
|
10
|
+
const direction = ref(initial);
|
|
11
|
+
watch(direction, (val) => {
|
|
12
|
+
if (el)
|
|
13
|
+
el.setAttribute("dir", val);
|
|
14
|
+
});
|
|
15
|
+
return direction;
|
|
16
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Ref } from '@stacksjs/stx';
|
|
2
|
+
/**
|
|
3
|
+
* Reactively track the user's text selection.
|
|
4
|
+
*/
|
|
5
|
+
export declare function useTextSelection(): UseTextSelectionReturn;
|
|
6
|
+
export declare interface UseTextSelectionReturn {
|
|
7
|
+
text: Ref<string>
|
|
8
|
+
ranges: Ref<Range[]>
|
|
9
|
+
selection: Ref<Selection | null>
|
|
10
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { onUnmounted, ref } from "@stacksjs/stx";
|
|
2
|
+
export function useTextSelection() {
|
|
3
|
+
const text = ref(""), ranges = ref([]), selection = ref(null);
|
|
4
|
+
if (typeof document > "u")
|
|
5
|
+
return { text, ranges, selection };
|
|
6
|
+
const handler = () => {
|
|
7
|
+
const sel = document.getSelection();
|
|
8
|
+
selection.value = sel;
|
|
9
|
+
if (sel && sel.rangeCount > 0) {
|
|
10
|
+
text.value = sel.toString();
|
|
11
|
+
const newRanges = [];
|
|
12
|
+
for (let i = 0;i < sel.rangeCount; i++)
|
|
13
|
+
newRanges.push(sel.getRangeAt(i));
|
|
14
|
+
ranges.value = newRanges;
|
|
15
|
+
} else {
|
|
16
|
+
text.value = "";
|
|
17
|
+
ranges.value = [];
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
document.addEventListener("selectionchange", handler, { passive: !0 });
|
|
21
|
+
try {
|
|
22
|
+
onUnmounted(() => {
|
|
23
|
+
document.removeEventListener("selectionchange", handler);
|
|
24
|
+
});
|
|
25
|
+
} catch {}
|
|
26
|
+
return { text, ranges, selection };
|
|
27
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Ref } from '@stacksjs/stx';
|
|
2
|
+
/**
|
|
3
|
+
* Automatically adjust textarea height based on content.
|
|
4
|
+
*/
|
|
5
|
+
export declare function useTextareaAutosize(options?: { element?: HTMLTextAreaElement, input?: string }): UseTextareaAutosizeReturn;
|
|
6
|
+
export declare interface UseTextareaAutosizeReturn {
|
|
7
|
+
textarea: Ref<HTMLTextAreaElement | null>
|
|
8
|
+
input: Ref<string>
|
|
9
|
+
triggerResize: () => void
|
|
10
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { ref, watch } from "@stacksjs/stx";
|
|
2
|
+
export function useTextareaAutosize(options) {
|
|
3
|
+
const textarea = ref(options?.element ?? null), input = ref(options?.input ?? "");
|
|
4
|
+
function triggerResize() {
|
|
5
|
+
const el = textarea.value;
|
|
6
|
+
if (!el)
|
|
7
|
+
return;
|
|
8
|
+
el.style.height = "auto";
|
|
9
|
+
el.style.height = `${el.scrollHeight}px`;
|
|
10
|
+
}
|
|
11
|
+
watch(input, () => {
|
|
12
|
+
if (textarea.value)
|
|
13
|
+
textarea.value.value = input.value;
|
|
14
|
+
if (typeof requestAnimationFrame < "u")
|
|
15
|
+
requestAnimationFrame(triggerResize);
|
|
16
|
+
else
|
|
17
|
+
triggerResize();
|
|
18
|
+
});
|
|
19
|
+
return { textarea, input, triggerResize };
|
|
20
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { MaybeRef } from './_shared';
|
|
2
|
+
import type { Ref } from '@stacksjs/stx';
|
|
3
|
+
/**
|
|
4
|
+
* Throttle a value. Returns a ref that updates at most once per interval.
|
|
5
|
+
*
|
|
6
|
+
* @param value - The source value or ref
|
|
7
|
+
* @param ms - Throttle interval in milliseconds. Default: 200
|
|
8
|
+
*/
|
|
9
|
+
export declare function useThrottle<T>(value: MaybeRef<T>, ms?: number): Ref<T>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { ref, watch } from "@stacksjs/stx";
|
|
2
|
+
import { isRef, unref } from "./_shared";
|
|
3
|
+
export function useThrottle(value, ms = 200) {
|
|
4
|
+
const throttled = ref(unref(value));
|
|
5
|
+
let lastUpdate = 0, timer = null;
|
|
6
|
+
if (isRef(value))
|
|
7
|
+
watch(value, (newVal) => {
|
|
8
|
+
const now = Date.now(), elapsed = now - lastUpdate;
|
|
9
|
+
if (elapsed >= ms) {
|
|
10
|
+
throttled.value = newVal;
|
|
11
|
+
lastUpdate = now;
|
|
12
|
+
} else {
|
|
13
|
+
if (timer !== null)
|
|
14
|
+
clearTimeout(timer);
|
|
15
|
+
timer = setTimeout(() => {
|
|
16
|
+
throttled.value = value.value;
|
|
17
|
+
lastUpdate = Date.now();
|
|
18
|
+
timer = null;
|
|
19
|
+
}, ms - elapsed);
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
return throttled;
|
|
23
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Throttle a function.
|
|
3
|
+
* Ensures the function is called at most once every `ms` milliseconds.
|
|
4
|
+
* Supports a trailing call: if the function is called during the throttle
|
|
5
|
+
* period, the last call will be executed after the period ends.
|
|
6
|
+
*
|
|
7
|
+
* @param fn - The function to throttle
|
|
8
|
+
* @param ms - Throttle interval in milliseconds (default 200)
|
|
9
|
+
* @returns The throttled function with a `cancel` method
|
|
10
|
+
*/
|
|
11
|
+
export declare function useThrottleFn<T extends (...args: any[]) => any>(fn: T, ms?: number): T & { cancel: () => void };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export function useThrottleFn(fn, ms = 200) {
|
|
2
|
+
let lastExec = 0, timeoutId = null, lastArgs = null, lastThis = null;
|
|
3
|
+
const throttled = function(...args) {
|
|
4
|
+
const now = Date.now(), elapsed = now - lastExec;
|
|
5
|
+
if (elapsed >= ms) {
|
|
6
|
+
lastExec = now;
|
|
7
|
+
if (timeoutId !== null) {
|
|
8
|
+
clearTimeout(timeoutId);
|
|
9
|
+
timeoutId = null;
|
|
10
|
+
}
|
|
11
|
+
fn.apply(this, args);
|
|
12
|
+
} else {
|
|
13
|
+
lastArgs = args;
|
|
14
|
+
lastThis = this;
|
|
15
|
+
if (timeoutId === null)
|
|
16
|
+
timeoutId = setTimeout(() => {
|
|
17
|
+
lastExec = Date.now();
|
|
18
|
+
timeoutId = null;
|
|
19
|
+
if (lastArgs !== null) {
|
|
20
|
+
fn.apply(lastThis, lastArgs);
|
|
21
|
+
lastArgs = null;
|
|
22
|
+
lastThis = null;
|
|
23
|
+
}
|
|
24
|
+
}, ms - elapsed);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
throttled.cancel = () => {
|
|
28
|
+
if (timeoutId !== null) {
|
|
29
|
+
clearTimeout(timeoutId);
|
|
30
|
+
timeoutId = null;
|
|
31
|
+
}
|
|
32
|
+
lastArgs = null;
|
|
33
|
+
lastThis = null;
|
|
34
|
+
};
|
|
35
|
+
return throttled;
|
|
36
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Ref } from '@stacksjs/stx';
|
|
2
|
+
/**
|
|
3
|
+
* A ref with throttled updates.
|
|
4
|
+
* Changes to the returned ref's value are propagated at most once every `delay` ms.
|
|
5
|
+
* The last value set during a throttle period will be applied after the period ends.
|
|
6
|
+
*
|
|
7
|
+
* @param value - The initial value
|
|
8
|
+
* @param delay - Throttle delay in milliseconds (default 200)
|
|
9
|
+
* @returns A Ref whose internal value updates are throttled
|
|
10
|
+
*/
|
|
11
|
+
export declare function useThrottledRef<T>(value: T, delay?: number): Ref<T>;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { ref, watch } from "@stacksjs/stx";
|
|
2
|
+
export function useThrottledRef(value, delay = 200) {
|
|
3
|
+
const source = ref(value), throttled = ref(value);
|
|
4
|
+
let lastUpdate = 0, timeoutId = null;
|
|
5
|
+
watch(source, (newVal) => {
|
|
6
|
+
const now = Date.now(), elapsed = now - lastUpdate;
|
|
7
|
+
if (elapsed >= delay) {
|
|
8
|
+
lastUpdate = now;
|
|
9
|
+
throttled.value = newVal;
|
|
10
|
+
if (timeoutId !== null) {
|
|
11
|
+
clearTimeout(timeoutId);
|
|
12
|
+
timeoutId = null;
|
|
13
|
+
}
|
|
14
|
+
} else if (timeoutId === null)
|
|
15
|
+
timeoutId = setTimeout(() => {
|
|
16
|
+
lastUpdate = Date.now();
|
|
17
|
+
throttled.value = source.value;
|
|
18
|
+
timeoutId = null;
|
|
19
|
+
}, delay - elapsed);
|
|
20
|
+
});
|
|
21
|
+
return {
|
|
22
|
+
get value() {
|
|
23
|
+
return throttled.value;
|
|
24
|
+
},
|
|
25
|
+
set value(newVal) {
|
|
26
|
+
source.value = newVal;
|
|
27
|
+
},
|
|
28
|
+
subscribe: throttled.subscribe
|
|
29
|
+
};
|
|
30
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { watch } from "@stacksjs/stx";
|
|
2
|
+
import { useManualRefHistory } from "./useManualRefHistory";
|
|
3
|
+
export function useThrottledRefHistory(source, options) {
|
|
4
|
+
const interval = options?.throttle ?? 200, { history, commit, undo, redo, canUndo, canRedo, clear, last } = useManualRefHistory(source, options);
|
|
5
|
+
let lastCommit = 0, timer = null, ignoreNext = !1;
|
|
6
|
+
watch(source, () => {
|
|
7
|
+
if (ignoreNext) {
|
|
8
|
+
ignoreNext = !1;
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
const now = Date.now();
|
|
12
|
+
if (now - lastCommit >= interval) {
|
|
13
|
+
lastCommit = now;
|
|
14
|
+
commit();
|
|
15
|
+
} else {
|
|
16
|
+
if (timer !== null)
|
|
17
|
+
clearTimeout(timer);
|
|
18
|
+
timer = setTimeout(() => {
|
|
19
|
+
timer = null;
|
|
20
|
+
lastCommit = Date.now();
|
|
21
|
+
commit();
|
|
22
|
+
}, interval - (now - lastCommit));
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
const origUndo = undo, origRedo = redo;
|
|
26
|
+
function throttledUndo() {
|
|
27
|
+
if (timer !== null) {
|
|
28
|
+
clearTimeout(timer);
|
|
29
|
+
timer = null;
|
|
30
|
+
}
|
|
31
|
+
ignoreNext = !0;
|
|
32
|
+
origUndo();
|
|
33
|
+
}
|
|
34
|
+
function throttledRedo() {
|
|
35
|
+
if (timer !== null) {
|
|
36
|
+
clearTimeout(timer);
|
|
37
|
+
timer = null;
|
|
38
|
+
}
|
|
39
|
+
ignoreNext = !0;
|
|
40
|
+
origRedo();
|
|
41
|
+
}
|
|
42
|
+
return { history, commit, undo: throttledUndo, redo: throttledRedo, canUndo, canRedo, clear, last };
|
|
43
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type MaybeRefOrGetter } from './_shared';
|
|
2
|
+
import type { Ref } from '@stacksjs/stx';
|
|
3
|
+
/**
|
|
4
|
+
* Reactive relative time string.
|
|
5
|
+
* Converts a date into a human-readable relative time string
|
|
6
|
+
* (e.g., "3 minutes ago", "in 2 hours", "just now").
|
|
7
|
+
* Supports both past and future dates.
|
|
8
|
+
*
|
|
9
|
+
* @param date - The target date (Date, number, or string, or a ref/getter of one)
|
|
10
|
+
* @param options - Configuration options
|
|
11
|
+
* @returns Ref<string> with the formatted time ago string
|
|
12
|
+
*/
|
|
13
|
+
export declare function useTimeAgo(date: MaybeRefOrGetter<Date | number | string>, options?: UseTimeAgoOptions): Ref<string>;
|
|
14
|
+
declare interface UseTimeAgoOptions {
|
|
15
|
+
updateInterval?: number
|
|
16
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { onUnmounted, ref } from "@stacksjs/stx";
|
|
2
|
+
import { toValue } from "./_shared";
|
|
3
|
+
const SECOND = 1000, MINUTE = 60 * SECOND, HOUR = 60 * MINUTE, DAY = 24 * HOUR, WEEK = 7 * DAY, MONTH = 30 * DAY, YEAR = 365 * DAY;
|
|
4
|
+
function formatTimeAgo(diff) {
|
|
5
|
+
const absDiff = Math.abs(diff);
|
|
6
|
+
if (absDiff < 5 * SECOND)
|
|
7
|
+
return "just now";
|
|
8
|
+
const isFuture = diff < 0;
|
|
9
|
+
let value, unit;
|
|
10
|
+
if (absDiff < MINUTE) {
|
|
11
|
+
value = Math.floor(absDiff / SECOND);
|
|
12
|
+
unit = "second";
|
|
13
|
+
} else if (absDiff < HOUR) {
|
|
14
|
+
value = Math.floor(absDiff / MINUTE);
|
|
15
|
+
unit = "minute";
|
|
16
|
+
} else if (absDiff < DAY) {
|
|
17
|
+
value = Math.floor(absDiff / HOUR);
|
|
18
|
+
unit = "hour";
|
|
19
|
+
} else if (absDiff < WEEK) {
|
|
20
|
+
value = Math.floor(absDiff / DAY);
|
|
21
|
+
unit = "day";
|
|
22
|
+
} else if (absDiff < MONTH) {
|
|
23
|
+
value = Math.floor(absDiff / WEEK);
|
|
24
|
+
unit = "week";
|
|
25
|
+
} else if (absDiff < YEAR) {
|
|
26
|
+
value = Math.floor(absDiff / MONTH);
|
|
27
|
+
unit = "month";
|
|
28
|
+
} else {
|
|
29
|
+
value = Math.floor(absDiff / YEAR);
|
|
30
|
+
unit = "year";
|
|
31
|
+
}
|
|
32
|
+
const plural = value !== 1 ? "s" : "";
|
|
33
|
+
if (isFuture)
|
|
34
|
+
return `in ${value} ${unit}${plural}`;
|
|
35
|
+
return `${value} ${unit}${plural} ago`;
|
|
36
|
+
}
|
|
37
|
+
export function useTimeAgo(date, options = {}) {
|
|
38
|
+
const { updateInterval = 30000 } = options;
|
|
39
|
+
function getTimestamp() {
|
|
40
|
+
const d = toValue(date);
|
|
41
|
+
if (d instanceof Date)
|
|
42
|
+
return d.getTime();
|
|
43
|
+
if (typeof d === "number")
|
|
44
|
+
return d;
|
|
45
|
+
return new Date(d).getTime();
|
|
46
|
+
}
|
|
47
|
+
function update() {
|
|
48
|
+
const now = Date.now(), target = getTimestamp(), diff = now - target;
|
|
49
|
+
return formatTimeAgo(diff);
|
|
50
|
+
}
|
|
51
|
+
const timeAgo = ref(update()), intervalId = setInterval(() => {
|
|
52
|
+
timeAgo.value = update();
|
|
53
|
+
}, updateInterval);
|
|
54
|
+
try {
|
|
55
|
+
onUnmounted(() => clearInterval(intervalId));
|
|
56
|
+
} catch {}
|
|
57
|
+
return timeAgo;
|
|
58
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Ref } from '@stacksjs/stx';
|
|
2
|
+
export declare function useTimeout(interval?: number, options?: { controls?: false, immediate?: boolean }): Ref<boolean>;
|
|
3
|
+
export declare function useTimeout(interval?: number, options?: UseTimeoutOptions): Ref<boolean> | UseTimeoutControls;
|
|
4
|
+
declare interface UseTimeoutControls {
|
|
5
|
+
ready: Ref<boolean>
|
|
6
|
+
start: () => void
|
|
7
|
+
stop: () => void
|
|
8
|
+
}
|
|
9
|
+
declare interface UseTimeoutOptions {
|
|
10
|
+
controls?: boolean
|
|
11
|
+
immediate?: boolean
|
|
12
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { onUnmounted, ref } from "@stacksjs/stx";
|
|
2
|
+
export function useTimeout(interval = 1000, options = {}) {
|
|
3
|
+
const { controls = !1, immediate = !0 } = options, ready = ref(!1);
|
|
4
|
+
let timeoutId = null;
|
|
5
|
+
function stop() {
|
|
6
|
+
if (timeoutId !== null) {
|
|
7
|
+
clearTimeout(timeoutId);
|
|
8
|
+
timeoutId = null;
|
|
9
|
+
}
|
|
10
|
+
ready.value = !1;
|
|
11
|
+
}
|
|
12
|
+
function start() {
|
|
13
|
+
stop();
|
|
14
|
+
timeoutId = setTimeout(() => {
|
|
15
|
+
ready.value = !0;
|
|
16
|
+
timeoutId = null;
|
|
17
|
+
}, interval);
|
|
18
|
+
}
|
|
19
|
+
if (immediate)
|
|
20
|
+
start();
|
|
21
|
+
try {
|
|
22
|
+
onUnmounted(() => {
|
|
23
|
+
if (timeoutId !== null) {
|
|
24
|
+
clearTimeout(timeoutId);
|
|
25
|
+
timeoutId = null;
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
} catch {}
|
|
29
|
+
if (controls)
|
|
30
|
+
return { ready, start, stop };
|
|
31
|
+
return ready;
|
|
32
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Ref } from '@stacksjs/stx';
|
|
2
|
+
/**
|
|
3
|
+
* Call a function after a timeout.
|
|
4
|
+
* Provides start/stop controls and auto-cleanup on unmount.
|
|
5
|
+
*
|
|
6
|
+
* @param cb - The callback to invoke after the timeout
|
|
7
|
+
* @param interval - Timeout duration in milliseconds (default 1000)
|
|
8
|
+
* @param options - Configuration options (immediate defaults to true)
|
|
9
|
+
* @returns Controls to start, stop, and check pending state
|
|
10
|
+
*/
|
|
11
|
+
export declare function useTimeoutFn(cb: () => void, interval?: number, options?: { immediate?: boolean }): UseTimeoutFnReturn;
|
|
12
|
+
declare interface UseTimeoutFnReturn {
|
|
13
|
+
start: () => void
|
|
14
|
+
stop: () => void
|
|
15
|
+
isPending: Ref<boolean>
|
|
16
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { onUnmounted, ref } from "@stacksjs/stx";
|
|
2
|
+
export function useTimeoutFn(cb, interval = 1000, options = {}) {
|
|
3
|
+
const { immediate = !0 } = options, isPending = ref(!1);
|
|
4
|
+
let timeoutId = null;
|
|
5
|
+
function stop() {
|
|
6
|
+
if (timeoutId !== null) {
|
|
7
|
+
clearTimeout(timeoutId);
|
|
8
|
+
timeoutId = null;
|
|
9
|
+
}
|
|
10
|
+
isPending.value = !1;
|
|
11
|
+
}
|
|
12
|
+
function start() {
|
|
13
|
+
stop();
|
|
14
|
+
isPending.value = !0;
|
|
15
|
+
timeoutId = setTimeout(() => {
|
|
16
|
+
timeoutId = null;
|
|
17
|
+
isPending.value = !1;
|
|
18
|
+
cb();
|
|
19
|
+
}, interval);
|
|
20
|
+
}
|
|
21
|
+
if (immediate)
|
|
22
|
+
start();
|
|
23
|
+
try {
|
|
24
|
+
onUnmounted(stop);
|
|
25
|
+
} catch {}
|
|
26
|
+
return { start, stop, isPending };
|
|
27
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Ref } from '@stacksjs/stx';
|
|
2
|
+
/**
|
|
3
|
+
* Poll a function with setTimeout. Waits for the function to complete before scheduling the next.
|
|
4
|
+
*/
|
|
5
|
+
export declare function useTimeoutPoll(fn: () => Promise<void> | void, interval: number, options?: { immediate?: boolean }): UseTimeoutPollReturn;
|
|
6
|
+
export declare interface UseTimeoutPollReturn {
|
|
7
|
+
isActive: Ref<boolean>
|
|
8
|
+
pause: () => void
|
|
9
|
+
resume: () => void
|
|
10
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { onUnmounted, ref } from "@stacksjs/stx";
|
|
2
|
+
export function useTimeoutPoll(fn, interval, options) {
|
|
3
|
+
const { immediate = !0 } = options ?? {}, isActive = ref(!1);
|
|
4
|
+
let timer = null;
|
|
5
|
+
async function poll() {
|
|
6
|
+
if (!isActive.value)
|
|
7
|
+
return;
|
|
8
|
+
try {
|
|
9
|
+
await fn();
|
|
10
|
+
} catch {}
|
|
11
|
+
if (isActive.value)
|
|
12
|
+
timer = setTimeout(poll, interval);
|
|
13
|
+
}
|
|
14
|
+
function resume() {
|
|
15
|
+
if (isActive.value)
|
|
16
|
+
return;
|
|
17
|
+
isActive.value = !0;
|
|
18
|
+
poll();
|
|
19
|
+
}
|
|
20
|
+
function pause() {
|
|
21
|
+
isActive.value = !1;
|
|
22
|
+
if (timer !== null) {
|
|
23
|
+
clearTimeout(timer);
|
|
24
|
+
timer = null;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
if (immediate)
|
|
28
|
+
resume();
|
|
29
|
+
try {
|
|
30
|
+
onUnmounted(() => pause());
|
|
31
|
+
} catch {}
|
|
32
|
+
return { isActive, pause, resume };
|
|
33
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Ref } from '@stacksjs/stx';
|
|
2
|
+
/**
|
|
3
|
+
* Reactive current timestamp.
|
|
4
|
+
* Returns a ref containing Date.now() + offset, updated on a regular interval
|
|
5
|
+
* or via requestAnimationFrame.
|
|
6
|
+
*
|
|
7
|
+
* @param options - Configuration options
|
|
8
|
+
* @returns Ref<number> with the current timestamp
|
|
9
|
+
*/
|
|
10
|
+
export declare function useTimestamp(options?: UseTimestampOptions): Ref<number>;
|
|
11
|
+
declare interface UseTimestampOptions {
|
|
12
|
+
offset?: number
|
|
13
|
+
interval?: number | 'requestAnimationFrame'
|
|
14
|
+
immediate?: boolean
|
|
15
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { onUnmounted, ref } from "@stacksjs/stx";
|
|
2
|
+
export function useTimestamp(options = {}) {
|
|
3
|
+
const { offset = 0, interval = 1000, immediate = !0 } = options, timestamp = ref(Date.now() + offset);
|
|
4
|
+
let intervalId = null, rafId = null, active = !1;
|
|
5
|
+
function update() {
|
|
6
|
+
timestamp.value = Date.now() + offset;
|
|
7
|
+
}
|
|
8
|
+
function cleanup() {
|
|
9
|
+
if (intervalId !== null) {
|
|
10
|
+
clearInterval(intervalId);
|
|
11
|
+
intervalId = null;
|
|
12
|
+
}
|
|
13
|
+
if (rafId !== null && typeof cancelAnimationFrame < "u") {
|
|
14
|
+
cancelAnimationFrame(rafId);
|
|
15
|
+
rafId = null;
|
|
16
|
+
}
|
|
17
|
+
active = !1;
|
|
18
|
+
}
|
|
19
|
+
function start() {
|
|
20
|
+
if (active)
|
|
21
|
+
return;
|
|
22
|
+
active = !0;
|
|
23
|
+
update();
|
|
24
|
+
if (interval === "requestAnimationFrame") {
|
|
25
|
+
if (typeof requestAnimationFrame < "u") {
|
|
26
|
+
const tick = () => {
|
|
27
|
+
if (!active)
|
|
28
|
+
return;
|
|
29
|
+
update();
|
|
30
|
+
rafId = requestAnimationFrame(tick);
|
|
31
|
+
};
|
|
32
|
+
rafId = requestAnimationFrame(tick);
|
|
33
|
+
}
|
|
34
|
+
} else
|
|
35
|
+
intervalId = setInterval(update, interval);
|
|
36
|
+
}
|
|
37
|
+
if (immediate)
|
|
38
|
+
start();
|
|
39
|
+
try {
|
|
40
|
+
onUnmounted(cleanup);
|
|
41
|
+
} catch {}
|
|
42
|
+
return timestamp;
|
|
43
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { MaybeRefOrGetter } from './_shared';
|
|
2
|
+
import type { Ref } from '@stacksjs/stx';
|
|
3
|
+
/**
|
|
4
|
+
* Reactive document title.
|
|
5
|
+
* Returns a writable ref synced with document.title.
|
|
6
|
+
* If no argument is provided, reads the current document title.
|
|
7
|
+
*
|
|
8
|
+
* @param newTitle - Optional initial title (or ref/getter)
|
|
9
|
+
*/
|
|
10
|
+
export declare function useTitle(newTitle?: MaybeRefOrGetter<string>): Ref<string>;
|
package/dist/useTitle.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ref, watch } from "@stacksjs/stx";
|
|
2
|
+
import { defaultDocument, toValue } from "./_shared";
|
|
3
|
+
export function useTitle(newTitle) {
|
|
4
|
+
const doc = defaultDocument(), initialTitle = newTitle !== void 0 ? toValue(newTitle) : doc?.title ?? "", title = ref(initialTitle);
|
|
5
|
+
if (doc && newTitle !== void 0)
|
|
6
|
+
doc.title = title.value;
|
|
7
|
+
watch(title, (val) => {
|
|
8
|
+
if (doc)
|
|
9
|
+
doc.title = val;
|
|
10
|
+
});
|
|
11
|
+
return title;
|
|
12
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { MaybeRefOrGetter } from './_shared';
|
|
2
|
+
import type { Ref } from '@stacksjs/stx';
|
|
3
|
+
/**
|
|
4
|
+
* Reactively convert a string ref to a number.
|
|
5
|
+
*/
|
|
6
|
+
export declare function useToNumber(value: MaybeRefOrGetter<string | number>, options?: UseToNumberOptions): Ref<number>;
|
|
7
|
+
export declare interface UseToNumberOptions {
|
|
8
|
+
method?: 'parseFloat' | 'parseInt'
|
|
9
|
+
radix?: number
|
|
10
|
+
nanToZero?: boolean
|
|
11
|
+
}
|