@d-matrix/utils 1.18.0 → 1.19.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/echarts.js CHANGED
@@ -68,7 +68,7 @@ export function fill(dataSource, xAxisField, yAxisField) {
68
68
  */
69
69
  export const getDiffRate = (first, maxDiff, decimalPlaces = 2, splitNumber = 5) => {
70
70
  let diffRate = 1.2;
71
- if (first === 0)
71
+ if (first === 0 || maxDiff === 0)
72
72
  return diffRate;
73
73
  const minDiff = 1 / Math.pow(10, decimalPlaces);
74
74
  function calc(f, d) {
@@ -7,3 +7,5 @@ export * from './enhancedComponent';
7
7
  export * from './useDeepCompareRef';
8
8
  export * from './types';
9
9
  export * from './useForwardRef';
10
+ export * from './useIsomorphicLayoutEffect';
11
+ export * from './useMediaQuery';
@@ -7,3 +7,5 @@ export * from './enhancedComponent';
7
7
  export * from './useDeepCompareRef';
8
8
  export * from './types';
9
9
  export * from './useForwardRef';
10
+ export * from './useIsomorphicLayoutEffect';
11
+ export * from './useMediaQuery';
@@ -0,0 +1,2 @@
1
+ import { useEffect } from 'react';
2
+ export declare const useIsomorphicLayoutEffect: typeof useEffect;
@@ -0,0 +1,2 @@
1
+ import { useEffect, useLayoutEffect } from 'react';
2
+ export const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;
@@ -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/dist/types.d.ts CHANGED
@@ -13,3 +13,5 @@ export declare type FunctionPropertyNames<T> = {
13
13
  export declare type NonFunctionPropertyNames<T> = {
14
14
  [P in keyof T]-?: T[P] extends Function ? never : P;
15
15
  }[keyof T];
16
+ /** 获取对象中key的值,返回由这些值组成的union type */
17
+ export declare type ValueOf<T> = T[keyof T];
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@d-matrix/utils",
3
3
  "sideEffects": false,
4
- "version": "1.18.0",
4
+ "version": "1.19.0",
5
5
  "description": "A dozen of utils for Front-End Development",
6
6
  "main": "dist/index.js",
7
7
  "scripts": {
package/readme.md CHANGED
@@ -99,7 +99,7 @@ const Test = () => {
99
99
 
100
100
  - `EnhancedComponent.prototype.setStateAsync(state)`
101
101
 
102
- setState 方法的同步版本
102
+ `setState()`方法的同步版本
103
103
 
104
104
  ```ts
105
105
  import { react } from '@d-matrix/utils';
@@ -178,6 +178,24 @@ const Input = React.forwardRef<HTMLInputElement, React.ComponentPropsWithRef<'in
178
178
  });
179
179
  ```
180
180
 
181
+ - `useMediaQuery(query, options?): boolean`
182
+
183
+ 使用[Match Media API](https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia) 检测当前document是否匹配media query
184
+
185
+ ```ts
186
+ import { useMediaQuery } from '@d-matrix/utils/react'
187
+
188
+ export default function Component() {
189
+ const matches = useMediaQuery('(min-width: 768px)')
190
+
191
+ return (
192
+ <div>
193
+ {`The view port is ${matches ? 'at least' : 'less than'} 768 pixels wide`}
194
+ </div>
195
+ )
196
+ }
197
+ ```
198
+
181
199
  ### dom
182
200
 
183
201
  - `scrollToTop(element: Element | null | undefined): void`
@@ -302,6 +320,24 @@ const t1 = {
302
320
  type T1 = FunctionPropertyNames<typeof t1>; // 'result'
303
321
  ```
304
322
 
323
+ - `ValueOf<T>`
324
+
325
+ 获取对象中`key`的值,返回由这些值组成的union type
326
+
327
+ ```ts
328
+ const map = {
329
+ 0: '0m',
330
+ 1: '1m',
331
+ 2: '2m',
332
+ 3: '3m',
333
+ 4: '4m',
334
+ 5: '5m',
335
+ 6: '6m',
336
+ } as const;
337
+
338
+ type T0 = ValueOf<typeof map>; // '0m' | '1m' | '2m' | '3m' | '4m' | '5m' | '6m'
339
+ ```
340
+
305
341
  ### algorithm
306
342
 
307
343
  - `moveMulti<T extends unknown>(arr: T[], indexes: number[], start: number): T[]`