@stacksjs/composables 0.70.257 → 0.70.259

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.
Files changed (167) hide show
  1. package/dist/_shared.js +1 -23
  2. package/dist/cloneFnJSON.js +1 -3
  3. package/dist/computedAsync.js +1 -20
  4. package/dist/computedEager.js +1 -10
  5. package/dist/createEventHook.js +1 -15
  6. package/dist/createFetch.js +1 -8
  7. package/dist/createGlobalState.js +1 -10
  8. package/dist/createSharedComposable.js +1 -9
  9. package/dist/extendRef.js +1 -6
  10. package/dist/formatTimeAgo.js +1 -33
  11. package/dist/index.js +1 -210
  12. package/dist/isDefined.js +1 -3
  13. package/dist/makeDestructurable.js +1 -16
  14. package/dist/onClickOutside.js +1 -25
  15. package/dist/onKeyStroke.js +1 -31
  16. package/dist/onLongPress.js +1 -43
  17. package/dist/onStartTyping.js +1 -23
  18. package/dist/refAutoReset.js +1 -15
  19. package/dist/refDefault.js +1 -8
  20. package/dist/resolveRef.js +1 -7
  21. package/dist/ssrHandlers.js +1 -8
  22. package/dist/syncRef.js +1 -35
  23. package/dist/tryOnMounted.js +1 -22
  24. package/dist/unrefElement.js +1 -9
  25. package/dist/until.js +1 -62
  26. package/dist/useActiveElement.js +1 -19
  27. package/dist/useAnimate.js +1 -48
  28. package/dist/useArrayUtils.js +1 -114
  29. package/dist/useAsyncQueue.js +1 -22
  30. package/dist/useAsyncState.js +1 -35
  31. package/dist/useBase64.js +1 -45
  32. package/dist/useBattery.js +1 -38
  33. package/dist/useBreakpoints.js +1 -97
  34. package/dist/useBroadcastChannel.js +1 -31
  35. package/dist/useBrowserLocation.js +1 -64
  36. package/dist/useClipboard.js +1 -32
  37. package/dist/useClipboardItems.js +1 -15
  38. package/dist/useCloned.js +1 -16
  39. package/dist/useColorMode.js +1 -60
  40. package/dist/useConfirmDialog.js +1 -34
  41. package/dist/useCounter.js +1 -28
  42. package/dist/useCssVar.js +1 -23
  43. package/dist/useCycleList.js +1 -27
  44. package/dist/useDark.js +1 -38
  45. package/dist/useDateFormat.js +1 -11
  46. package/dist/useDebounce.js +1 -16
  47. package/dist/useDebounceFn.js +1 -18
  48. package/dist/useDebouncedRef.js +1 -22
  49. package/dist/useDebouncedRefHistory.js +1 -36
  50. package/dist/useDeviceMotion.js +1 -53
  51. package/dist/useDeviceOrientation.js +1 -23
  52. package/dist/useDevicePixelRatio.js +1 -24
  53. package/dist/useDocumentVisibility.js +1 -16
  54. package/dist/useDraggable.js +1 -58
  55. package/dist/useDropZone.js +1 -52
  56. package/dist/useElementBounding.js +1 -59
  57. package/dist/useElementByPoint.js +1 -18
  58. package/dist/useElementHover.js +1 -28
  59. package/dist/useElementSize.js +1 -34
  60. package/dist/useElementVisibility.js +1 -10
  61. package/dist/useEventBus.js +1 -41
  62. package/dist/useEventListener.js +1 -29
  63. package/dist/useEventSource.js +1 -72
  64. package/dist/useEyeDropper.js +1 -17
  65. package/dist/useFavicon.js +1 -22
  66. package/dist/useFetch.js +1 -61
  67. package/dist/useFileDialog.js +1 -24
  68. package/dist/useFocus.js +1 -42
  69. package/dist/useFocusWithin.js +1 -29
  70. package/dist/useForm.js +1 -182
  71. package/dist/useFps.js +1 -22
  72. package/dist/useFullscreen.js +1 -67
  73. package/dist/useGeolocation.js +1 -55
  74. package/dist/useIdle.js +1 -46
  75. package/dist/useImage.js +1 -32
  76. package/dist/useInfiniteScroll.js +1 -50
  77. package/dist/useIntersectionObserver.js +1 -27
  78. package/dist/useInterval.js +1 -32
  79. package/dist/useIntervalFn.js +1 -24
  80. package/dist/useKeyModifier.js +1 -22
  81. package/dist/useLastChanged.js +1 -8
  82. package/dist/useLocalStorage.js +1 -4
  83. package/dist/useMagicKeys.js +1 -81
  84. package/dist/useManualRefHistory.js +1 -64
  85. package/dist/useMath.js +1 -52
  86. package/dist/useMediaQuery.js +1 -19
  87. package/dist/useMemoize.js +1 -24
  88. package/dist/useMounted.js +1 -8
  89. package/dist/useMouse.js +1 -67
  90. package/dist/useMouseInElement.js +1 -31
  91. package/dist/useMousePressed.js +1 -31
  92. package/dist/useMutationObserver.js +1 -27
  93. package/dist/useNavigatorLanguage.js +1 -17
  94. package/dist/useNetwork.js +1 -58
  95. package/dist/useNow.js +1 -10
  96. package/dist/useObjectUrl.js +1 -23
  97. package/dist/useOffsetPagination.js +1 -45
  98. package/dist/useOnline.js +1 -20
  99. package/dist/usePageLeave.js +1 -27
  100. package/dist/useParallax.js +1 -33
  101. package/dist/useParentElement.js +1 -8
  102. package/dist/usePermission.js +1 -21
  103. package/dist/usePointer.js +1 -32
  104. package/dist/usePointerLock.js +1 -33
  105. package/dist/usePointerSwipe.js +1 -52
  106. package/dist/usePreferredColorScheme.js +1 -25
  107. package/dist/usePreferredContrast.js +1 -29
  108. package/dist/usePreferredLanguages.js +1 -16
  109. package/dist/usePreferredReducedMotion.js +1 -18
  110. package/dist/usePrevious.js +1 -8
  111. package/dist/useQuery.js +1 -265
  112. package/dist/useRafFn.js +1 -37
  113. package/dist/useRefHistory.js +1 -72
  114. package/dist/useResizeObserver.js +1 -27
  115. package/dist/useScreenOrientation.js +1 -27
  116. package/dist/useScreenSafeArea.js +1 -25
  117. package/dist/useScriptTag.js +1 -52
  118. package/dist/useScroll.js +1 -116
  119. package/dist/useScrollLock.js +1 -39
  120. package/dist/useSessionStorage.js +1 -4
  121. package/dist/useShare.js +1 -11
  122. package/dist/useSorted.js +1 -10
  123. package/dist/useStepper.js +1 -46
  124. package/dist/useStorage.js +1 -54
  125. package/dist/useStyleTag.js +1 -39
  126. package/dist/useSupported.js +1 -10
  127. package/dist/useSwipe.js +1 -58
  128. package/dist/useTextDirection.js +1 -16
  129. package/dist/useTextSelection.js +1 -27
  130. package/dist/useTextareaAutosize.js +1 -20
  131. package/dist/useThrottle.js +1 -23
  132. package/dist/useThrottleFn.js +1 -36
  133. package/dist/useThrottledRef.js +1 -30
  134. package/dist/useThrottledRefHistory.js +1 -43
  135. package/dist/useTimeAgo.js +1 -58
  136. package/dist/useTimeout.js +1 -32
  137. package/dist/useTimeoutFn.js +1 -27
  138. package/dist/useTimeoutPoll.js +1 -33
  139. package/dist/useTimestamp.js +1 -43
  140. package/dist/useTitle.js +1 -12
  141. package/dist/useToNumber.js +1 -25
  142. package/dist/useToString.js +1 -10
  143. package/dist/useToggle.js +1 -12
  144. package/dist/useTransition.js +1 -89
  145. package/dist/useUrlSearchParams.js +1 -72
  146. package/dist/useVibrate.js +1 -14
  147. package/dist/useVirtualList.js +1 -49
  148. package/dist/useWakeLock.js +1 -32
  149. package/dist/useWebNotification.js +1 -39
  150. package/dist/useWebSocket.js +1 -103
  151. package/dist/useWebWorker.js +1 -56
  152. package/dist/useWindowFocus.js +1 -20
  153. package/dist/useWindowScroll.js +1 -17
  154. package/dist/useWindowSize.js +1 -28
  155. package/dist/watchArray.js +1 -9
  156. package/dist/watchAtMost.js +1 -12
  157. package/dist/watchDebounced.js +1 -20
  158. package/dist/watchDeep.js +1 -13
  159. package/dist/watchIgnorable.js +1 -19
  160. package/dist/watchImmediate.js +1 -7
  161. package/dist/watchOnce.js +1 -17
  162. package/dist/watchPausable.js +1 -23
  163. package/dist/watchThrottled.js +1 -37
  164. package/dist/watchTriggerable.js +1 -10
  165. package/dist/watchWithFilter.js +1 -6
  166. package/dist/whenever.js +1 -7
  167. package/package.json +2 -2
@@ -1,27 +1 @@
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
- }
1
+ import{onUnmounted,ref}from"@stacksjs/stx";export function useTextSelection(){const text=ref(""),ranges=ref([]),selection=ref(null);if(typeof document>"u")return{text,ranges,selection};const handler=()=>{const sel=document.getSelection();selection.value=sel;if(sel&&sel.rangeCount>0){text.value=sel.toString();const newRanges=[];for(let i=0;i<sel.rangeCount;i++)newRanges.push(sel.getRangeAt(i));ranges.value=newRanges}else{text.value="";ranges.value=[]}};document.addEventListener("selectionchange",handler,{passive:!0});try{onUnmounted(()=>{document.removeEventListener("selectionchange",handler)})}catch{}return{text,ranges,selection}}
@@ -1,20 +1 @@
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
- }
1
+ import{ref,watch}from"@stacksjs/stx";export function useTextareaAutosize(options){const textarea=ref(options?.element??null),input=ref(options?.input??"");function triggerResize(){const el=textarea.value;if(!el)return;el.style.height="auto";el.style.height=`${el.scrollHeight}px`}watch(input,()=>{if(textarea.value)textarea.value.value=input.value;if(typeof requestAnimationFrame<"u")requestAnimationFrame(triggerResize);else triggerResize()});return{textarea,input,triggerResize}}
@@ -1,23 +1 @@
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
- }
1
+ import{ref,watch}from"@stacksjs/stx";import{isRef,unref}from"./_shared";export function useThrottle(value,ms=200){const throttled=ref(unref(value));let lastUpdate=0,timer=null;if(isRef(value))watch(value,(newVal)=>{const now=Date.now(),elapsed=now-lastUpdate;if(elapsed>=ms){throttled.value=newVal;lastUpdate=now}else{if(timer!==null)clearTimeout(timer);timer=setTimeout(()=>{throttled.value=value.value;lastUpdate=Date.now();timer=null},ms-elapsed)}});return throttled}
@@ -1,36 +1 @@
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
- }
1
+ export function useThrottleFn(fn,ms=200){let lastExec=0,timeoutId=null,lastArgs=null,lastThis=null;const throttled=function(...args){const now=Date.now(),elapsed=now-lastExec;if(elapsed>=ms){lastExec=now;if(timeoutId!==null){clearTimeout(timeoutId);timeoutId=null}fn.apply(this,args)}else{lastArgs=args;lastThis=this;if(timeoutId===null)timeoutId=setTimeout(()=>{lastExec=Date.now();timeoutId=null;if(lastArgs!==null){fn.apply(lastThis,lastArgs);lastArgs=null;lastThis=null}},ms-elapsed)}};throttled.cancel=()=>{if(timeoutId!==null){clearTimeout(timeoutId);timeoutId=null}lastArgs=null;lastThis=null};return throttled}
@@ -1,30 +1 @@
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
- }
1
+ import{ref,watch}from"@stacksjs/stx";export function useThrottledRef(value,delay=200){const source=ref(value),throttled=ref(value);let lastUpdate=0,timeoutId=null;watch(source,(newVal)=>{const now=Date.now(),elapsed=now-lastUpdate;if(elapsed>=delay){lastUpdate=now;throttled.value=newVal;if(timeoutId!==null){clearTimeout(timeoutId);timeoutId=null}}else if(timeoutId===null)timeoutId=setTimeout(()=>{lastUpdate=Date.now();throttled.value=source.value;timeoutId=null},delay-elapsed)});return{get value(){return throttled.value},set value(newVal){source.value=newVal},subscribe:throttled.subscribe}}
@@ -1,43 +1 @@
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
- }
1
+ import{watch}from"@stacksjs/stx";import{useManualRefHistory}from"./useManualRefHistory";export function useThrottledRefHistory(source,options){const interval=options?.throttle??200,{history,commit,undo,redo,canUndo,canRedo,clear,last}=useManualRefHistory(source,options);let lastCommit=0,timer=null,ignoreNext=!1;watch(source,()=>{if(ignoreNext){ignoreNext=!1;return}const now=Date.now();if(now-lastCommit>=interval){lastCommit=now;commit()}else{if(timer!==null)clearTimeout(timer);timer=setTimeout(()=>{timer=null;lastCommit=Date.now();commit()},interval-(now-lastCommit))}});const origUndo=undo,origRedo=redo;function throttledUndo(){if(timer!==null){clearTimeout(timer);timer=null}ignoreNext=!0;origUndo()}function throttledRedo(){if(timer!==null){clearTimeout(timer);timer=null}ignoreNext=!0;origRedo()}return{history,commit,undo:throttledUndo,redo:throttledRedo,canUndo,canRedo,clear,last}}
@@ -1,58 +1 @@
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
- }
1
+ import{onUnmounted,ref}from"@stacksjs/stx";import{toValue}from"./_shared";const SECOND=1000,MINUTE=60*SECOND,HOUR=60*MINUTE,DAY=24*HOUR,WEEK=7*DAY,MONTH=30*DAY,YEAR=365*DAY;function formatTimeAgo(diff){const absDiff=Math.abs(diff);if(absDiff<5*SECOND)return"just now";const isFuture=diff<0;let value,unit;if(absDiff<MINUTE){value=Math.floor(absDiff/SECOND);unit="second"}else if(absDiff<HOUR){value=Math.floor(absDiff/MINUTE);unit="minute"}else if(absDiff<DAY){value=Math.floor(absDiff/HOUR);unit="hour"}else if(absDiff<WEEK){value=Math.floor(absDiff/DAY);unit="day"}else if(absDiff<MONTH){value=Math.floor(absDiff/WEEK);unit="week"}else if(absDiff<YEAR){value=Math.floor(absDiff/MONTH);unit="month"}else{value=Math.floor(absDiff/YEAR);unit="year"}const plural=value!==1?"s":"";if(isFuture)return`in ${value} ${unit}${plural}`;return`${value} ${unit}${plural} ago`}export function useTimeAgo(date,options={}){const{updateInterval=30000}=options;function getTimestamp(){const d=toValue(date);if(d instanceof Date)return d.getTime();if(typeof d==="number")return d;return new Date(d).getTime()}function update(){const now=Date.now(),target=getTimestamp(),diff=now-target;return formatTimeAgo(diff)}const timeAgo=ref(update()),intervalId=setInterval(()=>{timeAgo.value=update()},updateInterval);try{onUnmounted(()=>clearInterval(intervalId))}catch{}return timeAgo}
@@ -1,32 +1 @@
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
- }
1
+ import{onUnmounted,ref}from"@stacksjs/stx";export function useTimeout(interval=1000,options={}){const{controls=!1,immediate=!0}=options,ready=ref(!1);let timeoutId=null;function stop(){if(timeoutId!==null){clearTimeout(timeoutId);timeoutId=null}ready.value=!1}function start(){stop();timeoutId=setTimeout(()=>{ready.value=!0;timeoutId=null},interval)}if(immediate)start();try{onUnmounted(()=>{if(timeoutId!==null){clearTimeout(timeoutId);timeoutId=null}})}catch{}if(controls)return{ready,start,stop};return ready}
@@ -1,27 +1 @@
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
- }
1
+ import{onUnmounted,ref}from"@stacksjs/stx";export function useTimeoutFn(cb,interval=1000,options={}){const{immediate=!0}=options,isPending=ref(!1);let timeoutId=null;function stop(){if(timeoutId!==null){clearTimeout(timeoutId);timeoutId=null}isPending.value=!1}function start(){stop();isPending.value=!0;timeoutId=setTimeout(()=>{timeoutId=null;isPending.value=!1;cb()},interval)}if(immediate)start();try{onUnmounted(stop)}catch{}return{start,stop,isPending}}
@@ -1,33 +1 @@
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
- }
1
+ import{onUnmounted,ref}from"@stacksjs/stx";export function useTimeoutPoll(fn,interval,options){const{immediate=!0}=options??{},isActive=ref(!1);let timer=null;async function poll(){if(!isActive.value)return;try{await fn()}catch{}if(isActive.value)timer=setTimeout(poll,interval)}function resume(){if(isActive.value)return;isActive.value=!0;poll()}function pause(){isActive.value=!1;if(timer!==null){clearTimeout(timer);timer=null}}if(immediate)resume();try{onUnmounted(()=>pause())}catch{}return{isActive,pause,resume}}
@@ -1,43 +1 @@
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
- }
1
+ import{onUnmounted,ref}from"@stacksjs/stx";export function useTimestamp(options={}){const{offset=0,interval=1000,immediate=!0}=options,timestamp=ref(Date.now()+offset);let intervalId=null,rafId=null,active=!1;function update(){timestamp.value=Date.now()+offset}function cleanup(){if(intervalId!==null){clearInterval(intervalId);intervalId=null}if(rafId!==null&&typeof cancelAnimationFrame<"u"){cancelAnimationFrame(rafId);rafId=null}active=!1}function start(){if(active)return;active=!0;update();if(interval==="requestAnimationFrame"){if(typeof requestAnimationFrame<"u"){const tick=()=>{if(!active)return;update();rafId=requestAnimationFrame(tick)};rafId=requestAnimationFrame(tick)}}else intervalId=setInterval(update,interval)}if(immediate)start();try{onUnmounted(cleanup)}catch{}return timestamp}
package/dist/useTitle.js CHANGED
@@ -1,12 +1 @@
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
- }
1
+ import{ref,watch}from"@stacksjs/stx";import{defaultDocument,toValue}from"./_shared";export function useTitle(newTitle){const doc=defaultDocument(),initialTitle=newTitle!==void 0?toValue(newTitle):doc?.title??"",title=ref(initialTitle);if(doc&&newTitle!==void 0)doc.title=title.value;watch(title,(val)=>{if(doc)doc.title=val});return title}
@@ -1,25 +1 @@
1
- import { computed, ref, watch } from "@stacksjs/stx";
2
- import { toValue } from "./_shared";
3
- export function useToNumber(value, options = {}) {
4
- const { method = "parseFloat", radix = 10, nanToZero = !1 } = options, result = ref(0);
5
- function convert(val) {
6
- if (typeof val === "number")
7
- return val;
8
- let num;
9
- if (method === "parseInt")
10
- num = Number.parseInt(val, radix);
11
- else
12
- num = Number.parseFloat(val);
13
- if (nanToZero && Number.isNaN(num))
14
- return 0;
15
- return num;
16
- }
17
- result.value = convert(toValue(value));
18
- if (typeof value === "function")
19
- ;
20
- else if (value && typeof value === "object" && "value" in value && "subscribe" in value)
21
- watch(value, (newVal) => {
22
- result.value = convert(newVal);
23
- });
24
- return result;
25
- }
1
+ import{computed,ref,watch}from"@stacksjs/stx";import{toValue}from"./_shared";export function useToNumber(value,options={}){const{method="parseFloat",radix=10,nanToZero=!1}=options,result=ref(0);function convert(val){if(typeof val==="number")return val;let num;if(method==="parseInt")num=Number.parseInt(val,radix);else num=Number.parseFloat(val);if(nanToZero&&Number.isNaN(num))return 0;return num}result.value=convert(toValue(value));if(typeof value==="function");else if(value&&typeof value==="object"&&"value"in value&&"subscribe"in value)watch(value,(newVal)=>{result.value=convert(newVal)});return result}
@@ -1,10 +1 @@
1
- import { ref, watch } from "@stacksjs/stx";
2
- import { isRef, toValue } from "./_shared";
3
- export function useToString(value) {
4
- const result = ref(String(toValue(value) ?? ""));
5
- if (isRef(value))
6
- watch(value, (newVal) => {
7
- result.value = String(newVal ?? "");
8
- });
9
- return result;
10
- }
1
+ import{ref,watch}from"@stacksjs/stx";import{isRef,toValue}from"./_shared";export function useToString(value){const result=ref(String(toValue(value)??""));if(isRef(value))watch(value,(newVal)=>{result.value=String(newVal??"")});return result}
package/dist/useToggle.js CHANGED
@@ -1,12 +1 @@
1
- import { ref } from "@stacksjs/stx";
2
- export function useToggle(initialValue = !1) {
3
- const state = typeof initialValue === "boolean" ? ref(initialValue) : initialValue;
4
- function toggle(value) {
5
- if (typeof value === "boolean")
6
- state.value = value;
7
- else
8
- state.value = !state.value;
9
- return state.value;
10
- }
11
- return [state, toggle];
12
- }
1
+ import{ref}from"@stacksjs/stx";export function useToggle(initialValue=!1){const state=typeof initialValue==="boolean"?ref(initialValue):initialValue;function toggle(value){if(typeof value==="boolean")state.value=value;else state.value=!state.value;return state.value}return[state,toggle]}
@@ -1,89 +1 @@
1
- import { onUnmounted, ref, watch } from "@stacksjs/stx";
2
- import { unref } from "./_shared";
3
- export const TransitionPresets = {
4
- linear: (t) => t,
5
- easeInQuad: (t) => t * t,
6
- easeOutQuad: (t) => t * (2 - t),
7
- easeInOutQuad: (t) => t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t,
8
- easeInCubic: (t) => t * t * t,
9
- easeOutCubic: (t) => --t * t * t + 1,
10
- easeInOutCubic: (t) => t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1
11
- };
12
- export function useTransition(source, options = {}) {
13
- const {
14
- duration = 300,
15
- transition = TransitionPresets.linear,
16
- delay = 0,
17
- onFinished,
18
- onStarted
19
- } = options, output = ref(unref(source));
20
- let rafId = null;
21
- function startTransition(from, to) {
22
- if (rafId !== null)
23
- cancelAnimationFrame(rafId);
24
- const start = performance.now() + delay;
25
- onStarted?.();
26
- function tick(now) {
27
- const elapsed = now - start;
28
- if (elapsed < 0) {
29
- rafId = requestAnimationFrame(tick);
30
- return;
31
- }
32
- const progress = Math.min(elapsed / duration, 1), eased = transition(progress);
33
- output.value = from + (to - from) * eased;
34
- if (progress < 1)
35
- rafId = requestAnimationFrame(tick);
36
- else {
37
- rafId = null;
38
- output.value = to;
39
- onFinished?.();
40
- }
41
- }
42
- rafId = requestAnimationFrame(tick);
43
- }
44
- if (typeof source === "object" && source && "value" in source && "subscribe" in source)
45
- watch(source, (newValue) => {
46
- if (typeof requestAnimationFrame < "u")
47
- startTransition(output.value, newValue);
48
- else
49
- output.value = newValue;
50
- });
51
- try {
52
- onUnmounted(() => {
53
- if (rafId !== null)
54
- cancelAnimationFrame(rafId);
55
- });
56
- } catch {}
57
- return output;
58
- }
59
- export function executeTransition(_from, _to, options = {}) {
60
- return new Promise((resolve) => {
61
- const {
62
- duration = 300,
63
- transition = TransitionPresets.linear,
64
- delay = 0,
65
- onFinished,
66
- onStarted
67
- } = options;
68
- if (typeof requestAnimationFrame > "u") {
69
- onFinished?.();
70
- resolve();
71
- return;
72
- }
73
- const start = performance.now() + delay;
74
- onStarted?.();
75
- function tick(now) {
76
- const elapsed = now - start;
77
- if (elapsed < 0) {
78
- requestAnimationFrame(tick);
79
- return;
80
- }
81
- if (Math.min(elapsed / duration, 1) >= 1) {
82
- onFinished?.();
83
- resolve();
84
- } else
85
- requestAnimationFrame(tick);
86
- }
87
- requestAnimationFrame(tick);
88
- });
89
- }
1
+ import{onUnmounted,ref,watch}from"@stacksjs/stx";import{unref}from"./_shared";export const TransitionPresets={linear:(t)=>t,easeInQuad:(t)=>t*t,easeOutQuad:(t)=>t*(2-t),easeInOutQuad:(t)=>t<0.5?2*t*t:-1+(4-2*t)*t,easeInCubic:(t)=>t*t*t,easeOutCubic:(t)=>--t*t*t+1,easeInOutCubic:(t)=>t<0.5?4*t*t*t:(t-1)*(2*t-2)*(2*t-2)+1};export function useTransition(source,options={}){const{duration=300,transition=TransitionPresets.linear,delay=0,onFinished,onStarted}=options,output=ref(unref(source));let rafId=null;function startTransition(from,to){if(rafId!==null)cancelAnimationFrame(rafId);const start=performance.now()+delay;onStarted?.();function tick(now){const elapsed=now-start;if(elapsed<0){rafId=requestAnimationFrame(tick);return}const progress=Math.min(elapsed/duration,1),eased=transition(progress);output.value=from+(to-from)*eased;if(progress<1)rafId=requestAnimationFrame(tick);else{rafId=null;output.value=to;onFinished?.()}}rafId=requestAnimationFrame(tick)}if(typeof source==="object"&&source&&"value"in source&&"subscribe"in source)watch(source,(newValue)=>{if(typeof requestAnimationFrame<"u")startTransition(output.value,newValue);else output.value=newValue});try{onUnmounted(()=>{if(rafId!==null)cancelAnimationFrame(rafId)})}catch{}return output}export function executeTransition(_from,_to,options={}){return new Promise((resolve)=>{const{duration=300,transition=TransitionPresets.linear,delay=0,onFinished,onStarted}=options;if(typeof requestAnimationFrame>"u"){onFinished?.();resolve();return}const start=performance.now()+delay;onStarted?.();function tick(now){const elapsed=now-start;if(elapsed<0){requestAnimationFrame(tick);return}if(Math.min(elapsed/duration,1)>=1){onFinished?.();resolve()}else requestAnimationFrame(tick)}requestAnimationFrame(tick)})}
@@ -1,72 +1 @@
1
- import { onUnmounted, ref } from "@stacksjs/stx";
2
- import { defaultWindow } from "./_shared";
3
- export function useUrlSearchParams(mode = "history", options) {
4
- const win = defaultWindow(), { initialValue } = options ?? {};
5
- function readParams() {
6
- if (!win)
7
- return { ...initialValue };
8
- let search = "";
9
- if (mode === "hash") {
10
- const hash = win.location.hash, idx = hash.indexOf("?");
11
- search = idx >= 0 ? hash.slice(idx + 1) : "";
12
- } else
13
- search = win.location.search.slice(1);
14
- const result = { ...initialValue };
15
- new URLSearchParams(search).forEach((value, key) => {
16
- result[key] = value;
17
- });
18
- return result;
19
- }
20
- const params = ref(readParams());
21
- function writeParams() {
22
- if (!win)
23
- return;
24
- const searchParams = new URLSearchParams;
25
- for (const [key, value] of Object.entries(params.value))
26
- if (value !== void 0 && value !== null && value !== "")
27
- searchParams.set(key, value);
28
- const query = searchParams.toString();
29
- if (mode === "hash") {
30
- const basePath = win.location.hash.split("?")[0] || "#";
31
- win.location.hash = query ? `${basePath}?${query}` : basePath;
32
- } else {
33
- const url = query ? `${win.location.pathname}?${query}` : win.location.pathname;
34
- win.history.replaceState(win.history.state, "", url);
35
- }
36
- }
37
- function get(key) {
38
- return params.value[key] ?? null;
39
- }
40
- function set(key, value) {
41
- const current = { ...params.value };
42
- current[key] = value;
43
- params.value = current;
44
- writeParams();
45
- }
46
- function del(key) {
47
- const current = { ...params.value };
48
- delete current[key];
49
- params.value = current;
50
- writeParams();
51
- }
52
- if (win) {
53
- const onPopState = () => {
54
- params.value = readParams();
55
- }, onHashChange = () => {
56
- if (mode === "hash")
57
- params.value = readParams();
58
- };
59
- win.addEventListener("popstate", onPopState);
60
- win.addEventListener("hashchange", onHashChange);
61
- onUnmounted(() => {
62
- win.removeEventListener("popstate", onPopState);
63
- win.removeEventListener("hashchange", onHashChange);
64
- });
65
- }
66
- return {
67
- params,
68
- get,
69
- set,
70
- delete: del
71
- };
72
- }
1
+ import{onUnmounted,ref}from"@stacksjs/stx";import{defaultWindow}from"./_shared";export function useUrlSearchParams(mode="history",options){const win=defaultWindow(),{initialValue}=options??{};function readParams(){if(!win)return{...initialValue};let search="";if(mode==="hash"){const hash=win.location.hash,idx=hash.indexOf("?");search=idx>=0?hash.slice(idx+1):""}else search=win.location.search.slice(1);const result={...initialValue};new URLSearchParams(search).forEach((value,key)=>{result[key]=value});return result}const params=ref(readParams());function writeParams(){if(!win)return;const searchParams=new URLSearchParams;for(const[key,value]of Object.entries(params.value))if(value!==void 0&&value!==null&&value!=="")searchParams.set(key,value);const query=searchParams.toString();if(mode==="hash"){const basePath=win.location.hash.split("?")[0]||"#";win.location.hash=query?`${basePath}?${query}`:basePath}else{const url=query?`${win.location.pathname}?${query}`:win.location.pathname;win.history.replaceState(win.history.state,"",url)}}function get(key){return params.value[key]??null}function set(key,value){const current={...params.value};current[key]=value;params.value=current;writeParams()}function del(key){const current={...params.value};delete current[key];params.value=current;writeParams()}if(win){const onPopState=()=>{params.value=readParams()},onHashChange=()=>{if(mode==="hash")params.value=readParams()};win.addEventListener("popstate",onPopState);win.addEventListener("hashchange",onHashChange);onUnmounted(()=>{win.removeEventListener("popstate",onPopState);win.removeEventListener("hashchange",onHashChange)})}return{params,get,set,delete:del}}
@@ -1,14 +1 @@
1
- import { ref } from "@stacksjs/stx";
2
- export function useVibrate(options = {}) {
3
- const { pattern = 200 } = options, isSupported = ref(typeof navigator < "u" && "vibrate" in navigator);
4
- function vibrate(p) {
5
- if (!isSupported.value)
6
- return !1;
7
- return navigator.vibrate(p ?? pattern);
8
- }
9
- function stop() {
10
- if (isSupported.value)
11
- navigator.vibrate(0);
12
- }
13
- return { isSupported, vibrate, stop };
14
- }
1
+ import{ref}from"@stacksjs/stx";export function useVibrate(options={}){const{pattern=200}=options,isSupported=ref(typeof navigator<"u"&&"vibrate"in navigator);function vibrate(p){if(!isSupported.value)return!1;return navigator.vibrate(p??pattern)}function stop(){if(isSupported.value)navigator.vibrate(0)}return{isSupported,vibrate,stop}}