@varlet/shared 2.15.0 → 2.15.2-alpha.1693502641730

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/lib/index.d.ts CHANGED
@@ -8,6 +8,7 @@ export declare const isArray: (val: unknown) => val is any[];
8
8
  export declare const isURL: (val: string | undefined | null) => boolean;
9
9
  export declare const isEmpty: (val: unknown) => boolean;
10
10
  export declare const isWindow: (val: unknown) => val is Window;
11
+ export declare const supportTouch: () => boolean;
11
12
  export declare const toNumber: (val: number | string | boolean | undefined | null) => number;
12
13
  export declare const removeItem: (arr: Array<unknown>, item: unknown) => unknown[] | undefined;
13
14
  export declare const toggleItem: (arr: Array<unknown>, item: unknown) => void;
@@ -21,3 +22,11 @@ export declare const find: <T>(arr: T[], callback: (item: T, index: number, arra
21
22
  export declare const normalizeToArray: <T>(value: T | T[]) => T[];
22
23
  export declare const clamp: (num: number, min: number, max: number) => number;
23
24
  export declare const clampArrayRange: (index: number, arr: Array<unknown>) => number;
25
+ export declare const getGlobalThis: () => typeof globalThis;
26
+ export declare const requestAnimationFrame: (fn: FrameRequestCallback) => number;
27
+ export declare const cancelAnimationFrame: (handle: number) => void;
28
+ export declare const raf: () => Promise<unknown>;
29
+ export declare const doubleRaf: () => Promise<unknown>;
30
+ export declare const getStyle: (element: Element) => CSSStyleDeclaration;
31
+ export declare const getRect: (element: Element | Window) => DOMRect;
32
+ export declare const inViewport: (element: HTMLElement) => boolean;
package/lib/index.js CHANGED
@@ -14,6 +14,7 @@ export const isURL = (val) => {
14
14
  };
15
15
  export const isEmpty = (val) => val === undefined || val === null || val === '' || (Array.isArray(val) && !val.length);
16
16
  export const isWindow = (val) => val === window;
17
+ export const supportTouch = () => inBrowser() && 'ontouchstart' in window;
17
18
  export const toNumber = (val) => {
18
19
  if (val == null)
19
20
  return 0;
@@ -82,3 +83,55 @@ export const find = (arr, callback, from = 'start') => {
82
83
  export const normalizeToArray = (value) => (isArray(value) ? value : [value]);
83
84
  export const clamp = (num, min, max) => Math.min(max, Math.max(min, num));
84
85
  export const clampArrayRange = (index, arr) => clamp(index, 0, arr.length - 1);
86
+ export const getGlobalThis = () => {
87
+ if (typeof globalThis !== 'undefined') {
88
+ return globalThis;
89
+ }
90
+ if (inBrowser()) {
91
+ return window;
92
+ }
93
+ return typeof global !== 'undefined' ? global : self;
94
+ };
95
+ export const requestAnimationFrame = (fn) => {
96
+ const globalThis = getGlobalThis();
97
+ return globalThis.requestAnimationFrame ? globalThis.requestAnimationFrame(fn) : globalThis.setTimeout(fn);
98
+ };
99
+ export const cancelAnimationFrame = (handle) => {
100
+ const globalThis = getGlobalThis();
101
+ globalThis.cancelAnimationFrame ? globalThis.cancelAnimationFrame(handle) : globalThis.clearTimeout(handle);
102
+ };
103
+ export const raf = () => new Promise((resolve) => {
104
+ requestAnimationFrame(resolve);
105
+ });
106
+ export const doubleRaf = () => new Promise((resolve) => {
107
+ requestAnimationFrame(() => {
108
+ requestAnimationFrame(resolve);
109
+ });
110
+ });
111
+ // shorthand only
112
+ export const getStyle = (element) => window.getComputedStyle(element);
113
+ export const getRect = (element) => {
114
+ if (isWindow(element)) {
115
+ const width = element.innerWidth;
116
+ const height = element.innerHeight;
117
+ const rect = {
118
+ x: 0,
119
+ y: 0,
120
+ top: 0,
121
+ left: 0,
122
+ right: width,
123
+ bottom: height,
124
+ width,
125
+ height,
126
+ };
127
+ return Object.assign(Object.assign({}, rect), { toJSON: () => rect });
128
+ }
129
+ return element.getBoundingClientRect();
130
+ };
131
+ export const inViewport = (element) => {
132
+ const { top, bottom, left, right } = getRect(element);
133
+ const { width, height } = getRect(window);
134
+ const xInViewport = left <= width && right >= 0;
135
+ const yInViewport = top <= height && bottom >= 0;
136
+ return xInViewport && yInViewport;
137
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@varlet/shared",
3
- "version": "2.15.0",
3
+ "version": "2.15.2-alpha.1693502641730",
4
4
  "type": "module",
5
5
  "main": "lib/index.js",
6
6
  "module": "lib/index.js",
@@ -24,6 +24,7 @@
24
24
  "url": "https://github.com/varletjs/varlet/issues"
25
25
  },
26
26
  "devDependencies": {
27
+ "@types/node": "^18.7.18",
27
28
  "typescript": "^5.1.5",
28
29
  "rimraf": "^5.0.1"
29
30
  },