zhl-button 1.0.1

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 ADDED
@@ -0,0 +1,274 @@
1
+ # ZhlButton - 简单实用的按钮组件
2
+
3
+ 一个轻量级、高度可定制的 JavaScript 按钮组件,支持多种类型、尺寸和交互状态。
4
+
5
+ ## 特性
6
+
7
+ - 🎨 **多种按钮类型**: primary, secondary, success, danger, warning, info, light, dark, outline
8
+ - 📏 **三种尺寸**: small, medium, large
9
+ - ⚡ **加载状态**: 内置加载动画效果
10
+ - ♿ **无障碍支持**: 完整的键盘导航和焦点管理
11
+ - 📱 **响应式设计**: 适配移动端和桌面端
12
+ - 🔧 **完全可定制**: 支持动态更新所有属性
13
+ - 💫 **流畅动画**: hover 和 active 状态的平滑过渡效果
14
+
15
+ ## 安装
16
+
17
+ ### 浏览器直接引入
18
+
19
+ ### 版本号自行修改
20
+
21
+ ```html
22
+ <script src="https://cdn.jsdelivr.net/npm/zhl-button@1.0.8/index.js"></script>
23
+ <link
24
+ rel="stylesheet"
25
+ href="https://cdn.jsdelivr.net/npm/zhl-button@1.0.8/style.css"
26
+ />
27
+ ```
28
+
29
+ ### npm 安装
30
+
31
+ ```bash
32
+ npm install zhl-button
33
+ ```
34
+
35
+ ## 使用方法
36
+
37
+ ### 基本用法
38
+
39
+ ```javascript
40
+ // 创建基本按钮
41
+ const button = new ZhlButton({
42
+ text: "点击我",
43
+ type: "primary",
44
+ onClick: () => {
45
+ console.log("按钮被点击了!");
46
+ },
47
+ });
48
+
49
+ // 添加到页面
50
+ document.body.appendChild(button.element);
51
+ ```
52
+
53
+ ### 使用工厂方法
54
+
55
+ ```javascript
56
+ // 使用静态方法创建按钮
57
+ const button = ZhlButton.create({
58
+ text: "提交",
59
+ type: "success",
60
+ size: "large",
61
+ onClick: handleSubmit,
62
+ });
63
+ ```
64
+
65
+ ### 完整示例
66
+
67
+ ```javascript
68
+ // HTML 结构
69
+ // <div id="my-buttons"></div>
70
+
71
+ // JavaScript 代码
72
+ const buttonContainer = document.getElementById("my-buttons");
73
+
74
+ // 创建不同类型的按钮
75
+ const buttons = [
76
+ { text: "主要按钮", type: "primary" },
77
+ { text: "次要按钮", type: "secondary" },
78
+ { text: "危险操作", type: "danger" },
79
+ { text: "警告", type: "warning" },
80
+ { text: "信息", type: "info" },
81
+ ];
82
+
83
+ buttons.forEach((config) => {
84
+ const button = new ZhlButton({
85
+ ...config,
86
+ onClick: () => console.log(`点击了: ${config.text}`),
87
+ });
88
+
89
+ buttonContainer.appendChild(button.element);
90
+ });
91
+ ```
92
+
93
+ ## 配置选项
94
+
95
+ ### 构造函数参数
96
+
97
+ | 参数 | 类型 | 默认值 | 描述 |
98
+ | ---------- | -------- | --------- | ----------------------------------------------------------------------------------- |
99
+ | `text` | string | 'Button' | 按钮显示文本 |
100
+ | `type` | string | 'primary' | 按钮类型 (primary, secondary, success, danger, warning, info, light, dark, outline) |
101
+ | `size` | string | 'medium' | 按钮尺寸 (small, medium, large) |
102
+ | `disabled` | boolean | false | 是否禁用按钮 |
103
+ | `loading` | boolean | false | 是否显示加载状态 |
104
+ | `onClick` | function | null | 点击事件回调函数 |
105
+
106
+ ### 按钮类型
107
+
108
+ - `primary` - 主要按钮,蓝色主题
109
+ - `secondary` - 次要按钮,灰色主题
110
+ - `success` - 成功按钮,绿色主题
111
+ - `danger` - 危险按钮,红色主题
112
+ - `warning` - 警告按钮,黄色主题
113
+ - `info` - 信息按钮,青色主题
114
+ - `light` - 浅色按钮,浅灰主题
115
+ - `dark` - 深色按钮,深灰主题
116
+ - `outline` - 边框按钮,透明背景蓝色边框
117
+
118
+ ### 按钮尺寸
119
+
120
+ - `small` - 小尺寸,适合密集布局
121
+ - `medium` - 中等尺寸,默认大小
122
+ - `large` - 大尺寸,适合重要操作
123
+
124
+ ## API 方法
125
+
126
+ ### 实例方法
127
+
128
+ ```javascript
129
+ const button = new ZhlButton({ text: "我的按钮" });
130
+
131
+ // 更新按钮文本
132
+ button.setText("新的文本");
133
+
134
+ // 更新按钮类型
135
+ button.setType("success");
136
+
137
+ // 更新按钮尺寸
138
+ button.setSize("large");
139
+
140
+ // 设置禁用状态
141
+ button.setDisabled(true);
142
+
143
+ // 设置加载状态
144
+ button.setLoading(true);
145
+
146
+ // 销毁按钮
147
+ button.destroy();
148
+ ```
149
+
150
+ ### 静态方法
151
+
152
+ ```javascript
153
+ // 使用工厂方法创建按钮
154
+ const button = ZhlButton.create({
155
+ text: "工厂按钮",
156
+ type: "warning",
157
+ size: "medium",
158
+ });
159
+ ```
160
+
161
+ ## 样式定制
162
+
163
+ ### CSS 变量
164
+
165
+ 你可以通过覆盖 CSS 变量来自定义按钮样式:
166
+
167
+ ```css
168
+ :root {
169
+ --zhl-btn-primary-bg: #your-color;
170
+ --zhl-btn-border-radius: 8px;
171
+ --zhl-btn-transition: all 0.3s ease;
172
+ }
173
+ ```
174
+
175
+ ### 自定义样式
176
+
177
+ ```css
178
+ /* 为特定按钮添加自定义样式 */
179
+ .my-custom-btn {
180
+ background: linear-gradient(45deg, #ff6b6b, #ee5a24);
181
+ border-radius: 20px;
182
+ font-weight: bold;
183
+ }
184
+
185
+ .my-custom-btn:hover {
186
+ transform: scale(1.05);
187
+ }
188
+ ```
189
+
190
+ ## 事件处理
191
+
192
+ ### 点击事件
193
+
194
+ ```javascript
195
+ const button = new ZhlButton({
196
+ text: "提交表单",
197
+ type: "primary",
198
+ onClick: async (event) => {
199
+ // event 是原生的点击事件对象
200
+ console.log("按钮被点击", event);
201
+
202
+ // 显示加载状态
203
+ button.setLoading(true);
204
+
205
+ try {
206
+ // 执行异步操作
207
+ await submitForm();
208
+ console.log("提交成功");
209
+ } catch (error) {
210
+ console.error("提交失败:", error);
211
+ } finally {
212
+ button.setLoading(false);
213
+ }
214
+ },
215
+ });
216
+ ```
217
+
218
+ ### 键盘事件
219
+
220
+ 按钮组件原生支持键盘导航:
221
+
222
+ - `Tab` - 焦点切换
223
+ - `Enter` 或 `Space` - 触发点击事件
224
+
225
+ ## 无障碍支持
226
+
227
+ 组件遵循 WCAG 2.1 无障碍标准:
228
+
229
+ - 正确的 ARIA 属性
230
+ - 键盘导航支持
231
+ - 焦点管理
232
+ - 屏幕阅读器友好
233
+
234
+ ## 浏览器兼容性
235
+
236
+ - Chrome 60+
237
+ - Firefox 60+
238
+ - Safari 12+
239
+ - Edge 79+
240
+ - iOS Safari 12+
241
+ - Android Chrome 60+
242
+
243
+ ## 开发
244
+
245
+ ### 运行演示
246
+
247
+ ```bash
248
+ npm run demo
249
+ ```
250
+
251
+ ### 文件结构
252
+
253
+ ```
254
+ zhl-button/
255
+ ├── index.js # 主组件文件
256
+ ├── style.css # 样式文件
257
+ ├── demo.js # 演示文件
258
+ ├── package.json # 包配置
259
+ └── README.md # 说明文档
260
+ ```
261
+
262
+ ## 许可证
263
+
264
+ MIT License
265
+
266
+ ## 更新日志
267
+
268
+ ### v1.0.0
269
+
270
+ - 初始版本发布
271
+ - 支持 9 种按钮类型
272
+ - 支持 3 种尺寸
273
+ - 加载状态和禁用状态
274
+ - 完整的 API 和文档
@@ -0,0 +1,459 @@
1
+ # 🎯 Vue 3 使用指南
2
+
3
+ 本文档说明如何在 Vue 3 项目中直接导入使用 ZhlButton 组件。
4
+
5
+ ## 📦 安装
6
+
7
+ ```bash
8
+ npm install zhl-button
9
+ # 或
10
+ yarn add zhl-button
11
+ # 或
12
+ pnpm add zhl-button
13
+ ```
14
+
15
+ ## 🚀 快速开始
16
+
17
+ ### ✨ 极简导入 - 无需引入 CSS!
18
+
19
+ ```vue
20
+ <template>
21
+ <div>
22
+ <ZhlButton text="点击我" type="primary" @click="handleClick" />
23
+ </div>
24
+ </template>
25
+
26
+ <script setup>
27
+ import ZhlButton from "zhl-button"; // 使用默认导入
28
+
29
+ const handleClick = () => {
30
+ console.log("按钮被点击了!");
31
+ };
32
+ </script>
33
+ ```
34
+
35
+ 🎉 **样式会自动注入**,无需额外引入 CSS 文件!
36
+
37
+ ### 📝 导入方式说明
38
+
39
+ ```javascript
40
+ // ✅ 推荐方式 - 默认导入
41
+ import ZhlButton from "zhl-button";
42
+
43
+ // ✅ 备用方式 - 命名导入
44
+ import { ZhlButton } from "zhl-button";
45
+
46
+ // ✅ 最安全方式 - 同时支持两种
47
+ import ZhlButton, { ZhlButton as ZhlButtonComponent } from "zhl-button";
48
+ ```
49
+
50
+ ### 可选:手动引入样式(如果你需要自定义 CSS 变量)
51
+
52
+ ```vue
53
+ <script setup>
54
+ import { ZhlButton } from "zhl-button";
55
+ import "zhl-button/style.css"; // 可选:用于CSS变量自定义
56
+ </script>
57
+ ```
58
+
59
+ ## 🔧 Props 属性
60
+
61
+ | 属性 | 类型 | 默认值 | 说明 |
62
+ | ---------- | ------- | --------- | ------------ |
63
+ | `text` | String | '按钮' | 按钮显示文本 |
64
+ | `type` | String | 'primary' | 按钮类型 |
65
+ | `size` | String | 'medium' | 按钮尺寸 |
66
+ | `disabled` | Boolean | false | 是否禁用 |
67
+ | `loading` | Boolean | false | 是否加载中 |
68
+
69
+ ### 按钮类型
70
+
71
+ - `primary` - 主要按钮(蓝色)
72
+ - `secondary` - 次要按钮(灰色)
73
+ - `success` - 成功按钮(绿色)
74
+ - `danger` - 危险按钮(红色)
75
+ - `warning` - 警告按钮(橙色)
76
+ - `info` - 信息按钮(浅蓝色)
77
+ - `light` - 浅色按钮(白色)
78
+ - `dark` - 深色按钮(深色)
79
+ - `outline` - 轮廓按钮(透明背景)
80
+
81
+ ### 按钮尺寸
82
+
83
+ - `small` - 小按钮
84
+ - `medium` - 中按钮
85
+ - `large` - 大按钮
86
+
87
+ ## 📡 事件
88
+
89
+ | 事件名 | 参数 | 说明 |
90
+ | ----------------- | -------- | ------------ |
91
+ | `click` | event | 按钮点击事件 |
92
+ | `update:loading` | loading | 加载状态更新 |
93
+ | `update:disabled` | disabled | 禁用状态更新 |
94
+
95
+ ## 💡 使用示例
96
+
97
+ ### 基础按钮
98
+
99
+ ```vue
100
+ <template>
101
+ <div class="button-group">
102
+ <ZhlButton text="主要" type="primary" />
103
+ <ZhlButton text="次要" type="secondary" />
104
+ <ZhlButton text="成功" type="success" />
105
+ <ZhlButton text="危险" type="danger" />
106
+ </div>
107
+ </template>
108
+
109
+ <script setup>
110
+ import { ZhlButton } from "zhl-button";
111
+ import "zhl-button/style.css";
112
+ </script>
113
+ ```
114
+
115
+ ### 不同尺寸
116
+
117
+ ```vue
118
+ <template>
119
+ <div>
120
+ <ZhlButton text="小按钮" size="small" type="primary" />
121
+ <ZhlButton text="中按钮" size="medium" type="primary" />
122
+ <ZhlButton text="大按钮" size="large" type="primary" />
123
+ </div>
124
+ </template>
125
+ ```
126
+
127
+ ### 响应式控制
128
+
129
+ ```vue
130
+ <template>
131
+ <div>
132
+ <ZhlButton
133
+ :text="buttonText"
134
+ :type="buttonType"
135
+ :size="buttonSize"
136
+ :disabled="isDisabled"
137
+ :loading="isLoading"
138
+ @click="handleClick"
139
+ />
140
+
141
+ <div class="controls">
142
+ <input v-model="buttonText" placeholder="按钮文本" />
143
+ <select v-model="buttonType">
144
+ <option value="primary">主要</option>
145
+ <option value="secondary">次要</option>
146
+ <option value="success">成功</option>
147
+ </select>
148
+ <label> <input type="checkbox" v-model="isDisabled" /> 禁用 </label>
149
+ <label> <input type="checkbox" v-model="isLoading" /> 加载 </label>
150
+ </div>
151
+ </div>
152
+ </template>
153
+
154
+ <script setup>
155
+ import { ref } from "vue";
156
+ import { ZhlButton } from "zhl-button";
157
+ import "zhl-button/style.css";
158
+
159
+ const buttonText = ref("可编辑按钮");
160
+ const buttonType = ref("primary");
161
+ const buttonSize = ref("medium");
162
+ const isDisabled = ref(false);
163
+ const isLoading = ref(false);
164
+
165
+ const handleClick = () => {
166
+ console.log("按钮被点击");
167
+ };
168
+ </script>
169
+ ```
170
+
171
+ ### 表单提交示例
172
+
173
+ ```vue
174
+ <template>
175
+ <div>
176
+ <form @submit.prevent="handleSubmit">
177
+ <input v-model="formData.email" placeholder="邮箱" />
178
+ <input v-model="formData.password" placeholder="密码" type="password" />
179
+
180
+ <ZhlButton
181
+ text="登录"
182
+ type="primary"
183
+ :loading="isSubmitting"
184
+ :disabled="!isFormValid || isSubmitting"
185
+ @click="handleSubmit"
186
+ />
187
+ </form>
188
+ </div>
189
+ </template>
190
+
191
+ <script setup>
192
+ import { ref, computed } from "vue";
193
+ import { ZhlButton } from "zhl-button";
194
+ import "zhl-button/style.css";
195
+
196
+ const formData = ref({
197
+ email: "",
198
+ password: "",
199
+ });
200
+
201
+ const isSubmitting = ref(false);
202
+
203
+ const isFormValid = computed(() => {
204
+ return formData.value.email && formData.value.password.length >= 6;
205
+ });
206
+
207
+ const handleSubmit = async () => {
208
+ if (!isFormValid.value) return;
209
+
210
+ isSubmitting.value = true;
211
+
212
+ try {
213
+ // 模拟API请求
214
+ await new Promise((resolve) => setTimeout(resolve, 2000));
215
+ alert("登录成功!");
216
+ } catch (error) {
217
+ alert("登录失败");
218
+ } finally {
219
+ isSubmitting.value = false;
220
+ }
221
+ };
222
+ </script>
223
+ ```
224
+
225
+ ## 🌐 全局注册
226
+
227
+ ### 在 main.js 中注册
228
+
229
+ ```javascript
230
+ import { createApp } from "vue";
231
+ import App from "./App.vue";
232
+ import { ZhlButton } from "zhl-button";
233
+ import "zhl-button/style.css";
234
+
235
+ const app = createApp(App);
236
+
237
+ // 全局注册组件
238
+ app.component("ZhlButton", ZhlButton);
239
+
240
+ app.mount("#app");
241
+ ```
242
+
243
+ ### 在组件中使用
244
+
245
+ ```vue
246
+ <template>
247
+ <div>
248
+ <ZhlButton text="全局按钮" type="success" />
249
+ </div>
250
+ </template>
251
+
252
+ <!-- 无需导入,直接使用 -->
253
+ ```
254
+
255
+ ## 🎨 样式自定义
256
+
257
+ ### CSS 变量覆盖
258
+
259
+ ```css
260
+ /* 在全局样式中定义 */
261
+ :root {
262
+ --zhl-btn-primary-bg: #your-color;
263
+ --zhl-btn-primary-hover: #your-hover-color;
264
+ }
265
+ ```
266
+
267
+ ### 深度样式覆盖
268
+
269
+ ```vue
270
+ <template>
271
+ <div class="custom-container">
272
+ <ZhlButton text="自定义按钮" type="primary" class="custom-btn" />
273
+ </div>
274
+ </template>
275
+
276
+ <style scoped>
277
+ .custom-container :deep(.zhl-btn-primary) {
278
+ background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
279
+ border-radius: 25px;
280
+ font-weight: bold;
281
+ }
282
+
283
+ .custom-container :deep(.zhl-btn-primary:hover) {
284
+ transform: translateY(-2px);
285
+ box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
286
+ }
287
+ </style>
288
+ ```
289
+
290
+ ## 📱 TypeScript 支持
291
+
292
+ 如果您使用 TypeScript,可以创建类型定义:
293
+
294
+ ```typescript
295
+ // types/zhl-button.d.ts
296
+ declare module "zhl-button" {
297
+ export interface ZhlButtonProps {
298
+ text?: string;
299
+ type?:
300
+ | "primary"
301
+ | "secondary"
302
+ | "success"
303
+ | "danger"
304
+ | "warning"
305
+ | "info"
306
+ | "light"
307
+ | "dark"
308
+ | "outline";
309
+ size?: "small" | "medium" | "large";
310
+ disabled?: boolean;
311
+ loading?: boolean;
312
+ }
313
+
314
+ export interface ZhlButtonEmits {
315
+ click: [event: Event];
316
+ "update:loading": [loading: boolean];
317
+ "update:disabled": [disabled: boolean];
318
+ }
319
+ }
320
+ ```
321
+
322
+ ## 📦 包内容
323
+
324
+ npm 包包含以下文件:
325
+
326
+ - `index.js` - 原始 JavaScript 组件
327
+ - `ZhlButton.vue` - Vue 3 组件
328
+ - `style.css` - 样式文件
329
+ - `vue.js` - Vue 入口文件
330
+ - `README.md` - 文档
331
+
332
+ ## 🚀 完整示例
333
+
334
+ 这是一个完整的 Vue 3 页面示例:
335
+
336
+ ```vue
337
+ <template>
338
+ <div class="app">
339
+ <h1>🎯 ZhlButton Vue 3 示例</h1>
340
+
341
+ <section>
342
+ <h2>基础按钮</h2>
343
+ <div class="button-group">
344
+ <ZhlButton text="主要" type="primary" @click="log('主要按钮')" />
345
+ <ZhlButton text="次要" type="secondary" @click="log('次要按钮')" />
346
+ <ZhlButton text="成功" type="success" @click="log('成功按钮')" />
347
+ </div>
348
+ </section>
349
+
350
+ <section>
351
+ <h2>响应式按钮</h2>
352
+ <ZhlButton
353
+ :text="dynamicText"
354
+ :type="dynamicType"
355
+ :size="dynamicSize"
356
+ :loading="isLoading"
357
+ @click="handleDynamicClick"
358
+ />
359
+
360
+ <div class="controls">
361
+ <input v-model="dynamicText" placeholder="按钮文本" />
362
+ <select v-model="dynamicType">
363
+ <option value="primary">主要</option>
364
+ <option value="secondary">次要</option>
365
+ <option value="success">成功</option>
366
+ <option value="danger">危险</option>
367
+ </select>
368
+ <select v-model="dynamicSize">
369
+ <option value="small">小</option>
370
+ <option value="medium">中</option>
371
+ <option value="large">大</option>
372
+ </select>
373
+ <label> <input type="checkbox" v-model="isLoading" /> 加载 </label>
374
+ </div>
375
+ </section>
376
+ </div>
377
+ </template>
378
+
379
+ <script setup>
380
+ import { ref } from "vue";
381
+ import { ZhlButton } from "zhl-button";
382
+ import "zhl-button/style.css";
383
+
384
+ const dynamicText = ref("可编辑按钮");
385
+ const dynamicType = ref("primary");
386
+ const dynamicSize = ref("medium");
387
+ const isLoading = ref(false);
388
+
389
+ const log = (message) => {
390
+ console.log(message);
391
+ alert(`点击了: ${message}`);
392
+ };
393
+
394
+ const handleDynamicClick = () => {
395
+ isLoading.value = true;
396
+ setTimeout(() => {
397
+ isLoading.value = false;
398
+ alert("操作完成!");
399
+ }, 1500);
400
+ };
401
+ </script>
402
+
403
+ <style scoped>
404
+ .app {
405
+ padding: 40px 20px;
406
+ max-width: 1000px;
407
+ margin: 0 auto;
408
+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto",
409
+ sans-serif;
410
+ }
411
+
412
+ h1 {
413
+ text-align: center;
414
+ color: #333;
415
+ margin-bottom: 40px;
416
+ }
417
+
418
+ section {
419
+ margin-bottom: 50px;
420
+ padding: 30px;
421
+ background: #f8f9fa;
422
+ border-radius: 12px;
423
+ }
424
+
425
+ h2 {
426
+ color: #495057;
427
+ margin-bottom: 20px;
428
+ }
429
+
430
+ .button-group {
431
+ display: flex;
432
+ gap: 15px;
433
+ flex-wrap: wrap;
434
+ }
435
+
436
+ .controls {
437
+ margin-top: 20px;
438
+ display: flex;
439
+ gap: 10px;
440
+ flex-wrap: wrap;
441
+ }
442
+
443
+ .controls input,
444
+ .controls select {
445
+ padding: 8px 12px;
446
+ border: 1px solid #ced4da;
447
+ border-radius: 4px;
448
+ }
449
+ </style>
450
+ ```
451
+
452
+ ## 🔗 相关资源
453
+
454
+ - `README.md` - 总体文档
455
+ - `Vue3使用指南.md` - 本文档
456
+ - `index.js` - 原始组件 API
457
+ - `ZhlButton.vue` - Vue 组件源码
458
+
459
+ 立即在您的 Vue 3 项目中使用 ZhlButton 组件吧!