@yh-ui/hooks 0.1.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/LICENSE +21 -0
- package/dist/index.cjs +585 -0
- package/dist/index.d.cts +259 -0
- package/dist/index.d.mts +259 -0
- package/dist/index.d.ts +259 -0
- package/dist/index.mjs +542 -0
- package/dist/use-cache/index.cjs +21 -0
- package/dist/use-cache/index.d.ts +10 -0
- package/dist/use-cache/index.mjs +15 -0
- package/dist/use-click-outside/index.cjs +20 -0
- package/dist/use-click-outside/index.d.ts +2 -0
- package/dist/use-click-outside/index.mjs +14 -0
- package/dist/use-config/index.cjs +30 -0
- package/dist/use-config/index.d.ts +22 -0
- package/dist/use-config/index.mjs +25 -0
- package/dist/use-event-listener/index.cjs +40 -0
- package/dist/use-event-listener/index.d.ts +2 -0
- package/dist/use-event-listener/index.mjs +34 -0
- package/dist/use-form-item/index.cjs +27 -0
- package/dist/use-form-item/index.d.ts +43 -0
- package/dist/use-form-item/index.mjs +20 -0
- package/dist/use-id/index.cjs +29 -0
- package/dist/use-id/index.d.ts +21 -0
- package/dist/use-id/index.mjs +21 -0
- package/dist/use-locale/dayjs-locale.cjs +129 -0
- package/dist/use-locale/dayjs-locale.d.ts +37 -0
- package/dist/use-locale/dayjs-locale.mjs +131 -0
- package/dist/use-locale/index.cjs +88 -0
- package/dist/use-locale/index.d.ts +16 -0
- package/dist/use-locale/index.mjs +63 -0
- package/dist/use-namespace/index.cjs +76 -0
- package/dist/use-namespace/index.d.ts +34 -0
- package/dist/use-namespace/index.mjs +68 -0
- package/dist/use-scroll-lock/index.cjs +73 -0
- package/dist/use-scroll-lock/index.d.ts +8 -0
- package/dist/use-scroll-lock/index.mjs +60 -0
- package/dist/use-virtual-scroll/index.cjs +64 -0
- package/dist/use-virtual-scroll/index.d.ts +35 -0
- package/dist/use-virtual-scroll/index.mjs +53 -0
- package/dist/use-z-index/index.cjs +57 -0
- package/dist/use-z-index/index.d.ts +30 -0
- package/dist/use-z-index/index.mjs +45 -0
- package/package.json +56 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,542 @@
|
|
|
1
|
+
import { inject, ref, unref, computed, watch, useId as useId$1, shallowRef, onMounted, onBeforeUnmount, isRef, onUnmounted } from 'vue';
|
|
2
|
+
import { zhCn } from '@yh-ui/locale';
|
|
3
|
+
import * as _dayjs from 'dayjs';
|
|
4
|
+
import 'dayjs/locale/en';
|
|
5
|
+
import 'dayjs/locale/zh-cn';
|
|
6
|
+
import 'dayjs/locale/zh-tw';
|
|
7
|
+
import 'dayjs/locale/ja';
|
|
8
|
+
import 'dayjs/locale/ko';
|
|
9
|
+
|
|
10
|
+
const defaultNamespace = "yh";
|
|
11
|
+
const statePrefix = "is-";
|
|
12
|
+
const namespaceContextKey = Symbol("namespaceContextKey");
|
|
13
|
+
const useGlobalNamespace = () => {
|
|
14
|
+
return inject(namespaceContextKey, ref(defaultNamespace));
|
|
15
|
+
};
|
|
16
|
+
const useNamespace = (block) => {
|
|
17
|
+
const namespace = useGlobalNamespace();
|
|
18
|
+
const b = (blockSuffix = "") => {
|
|
19
|
+
const ns = unref(namespace);
|
|
20
|
+
return blockSuffix ? `${ns}-${block}-${blockSuffix}` : `${ns}-${block}`;
|
|
21
|
+
};
|
|
22
|
+
const e = (element) => {
|
|
23
|
+
return element ? `${b()}__${element}` : "";
|
|
24
|
+
};
|
|
25
|
+
const m = (modifier) => {
|
|
26
|
+
return modifier ? `${b()}--${modifier}` : "";
|
|
27
|
+
};
|
|
28
|
+
const bem = (blockSuffix, element, modifier) => {
|
|
29
|
+
let cls = b(blockSuffix);
|
|
30
|
+
if (element) cls += `__${element}`;
|
|
31
|
+
if (modifier) cls += `--${modifier}`;
|
|
32
|
+
return cls;
|
|
33
|
+
};
|
|
34
|
+
const em = (element, modifier) => {
|
|
35
|
+
return element && modifier ? `${b()}__${element}--${modifier}` : "";
|
|
36
|
+
};
|
|
37
|
+
function is(state, value) {
|
|
38
|
+
if (arguments.length === 1) {
|
|
39
|
+
return `${statePrefix}${state}`;
|
|
40
|
+
}
|
|
41
|
+
return value ? `${statePrefix}${state}` : "";
|
|
42
|
+
}
|
|
43
|
+
const cssVar = (name) => {
|
|
44
|
+
return `--${unref(namespace)}-${block}-${name}`;
|
|
45
|
+
};
|
|
46
|
+
const cssVarObj = (vars) => {
|
|
47
|
+
const obj = {};
|
|
48
|
+
Object.entries(vars).forEach(([key, value]) => {
|
|
49
|
+
obj[cssVar(key)] = value;
|
|
50
|
+
});
|
|
51
|
+
return obj;
|
|
52
|
+
};
|
|
53
|
+
const cssVarBlock = (name) => {
|
|
54
|
+
return `--${unref(namespace)}-${name}`;
|
|
55
|
+
};
|
|
56
|
+
const cssVarBlockObj = (vars) => {
|
|
57
|
+
const obj = {};
|
|
58
|
+
Object.entries(vars).forEach(([key, value]) => {
|
|
59
|
+
obj[cssVarBlock(key)] = value;
|
|
60
|
+
});
|
|
61
|
+
return obj;
|
|
62
|
+
};
|
|
63
|
+
return {
|
|
64
|
+
namespace,
|
|
65
|
+
b,
|
|
66
|
+
e,
|
|
67
|
+
m,
|
|
68
|
+
bem,
|
|
69
|
+
em,
|
|
70
|
+
is,
|
|
71
|
+
cssVar,
|
|
72
|
+
cssVarObj,
|
|
73
|
+
cssVarBlock,
|
|
74
|
+
cssVarBlockObj
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const defaultInitialZIndex = 2e3;
|
|
79
|
+
const zIndexContextKey = Symbol("zIndexContextKey");
|
|
80
|
+
const zIndexCounterKey = Symbol("zIndexCounterKey");
|
|
81
|
+
const getNextZIndex = () => {
|
|
82
|
+
if (typeof window !== "undefined") {
|
|
83
|
+
const windowContext = window;
|
|
84
|
+
if (windowContext.__YH_Z_INDEX__ === void 0) {
|
|
85
|
+
windowContext.__YH_Z_INDEX__ = defaultInitialZIndex;
|
|
86
|
+
}
|
|
87
|
+
return ++windowContext.__YH_Z_INDEX__;
|
|
88
|
+
}
|
|
89
|
+
return defaultInitialZIndex;
|
|
90
|
+
};
|
|
91
|
+
const resetZIndex = (value = defaultInitialZIndex) => {
|
|
92
|
+
if (typeof window !== "undefined") {
|
|
93
|
+
window.__YH_Z_INDEX__ = value;
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
const createZIndexCounter = (initialValue = defaultInitialZIndex) => {
|
|
97
|
+
return { current: initialValue };
|
|
98
|
+
};
|
|
99
|
+
const useZIndex = (zIndexOverrides) => {
|
|
100
|
+
const injectedZIndex = inject(zIndexContextKey, void 0);
|
|
101
|
+
const appCounter = inject(zIndexCounterKey, null);
|
|
102
|
+
const initialZIndex = computed(() => {
|
|
103
|
+
const override = unref(zIndexOverrides);
|
|
104
|
+
return override ?? unref(injectedZIndex) ?? defaultInitialZIndex;
|
|
105
|
+
});
|
|
106
|
+
const currentZIndex = computed(() => initialZIndex.value);
|
|
107
|
+
const nextZIndex = () => {
|
|
108
|
+
const override = unref(zIndexOverrides);
|
|
109
|
+
if (override !== void 0) return override;
|
|
110
|
+
if (appCounter) {
|
|
111
|
+
return ++appCounter.current;
|
|
112
|
+
}
|
|
113
|
+
return getNextZIndex();
|
|
114
|
+
};
|
|
115
|
+
return {
|
|
116
|
+
initialZIndex,
|
|
117
|
+
currentZIndex,
|
|
118
|
+
nextZIndex
|
|
119
|
+
};
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
const configProviderContextKey = Symbol(
|
|
123
|
+
"configProviderContextKey"
|
|
124
|
+
);
|
|
125
|
+
const useConfig = () => {
|
|
126
|
+
const configRef = inject(configProviderContextKey, null);
|
|
127
|
+
const globalSize = computed(() => {
|
|
128
|
+
const config = unref(configRef);
|
|
129
|
+
return config?.size || "default";
|
|
130
|
+
});
|
|
131
|
+
const globalZIndex = computed(() => {
|
|
132
|
+
const config = unref(configRef);
|
|
133
|
+
return config?.zIndex || 2e3;
|
|
134
|
+
});
|
|
135
|
+
const globalLocale = computed(() => {
|
|
136
|
+
const config = unref(configRef);
|
|
137
|
+
return config?.locale;
|
|
138
|
+
});
|
|
139
|
+
return {
|
|
140
|
+
config: configRef,
|
|
141
|
+
globalSize,
|
|
142
|
+
globalZIndex,
|
|
143
|
+
globalLocale
|
|
144
|
+
};
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const dayjs = _dayjs.default || _dayjs;
|
|
148
|
+
const loadedLocales = /* @__PURE__ */ new Set(["en", "zh-cn", "zh-tw", "ja", "ko"]);
|
|
149
|
+
const localeMapping = {
|
|
150
|
+
"zh-cn": "zh-cn",
|
|
151
|
+
"zh-tw": "zh-tw",
|
|
152
|
+
"zh-hk": "zh-hk",
|
|
153
|
+
"zh-mo": "zh-tw",
|
|
154
|
+
// 澳门使用繁体
|
|
155
|
+
en: "en",
|
|
156
|
+
ja: "ja",
|
|
157
|
+
ko: "ko",
|
|
158
|
+
de: "de",
|
|
159
|
+
fr: "fr",
|
|
160
|
+
es: "es",
|
|
161
|
+
pt: "pt",
|
|
162
|
+
"pt-br": "pt-br",
|
|
163
|
+
ru: "ru",
|
|
164
|
+
ar: "ar",
|
|
165
|
+
"ar-eg": "ar",
|
|
166
|
+
tr: "tr",
|
|
167
|
+
it: "it",
|
|
168
|
+
nl: "nl",
|
|
169
|
+
pl: "pl",
|
|
170
|
+
th: "th",
|
|
171
|
+
vi: "vi",
|
|
172
|
+
id: "id",
|
|
173
|
+
ms: "ms",
|
|
174
|
+
da: "da",
|
|
175
|
+
sv: "sv",
|
|
176
|
+
fi: "fi",
|
|
177
|
+
no: "nb",
|
|
178
|
+
"nb-NO": "nb",
|
|
179
|
+
cs: "cs",
|
|
180
|
+
sk: "sk",
|
|
181
|
+
uk: "uk",
|
|
182
|
+
hu: "hu",
|
|
183
|
+
ro: "ro",
|
|
184
|
+
bg: "bg",
|
|
185
|
+
az: "az",
|
|
186
|
+
fa: "fa",
|
|
187
|
+
hi: "hi",
|
|
188
|
+
pa: "pa-in",
|
|
189
|
+
el: "el",
|
|
190
|
+
ca: "ca",
|
|
191
|
+
tk: "tk",
|
|
192
|
+
ta: "ta",
|
|
193
|
+
lv: "lv",
|
|
194
|
+
af: "af",
|
|
195
|
+
et: "et",
|
|
196
|
+
sl: "sl",
|
|
197
|
+
he: "he",
|
|
198
|
+
lo: "lo",
|
|
199
|
+
lt: "lt",
|
|
200
|
+
mn: "mn",
|
|
201
|
+
kk: "kk",
|
|
202
|
+
ku: "ku",
|
|
203
|
+
ckb: "ku",
|
|
204
|
+
"ug-cn": "ug-cn",
|
|
205
|
+
km: "km",
|
|
206
|
+
sr: "sr",
|
|
207
|
+
eu: "eu",
|
|
208
|
+
ky: "ky",
|
|
209
|
+
"hy-am": "hy-am",
|
|
210
|
+
hr: "hr",
|
|
211
|
+
eo: "eo",
|
|
212
|
+
bn: "bn",
|
|
213
|
+
mg: "mg",
|
|
214
|
+
sw: "sw",
|
|
215
|
+
"uz-uz": "uz",
|
|
216
|
+
my: "my",
|
|
217
|
+
te: "te"
|
|
218
|
+
};
|
|
219
|
+
const getDayjsLocale = (localeCode) => {
|
|
220
|
+
return localeMapping[localeCode] || "en";
|
|
221
|
+
};
|
|
222
|
+
const setDayjsLocale = async (localeCode) => {
|
|
223
|
+
const dayjsLocale = getDayjsLocale(localeCode);
|
|
224
|
+
if (loadedLocales.has(dayjsLocale)) {
|
|
225
|
+
dayjs.locale(dayjsLocale);
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
try {
|
|
229
|
+
await import(`../../../../node_modules/dayjs/locale/${dayjsLocale}.js`);
|
|
230
|
+
loadedLocales.add(dayjsLocale);
|
|
231
|
+
dayjs.locale(dayjsLocale);
|
|
232
|
+
} catch {
|
|
233
|
+
console.warn(`[yh-ui] Failed to load dayjs locale: ${dayjsLocale}, falling back to 'en'`);
|
|
234
|
+
dayjs.locale("en");
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
const setDayjsLocaleSync = (localeCode) => {
|
|
238
|
+
const dayjsLocale = getDayjsLocale(localeCode);
|
|
239
|
+
if (loadedLocales.has(dayjsLocale)) {
|
|
240
|
+
dayjs.locale(dayjsLocale);
|
|
241
|
+
} else {
|
|
242
|
+
dayjs.locale("en");
|
|
243
|
+
setDayjsLocale(localeCode);
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
const updateDayjsMonths = (localeCode, months) => {
|
|
247
|
+
const dayjsLocale = getDayjsLocale(localeCode);
|
|
248
|
+
const monthsArray = [
|
|
249
|
+
months.jan,
|
|
250
|
+
months.feb,
|
|
251
|
+
months.mar,
|
|
252
|
+
months.apr,
|
|
253
|
+
months.may,
|
|
254
|
+
months.jun,
|
|
255
|
+
months.jul,
|
|
256
|
+
months.aug,
|
|
257
|
+
months.sep,
|
|
258
|
+
months.oct,
|
|
259
|
+
months.nov,
|
|
260
|
+
months.dec
|
|
261
|
+
];
|
|
262
|
+
try {
|
|
263
|
+
if (dayjs.updateLocale) {
|
|
264
|
+
dayjs.updateLocale(dayjsLocale, {
|
|
265
|
+
months: monthsArray,
|
|
266
|
+
monthsShort: monthsArray
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
} catch {
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
const useLocale = (localeOverrides) => {
|
|
274
|
+
const { globalLocale } = useConfig();
|
|
275
|
+
const locale = computed(() => {
|
|
276
|
+
return unref(localeOverrides) ?? unref(globalLocale) ?? zhCn;
|
|
277
|
+
});
|
|
278
|
+
const lang = computed(() => locale.value.name);
|
|
279
|
+
watch(
|
|
280
|
+
lang,
|
|
281
|
+
(newLang) => {
|
|
282
|
+
setDayjsLocale(newLang);
|
|
283
|
+
},
|
|
284
|
+
{ immediate: true }
|
|
285
|
+
);
|
|
286
|
+
const t = (path, options) => {
|
|
287
|
+
const keys = path.split(".");
|
|
288
|
+
let result = locale.value.yh;
|
|
289
|
+
for (const key of keys) {
|
|
290
|
+
if (result && typeof result === "object") {
|
|
291
|
+
result = result[key];
|
|
292
|
+
} else {
|
|
293
|
+
result = void 0;
|
|
294
|
+
}
|
|
295
|
+
if (result === void 0) return path;
|
|
296
|
+
}
|
|
297
|
+
if (typeof result !== "string") return path;
|
|
298
|
+
if (options) {
|
|
299
|
+
return result.replace(/\{(\w+)\}/g, (_match, key) => {
|
|
300
|
+
const val = options[key];
|
|
301
|
+
return val !== void 0 ? String(val) : `{${key}}`;
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
return result;
|
|
305
|
+
};
|
|
306
|
+
const tRaw = (path) => {
|
|
307
|
+
const keys = path.split(".");
|
|
308
|
+
let result = locale.value.yh;
|
|
309
|
+
for (const key of keys) {
|
|
310
|
+
if (result && typeof result === "object") {
|
|
311
|
+
result = result[key];
|
|
312
|
+
} else {
|
|
313
|
+
result = void 0;
|
|
314
|
+
}
|
|
315
|
+
if (result === void 0) return path;
|
|
316
|
+
}
|
|
317
|
+
return result;
|
|
318
|
+
};
|
|
319
|
+
return {
|
|
320
|
+
locale,
|
|
321
|
+
lang,
|
|
322
|
+
t,
|
|
323
|
+
tRaw
|
|
324
|
+
};
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
const idInjectionKey = Symbol("idInjectionKey");
|
|
328
|
+
const useId = (idOverrides) => {
|
|
329
|
+
const injectedId = inject(idInjectionKey, void 0);
|
|
330
|
+
const nativeId = useId$1();
|
|
331
|
+
const id = computed(() => {
|
|
332
|
+
const override = unref(idOverrides);
|
|
333
|
+
if (override) return override;
|
|
334
|
+
const injected = unref(injectedId);
|
|
335
|
+
if (injected) return injected;
|
|
336
|
+
return nativeId;
|
|
337
|
+
});
|
|
338
|
+
return id;
|
|
339
|
+
};
|
|
340
|
+
const useIdInjection = () => {
|
|
341
|
+
return {
|
|
342
|
+
prefix: computed(() => `yh-${Date.now()}`),
|
|
343
|
+
current: 0
|
|
344
|
+
// No longer using counter
|
|
345
|
+
};
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
const FormContextKey = Symbol("FormContextKey");
|
|
349
|
+
const FormItemContextKey = Symbol("FormItemContextKey");
|
|
350
|
+
const useFormItem = () => {
|
|
351
|
+
const form = inject(FormContextKey, void 0);
|
|
352
|
+
const formItem = inject(FormItemContextKey, void 0);
|
|
353
|
+
return {
|
|
354
|
+
form,
|
|
355
|
+
formItem,
|
|
356
|
+
// 触发校验
|
|
357
|
+
validate: (trigger) => {
|
|
358
|
+
if (formItem) {
|
|
359
|
+
return formItem.validate(trigger).catch(() => {
|
|
360
|
+
return false;
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
return Promise.resolve(true);
|
|
364
|
+
}
|
|
365
|
+
};
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
function useVirtualScroll(options) {
|
|
369
|
+
const { itemHeight, containerHeight, overscan = 3 } = options;
|
|
370
|
+
const containerRef = ref(null);
|
|
371
|
+
const scrollTop = ref(0);
|
|
372
|
+
const itemsRef = computed(() => {
|
|
373
|
+
const items = options.items;
|
|
374
|
+
return Array.isArray(items) ? items : items.value;
|
|
375
|
+
});
|
|
376
|
+
const totalHeight = computed(() => itemsRef.value.length * itemHeight);
|
|
377
|
+
const visibleCount = computed(() => Math.ceil(containerHeight / itemHeight));
|
|
378
|
+
const startIndex = computed(() => {
|
|
379
|
+
const items = itemsRef.value;
|
|
380
|
+
if (items.length === 0) return 0;
|
|
381
|
+
const start = Math.floor(scrollTop.value / itemHeight);
|
|
382
|
+
return Math.max(0, start - overscan);
|
|
383
|
+
});
|
|
384
|
+
const endIndex = computed(() => {
|
|
385
|
+
const items = itemsRef.value;
|
|
386
|
+
if (items.length === 0) return 0;
|
|
387
|
+
const start = Math.floor(scrollTop.value / itemHeight);
|
|
388
|
+
const end = start + visibleCount.value;
|
|
389
|
+
return Math.min(items.length, end + overscan);
|
|
390
|
+
});
|
|
391
|
+
const visibleItems = computed(() => {
|
|
392
|
+
const items = itemsRef.value;
|
|
393
|
+
if (items.length === 0) return [];
|
|
394
|
+
return items.slice(startIndex.value, endIndex.value);
|
|
395
|
+
});
|
|
396
|
+
const offsetY = computed(() => startIndex.value * itemHeight);
|
|
397
|
+
const onScroll = (event) => {
|
|
398
|
+
const target = event.target;
|
|
399
|
+
scrollTop.value = target.scrollTop;
|
|
400
|
+
};
|
|
401
|
+
const scrollToIndex = (index) => {
|
|
402
|
+
if (containerRef.value) {
|
|
403
|
+
const targetScrollTop = index * itemHeight;
|
|
404
|
+
containerRef.value.scrollTop = targetScrollTop;
|
|
405
|
+
scrollTop.value = targetScrollTop;
|
|
406
|
+
}
|
|
407
|
+
};
|
|
408
|
+
return {
|
|
409
|
+
visibleItems,
|
|
410
|
+
totalHeight,
|
|
411
|
+
offsetY,
|
|
412
|
+
startIndex: computed(() => startIndex.value),
|
|
413
|
+
endIndex: computed(() => endIndex.value),
|
|
414
|
+
onScroll,
|
|
415
|
+
scrollToIndex,
|
|
416
|
+
containerRef
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
function useCache(key, fetcher) {
|
|
421
|
+
const data = shallowRef(null);
|
|
422
|
+
const execute = async () => {
|
|
423
|
+
try {
|
|
424
|
+
data.value = await fetcher();
|
|
425
|
+
} catch (err) {
|
|
426
|
+
console.error(`[YH-UI] Cache fetcher error for key ${key}:`, err);
|
|
427
|
+
}
|
|
428
|
+
};
|
|
429
|
+
return {
|
|
430
|
+
data,
|
|
431
|
+
execute
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
function useEventListener(target, event, handler, options) {
|
|
436
|
+
if (typeof window === "undefined") return;
|
|
437
|
+
const getTarget = () => {
|
|
438
|
+
if (typeof target === "function") {
|
|
439
|
+
return target();
|
|
440
|
+
}
|
|
441
|
+
return unref(target);
|
|
442
|
+
};
|
|
443
|
+
const add = () => {
|
|
444
|
+
const el = getTarget();
|
|
445
|
+
if (el) {
|
|
446
|
+
el.addEventListener(event, handler, options);
|
|
447
|
+
}
|
|
448
|
+
};
|
|
449
|
+
const remove = () => {
|
|
450
|
+
const el = getTarget();
|
|
451
|
+
if (el) {
|
|
452
|
+
el.removeEventListener(event, handler, options);
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
onMounted(add);
|
|
456
|
+
onBeforeUnmount(remove);
|
|
457
|
+
if (isRef(target)) {
|
|
458
|
+
watch(target, (newVal, oldVal) => {
|
|
459
|
+
if (oldVal) {
|
|
460
|
+
oldVal.removeEventListener(event, handler, options);
|
|
461
|
+
}
|
|
462
|
+
if (newVal) {
|
|
463
|
+
newVal.addEventListener(event, handler, options);
|
|
464
|
+
}
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
const useScrollLock = (trigger) => {
|
|
470
|
+
const isLocked = ref(false);
|
|
471
|
+
let initialHtmlStyle = { overflow: "", paddingRight: "" };
|
|
472
|
+
let initialBodyStyle = { overflow: "", paddingRight: "" };
|
|
473
|
+
const getScrollbarWidth = () => {
|
|
474
|
+
return window.innerWidth - document.documentElement.clientWidth;
|
|
475
|
+
};
|
|
476
|
+
const lock = () => {
|
|
477
|
+
if (isLocked.value) return;
|
|
478
|
+
const width = getScrollbarWidth();
|
|
479
|
+
const html = document.documentElement;
|
|
480
|
+
const body = document.body;
|
|
481
|
+
initialHtmlStyle = {
|
|
482
|
+
overflow: html.style.overflow,
|
|
483
|
+
paddingRight: html.style.paddingRight
|
|
484
|
+
};
|
|
485
|
+
initialBodyStyle = {
|
|
486
|
+
overflow: body.style.overflow,
|
|
487
|
+
paddingRight: body.style.paddingRight
|
|
488
|
+
};
|
|
489
|
+
if (width > 0) {
|
|
490
|
+
const scrollbarWidth = `${width}px`;
|
|
491
|
+
html.style.setProperty("--yh-scrollbar-width", scrollbarWidth);
|
|
492
|
+
const computedBodyPadding = window.getComputedStyle(body).paddingRight;
|
|
493
|
+
body.style.paddingRight = `calc(${computedBodyPadding} + ${scrollbarWidth})`;
|
|
494
|
+
}
|
|
495
|
+
html.style.overflow = "hidden";
|
|
496
|
+
body.style.overflow = "hidden";
|
|
497
|
+
html.classList.add("yh-popup-parent--hidden");
|
|
498
|
+
isLocked.value = true;
|
|
499
|
+
};
|
|
500
|
+
const unlock = () => {
|
|
501
|
+
if (!isLocked.value) return;
|
|
502
|
+
const html = document.documentElement;
|
|
503
|
+
const body = document.body;
|
|
504
|
+
html.style.overflow = initialHtmlStyle.overflow;
|
|
505
|
+
html.style.paddingRight = initialHtmlStyle.paddingRight;
|
|
506
|
+
body.style.overflow = initialBodyStyle.overflow;
|
|
507
|
+
body.style.paddingRight = initialBodyStyle.paddingRight;
|
|
508
|
+
html.classList.remove("yh-popup-parent--hidden");
|
|
509
|
+
setTimeout(() => {
|
|
510
|
+
if (!html.classList.contains("yh-popup-parent--hidden")) {
|
|
511
|
+
html.style.removeProperty("--yh-scrollbar-width");
|
|
512
|
+
}
|
|
513
|
+
}, 400);
|
|
514
|
+
isLocked.value = false;
|
|
515
|
+
};
|
|
516
|
+
watch(trigger, (val) => {
|
|
517
|
+
if (val) {
|
|
518
|
+
lock();
|
|
519
|
+
} else {
|
|
520
|
+
unlock();
|
|
521
|
+
}
|
|
522
|
+
});
|
|
523
|
+
onUnmounted(unlock);
|
|
524
|
+
return {
|
|
525
|
+
isLocked
|
|
526
|
+
};
|
|
527
|
+
};
|
|
528
|
+
|
|
529
|
+
function useClickOutside(target, handler) {
|
|
530
|
+
if (typeof window === "undefined") return;
|
|
531
|
+
const listener = (event) => {
|
|
532
|
+
const el = unref(target);
|
|
533
|
+
if (!el) return;
|
|
534
|
+
const path = event.composedPath();
|
|
535
|
+
if (path.includes(el)) return;
|
|
536
|
+
handler(event);
|
|
537
|
+
};
|
|
538
|
+
useEventListener(window, "mousedown", listener, true);
|
|
539
|
+
useEventListener(window, "touchstart", listener, true);
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
export { FormContextKey, FormItemContextKey, configProviderContextKey, createZIndexCounter, defaultNamespace, getDayjsLocale, getNextZIndex, idInjectionKey, namespaceContextKey, resetZIndex, setDayjsLocale, setDayjsLocaleSync, updateDayjsMonths, useCache, useClickOutside, useConfig, useEventListener, useFormItem, useGlobalNamespace, useId, useIdInjection, useLocale, useNamespace, useScrollLock, useVirtualScroll, useZIndex, zIndexContextKey, zIndexCounterKey };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.useCache = useCache;
|
|
7
|
+
var _vue = require("vue");
|
|
8
|
+
function useCache(key, fetcher) {
|
|
9
|
+
const data = (0, _vue.shallowRef)(null);
|
|
10
|
+
const execute = async () => {
|
|
11
|
+
try {
|
|
12
|
+
data.value = await fetcher();
|
|
13
|
+
} catch (err) {
|
|
14
|
+
console.error(`[YH-UI] Cache fetcher error for key ${key}:`, err);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
return {
|
|
18
|
+
data,
|
|
19
|
+
execute
|
|
20
|
+
};
|
|
21
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type ShallowRef } from 'vue';
|
|
2
|
+
export interface UseCacheReturn<T> {
|
|
3
|
+
data: ShallowRef<T | null>;
|
|
4
|
+
execute: () => Promise<void>;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* 一个简单的 SSR 友好的缓存 Hook
|
|
8
|
+
* 在服务端可以将结果存入缓存,客户端可以从缓存中恢复或重新计算
|
|
9
|
+
*/
|
|
10
|
+
export declare function useCache<T>(key: string, fetcher: () => T | Promise<T>): UseCacheReturn<T>;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { shallowRef } from "vue";
|
|
2
|
+
export function useCache(key, fetcher) {
|
|
3
|
+
const data = shallowRef(null);
|
|
4
|
+
const execute = async () => {
|
|
5
|
+
try {
|
|
6
|
+
data.value = await fetcher();
|
|
7
|
+
} catch (err) {
|
|
8
|
+
console.error(`[YH-UI] Cache fetcher error for key ${key}:`, err);
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
return {
|
|
12
|
+
data,
|
|
13
|
+
execute
|
|
14
|
+
};
|
|
15
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.useClickOutside = useClickOutside;
|
|
7
|
+
var _vue = require("vue");
|
|
8
|
+
var _useEventListener = require("../use-event-listener/index.cjs");
|
|
9
|
+
function useClickOutside(target, handler) {
|
|
10
|
+
if (typeof window === "undefined") return;
|
|
11
|
+
const listener = event => {
|
|
12
|
+
const el = (0, _vue.unref)(target);
|
|
13
|
+
if (!el) return;
|
|
14
|
+
const path = event.composedPath();
|
|
15
|
+
if (path.includes(el)) return;
|
|
16
|
+
handler(event);
|
|
17
|
+
};
|
|
18
|
+
(0, _useEventListener.useEventListener)(window, "mousedown", listener, true);
|
|
19
|
+
(0, _useEventListener.useEventListener)(window, "touchstart", listener, true);
|
|
20
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { unref } from "vue";
|
|
2
|
+
import { useEventListener } from "../use-event-listener/index.mjs";
|
|
3
|
+
export function useClickOutside(target, handler) {
|
|
4
|
+
if (typeof window === "undefined") return;
|
|
5
|
+
const listener = (event) => {
|
|
6
|
+
const el = unref(target);
|
|
7
|
+
if (!el) return;
|
|
8
|
+
const path = event.composedPath();
|
|
9
|
+
if (path.includes(el)) return;
|
|
10
|
+
handler(event);
|
|
11
|
+
};
|
|
12
|
+
useEventListener(window, "mousedown", listener, true);
|
|
13
|
+
useEventListener(window, "touchstart", listener, true);
|
|
14
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.useConfig = exports.configProviderContextKey = void 0;
|
|
7
|
+
var _vue = require("vue");
|
|
8
|
+
const configProviderContextKey = exports.configProviderContextKey = Symbol("configProviderContextKey");
|
|
9
|
+
const useConfig = () => {
|
|
10
|
+
const configRef = (0, _vue.inject)(configProviderContextKey, null);
|
|
11
|
+
const globalSize = (0, _vue.computed)(() => {
|
|
12
|
+
const config = (0, _vue.unref)(configRef);
|
|
13
|
+
return config?.size || "default";
|
|
14
|
+
});
|
|
15
|
+
const globalZIndex = (0, _vue.computed)(() => {
|
|
16
|
+
const config = (0, _vue.unref)(configRef);
|
|
17
|
+
return config?.zIndex || 2e3;
|
|
18
|
+
});
|
|
19
|
+
const globalLocale = (0, _vue.computed)(() => {
|
|
20
|
+
const config = (0, _vue.unref)(configRef);
|
|
21
|
+
return config?.locale;
|
|
22
|
+
});
|
|
23
|
+
return {
|
|
24
|
+
config: configRef,
|
|
25
|
+
globalSize,
|
|
26
|
+
globalZIndex,
|
|
27
|
+
globalLocale
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
exports.useConfig = useConfig;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { InjectionKey, ComputedRef } from 'vue';
|
|
2
|
+
import type { Language } from '@yh-ui/locale';
|
|
3
|
+
export interface ConfigProviderContext {
|
|
4
|
+
size?: 'small' | 'default' | 'large';
|
|
5
|
+
zIndex?: number;
|
|
6
|
+
locale?: Language;
|
|
7
|
+
message?: {
|
|
8
|
+
max?: number;
|
|
9
|
+
duration?: number;
|
|
10
|
+
offset?: number;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
export declare const configProviderContextKey: InjectionKey<ComputedRef<ConfigProviderContext>>;
|
|
14
|
+
/**
|
|
15
|
+
* 获取全局配置 Hook
|
|
16
|
+
*/
|
|
17
|
+
export declare const useConfig: () => {
|
|
18
|
+
config: ComputedRef<ConfigProviderContext> | null;
|
|
19
|
+
globalSize: ComputedRef<"small" | "default" | "large">;
|
|
20
|
+
globalZIndex: ComputedRef<number>;
|
|
21
|
+
globalLocale: ComputedRef<Language | undefined>;
|
|
22
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { inject, computed, unref } from "vue";
|
|
2
|
+
export const configProviderContextKey = Symbol(
|
|
3
|
+
"configProviderContextKey"
|
|
4
|
+
);
|
|
5
|
+
export const useConfig = () => {
|
|
6
|
+
const configRef = inject(configProviderContextKey, null);
|
|
7
|
+
const globalSize = computed(() => {
|
|
8
|
+
const config = unref(configRef);
|
|
9
|
+
return config?.size || "default";
|
|
10
|
+
});
|
|
11
|
+
const globalZIndex = computed(() => {
|
|
12
|
+
const config = unref(configRef);
|
|
13
|
+
return config?.zIndex || 2e3;
|
|
14
|
+
});
|
|
15
|
+
const globalLocale = computed(() => {
|
|
16
|
+
const config = unref(configRef);
|
|
17
|
+
return config?.locale;
|
|
18
|
+
});
|
|
19
|
+
return {
|
|
20
|
+
config: configRef,
|
|
21
|
+
globalSize,
|
|
22
|
+
globalZIndex,
|
|
23
|
+
globalLocale
|
|
24
|
+
};
|
|
25
|
+
};
|