@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.
- package/dist/_shared.js +1 -23
- package/dist/cloneFnJSON.js +1 -3
- package/dist/computedAsync.js +1 -20
- package/dist/computedEager.js +1 -10
- package/dist/createEventHook.js +1 -15
- package/dist/createFetch.js +1 -8
- package/dist/createGlobalState.js +1 -10
- package/dist/createSharedComposable.js +1 -9
- package/dist/extendRef.js +1 -6
- package/dist/formatTimeAgo.js +1 -33
- package/dist/index.js +1 -210
- package/dist/isDefined.js +1 -3
- package/dist/makeDestructurable.js +1 -16
- package/dist/onClickOutside.js +1 -25
- package/dist/onKeyStroke.js +1 -31
- package/dist/onLongPress.js +1 -43
- package/dist/onStartTyping.js +1 -23
- package/dist/refAutoReset.js +1 -15
- package/dist/refDefault.js +1 -8
- package/dist/resolveRef.js +1 -7
- package/dist/ssrHandlers.js +1 -8
- package/dist/syncRef.js +1 -35
- package/dist/tryOnMounted.js +1 -22
- package/dist/unrefElement.js +1 -9
- package/dist/until.js +1 -62
- package/dist/useActiveElement.js +1 -19
- package/dist/useAnimate.js +1 -48
- package/dist/useArrayUtils.js +1 -114
- package/dist/useAsyncQueue.js +1 -22
- package/dist/useAsyncState.js +1 -35
- package/dist/useBase64.js +1 -45
- package/dist/useBattery.js +1 -38
- package/dist/useBreakpoints.js +1 -97
- package/dist/useBroadcastChannel.js +1 -31
- package/dist/useBrowserLocation.js +1 -64
- package/dist/useClipboard.js +1 -32
- package/dist/useClipboardItems.js +1 -15
- package/dist/useCloned.js +1 -16
- package/dist/useColorMode.js +1 -60
- package/dist/useConfirmDialog.js +1 -34
- package/dist/useCounter.js +1 -28
- package/dist/useCssVar.js +1 -23
- package/dist/useCycleList.js +1 -27
- package/dist/useDark.js +1 -38
- package/dist/useDateFormat.js +1 -11
- package/dist/useDebounce.js +1 -16
- package/dist/useDebounceFn.js +1 -18
- package/dist/useDebouncedRef.js +1 -22
- package/dist/useDebouncedRefHistory.js +1 -36
- package/dist/useDeviceMotion.js +1 -53
- package/dist/useDeviceOrientation.js +1 -23
- package/dist/useDevicePixelRatio.js +1 -24
- package/dist/useDocumentVisibility.js +1 -16
- package/dist/useDraggable.js +1 -58
- package/dist/useDropZone.js +1 -52
- package/dist/useElementBounding.js +1 -59
- package/dist/useElementByPoint.js +1 -18
- package/dist/useElementHover.js +1 -28
- package/dist/useElementSize.js +1 -34
- package/dist/useElementVisibility.js +1 -10
- package/dist/useEventBus.js +1 -41
- package/dist/useEventListener.js +1 -29
- package/dist/useEventSource.js +1 -72
- package/dist/useEyeDropper.js +1 -17
- package/dist/useFavicon.js +1 -22
- package/dist/useFetch.js +1 -61
- package/dist/useFileDialog.js +1 -24
- package/dist/useFocus.js +1 -42
- package/dist/useFocusWithin.js +1 -29
- package/dist/useForm.js +1 -182
- package/dist/useFps.js +1 -22
- package/dist/useFullscreen.js +1 -67
- package/dist/useGeolocation.js +1 -55
- package/dist/useIdle.js +1 -46
- package/dist/useImage.js +1 -32
- package/dist/useInfiniteScroll.js +1 -50
- package/dist/useIntersectionObserver.js +1 -27
- package/dist/useInterval.js +1 -32
- package/dist/useIntervalFn.js +1 -24
- package/dist/useKeyModifier.js +1 -22
- package/dist/useLastChanged.js +1 -8
- package/dist/useLocalStorage.js +1 -4
- package/dist/useMagicKeys.js +1 -81
- package/dist/useManualRefHistory.js +1 -64
- package/dist/useMath.js +1 -52
- package/dist/useMediaQuery.js +1 -19
- package/dist/useMemoize.js +1 -24
- package/dist/useMounted.js +1 -8
- package/dist/useMouse.js +1 -67
- package/dist/useMouseInElement.js +1 -31
- package/dist/useMousePressed.js +1 -31
- package/dist/useMutationObserver.js +1 -27
- package/dist/useNavigatorLanguage.js +1 -17
- package/dist/useNetwork.js +1 -58
- package/dist/useNow.js +1 -10
- package/dist/useObjectUrl.js +1 -23
- package/dist/useOffsetPagination.js +1 -45
- package/dist/useOnline.js +1 -20
- package/dist/usePageLeave.js +1 -27
- package/dist/useParallax.js +1 -33
- package/dist/useParentElement.js +1 -8
- package/dist/usePermission.js +1 -21
- package/dist/usePointer.js +1 -32
- package/dist/usePointerLock.js +1 -33
- package/dist/usePointerSwipe.js +1 -52
- package/dist/usePreferredColorScheme.js +1 -25
- package/dist/usePreferredContrast.js +1 -29
- package/dist/usePreferredLanguages.js +1 -16
- package/dist/usePreferredReducedMotion.js +1 -18
- package/dist/usePrevious.js +1 -8
- package/dist/useQuery.js +1 -265
- package/dist/useRafFn.js +1 -37
- package/dist/useRefHistory.js +1 -72
- package/dist/useResizeObserver.js +1 -27
- package/dist/useScreenOrientation.js +1 -27
- package/dist/useScreenSafeArea.js +1 -25
- package/dist/useScriptTag.js +1 -52
- package/dist/useScroll.js +1 -116
- package/dist/useScrollLock.js +1 -39
- package/dist/useSessionStorage.js +1 -4
- package/dist/useShare.js +1 -11
- package/dist/useSorted.js +1 -10
- package/dist/useStepper.js +1 -46
- package/dist/useStorage.js +1 -54
- package/dist/useStyleTag.js +1 -39
- package/dist/useSupported.js +1 -10
- package/dist/useSwipe.js +1 -58
- package/dist/useTextDirection.js +1 -16
- package/dist/useTextSelection.js +1 -27
- package/dist/useTextareaAutosize.js +1 -20
- package/dist/useThrottle.js +1 -23
- package/dist/useThrottleFn.js +1 -36
- package/dist/useThrottledRef.js +1 -30
- package/dist/useThrottledRefHistory.js +1 -43
- package/dist/useTimeAgo.js +1 -58
- package/dist/useTimeout.js +1 -32
- package/dist/useTimeoutFn.js +1 -27
- package/dist/useTimeoutPoll.js +1 -33
- package/dist/useTimestamp.js +1 -43
- package/dist/useTitle.js +1 -12
- package/dist/useToNumber.js +1 -25
- package/dist/useToString.js +1 -10
- package/dist/useToggle.js +1 -12
- package/dist/useTransition.js +1 -89
- package/dist/useUrlSearchParams.js +1 -72
- package/dist/useVibrate.js +1 -14
- package/dist/useVirtualList.js +1 -49
- package/dist/useWakeLock.js +1 -32
- package/dist/useWebNotification.js +1 -39
- package/dist/useWebSocket.js +1 -103
- package/dist/useWebWorker.js +1 -56
- package/dist/useWindowFocus.js +1 -20
- package/dist/useWindowScroll.js +1 -17
- package/dist/useWindowSize.js +1 -28
- package/dist/watchArray.js +1 -9
- package/dist/watchAtMost.js +1 -12
- package/dist/watchDebounced.js +1 -20
- package/dist/watchDeep.js +1 -13
- package/dist/watchIgnorable.js +1 -19
- package/dist/watchImmediate.js +1 -7
- package/dist/watchOnce.js +1 -17
- package/dist/watchPausable.js +1 -23
- package/dist/watchThrottled.js +1 -37
- package/dist/watchTriggerable.js +1 -10
- package/dist/watchWithFilter.js +1 -6
- package/dist/whenever.js +1 -7
- package/package.json +2 -2
package/dist/useClipboard.js
CHANGED
|
@@ -1,32 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { defaultNavigator, toValue } from "./_shared";
|
|
3
|
-
export function useClipboard(options) {
|
|
4
|
-
const {
|
|
5
|
-
source,
|
|
6
|
-
copiedDuring = 1500
|
|
7
|
-
} = options ?? {}, nav = defaultNavigator(), isSupported = ref(!!nav && "clipboard" in nav), text = ref(""), copied = ref(!1);
|
|
8
|
-
let copiedTimer;
|
|
9
|
-
async function copy(value) {
|
|
10
|
-
if (!isSupported.value)
|
|
11
|
-
return;
|
|
12
|
-
const valueToCopy = value ?? (source !== void 0 ? toValue(source) : void 0);
|
|
13
|
-
if (valueToCopy === void 0)
|
|
14
|
-
return;
|
|
15
|
-
try {
|
|
16
|
-
await nav.clipboard.writeText(valueToCopy);
|
|
17
|
-
text.value = valueToCopy;
|
|
18
|
-
copied.value = !0;
|
|
19
|
-
if (copiedTimer)
|
|
20
|
-
clearTimeout(copiedTimer);
|
|
21
|
-
copiedTimer = setTimeout(() => {
|
|
22
|
-
copied.value = !1;
|
|
23
|
-
}, copiedDuring);
|
|
24
|
-
} catch {}
|
|
25
|
-
}
|
|
26
|
-
return {
|
|
27
|
-
text,
|
|
28
|
-
isSupported,
|
|
29
|
-
copied,
|
|
30
|
-
copy
|
|
31
|
-
};
|
|
32
|
-
}
|
|
1
|
+
import{ref}from"@stacksjs/stx";import{defaultNavigator,toValue}from"./_shared";export function useClipboard(options){const{source,copiedDuring=1500}=options??{},nav=defaultNavigator(),isSupported=ref(!!nav&&"clipboard"in nav),text=ref(""),copied=ref(!1);let copiedTimer;async function copy(value){if(!isSupported.value)return;const valueToCopy=value??(source!==void 0?toValue(source):void 0);if(valueToCopy===void 0)return;try{await nav.clipboard.writeText(valueToCopy);text.value=valueToCopy;copied.value=!0;if(copiedTimer)clearTimeout(copiedTimer);copiedTimer=setTimeout(()=>{copied.value=!1},copiedDuring)}catch{}}return{text,isSupported,copied,copy}}
|
|
@@ -1,15 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
export function useClipboardItems() {
|
|
3
|
-
const isSupported = ref(typeof navigator < "u" && "clipboard" in navigator && typeof ClipboardItem < "u"), content = ref([]);
|
|
4
|
-
async function copy(items) {
|
|
5
|
-
if (isSupported.value) {
|
|
6
|
-
await navigator.clipboard.write(items);
|
|
7
|
-
content.value = items;
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
async function read() {
|
|
11
|
-
if (isSupported.value)
|
|
12
|
-
content.value = await navigator.clipboard.read();
|
|
13
|
-
}
|
|
14
|
-
return { isSupported, content, copy, read };
|
|
15
|
-
}
|
|
1
|
+
import{ref}from"@stacksjs/stx";export function useClipboardItems(){const isSupported=ref(typeof navigator<"u"&&"clipboard"in navigator&&typeof ClipboardItem<"u"),content=ref([]);async function copy(items){if(isSupported.value){await navigator.clipboard.write(items);content.value=items}}async function read(){if(isSupported.value)content.value=await navigator.clipboard.read()}return{isSupported,content,copy,read}}
|
package/dist/useCloned.js
CHANGED
|
@@ -1,16 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
function defaultClone(val) {
|
|
3
|
-
if (typeof structuredClone === "function")
|
|
4
|
-
return structuredClone(val);
|
|
5
|
-
return JSON.parse(JSON.stringify(val));
|
|
6
|
-
}
|
|
7
|
-
export function useCloned(source, options = {}) {
|
|
8
|
-
const cloneFn = options.clone ?? defaultClone, cloned = ref(cloneFn(source.value));
|
|
9
|
-
function sync() {
|
|
10
|
-
cloned.value = cloneFn(source.value);
|
|
11
|
-
}
|
|
12
|
-
watch(source, () => {
|
|
13
|
-
sync();
|
|
14
|
-
});
|
|
15
|
-
return { cloned, sync };
|
|
16
|
-
}
|
|
1
|
+
import{ref,watch}from"@stacksjs/stx";function defaultClone(val){if(typeof structuredClone==="function")return structuredClone(val);return JSON.parse(JSON.stringify(val))}export function useCloned(source,options={}){const cloneFn=options.clone??defaultClone,cloned=ref(cloneFn(source.value));function sync(){cloned.value=cloneFn(source.value)}watch(source,()=>{sync()});return{cloned,sync}}
|
package/dist/useColorMode.js
CHANGED
|
@@ -1,60 +1 @@
|
|
|
1
|
-
import { onUnmounted, ref,
|
|
2
|
-
export function useColorMode(options = {}) {
|
|
3
|
-
const {
|
|
4
|
-
attribute = "class",
|
|
5
|
-
initialValue = "auto",
|
|
6
|
-
modes = {},
|
|
7
|
-
storageKey = "color-mode",
|
|
8
|
-
selector
|
|
9
|
-
} = options, system = ref("light");
|
|
10
|
-
if (typeof window < "u" && window.matchMedia) {
|
|
11
|
-
const mq = window.matchMedia("(prefers-color-scheme: dark)");
|
|
12
|
-
system.value = mq.matches ? "dark" : "light";
|
|
13
|
-
const onChange = (e) => {
|
|
14
|
-
system.value = e.matches ? "dark" : "light";
|
|
15
|
-
};
|
|
16
|
-
mq.addEventListener("change", onChange);
|
|
17
|
-
try {
|
|
18
|
-
onUnmounted(() => {
|
|
19
|
-
mq.removeEventListener("change", onChange);
|
|
20
|
-
});
|
|
21
|
-
} catch {}
|
|
22
|
-
}
|
|
23
|
-
let stored = initialValue;
|
|
24
|
-
if (typeof localStorage < "u") {
|
|
25
|
-
const val = localStorage.getItem(storageKey);
|
|
26
|
-
if (val)
|
|
27
|
-
stored = val;
|
|
28
|
-
}
|
|
29
|
-
const store = ref(stored), mode = ref(store.value === "auto" ? system.value : store.value);
|
|
30
|
-
function applyMode(m) {
|
|
31
|
-
if (typeof document > "u")
|
|
32
|
-
return;
|
|
33
|
-
const el = selector ? document.querySelector(selector) : document.documentElement;
|
|
34
|
-
if (!el)
|
|
35
|
-
return;
|
|
36
|
-
const value = modes[m] ?? m;
|
|
37
|
-
if (attribute === "class") {
|
|
38
|
-
const allModes = ["dark", "light", ...Object.keys(modes), ...Object.values(modes)];
|
|
39
|
-
for (const cls of allModes)
|
|
40
|
-
el.classList.remove(cls);
|
|
41
|
-
if (value)
|
|
42
|
-
el.classList.add(value);
|
|
43
|
-
} else
|
|
44
|
-
el.setAttribute(attribute, value);
|
|
45
|
-
}
|
|
46
|
-
watch(store, (val) => {
|
|
47
|
-
if (typeof localStorage < "u")
|
|
48
|
-
localStorage.setItem(storageKey, val);
|
|
49
|
-
mode.value = val === "auto" ? system.value : val;
|
|
50
|
-
});
|
|
51
|
-
watch(system, (val) => {
|
|
52
|
-
if (store.value === "auto")
|
|
53
|
-
mode.value = val;
|
|
54
|
-
});
|
|
55
|
-
watch(mode, (val) => {
|
|
56
|
-
applyMode(val);
|
|
57
|
-
});
|
|
58
|
-
applyMode(mode.value);
|
|
59
|
-
return { mode, system, store };
|
|
60
|
-
}
|
|
1
|
+
import{onUnmounted,ref,watch}from"@stacksjs/stx";export function useColorMode(options={}){const{attribute="class",initialValue="auto",modes={},storageKey="color-mode",selector}=options,system=ref("light");if(typeof window<"u"&&window.matchMedia){const mq=window.matchMedia("(prefers-color-scheme: dark)");system.value=mq.matches?"dark":"light";const onChange=(e)=>{system.value=e.matches?"dark":"light"};mq.addEventListener("change",onChange);try{onUnmounted(()=>{mq.removeEventListener("change",onChange)})}catch{}}let stored=initialValue;if(typeof localStorage<"u"){const val=localStorage.getItem(storageKey);if(val)stored=val}const store=ref(stored),mode=ref(store.value==="auto"?system.value:store.value);function applyMode(m){if(typeof document>"u")return;const el=selector?document.querySelector(selector):document.documentElement;if(!el)return;const value=modes[m]??m;if(attribute==="class"){const allModes=["dark","light",...Object.keys(modes),...Object.values(modes)];for(const cls of allModes)el.classList.remove(cls);if(value)el.classList.add(value)}else el.setAttribute(attribute,value)}watch(store,(val)=>{if(typeof localStorage<"u")localStorage.setItem(storageKey,val);mode.value=val==="auto"?system.value:val});watch(system,(val)=>{if(store.value==="auto")mode.value=val});watch(mode,(val)=>{applyMode(val)});applyMode(mode.value);return{mode,system,store}}
|
package/dist/useConfirmDialog.js
CHANGED
|
@@ -1,34 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { createEventHook } from "./createEventHook";
|
|
3
|
-
export function useConfirmDialog() {
|
|
4
|
-
const isRevealed = ref(!1), confirmHook = createEventHook(), cancelHook = createEventHook(), revealHook = createEventHook();
|
|
5
|
-
let resolvePromise = null;
|
|
6
|
-
function reveal(data) {
|
|
7
|
-
isRevealed.value = !0;
|
|
8
|
-
revealHook.trigger(data);
|
|
9
|
-
return new Promise((resolve) => {
|
|
10
|
-
resolvePromise = resolve;
|
|
11
|
-
});
|
|
12
|
-
}
|
|
13
|
-
function confirm(data) {
|
|
14
|
-
isRevealed.value = !1;
|
|
15
|
-
confirmHook.trigger(data);
|
|
16
|
-
resolvePromise?.({ data, isCanceled: !1 });
|
|
17
|
-
resolvePromise = null;
|
|
18
|
-
}
|
|
19
|
-
function cancel() {
|
|
20
|
-
isRevealed.value = !1;
|
|
21
|
-
cancelHook.trigger(void 0);
|
|
22
|
-
resolvePromise?.({ isCanceled: !0 });
|
|
23
|
-
resolvePromise = null;
|
|
24
|
-
}
|
|
25
|
-
return {
|
|
26
|
-
isRevealed,
|
|
27
|
-
reveal,
|
|
28
|
-
confirm,
|
|
29
|
-
cancel,
|
|
30
|
-
onConfirm: confirmHook.on,
|
|
31
|
-
onCancel: cancelHook.on,
|
|
32
|
-
onReveal: revealHook.on
|
|
33
|
-
};
|
|
34
|
-
}
|
|
1
|
+
import{ref}from"@stacksjs/stx";import{createEventHook}from"./createEventHook";export function useConfirmDialog(){const isRevealed=ref(!1),confirmHook=createEventHook(),cancelHook=createEventHook(),revealHook=createEventHook();let resolvePromise=null;function reveal(data){isRevealed.value=!0;revealHook.trigger(data);return new Promise((resolve)=>{resolvePromise=resolve})}function confirm(data){isRevealed.value=!1;confirmHook.trigger(data);resolvePromise?.({data,isCanceled:!1});resolvePromise=null}function cancel(){isRevealed.value=!1;cancelHook.trigger(void 0);resolvePromise?.({isCanceled:!0});resolvePromise=null}return{isRevealed,reveal,confirm,cancel,onConfirm:confirmHook.on,onCancel:cancelHook.on,onReveal:revealHook.on}}
|
package/dist/useCounter.js
CHANGED
|
@@ -1,28 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
export function useCounter(initialValue = 0, options = {}) {
|
|
3
|
-
const { min = -1 / 0, max = 1 / 0 } = options;
|
|
4
|
-
function clamp(val) {
|
|
5
|
-
return Math.min(max, Math.max(min, val));
|
|
6
|
-
}
|
|
7
|
-
const count = ref(clamp(initialValue));
|
|
8
|
-
function inc(delta = 1) {
|
|
9
|
-
count.value = clamp(count.value + delta);
|
|
10
|
-
return count.value;
|
|
11
|
-
}
|
|
12
|
-
function dec(delta = 1) {
|
|
13
|
-
count.value = clamp(count.value - delta);
|
|
14
|
-
return count.value;
|
|
15
|
-
}
|
|
16
|
-
function set(val) {
|
|
17
|
-
count.value = clamp(val);
|
|
18
|
-
return count.value;
|
|
19
|
-
}
|
|
20
|
-
function reset(val = initialValue) {
|
|
21
|
-
count.value = clamp(val);
|
|
22
|
-
return count.value;
|
|
23
|
-
}
|
|
24
|
-
function get() {
|
|
25
|
-
return count.value;
|
|
26
|
-
}
|
|
27
|
-
return { count, inc, dec, set, reset, get };
|
|
28
|
-
}
|
|
1
|
+
import{ref}from"@stacksjs/stx";export function useCounter(initialValue=0,options={}){const{min=-1/0,max=1/0}=options;function clamp(val){return Math.min(max,Math.max(min,val))}const count=ref(clamp(initialValue));function inc(delta=1){count.value=clamp(count.value+delta);return count.value}function dec(delta=1){count.value=clamp(count.value-delta);return count.value}function set(val){count.value=clamp(val);return count.value}function reset(val=initialValue){count.value=clamp(val);return count.value}function get(){return count.value}return{count,inc,dec,set,reset,get}}
|
package/dist/useCssVar.js
CHANGED
|
@@ -1,23 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { unref } from "./_shared";
|
|
3
|
-
export function useCssVar(name, target, defaultValue) {
|
|
4
|
-
const variable = ref(defaultValue ?? ""), getEl = () => {
|
|
5
|
-
const t = unref(target);
|
|
6
|
-
if (t)
|
|
7
|
-
return t;
|
|
8
|
-
if (typeof document < "u")
|
|
9
|
-
return document.documentElement;
|
|
10
|
-
return null;
|
|
11
|
-
}, el = getEl();
|
|
12
|
-
if (el) {
|
|
13
|
-
const current = getComputedStyle(el).getPropertyValue(unref(name)).trim();
|
|
14
|
-
if (current)
|
|
15
|
-
variable.value = current;
|
|
16
|
-
}
|
|
17
|
-
watch(variable, (val) => {
|
|
18
|
-
const element = getEl();
|
|
19
|
-
if (element)
|
|
20
|
-
element.style.setProperty(unref(name), val);
|
|
21
|
-
});
|
|
22
|
-
return variable;
|
|
23
|
-
}
|
|
1
|
+
import{onUnmounted,ref,watch}from"@stacksjs/stx";import{unref}from"./_shared";export function useCssVar(name,target,defaultValue){const variable=ref(defaultValue??""),getEl=()=>{const t=unref(target);if(t)return t;if(typeof document<"u")return document.documentElement;return null},el=getEl();if(el){const current=getComputedStyle(el).getPropertyValue(unref(name)).trim();if(current)variable.value=current}watch(variable,(val)=>{const element=getEl();if(element)element.style.setProperty(unref(name),val)});return variable}
|
package/dist/useCycleList.js
CHANGED
|
@@ -1,27 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
export function useCycleList(list, options = {}) {
|
|
3
|
-
const length = list.length;
|
|
4
|
-
if (length === 0)
|
|
5
|
-
throw Error("useCycleList requires a non-empty list");
|
|
6
|
-
const initialIndex = options.initialValue !== void 0 ? list.indexOf(options.initialValue) : 0, index = ref(initialIndex >= 0 ? initialIndex : 0);
|
|
7
|
-
function at(i) {
|
|
8
|
-
return list[i];
|
|
9
|
-
}
|
|
10
|
-
const state = computed(() => at(index.value));
|
|
11
|
-
function normalizeIndex(i) {
|
|
12
|
-
return (i % length + length) % length;
|
|
13
|
-
}
|
|
14
|
-
function next(n = 1) {
|
|
15
|
-
index.value = normalizeIndex(index.value + n);
|
|
16
|
-
return at(index.value);
|
|
17
|
-
}
|
|
18
|
-
function prev(n = 1) {
|
|
19
|
-
index.value = normalizeIndex(index.value - n);
|
|
20
|
-
return at(index.value);
|
|
21
|
-
}
|
|
22
|
-
function go(i) {
|
|
23
|
-
index.value = normalizeIndex(i);
|
|
24
|
-
return at(index.value);
|
|
25
|
-
}
|
|
26
|
-
return { state, index, next, prev, go };
|
|
27
|
-
}
|
|
1
|
+
import{computed,ref}from"@stacksjs/stx";export function useCycleList(list,options={}){const length=list.length;if(length===0)throw Error("useCycleList requires a non-empty list");const initialIndex=options.initialValue!==void 0?list.indexOf(options.initialValue):0,index=ref(initialIndex>=0?initialIndex:0);function at(i){return list[i]}const state=computed(()=>at(index.value));function normalizeIndex(i){return(i%length+length)%length}function next(n=1){index.value=normalizeIndex(index.value+n);return at(index.value)}function prev(n=1){index.value=normalizeIndex(index.value-n);return at(index.value)}function go(i){index.value=normalizeIndex(i);return at(index.value)}return{state,index,next,prev,go}}
|
package/dist/useDark.js
CHANGED
|
@@ -1,38 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
export function usePreferredDark() {
|
|
3
|
-
const matches = ref(!1);
|
|
4
|
-
if (typeof window < "u" && window.matchMedia) {
|
|
5
|
-
const mq = window.matchMedia("(prefers-color-scheme: dark)");
|
|
6
|
-
matches.value = mq.matches;
|
|
7
|
-
const onChange = (e) => {
|
|
8
|
-
matches.value = e.matches;
|
|
9
|
-
};
|
|
10
|
-
mq.addEventListener("change", onChange);
|
|
11
|
-
try {
|
|
12
|
-
onUnmounted(() => {
|
|
13
|
-
mq.removeEventListener("change", onChange);
|
|
14
|
-
});
|
|
15
|
-
} catch {}
|
|
16
|
-
}
|
|
17
|
-
return matches;
|
|
18
|
-
}
|
|
19
|
-
export function useDark() {
|
|
20
|
-
let initial = usePreferredDark().value;
|
|
21
|
-
if (typeof localStorage < "u") {
|
|
22
|
-
const stored = localStorage.getItem("color-scheme");
|
|
23
|
-
if (stored === "dark")
|
|
24
|
-
initial = !0;
|
|
25
|
-
else if (stored === "light")
|
|
26
|
-
initial = !1;
|
|
27
|
-
}
|
|
28
|
-
const isDark = ref(initial);
|
|
29
|
-
watch(isDark, (val) => {
|
|
30
|
-
if (typeof document < "u")
|
|
31
|
-
document.documentElement.classList.toggle("dark", val);
|
|
32
|
-
if (typeof localStorage < "u")
|
|
33
|
-
localStorage.setItem("color-scheme", val ? "dark" : "light");
|
|
34
|
-
});
|
|
35
|
-
if (typeof document < "u")
|
|
36
|
-
document.documentElement.classList.toggle("dark", isDark.value);
|
|
37
|
-
return isDark;
|
|
38
|
-
}
|
|
1
|
+
import{onUnmounted,ref,watch}from"@stacksjs/stx";export function usePreferredDark(){const matches=ref(!1);if(typeof window<"u"&&window.matchMedia){const mq=window.matchMedia("(prefers-color-scheme: dark)");matches.value=mq.matches;const onChange=(e)=>{matches.value=e.matches};mq.addEventListener("change",onChange);try{onUnmounted(()=>{mq.removeEventListener("change",onChange)})}catch{}}return matches}export function useDark(){let initial=usePreferredDark().value;if(typeof localStorage<"u"){const stored=localStorage.getItem("color-scheme");if(stored==="dark")initial=!0;else if(stored==="light")initial=!1}const isDark=ref(initial);watch(isDark,(val)=>{if(typeof document<"u")document.documentElement.classList.toggle("dark",val);if(typeof localStorage<"u")localStorage.setItem("color-scheme",val?"dark":"light")});if(typeof document<"u")document.documentElement.classList.toggle("dark",isDark.value);return isDark}
|
package/dist/useDateFormat.js
CHANGED
|
@@ -1,11 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { format } from "@stacksjs/datetime";
|
|
3
|
-
export function useDateFormat(date, formatStr) {
|
|
4
|
-
if (typeof date === "object" && date !== null && "value" in date && "subscribe" in date)
|
|
5
|
-
return computed(() => {
|
|
6
|
-
const d = date.value;
|
|
7
|
-
return format(d instanceof Date ? d : new Date(d), formatStr);
|
|
8
|
-
});
|
|
9
|
-
const d = date instanceof Date ? date : new Date(date);
|
|
10
|
-
return ref(format(d, formatStr));
|
|
11
|
-
}
|
|
1
|
+
import{computed,ref}from"@stacksjs/stx";import{format}from"@stacksjs/datetime";export function useDateFormat(date,formatStr){if(typeof date==="object"&&date!==null&&"value"in date&&"subscribe"in date)return computed(()=>{const d=date.value;return format(d instanceof Date?d:new Date(d),formatStr)});const d=date instanceof Date?date:new Date(date);return ref(format(d,formatStr))}
|
package/dist/useDebounce.js
CHANGED
|
@@ -1,16 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { isRef, unref } from "./_shared";
|
|
3
|
-
export function useDebounce(value, ms = 200) {
|
|
4
|
-
const debounced = ref(unref(value));
|
|
5
|
-
let timer = null;
|
|
6
|
-
if (isRef(value))
|
|
7
|
-
watch(value, (newVal) => {
|
|
8
|
-
if (timer !== null)
|
|
9
|
-
clearTimeout(timer);
|
|
10
|
-
timer = setTimeout(() => {
|
|
11
|
-
debounced.value = newVal;
|
|
12
|
-
timer = null;
|
|
13
|
-
}, ms);
|
|
14
|
-
});
|
|
15
|
-
return debounced;
|
|
16
|
-
}
|
|
1
|
+
import{ref,watch}from"@stacksjs/stx";import{isRef,unref}from"./_shared";export function useDebounce(value,ms=200){const debounced=ref(unref(value));let timer=null;if(isRef(value))watch(value,(newVal)=>{if(timer!==null)clearTimeout(timer);timer=setTimeout(()=>{debounced.value=newVal;timer=null},ms)});return debounced}
|
package/dist/useDebounceFn.js
CHANGED
|
@@ -1,18 +1 @@
|
|
|
1
|
-
export function useDebounceFn(fn,
|
|
2
|
-
let timeoutId = null;
|
|
3
|
-
const debounced = function(...args) {
|
|
4
|
-
if (timeoutId !== null)
|
|
5
|
-
clearTimeout(timeoutId);
|
|
6
|
-
timeoutId = setTimeout(() => {
|
|
7
|
-
timeoutId = null;
|
|
8
|
-
fn.apply(this, args);
|
|
9
|
-
}, ms);
|
|
10
|
-
};
|
|
11
|
-
debounced.cancel = () => {
|
|
12
|
-
if (timeoutId !== null) {
|
|
13
|
-
clearTimeout(timeoutId);
|
|
14
|
-
timeoutId = null;
|
|
15
|
-
}
|
|
16
|
-
};
|
|
17
|
-
return debounced;
|
|
18
|
-
}
|
|
1
|
+
export function useDebounceFn(fn,ms=200){let timeoutId=null;const debounced=function(...args){if(timeoutId!==null)clearTimeout(timeoutId);timeoutId=setTimeout(()=>{timeoutId=null;fn.apply(this,args)},ms)};debounced.cancel=()=>{if(timeoutId!==null){clearTimeout(timeoutId);timeoutId=null}};return debounced}
|
package/dist/useDebouncedRef.js
CHANGED
|
@@ -1,22 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
export function useDebouncedRef(value, delay = 200) {
|
|
3
|
-
const source = ref(value), debounced = ref(value);
|
|
4
|
-
let timeoutId = null;
|
|
5
|
-
watch(source, (newVal) => {
|
|
6
|
-
if (timeoutId !== null)
|
|
7
|
-
clearTimeout(timeoutId);
|
|
8
|
-
timeoutId = setTimeout(() => {
|
|
9
|
-
debounced.value = newVal;
|
|
10
|
-
timeoutId = null;
|
|
11
|
-
}, delay);
|
|
12
|
-
});
|
|
13
|
-
return {
|
|
14
|
-
get value() {
|
|
15
|
-
return debounced.value;
|
|
16
|
-
},
|
|
17
|
-
set value(newVal) {
|
|
18
|
-
source.value = newVal;
|
|
19
|
-
},
|
|
20
|
-
subscribe: debounced.subscribe
|
|
21
|
-
};
|
|
22
|
-
}
|
|
1
|
+
import{ref,watch}from"@stacksjs/stx";export function useDebouncedRef(value,delay=200){const source=ref(value),debounced=ref(value);let timeoutId=null;watch(source,(newVal)=>{if(timeoutId!==null)clearTimeout(timeoutId);timeoutId=setTimeout(()=>{debounced.value=newVal;timeoutId=null},delay)});return{get value(){return debounced.value},set value(newVal){source.value=newVal},subscribe:debounced.subscribe}}
|
|
@@ -1,36 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { watch } from "@stacksjs/stx";
|
|
3
|
-
export function useDebouncedRefHistory(source, options) {
|
|
4
|
-
const delay = options?.debounce ?? 200, { history, commit, undo, redo, canUndo, canRedo, clear, last } = useManualRefHistory(source, options);
|
|
5
|
-
let timer = null, ignoreNext = !1;
|
|
6
|
-
watch(source, () => {
|
|
7
|
-
if (ignoreNext) {
|
|
8
|
-
ignoreNext = !1;
|
|
9
|
-
return;
|
|
10
|
-
}
|
|
11
|
-
if (timer !== null)
|
|
12
|
-
clearTimeout(timer);
|
|
13
|
-
timer = setTimeout(() => {
|
|
14
|
-
timer = null;
|
|
15
|
-
commit();
|
|
16
|
-
}, delay);
|
|
17
|
-
});
|
|
18
|
-
const origUndo = undo, origRedo = redo;
|
|
19
|
-
function debouncedUndo() {
|
|
20
|
-
if (timer !== null) {
|
|
21
|
-
clearTimeout(timer);
|
|
22
|
-
timer = null;
|
|
23
|
-
}
|
|
24
|
-
ignoreNext = !0;
|
|
25
|
-
origUndo();
|
|
26
|
-
}
|
|
27
|
-
function debouncedRedo() {
|
|
28
|
-
if (timer !== null) {
|
|
29
|
-
clearTimeout(timer);
|
|
30
|
-
timer = null;
|
|
31
|
-
}
|
|
32
|
-
ignoreNext = !0;
|
|
33
|
-
origRedo();
|
|
34
|
-
}
|
|
35
|
-
return { history, commit, undo: debouncedUndo, redo: debouncedRedo, canUndo, canRedo, clear, last };
|
|
36
|
-
}
|
|
1
|
+
import{useManualRefHistory}from"./useManualRefHistory";import{watch}from"@stacksjs/stx";export function useDebouncedRefHistory(source,options){const delay=options?.debounce??200,{history,commit,undo,redo,canUndo,canRedo,clear,last}=useManualRefHistory(source,options);let timer=null,ignoreNext=!1;watch(source,()=>{if(ignoreNext){ignoreNext=!1;return}if(timer!==null)clearTimeout(timer);timer=setTimeout(()=>{timer=null;commit()},delay)});const origUndo=undo,origRedo=redo;function debouncedUndo(){if(timer!==null){clearTimeout(timer);timer=null}ignoreNext=!0;origUndo()}function debouncedRedo(){if(timer!==null){clearTimeout(timer);timer=null}ignoreNext=!0;origRedo()}return{history,commit,undo:debouncedUndo,redo:debouncedRedo,canUndo,canRedo,clear,last}}
|
package/dist/useDeviceMotion.js
CHANGED
|
@@ -1,53 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { noop } from "./_shared";
|
|
3
|
-
export function useDeviceMotion() {
|
|
4
|
-
const isSupported = ref(typeof window < "u" && "DeviceMotionEvent" in window), acceleration = {
|
|
5
|
-
x: ref(null),
|
|
6
|
-
y: ref(null),
|
|
7
|
-
z: ref(null)
|
|
8
|
-
}, accelerationIncludingGravity = {
|
|
9
|
-
x: ref(null),
|
|
10
|
-
y: ref(null),
|
|
11
|
-
z: ref(null)
|
|
12
|
-
}, rotationRate = {
|
|
13
|
-
alpha: ref(null),
|
|
14
|
-
beta: ref(null),
|
|
15
|
-
gamma: ref(null)
|
|
16
|
-
}, interval = ref(0);
|
|
17
|
-
let cleanup = noop;
|
|
18
|
-
if (typeof window < "u" && isSupported.value) {
|
|
19
|
-
const handler = (event) => {
|
|
20
|
-
if (event.acceleration) {
|
|
21
|
-
acceleration.x.value = event.acceleration.x;
|
|
22
|
-
acceleration.y.value = event.acceleration.y;
|
|
23
|
-
acceleration.z.value = event.acceleration.z;
|
|
24
|
-
}
|
|
25
|
-
if (event.accelerationIncludingGravity) {
|
|
26
|
-
accelerationIncludingGravity.x.value = event.accelerationIncludingGravity.x;
|
|
27
|
-
accelerationIncludingGravity.y.value = event.accelerationIncludingGravity.y;
|
|
28
|
-
accelerationIncludingGravity.z.value = event.accelerationIncludingGravity.z;
|
|
29
|
-
}
|
|
30
|
-
if (event.rotationRate) {
|
|
31
|
-
rotationRate.alpha.value = event.rotationRate.alpha;
|
|
32
|
-
rotationRate.beta.value = event.rotationRate.beta;
|
|
33
|
-
rotationRate.gamma.value = event.rotationRate.gamma;
|
|
34
|
-
}
|
|
35
|
-
interval.value = event.interval;
|
|
36
|
-
};
|
|
37
|
-
window.addEventListener("devicemotion", handler, { passive: !0 });
|
|
38
|
-
cleanup = () => {
|
|
39
|
-
window.removeEventListener("devicemotion", handler);
|
|
40
|
-
cleanup = noop;
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
try {
|
|
44
|
-
onUnmounted(() => cleanup());
|
|
45
|
-
} catch {}
|
|
46
|
-
return {
|
|
47
|
-
acceleration,
|
|
48
|
-
accelerationIncludingGravity,
|
|
49
|
-
rotationRate,
|
|
50
|
-
interval,
|
|
51
|
-
isSupported
|
|
52
|
-
};
|
|
53
|
-
}
|
|
1
|
+
import{onUnmounted,ref}from"@stacksjs/stx";import{noop}from"./_shared";export function useDeviceMotion(){const isSupported=ref(typeof window<"u"&&"DeviceMotionEvent"in window),acceleration={x:ref(null),y:ref(null),z:ref(null)},accelerationIncludingGravity={x:ref(null),y:ref(null),z:ref(null)},rotationRate={alpha:ref(null),beta:ref(null),gamma:ref(null)},interval=ref(0);let cleanup=noop;if(typeof window<"u"&&isSupported.value){const handler=(event)=>{if(event.acceleration){acceleration.x.value=event.acceleration.x;acceleration.y.value=event.acceleration.y;acceleration.z.value=event.acceleration.z}if(event.accelerationIncludingGravity){accelerationIncludingGravity.x.value=event.accelerationIncludingGravity.x;accelerationIncludingGravity.y.value=event.accelerationIncludingGravity.y;accelerationIncludingGravity.z.value=event.accelerationIncludingGravity.z}if(event.rotationRate){rotationRate.alpha.value=event.rotationRate.alpha;rotationRate.beta.value=event.rotationRate.beta;rotationRate.gamma.value=event.rotationRate.gamma}interval.value=event.interval};window.addEventListener("devicemotion",handler,{passive:!0});cleanup=()=>{window.removeEventListener("devicemotion",handler);cleanup=noop}}try{onUnmounted(()=>cleanup())}catch{}return{acceleration,accelerationIncludingGravity,rotationRate,interval,isSupported}}
|
|
@@ -1,23 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { noop } from "./_shared";
|
|
3
|
-
export function useDeviceOrientation() {
|
|
4
|
-
const isSupported = ref(typeof window < "u" && "DeviceOrientationEvent" in window), alpha = ref(null), beta = ref(null), gamma = ref(null), absolute = ref(!1);
|
|
5
|
-
let cleanup = noop;
|
|
6
|
-
if (typeof window < "u" && isSupported.value) {
|
|
7
|
-
const handler = (event) => {
|
|
8
|
-
alpha.value = event.alpha;
|
|
9
|
-
beta.value = event.beta;
|
|
10
|
-
gamma.value = event.gamma;
|
|
11
|
-
absolute.value = event.absolute;
|
|
12
|
-
};
|
|
13
|
-
window.addEventListener("deviceorientation", handler, { passive: !0 });
|
|
14
|
-
cleanup = () => {
|
|
15
|
-
window.removeEventListener("deviceorientation", handler);
|
|
16
|
-
cleanup = noop;
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
try {
|
|
20
|
-
onUnmounted(() => cleanup());
|
|
21
|
-
} catch {}
|
|
22
|
-
return { alpha, beta, gamma, absolute, isSupported };
|
|
23
|
-
}
|
|
1
|
+
import{onUnmounted,ref}from"@stacksjs/stx";import{noop}from"./_shared";export function useDeviceOrientation(){const isSupported=ref(typeof window<"u"&&"DeviceOrientationEvent"in window),alpha=ref(null),beta=ref(null),gamma=ref(null),absolute=ref(!1);let cleanup=noop;if(typeof window<"u"&&isSupported.value){const handler=(event)=>{alpha.value=event.alpha;beta.value=event.beta;gamma.value=event.gamma;absolute.value=event.absolute};window.addEventListener("deviceorientation",handler,{passive:!0});cleanup=()=>{window.removeEventListener("deviceorientation",handler);cleanup=noop}}try{onUnmounted(()=>cleanup())}catch{}return{alpha,beta,gamma,absolute,isSupported}}
|
|
@@ -1,24 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
export function useDevicePixelRatio() {
|
|
3
|
-
const pixelRatio = ref(typeof window < "u" ? window.devicePixelRatio : 1);
|
|
4
|
-
if (typeof window > "u")
|
|
5
|
-
return { pixelRatio };
|
|
6
|
-
let mq = null, cleanup = () => {};
|
|
7
|
-
function observe() {
|
|
8
|
-
cleanup();
|
|
9
|
-
mq = window.matchMedia(`(resolution: ${window.devicePixelRatio}dppx)`);
|
|
10
|
-
const handler = () => {
|
|
11
|
-
pixelRatio.value = window.devicePixelRatio;
|
|
12
|
-
observe();
|
|
13
|
-
};
|
|
14
|
-
mq.addEventListener("change", handler, { once: !0 });
|
|
15
|
-
cleanup = () => {
|
|
16
|
-
mq?.removeEventListener("change", handler);
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
observe();
|
|
20
|
-
try {
|
|
21
|
-
onUnmounted(() => cleanup());
|
|
22
|
-
} catch {}
|
|
23
|
-
return { pixelRatio };
|
|
24
|
-
}
|
|
1
|
+
import{onUnmounted,ref}from"@stacksjs/stx";export function useDevicePixelRatio(){const pixelRatio=ref(typeof window<"u"?window.devicePixelRatio:1);if(typeof window>"u")return{pixelRatio};let mq=null,cleanup=()=>{};function observe(){cleanup();mq=window.matchMedia(`(resolution: ${window.devicePixelRatio}dppx)`);const handler=()=>{pixelRatio.value=window.devicePixelRatio;observe()};mq.addEventListener("change",handler,{once:!0});cleanup=()=>{mq?.removeEventListener("change",handler)}}observe();try{onUnmounted(()=>cleanup())}catch{}return{pixelRatio}}
|
|
@@ -1,16 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
export function useDocumentVisibility() {
|
|
3
|
-
const visibility = ref(typeof document < "u" ? document.visibilityState : "visible");
|
|
4
|
-
if (typeof document > "u")
|
|
5
|
-
return visibility;
|
|
6
|
-
const handler = () => {
|
|
7
|
-
visibility.value = document.visibilityState;
|
|
8
|
-
};
|
|
9
|
-
document.addEventListener("visibilitychange", handler, { passive: !0 });
|
|
10
|
-
try {
|
|
11
|
-
onUnmounted(() => {
|
|
12
|
-
document.removeEventListener("visibilitychange", handler);
|
|
13
|
-
});
|
|
14
|
-
} catch {}
|
|
15
|
-
return visibility;
|
|
16
|
-
}
|
|
1
|
+
import{onUnmounted,ref}from"@stacksjs/stx";export function useDocumentVisibility(){const visibility=ref(typeof document<"u"?document.visibilityState:"visible");if(typeof document>"u")return visibility;const handler=()=>{visibility.value=document.visibilityState};document.addEventListener("visibilitychange",handler,{passive:!0});try{onUnmounted(()=>{document.removeEventListener("visibilitychange",handler)})}catch{}return visibility}
|
package/dist/useDraggable.js
CHANGED
|
@@ -1,58 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { unref, noop } from "./_shared";
|
|
3
|
-
export function useDraggable(target, options) {
|
|
4
|
-
const initialX = options?.initialValue?.x ?? 0, initialY = options?.initialValue?.y ?? 0, x = ref(initialX), y = ref(initialY), isDragging = ref(!1), style = ref(`left: ${initialX}px; top: ${initialY}px;`);
|
|
5
|
-
let cleanup = noop;
|
|
6
|
-
if (typeof window < "u") {
|
|
7
|
-
const cleanups = [];
|
|
8
|
-
let startX = 0, startY = 0, offsetX = 0, offsetY = 0;
|
|
9
|
-
const updateStyle = () => {
|
|
10
|
-
style.value = `left: ${x.value}px; top: ${y.value}px;`;
|
|
11
|
-
}, onPointerMove = (event) => {
|
|
12
|
-
if (!isDragging.value)
|
|
13
|
-
return;
|
|
14
|
-
x.value = event.clientX - offsetX;
|
|
15
|
-
y.value = event.clientY - offsetY;
|
|
16
|
-
updateStyle();
|
|
17
|
-
if (options?.onMove)
|
|
18
|
-
options.onMove({ x: x.value, y: y.value }, event);
|
|
19
|
-
}, onPointerUp = (event) => {
|
|
20
|
-
if (!isDragging.value)
|
|
21
|
-
return;
|
|
22
|
-
isDragging.value = !1;
|
|
23
|
-
if (options?.onEnd)
|
|
24
|
-
options.onEnd({ x: x.value, y: y.value }, event);
|
|
25
|
-
window.removeEventListener("pointermove", onPointerMove);
|
|
26
|
-
window.removeEventListener("pointerup", onPointerUp);
|
|
27
|
-
}, onPointerDown = (event) => {
|
|
28
|
-
const pos = { x: x.value, y: y.value };
|
|
29
|
-
if (options?.onStart) {
|
|
30
|
-
if (options.onStart(pos, event) === !1)
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
startX = event.clientX;
|
|
34
|
-
startY = event.clientY;
|
|
35
|
-
offsetX = startX - x.value;
|
|
36
|
-
offsetY = startY - y.value;
|
|
37
|
-
isDragging.value = !0;
|
|
38
|
-
window.addEventListener("pointermove", onPointerMove);
|
|
39
|
-
window.addEventListener("pointerup", onPointerUp);
|
|
40
|
-
}, el = unref(target);
|
|
41
|
-
if (el) {
|
|
42
|
-
el.addEventListener("pointerdown", onPointerDown);
|
|
43
|
-
cleanups.push(() => el.removeEventListener("pointerdown", onPointerDown));
|
|
44
|
-
}
|
|
45
|
-
cleanup = () => {
|
|
46
|
-
for (const fn of cleanups)
|
|
47
|
-
fn();
|
|
48
|
-
cleanups.length = 0;
|
|
49
|
-
window.removeEventListener("pointermove", onPointerMove);
|
|
50
|
-
window.removeEventListener("pointerup", onPointerUp);
|
|
51
|
-
cleanup = noop;
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
try {
|
|
55
|
-
onUnmounted(() => cleanup());
|
|
56
|
-
} catch {}
|
|
57
|
-
return { x, y, isDragging, style };
|
|
58
|
-
}
|
|
1
|
+
import{onUnmounted,ref}from"@stacksjs/stx";import{unref,noop}from"./_shared";export function useDraggable(target,options){const initialX=options?.initialValue?.x??0,initialY=options?.initialValue?.y??0,x=ref(initialX),y=ref(initialY),isDragging=ref(!1),style=ref(`left: ${initialX}px; top: ${initialY}px;`);let cleanup=noop;if(typeof window<"u"){const cleanups=[];let startX=0,startY=0,offsetX=0,offsetY=0;const updateStyle=()=>{style.value=`left: ${x.value}px; top: ${y.value}px;`},onPointerMove=(event)=>{if(!isDragging.value)return;x.value=event.clientX-offsetX;y.value=event.clientY-offsetY;updateStyle();if(options?.onMove)options.onMove({x:x.value,y:y.value},event)},onPointerUp=(event)=>{if(!isDragging.value)return;isDragging.value=!1;if(options?.onEnd)options.onEnd({x:x.value,y:y.value},event);window.removeEventListener("pointermove",onPointerMove);window.removeEventListener("pointerup",onPointerUp)},onPointerDown=(event)=>{const pos={x:x.value,y:y.value};if(options?.onStart){if(options.onStart(pos,event)===!1)return}startX=event.clientX;startY=event.clientY;offsetX=startX-x.value;offsetY=startY-y.value;isDragging.value=!0;window.addEventListener("pointermove",onPointerMove);window.addEventListener("pointerup",onPointerUp)},el=unref(target);if(el){el.addEventListener("pointerdown",onPointerDown);cleanups.push(()=>el.removeEventListener("pointerdown",onPointerDown))}cleanup=()=>{for(const fn of cleanups)fn();cleanups.length=0;window.removeEventListener("pointermove",onPointerMove);window.removeEventListener("pointerup",onPointerUp);cleanup=noop}}try{onUnmounted(()=>cleanup())}catch{}return{x,y,isDragging,style}}
|