@v-c/util 1.0.21 → 1.2.0
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/hooks/useDelayState.d.ts +16 -0
- package/dist/hooks/useDelayState.js +33 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -1
- package/dist/is.d.ts +14 -0
- package/dist/is.js +21 -0
- package/dist/pickAttrs.js +6 -1
- package/package.json +1 -1
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Ref } from 'vue';
|
|
2
|
+
export type DelayConfig = {
|
|
3
|
+
frame: number;
|
|
4
|
+
ms?: never;
|
|
5
|
+
} | {
|
|
6
|
+
frame?: never;
|
|
7
|
+
ms: number;
|
|
8
|
+
};
|
|
9
|
+
export type SetDelayState<T> = (nextValue: T | ((prev: T) => T),
|
|
10
|
+
/** `true` updates immediately. `false` delays the update by one frame. */
|
|
11
|
+
immediatelyOrDelay?: boolean | DelayConfig) => void;
|
|
12
|
+
/**
|
|
13
|
+
* Similar to `useState`, but updates on the next frame by default.
|
|
14
|
+
* Pending updates are always replaced by the latest one.
|
|
15
|
+
*/
|
|
16
|
+
export default function useDelayState<T>(defaultValue: T | (() => T)): [Ref<T>, SetDelayState<T>];
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import wrapperRaf from "../raf.js";
|
|
2
|
+
import { onBeforeUnmount, ref } from "vue";
|
|
3
|
+
//#region src/hooks/useDelayState.ts
|
|
4
|
+
/**
|
|
5
|
+
* Similar to `useState`, but updates on the next frame by default.
|
|
6
|
+
* Pending updates are always replaced by the latest one.
|
|
7
|
+
*/
|
|
8
|
+
function useDelayState(defaultValue) {
|
|
9
|
+
const value = ref(typeof defaultValue === "function" ? defaultValue() : defaultValue);
|
|
10
|
+
let delayInfo = null;
|
|
11
|
+
const cancelPending = () => {
|
|
12
|
+
if (delayInfo) {
|
|
13
|
+
const [isRaf, delay] = delayInfo;
|
|
14
|
+
if (isRaf) wrapperRaf.cancel(delay);
|
|
15
|
+
else clearTimeout(delay);
|
|
16
|
+
delayInfo = null;
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
const applyValue = (nextValue) => {
|
|
20
|
+
value.value = typeof nextValue === "function" ? nextValue(value.value) : nextValue;
|
|
21
|
+
};
|
|
22
|
+
const setDelayValue = (nextValue, immediatelyOrDelay) => {
|
|
23
|
+
const delayConfig = immediatelyOrDelay || { frame: 1 };
|
|
24
|
+
cancelPending();
|
|
25
|
+
if (delayConfig === true) applyValue(nextValue);
|
|
26
|
+
else if ("ms" in delayConfig) delayInfo = [false, setTimeout(() => applyValue(nextValue), delayConfig.ms)];
|
|
27
|
+
else delayInfo = [true, wrapperRaf(() => applyValue(nextValue), delayConfig.frame)];
|
|
28
|
+
};
|
|
29
|
+
onBeforeUnmount(cancelPending);
|
|
30
|
+
return [value, setDelayValue];
|
|
31
|
+
}
|
|
32
|
+
//#endregion
|
|
33
|
+
export { useDelayState as default };
|
package/dist/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ export { useControlledState } from './hooks/useControlledState';
|
|
|
3
3
|
export { default as useId } from './hooks/useId';
|
|
4
4
|
export { default as useMergedState } from './hooks/useMergedState';
|
|
5
5
|
export { default as KeyCode } from './KeyCode';
|
|
6
|
+
export { isNonNullable, isVueRenderable } from './is';
|
|
6
7
|
export { default as omit } from './omit';
|
|
7
8
|
export { default as raf } from './raf';
|
|
8
9
|
export { default as RenderComponent } from './RenderComponent';
|
package/dist/index.js
CHANGED
|
@@ -6,7 +6,8 @@ import classNames, { clsx } from "./classnames.js";
|
|
|
6
6
|
import { useControlledState } from "./hooks/useControlledState.js";
|
|
7
7
|
import useId_default from "./hooks/useId.js";
|
|
8
8
|
import useMergedState from "./hooks/useMergedState.js";
|
|
9
|
+
import { isNonNullable, isVueRenderable } from "./is.js";
|
|
9
10
|
import get from "./utils/get.js";
|
|
10
11
|
import set, { merge, mergeWith } from "./utils/set.js";
|
|
11
12
|
import warningOnce from "./warning.js";
|
|
12
|
-
export { KeyCode, RenderComponent_default as RenderComponent, classNames, clsx, get, merge, mergeWith, omit, wrapperRaf as raf, set, useControlledState, useId_default as useId, useMergedState, warningOnce as warning };
|
|
13
|
+
export { KeyCode, RenderComponent_default as RenderComponent, classNames, clsx, get, isNonNullable, isVueRenderable, merge, mergeWith, omit, wrapperRaf as raf, set, useControlledState, useId_default as useId, useMergedState, warningOnce as warning };
|
package/dist/is.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Check if a value is neither `null` nor `undefined`.
|
|
3
|
+
*/
|
|
4
|
+
export declare function isNonNullable<T>(value: T): value is NonNullable<T>;
|
|
5
|
+
/**
|
|
6
|
+
* Check whether a value should be treated as renderable content.
|
|
7
|
+
*
|
|
8
|
+
* Returns `false` only for `null`, `undefined`, `false`, and `''`; all other
|
|
9
|
+
* values, including `0` and `true`, are treated as renderable.
|
|
10
|
+
*
|
|
11
|
+
* This is a compatibility-oriented presence check, not a complete Vue node
|
|
12
|
+
* validator.
|
|
13
|
+
*/
|
|
14
|
+
export declare function isVueRenderable<T>(value: T): value is Exclude<NonNullable<T>, false | ''>;
|
package/dist/is.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
//#region src/is.ts
|
|
2
|
+
/**
|
|
3
|
+
* Check if a value is neither `null` nor `undefined`.
|
|
4
|
+
*/
|
|
5
|
+
function isNonNullable(value) {
|
|
6
|
+
return value !== void 0 && value !== null;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Check whether a value should be treated as renderable content.
|
|
10
|
+
*
|
|
11
|
+
* Returns `false` only for `null`, `undefined`, `false`, and `''`; all other
|
|
12
|
+
* values, including `0` and `true`, are treated as renderable.
|
|
13
|
+
*
|
|
14
|
+
* This is a compatibility-oriented presence check, not a complete Vue node
|
|
15
|
+
* validator.
|
|
16
|
+
*/
|
|
17
|
+
function isVueRenderable(value) {
|
|
18
|
+
return isNonNullable(value) && value !== false && value !== "";
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
21
|
+
export { isNonNullable, isVueRenderable };
|
package/dist/pickAttrs.js
CHANGED
|
@@ -16,7 +16,12 @@ var propList = `accept acceptCharset accessKey action allowFullScreen allowTrans
|
|
|
16
16
|
onMouseEnter onMouseLeave onMouseMove onMouseOut onMouseOver onMouseUp onSelect onTouchCancel
|
|
17
17
|
onTouchEnd onTouchMove onTouchStart onScroll onWheel onAbort onCanPlay onCanPlayThrough
|
|
18
18
|
onDurationChange onEmptied onEncrypted onEnded onError onLoadedData onLoadedMetadata
|
|
19
|
-
onLoadStart onPause onPlay onPlaying onProgress onRateChange onSeeked onSeeking onStalled onSuspend onTimeUpdate onVolumeChange onWaiting onLoad
|
|
19
|
+
onLoadStart onPause onPlay onPlaying onProgress onRateChange onSeeked onSeeking onStalled onSuspend onTimeUpdate onVolumeChange onWaiting onLoad
|
|
20
|
+
onPointerDown onPointerMove onPointerUp onPointerCancel onPointerEnter onPointerLeave onPointerOver onPointerOut onGotPointerCapture onLostPointerCapture
|
|
21
|
+
onAnimationStart onAnimationEnd onAnimationIteration
|
|
22
|
+
onTransitionEnd onTransitionRun onTransitionStart onTransitionCancel
|
|
23
|
+
onBeforeInput onReset onInvalid
|
|
24
|
+
onAuxClick onToggle onBeforeToggle onCancel onClose onResize onScrollEnd`.split(/\s+/);
|
|
20
25
|
var ariaPrefix = "aria-";
|
|
21
26
|
var dataPrefix = "data-";
|
|
22
27
|
function match(key, prefix) {
|
package/package.json
CHANGED