earthnut 0.1.7 → 0.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/README.md CHANGED
@@ -8,6 +8,10 @@
8
8
  npm install --save earthnut
9
9
  ```
10
10
 
11
+ ## 文档
12
+
13
+ 参阅 [earthnut](https://earthnut.dev/quickUse/)
14
+
11
15
  ## 涟漪背景
12
16
 
13
17
  由于当前组件的开发进度较缓慢,目前:
@@ -23,6 +27,105 @@ npm install --save earthnut
23
27
 
24
28
  切换背景最好不好通过设置
25
29
 
26
- ## 文档
30
+ ## 自定义钩子
27
31
 
28
- 参阅 [earthnut](https://earthnut.dev/quickUse/)
32
+ 几个简单的自定义钩子,写着玩
33
+
34
+ ### useTimeId
35
+
36
+ 就是 `useRef` 和 `useEffect` 的简单使用。
37
+
38
+ ```ts
39
+ import { useTimeId } from 'earthnut';
40
+
41
+ export function Home () {
42
+
43
+ const timeId = useTimeId();
44
+
45
+ return <>
46
+ <button
47
+ onclick = { () => {
48
+ timeId.current = setTimeout(() =>
49
+ console.log('没有感情的按钮 A 打印了一条没有感情的消息'), 2500);
50
+ }
51
+ }>
52
+ 没有感情的按钮 A
53
+ </button>
54
+ <button onclick={()=> clearTimeout(timeId.current)}>没有感情的按钮 B</button>
55
+ </>
56
+ }
57
+ ```
58
+
59
+ ### useAnimationFrame
60
+
61
+ 使用下一帧动画。
62
+
63
+ ```ts
64
+ import { useAnimationFrame } from 'earthnut';
65
+ ```
66
+
67
+ ## scss 样式
68
+
69
+ 导出了一个 `common.css` 作为样式使用,还可以引用其中的 `scss` 文件使用其中的函数。
70
+
71
+ 如果使用 `common.css` 建议仅在跟下进行导入即可,避免在
72
+
73
+ 在使用 `webpack` 的应用中可以这样引入
74
+
75
+ ```javascript
76
+ // webpack.config.js
77
+ module.exports = {
78
+ module: {
79
+ rules: [
80
+ {
81
+ test: /\.scss$/,
82
+ use: [
83
+ 'style-loader',
84
+ 'css-loader',
85
+ {
86
+ loader: 'sass-loader',
87
+ options: {
88
+ sassOptions: {
89
+ includePaths: [path.resolve(__dirname, 'node_modules/ui-library-a/src/scss')], // UI 库 SCSS 路径
90
+ },
91
+ },
92
+ },
93
+ ],
94
+ },
95
+ ],
96
+ },
97
+ };
98
+ ```
99
+
100
+ 在使用 `vite` 的应用中可以这样引入
101
+
102
+ ```js
103
+ // vite.config.js
104
+ import { defineConfig } from 'vite';
105
+ import path from 'path';
106
+
107
+ export default defineConfig({
108
+ css: {
109
+ preprocessorOptions: {
110
+ scss: {
111
+ additionalData: `@use "${path.resolve(__dirname, 'node_modules/ui-library-a/src/scss')}" as lib;`,
112
+ includePaths: [path.resolve(__dirname, 'node_modules/ui-library-a/src/scss')],
113
+ },
114
+ },
115
+ },
116
+ });
117
+ ```
118
+
119
+ 为了防止意外覆盖别人的自定义 css 属性,该样式皆以 `en-` 为前缀。譬如:
120
+
121
+ ```tsx
122
+ import { xcn } from 'earthnut';
123
+
124
+ export function Home() {
125
+ return (
126
+ <div>
127
+ <p className={xcn('en-text-in=one-line')}>无论我字数多少,仅会会在同一行就行打印。</p>
128
+ </div>
129
+ );
130
+ }
131
+ ```
@@ -25,7 +25,7 @@ import * as React from 'react';
25
25
  * 使用:
26
26
  *
27
27
  * ```ts
28
- * import { BackgroundRipple } from 'earthnut/BackgroundRipple';
28
+ * import { BackgroundRipple } from 'earthnut';
29
29
  * // 也可以全量导入
30
30
  * // import { BackgroundRipple } from 'earthnut';
31
31
  * ...
@@ -13,36 +13,55 @@
13
13
  * @CreateDate 周二 01/07/2025
14
14
  * @Description 使用 animationFrame
15
15
  ****************************************************************************/
16
+ /** 使用动画结果 */
17
+ export interface UseAnimationFrameResult {
18
+ /** 取消执行动画 */
19
+ cancel(): void;
20
+ /** 当前取消的状态 */
21
+ canceled: boolean;
22
+ /** 再次执行 */
23
+ render(): void;
24
+ }
25
+ export type AnimationFrameOption = boolean | {
26
+ immediately?: boolean;
27
+ once?: boolean;
28
+ };
16
29
  /**
17
30
  *
18
- * ### 导出一个使用 `useRef` 创建的 `number`
31
+ * 该钩子在组件卸载时会自动调用 `window.cancelAnimationFrame` 清理传入的回调方法。
32
+ * 默认是传入即启动调用方法
19
33
  *
20
- * 该数值在组件卸载时会自动调用 `window.cancelAnimationFrame` 清理
21
- * @description 一个接收 `window.requestAnimationFrame` 的返回值的 `ref`
22
- * @returns <React.RefObject: number>
23
- * @version 0.0.3
24
- * @see https://earthnut.dev/use-animation-frame
34
+ * @description 创建一个会自己关闭(组建卸载时)的 animationFrame。当然,可以通过返回值手动终止
35
+ * @param callback 使用回调函数。回调函数有两个参数,除了默认的时间,还有另一个用于暂停后的记时时间。如果没有主动暂停,两者时间总是趋近于相等的(可能有其他延迟造成时间差越来越大)。如果没有暂停需求,使用第一个默认时间就可以了。
36
+ * @param [option=false] 使用第二参数,可指定是否立即执行及是否仅执行一次
37
+ * @returns 返回值包含执行状态
38
+ * @version 0.2.0
39
+ * @see https://earthnut.dev/custom-hooks/use-animation-frame
25
40
  * @example
26
41
  * 使用:
27
42
  *
28
43
  * ```ts
29
- * import { useAnimationFrame } from 'earthnut/useAnimationFrame';
30
- * // 也可以全量导入
31
44
  * // import { useAnimationFrame } from 'earthnut';
32
45
  * ...
33
- * const animationFrameId = useAnimationFrame();
34
46
  *
35
- * function performAction(time?: number){
36
- * useAnimationFrame.current = window.requestAnimationFrame(performAction);
37
- * ...
38
- * }
39
- *
40
- * useEffect(()=>{
41
- * animationFrameId.current = window.requestAnimationFrame(performAction);
47
+ * export function Home () {
48
+ * // 如果没有主动暂停的需要,直接使用默认的 time 最好。如果中间有使用暂停。那么 time 值是不连贯的,而 runTime 是连贯的
49
+ * const result = useAnimationFrame((time ,runTime) => {
50
+ * ... //
42
51
  * });
52
+ *
53
+ *
54
+ * return <>
55
+ *
56
+ * <button onclick={() => result.cancel()}>暂停</button>
57
+ * <button onclick={() => result.render()}>恢复</button>
58
+ * </>
59
+ * }
43
60
  * ...
44
61
  *
45
62
  * ```
46
63
  *
64
+ * 当然,可使用第二参数执行立即执行还是仅执行一次(作用不大,不与赘述)
65
+ *
47
66
  */
48
- export declare function useAnimationFrameId(): React.RefObject<number>;
67
+ export declare function useAnimationFrame(callback: (time: number, runtime: number) => void, option?: AnimationFrameOption): UseAnimationFrameResult;
@@ -22,30 +22,30 @@
22
22
  * @param inputRef [React.RefObject<HTMLInputElement | HTMLTextAreaElement>] 输入框的 ref
23
23
  * @returns React.RefObject<boolean>
24
24
  * @version 0.0.4
25
- * @see https://earthnut.dev/use-input-is-composing
25
+ * @see https://earthnut.dev/custom-hooks/use-input-is-composing
26
26
  * @example
27
27
  *
28
28
  * 使用:
29
29
  *
30
30
  * ```ts
31
- * import { useInputIsComposing } from 'earthnut/useInputIsComposing';
32
- * // 也可以全量导入
33
- * // import { useInputIsComposing } from 'earthnut';
31
+ * import { useInputIsComposing } from 'earthnut';
32
+ *
34
33
  * ...
35
34
  * const inputRef = useRef<HTMLInputElement>(null);
36
35
  *
37
36
  * const inputIsComposing = useInputIsComposing(inputRef);
38
37
  * ...
39
- * function enterDown()(e: React.KeyboardEvent<HTMLInputElement>) {
38
+ * function enterDown(e: React.KeyboardEvent<HTMLInputElement>) {
40
39
  * if (e.key === 'Enter') {
41
40
  * if (isComposing.current) {
42
41
  * console.log("此时此景,按回车键说明为了从候选词中挑选");
43
- * } else
42
+ * } else {
44
43
  * console.log("输入完毕,敲回车是为了看一些开发者是否绑定了其他事件");
44
+ * }
45
45
  * }
46
46
  * }
47
47
  * ...
48
- * <input type="text" onKeyDown={enterDown} />
48
+ * <input type="text" onKeyDown={enterDown} ref={inputRef} />
49
49
  * ...
50
50
  * ```
51
51
  */
@@ -14,16 +14,14 @@ import { Ripples } from './ripplesClass';
14
14
  * @param canvas `usrRef` 包裹的 `HTMLCanvasElement`,用于绘制图像
15
15
  * @param option 初始化的
16
16
  * @version 0.0.3
17
- * @see https://earthnut.dev/use-ripples
17
+ * @see https://earthnut.dev/custom-hooks/use-ripples
18
18
  * @see JQuery https://github.com/sirxemic/jquery.ripples
19
19
  * @example
20
20
  *
21
21
  * 下面是在 <BackgroundRipple> 中使用
22
22
  *
23
23
  * ```ts
24
- * import { useRipples } from 'earthnut/useRipples';
25
- * // 也可以使用包全量导入
26
- * // import { useRipples } from 'earthnut';
24
+ * import { useRipples } from 'earthnut';
27
25
  *
28
26
  * export function BackgroundRipple(props: BackgroundRipplesProps) {
29
27
  *
@@ -17,8 +17,6 @@
17
17
  * 使用:
18
18
  *
19
19
  * ```ts
20
- * import { useTimeId } from 'earthnut/useTimeId';
21
- * // 也可以全量导入
22
20
  * // import { useTimeId } from 'earthnut';
23
21
  * ...
24
22
  * const timeoutId = useTimeId();