@sunyard-szyy-ui/hooks 0.2.6 → 0.2.8

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.
Files changed (2) hide show
  1. package/README.md +233 -0
  2. package/package.json +2 -2
package/README.md ADDED
@@ -0,0 +1,233 @@
1
+ # @sunyard-szyy-ui/hooks
2
+
3
+ > Composition API Hooks - Vue 3 组合式 API 工具集
4
+
5
+ ## 📦 安装
6
+
7
+ ```bash
8
+ # 推荐使用主包
9
+ npm install sunyard-szyy-ui
10
+
11
+ # 或者单独安装
12
+ npm install @sunyard-szyy-ui/hooks
13
+ ```
14
+
15
+ ## 🎣 Hooks 列表
16
+
17
+ ### useNamespace
18
+
19
+ BEM 命名空间工具,用于生成标准的 CSS 类名。
20
+
21
+ **特性:**
22
+
23
+ - ✅ 支持 BEM 命名规范
24
+ - ✅ 自动添加前缀(默认 `sy-`)
25
+ - ✅ 类型安全
26
+
27
+ **使用示例:**
28
+
29
+ ```typescript
30
+ import { useNamespace } from '@sunyard-szyy-ui/hooks';
31
+
32
+ const ns = useNamespace('button');
33
+
34
+ // Block
35
+ ns.b(); // 'sy-button'
36
+
37
+ // Element
38
+ ns.e('icon'); // 'sy-button__icon'
39
+
40
+ // Modifier
41
+ ns.m('primary'); // 'sy-button--primary'
42
+
43
+ // Block + Modifier
44
+ ns.bm('large'); // 'sy-button sy-button--large'
45
+
46
+ // Element + Modifier
47
+ ns.em('icon', 'disabled'); // 'sy-button__icon sy-button__icon--disabled'
48
+
49
+ // 状态
50
+ ns.is('active'); // 'is-active'
51
+ ns.is('disabled', true); // 'is-disabled'
52
+ ```
53
+
54
+ ### useDebounce
55
+
56
+ 防抖 Hook,用于防抖响应式值。
57
+
58
+ **特性:**
59
+
60
+ - ✅ 响应式防抖
61
+ - ✅ 自动清理定时器
62
+ - ✅ 支持自定义延迟
63
+
64
+ **使用示例:**
65
+
66
+ ```typescript
67
+ import { ref } from 'vue';
68
+ import { useDebounce } from '@sunyard-szyy-ui/hooks';
69
+
70
+ const input = ref('');
71
+ const debouncedInput = useDebounce(input, 500);
72
+
73
+ // input 变化后,debouncedInput 会在 500ms 后更新
74
+ watch(debouncedInput, value => {
75
+ console.log('防抖后的值:', value);
76
+ });
77
+ ```
78
+
79
+ ### useToggle
80
+
81
+ 切换布尔值的 Hook。
82
+
83
+ **特性:**
84
+
85
+ - ✅ 简化布尔值状态管理
86
+ - ✅ 提供切换和设置方法
87
+ - ✅ 类型安全
88
+
89
+ **使用示例:**
90
+
91
+ ```typescript
92
+ import { useToggle } from '@sunyard-szyy-ui/hooks';
93
+
94
+ const { value: isOpen, toggle, setValue } = useToggle(false);
95
+
96
+ // 切换
97
+ toggle(); // isOpen.value = true
98
+ toggle(); // isOpen.value = false
99
+
100
+ // 设置特定值
101
+ setValue(true); // isOpen.value = true
102
+ ```
103
+
104
+ ### useLocalStorage
105
+
106
+ 本地存储 Hook,支持响应式和跨标签页同步。
107
+
108
+ **特性:**
109
+
110
+ - ✅ 响应式本地存储
111
+ - ✅ 自动序列化/反序列化
112
+ - ✅ 跨标签页同步
113
+ - ✅ TypeScript 类型支持
114
+
115
+ **使用示例:**
116
+
117
+ ```typescript
118
+ import { useLocalStorage } from '@sunyard-szyy-ui/hooks';
119
+
120
+ // 基础用法
121
+ const { value, setValue } = useLocalStorage('user', { name: 'Alice' });
122
+
123
+ // 修改值(自动同步到 localStorage)
124
+ setValue({ name: 'Bob' });
125
+
126
+ // 响应式更新
127
+ watch(value, newVal => {
128
+ console.log('用户信息变更:', newVal);
129
+ });
130
+ ```
131
+
132
+ ## 🎯 使用方式
133
+
134
+ ### 方式一:从主包导入(推荐)
135
+
136
+ ```typescript
137
+ import { useNamespace, useDebounce } from 'sunyard-szyy-ui';
138
+ ```
139
+
140
+ ### 方式二:从 Hooks 包导入
141
+
142
+ ```typescript
143
+ import { useNamespace, useDebounce } from '@sunyard-szyy-ui/hooks';
144
+ ```
145
+
146
+ ### 方式三:直接从文件导入
147
+
148
+ ```typescript
149
+ import { useNamespace } from '@sunyard-szyy-ui/hooks/use-namespace';
150
+ ```
151
+
152
+ ## 📖 完整 API
153
+
154
+ | Hook | 说明 | 参数 | 返回值 |
155
+ | ----------------- | ------------ | ------------------------------ | ----------------------------- |
156
+ | `useNamespace` | BEM 命名工具 | `block: string` | 命名空间方法对象 |
157
+ | `useDebounce` | 防抖值 | `value: Ref<T>, delay: number` | `Ref<T>` |
158
+ | `useToggle` | 布尔值切换 | `initialValue?: boolean` | `{ value, toggle, setValue }` |
159
+ | `useLocalStorage` | 本地存储 | `key: string, defaultValue: T` | `{ value, setValue }` |
160
+
161
+ ## 🔧 开发
162
+
163
+ ```bash
164
+ # 安装依赖
165
+ pnpm install
166
+
167
+ # 类型检查
168
+ pnpm typecheck
169
+
170
+ # 构建
171
+ pnpm build
172
+
173
+ # 清理构建产物
174
+ pnpm clean
175
+ ```
176
+
177
+ ## 📦 构建产物
178
+
179
+ ```
180
+ @sunyard-szyy-ui/hooks/
181
+ └── dist/
182
+ ├── index.js # ESM 格式
183
+ ├── index.cjs # CJS 格式
184
+ ├── index.d.ts # TypeScript 类型定义
185
+ └── index.d.cts # CJS 类型定义
186
+ ```
187
+
188
+ ## 🤝 依赖
189
+
190
+ - `@sunyard-szyy-ui/utils` - 工具函数
191
+ - `vue` (peer) - Vue 3 框架
192
+
193
+ ## 📝 注意事项
194
+
195
+ 1. **Vue 3 专用**:所有 Hooks 基于 Vue 3 Composition API
196
+ 2. **自动清理**:Hooks 会在组件卸载时自动清理副作用
197
+ 3. **类型安全**:完整的 TypeScript 类型支持
198
+ 4. **Tree-shaking**:支持按需导入,减少打包体积
199
+
200
+ ## 💡 最佳实践
201
+
202
+ ### 1. 在 setup 中使用
203
+
204
+ ```typescript
205
+ export default defineComponent({
206
+ setup() {
207
+ const ns = useNamespace('my-component');
208
+ const debouncedValue = useDebounce(inputValue, 300);
209
+
210
+ return { ns, debouncedValue };
211
+ }
212
+ });
213
+ ```
214
+
215
+ ### 2. 结合响应式 API
216
+
217
+ ```typescript
218
+ const input = ref('');
219
+ const debouncedInput = useDebounce(input, 500);
220
+
221
+ watchEffect(() => {
222
+ // 在这里使用防抖后的值
223
+ console.log(debouncedInput.value);
224
+ });
225
+ ```
226
+
227
+ ### 3. 组合多个 Hooks
228
+
229
+ ```typescript
230
+ const ns = useNamespace('form');
231
+ const { value: isValid, toggle: toggleValid } = useToggle(false);
232
+ const { value: formData, setValue } = useLocalStorage('form-draft', {});
233
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sunyard-szyy-ui/hooks",
3
- "version": "0.2.6",
3
+ "version": "0.2.8",
4
4
  "type": "module",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.js",
@@ -21,7 +21,7 @@
21
21
  ],
22
22
  "sideEffects": false,
23
23
  "dependencies": {
24
- "@sunyard-szyy-ui/utils": "0.2.6"
24
+ "@sunyard-szyy-ui/utils": "0.2.8"
25
25
  },
26
26
  "scripts": {
27
27
  "clean": "rimraf dist",