@d-matrix/utils 1.18.1 → 1.20.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/array.d.ts +2 -0
- package/dist/array.js +13 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/react/index.d.ts +2 -0
- package/dist/react/index.js +2 -0
- package/dist/react/useIsomorphicLayoutEffect.d.ts +2 -0
- package/dist/react/useIsomorphicLayoutEffect.js +2 -0
- package/dist/react/useMediaQuery.d.ts +14 -0
- package/dist/react/useMediaQuery.js +42 -0
- package/package.json +1 -1
- package/readme.md +43 -0
package/dist/array.d.ts
ADDED
package/dist/array.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export function arrayMoveMutable(array, fromIndex, toIndex) {
|
|
2
|
+
const startIndex = fromIndex < 0 ? array.length + fromIndex : fromIndex;
|
|
3
|
+
if (startIndex >= 0 && startIndex < array.length) {
|
|
4
|
+
const endIndex = toIndex < 0 ? array.length + toIndex : toIndex;
|
|
5
|
+
const [item] = array.splice(fromIndex, 1);
|
|
6
|
+
array.splice(endIndex, 0, item);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export function arrayMoveImmutable(array, fromIndex, toIndex) {
|
|
10
|
+
const newArray = [...array];
|
|
11
|
+
arrayMoveMutable(newArray, fromIndex, toIndex);
|
|
12
|
+
return newArray;
|
|
13
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/react/index.d.ts
CHANGED
package/dist/react/index.js
CHANGED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
declare type UseMediaQueryOptions = {
|
|
2
|
+
/**
|
|
3
|
+
* The default value to return if the hook is being run on the server.
|
|
4
|
+
* @default false
|
|
5
|
+
*/
|
|
6
|
+
defaultValue?: boolean;
|
|
7
|
+
/**
|
|
8
|
+
* If `true` (default), the hook will initialize reading the media query. In SSR, you should set it to `false`, returning `options.defaultValue` or `false` initially.
|
|
9
|
+
* @default true
|
|
10
|
+
*/
|
|
11
|
+
initializeWithValue?: boolean;
|
|
12
|
+
};
|
|
13
|
+
export declare function useMediaQuery(query: string, { defaultValue, initializeWithValue }?: UseMediaQueryOptions): boolean;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { useState } from 'react';
|
|
2
|
+
import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect';
|
|
3
|
+
const IS_SERVER = typeof window === 'undefined';
|
|
4
|
+
export function useMediaQuery(query, { defaultValue = false, initializeWithValue = true } = {}) {
|
|
5
|
+
const getMatches = (query) => {
|
|
6
|
+
if (IS_SERVER) {
|
|
7
|
+
return defaultValue;
|
|
8
|
+
}
|
|
9
|
+
return window.matchMedia(query).matches;
|
|
10
|
+
};
|
|
11
|
+
const [matches, setMatches] = useState(() => {
|
|
12
|
+
if (initializeWithValue) {
|
|
13
|
+
return getMatches(query);
|
|
14
|
+
}
|
|
15
|
+
return defaultValue;
|
|
16
|
+
});
|
|
17
|
+
// Handles the change event of the media query.
|
|
18
|
+
function handleChange() {
|
|
19
|
+
setMatches(getMatches(query));
|
|
20
|
+
}
|
|
21
|
+
useIsomorphicLayoutEffect(() => {
|
|
22
|
+
const matchMedia = window.matchMedia(query);
|
|
23
|
+
// Triggered at the first client-side load and if query changes
|
|
24
|
+
handleChange();
|
|
25
|
+
// Use deprecated `addListener` and `removeListener` to support Safari < 14 (#135)
|
|
26
|
+
if (matchMedia.addListener) {
|
|
27
|
+
matchMedia.addListener(handleChange);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
matchMedia.addEventListener('change', handleChange);
|
|
31
|
+
}
|
|
32
|
+
return () => {
|
|
33
|
+
if (matchMedia.removeListener) {
|
|
34
|
+
matchMedia.removeListener(handleChange);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
matchMedia.removeEventListener('change', handleChange);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
}, [query]);
|
|
41
|
+
return matches;
|
|
42
|
+
}
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -19,6 +19,7 @@ A dozen of utils for Front-End Development
|
|
|
19
19
|
- [operator](#operator)
|
|
20
20
|
- [decimal](#decimal)
|
|
21
21
|
- [object](#object)
|
|
22
|
+
- [array](#array)
|
|
22
23
|
- [echarts](#echarts)
|
|
23
24
|
|
|
24
25
|
### clipboard
|
|
@@ -178,6 +179,24 @@ const Input = React.forwardRef<HTMLInputElement, React.ComponentPropsWithRef<'in
|
|
|
178
179
|
});
|
|
179
180
|
```
|
|
180
181
|
|
|
182
|
+
- `useMediaQuery(query, options?): boolean`
|
|
183
|
+
|
|
184
|
+
使用[Match Media API](https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia) 检测当前document是否匹配media query
|
|
185
|
+
|
|
186
|
+
```ts
|
|
187
|
+
import { useMediaQuery } from '@d-matrix/utils/react'
|
|
188
|
+
|
|
189
|
+
export default function Component() {
|
|
190
|
+
const matches = useMediaQuery('(min-width: 768px)')
|
|
191
|
+
|
|
192
|
+
return (
|
|
193
|
+
<div>
|
|
194
|
+
{`The view port is ${matches ? 'at least' : 'less than'} 768 pixels wide`}
|
|
195
|
+
</div>
|
|
196
|
+
)
|
|
197
|
+
}
|
|
198
|
+
```
|
|
199
|
+
|
|
181
200
|
### dom
|
|
182
201
|
|
|
183
202
|
- `scrollToTop(element: Element | null | undefined): void`
|
|
@@ -466,6 +485,30 @@ removeZeroValueKeys({ a: '', b: 'abc', c: undefined, d: null, e: NaN, f: -1, g:
|
|
|
466
485
|
// { b: 'abc', f: -1 }
|
|
467
486
|
```
|
|
468
487
|
|
|
488
|
+
## array
|
|
489
|
+
|
|
490
|
+
- `arrayMoveImmutable<T>(array: T[], fromIndex: number, toIndex: number): T[]`
|
|
491
|
+
|
|
492
|
+
```js
|
|
493
|
+
import { arrayMoveImmutable } from '@d-matrix/utils';
|
|
494
|
+
|
|
495
|
+
const input = ['a', 'b', 'c'];
|
|
496
|
+
|
|
497
|
+
const array1 = arrayMoveImmutable(input, 1, 2);
|
|
498
|
+
console.log(array1);
|
|
499
|
+
//=> ['a', 'c', 'b']
|
|
500
|
+
|
|
501
|
+
const array2 = arrayMoveImmutable(input, -1, 0);
|
|
502
|
+
console.log(array2);
|
|
503
|
+
//=> ['c', 'a', 'b']
|
|
504
|
+
|
|
505
|
+
const array3 = arrayMoveImmutable(input, -2, -3);
|
|
506
|
+
console.log(array3);
|
|
507
|
+
//=> ['b', 'a', 'c']
|
|
508
|
+
```
|
|
509
|
+
|
|
510
|
+
- `arrayMoveMutable<T>(array: T[], fromIndex: number, toIndex: number): void`
|
|
511
|
+
|
|
469
512
|
## echarts
|
|
470
513
|
|
|
471
514
|
- `mergeOption(defaults: EChartsOption, overrides: EChartsOption, option?: deepmerge.Options): EChartsOption`
|