index.vue
9.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<script setup lang="ts">
import { onMounted, ref } from "vue";
import {
useIsMobile,
useSimpleLocale,
useSortable
} from "@venus/composables";
import { ElCard, ElOption, ElSelect, ElTag } from "element-plus";
// ==================== useIsMobile ====================
const { isMobile } = useIsMobile();
// ==================== useSortable ====================
const sortableList = ref([ "项目 1", "项目 2", "项目 3", "项目 4", "项目 5" ]);
const sortableContainer = ref<HTMLElement | null>(null);
onMounted(async () => {
if (sortableContainer.value) {
const { initializeSortable } = useSortable(sortableContainer.value, {
animation: 200,
ghostClass: "sortable-ghost",
});
await initializeSortable();
}
});
// ==================== useSimpleLocale ====================
const { $t, currentLocale, setSimpleLocale } = useSimpleLocale();
const localeOptions = [
{ label: "中文", value: "zh-CN" },
{ label: "English", value: "en-US" },
];
const changeLocale = (locale: "en-US" | "zh-CN") => {
setSimpleLocale(locale);
};
// 在模板中使用 $t 的辅助函数
const t = (key: string) => $t.value(key);
</script>
<template>
<div class="page-hooks-container">
<div class="page-header">
<h1>Composables Hooks 使用示例</h1>
<p class="page-description">
展示 @venus/composables 包中所有 Hooks 的使用方法和示例
</p>
</div>
<div class="hooks-grid">
<!-- useIsMobile -->
<ElCard class="hook-card" shadow="hover">
<template #header>
<div class="card-header">
<span class="card-title">useIsMobile</span>
<ElTag type="info">
响应式
</ElTag>
</div>
</template>
<div class="card-content">
<p class="card-description">
检测当前设备是否为移动端,基于 Tailwind 断点系统
</p>
<div class="demo-section">
<div class="demo-item">
<span class="demo-label">是否为移动端:</span>
<ElTag :type="isMobile ? 'warning' : 'success'">
{{ isMobile ? "是" : "否" }}
</ElTag>
</div>
<div class="demo-tip">
💡 提示:调整浏览器窗口大小,观察值的变化
</div>
</div>
<div class="code-example">
<pre>
<code>
const { isMobile } = useIsMobile();
// isMobile 是一个响应式的 ref,当窗口宽度小于 md 断点时返回 true
</code>
</pre>
</div>
</div>
</ElCard>
<!-- useSortable -->
<ElCard class="hook-card" shadow="hover">
<template #header>
<div class="card-header">
<span class="card-title">useSortable</span>
<ElTag type="primary">
交互
</ElTag>
</div>
</template>
<div class="card-content">
<p class="card-description">
基于 SortableJS 的拖拽排序功能
</p>
<div class="demo-section">
<div ref="sortableContainer"
class="sortable-list">
<div v-for="(item, index) in sortableList"
:key="index"
class="sortable-item">
{{ item }}
</div>
</div>
<div class="demo-tip">
💡 拖拽列表项进行排序
</div>
</div>
<div class="code-example">
<pre>
<code>
const container = ref<HTMLElement>(null);
const { initializeSortable } = useSortable(container.value, {
animation: 200,
ghostClass: "sortable-ghost"
});
const sortable = await initializeSortable();
</code>
</pre>
</div>
</div>
</ElCard>
<!-- useSimpleLocale -->
<ElCard class="hook-card" shadow="hover">
<template #header>
<div class="card-header">
<span class="card-title">useSimpleLocale</span>
<ElTag type="success">
国际化
</ElTag>
</div>
</template>
<div class="card-content">
<p class="card-description">
简单的国际化解决方案,支持多语言切换
</p>
<div class="demo-section">
<div class="demo-item">
<span class="demo-label">当前语言:</span>
<ElSelect v-model="currentLocale"
style="width: 150px;"
@change="changeLocale">
<ElOption v-for="option in localeOptions"
:key="option.value"
:label="option.label"
:value="option.value" />
</ElSelect>
</div>
<div class="demo-item">
<span class="demo-label">翻译示例:</span>
<div class="locale-examples">
<div class="locale-item">
<span class="locale-key">confirm:</span>
<span class="locale-value">{{ t("confirm") }}</span>
</div>
<div class="locale-item">
<span class="locale-key">cancel:</span>
<span class="locale-value">{{ t("cancel") }}</span>
</div>
<div class="locale-item">
<span class="locale-key">submit:</span>
<span class="locale-value">{{ t("submit") }}</span>
</div>
</div>
</div>
</div>
<div class="code-example">
<pre>
<code>
const { $t, currentLocale, setSimpleLocale } = useSimpleLocale();
setSimpleLocale("zh-CN");
const text = $t("confirm"); // "确认" 或 "Confirm"
</code>
</pre>
</div>
</div>
</ElCard>
</div>
</div>
</template>
<style scoped>
.page-hooks-container {
min-height: 100vh;
padding: 24px;
background: #f5f7fa;
}
.page-header {
margin-bottom: 24px;
text-align: center;
}
.page-header h1 {
margin: 0 0 12px;
font-size: 28px;
font-weight: 600;
color: #303133;
}
.page-description {
margin: 0;
font-size: 14px;
color: #909399;
}
.hooks-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(400px, 1fr));
gap: 24px;
}
.hook-card {
border-radius: 8px;
transition: all 0.3s ease;
}
.hook-card:hover {
box-shadow: 0 4px 12px rgb(0 0 0 / 10%);
transform: translateY(-2px);
}
.card-header {
display: flex;
align-items: center;
justify-content: space-between;
}
.card-title {
font-size: 18px;
font-weight: 600;
color: #303133;
}
.card-content {
padding: 0;
}
.card-description {
margin: 0 0 16px;
font-size: 14px;
line-height: 1.6;
color: #606266;
}
.demo-section {
margin-bottom: 16px;
}
.demo-item {
display: flex;
gap: 8px;
align-items: center;
margin-bottom: 12px;
}
.demo-label {
min-width: 100px;
font-size: 14px;
font-weight: 500;
color: #606266;
}
.demo-value {
font-size: 14px;
font-weight: 600;
color: #303133;
}
.demo-unit {
margin-left: 4px;
font-size: 14px;
color: #909399;
}
.demo-code {
padding: 2px 8px;
font-family: "Courier New", Consolas, monospace;
font-size: 13px;
color: #e6a23c;
background: #fdf6ec;
border-radius: 4px;
}
.demo-tip {
padding: 8px 12px;
margin-top: 12px;
font-size: 12px;
line-height: 1.6;
color: #909399;
background: #f4f4f5;
border-radius: 4px;
}
.code-example {
padding: 12px;
margin-top: 16px;
background: #f5f7fa;
border-left: 3px solid #409eff;
border-radius: 4px;
}
.code-example pre {
margin: 0;
font-family: "Courier New", Consolas, monospace;
font-size: 12px;
line-height: 1.6;
color: #303133;
word-wrap: break-word;
white-space: pre-wrap;
}
.code-example code {
font-family: inherit;
}
.sortable-list {
margin-top: 12px;
}
.sortable-item {
padding: 12px 16px;
margin-bottom: 8px;
cursor: move;
background: #fff;
border: 1px solid #dcdfe6;
border-radius: 4px;
transition: all 0.3s;
}
.sortable-item:hover {
background: #f5f7fa;
border-color: #409eff;
}
.sortable-ghost {
background: #f0f9ff;
opacity: 0.5;
}
.locale-examples {
display: flex;
flex-direction: column;
gap: 8px;
}
.locale-item {
display: flex;
gap: 8px;
align-items: center;
}
.locale-key {
font-family: "Courier New", Consolas, monospace;
font-size: 13px;
color: #909399;
}
.locale-value {
font-size: 14px;
font-weight: 500;
color: #303133;
}
@media (max-width: 768px) {
.hooks-grid {
grid-template-columns: 1fr;
}
}
</style>