@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/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-present YH-UI Team
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,585 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const vue = require('vue');
|
|
4
|
+
const locale = require('@yh-ui/locale');
|
|
5
|
+
const _dayjs = require('dayjs');
|
|
6
|
+
require('dayjs/locale/en');
|
|
7
|
+
require('dayjs/locale/zh-cn');
|
|
8
|
+
require('dayjs/locale/zh-tw');
|
|
9
|
+
require('dayjs/locale/ja');
|
|
10
|
+
require('dayjs/locale/ko');
|
|
11
|
+
|
|
12
|
+
function _interopNamespaceCompat(e) {
|
|
13
|
+
if (e && typeof e === 'object' && 'default' in e) return e;
|
|
14
|
+
const n = Object.create(null);
|
|
15
|
+
if (e) {
|
|
16
|
+
for (const k in e) {
|
|
17
|
+
n[k] = e[k];
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
n.default = e;
|
|
21
|
+
return n;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const _dayjs__namespace = /*#__PURE__*/_interopNamespaceCompat(_dayjs);
|
|
25
|
+
|
|
26
|
+
const defaultNamespace = "yh";
|
|
27
|
+
const statePrefix = "is-";
|
|
28
|
+
const namespaceContextKey = Symbol("namespaceContextKey");
|
|
29
|
+
const useGlobalNamespace = () => {
|
|
30
|
+
return vue.inject(namespaceContextKey, vue.ref(defaultNamespace));
|
|
31
|
+
};
|
|
32
|
+
const useNamespace = (block) => {
|
|
33
|
+
const namespace = useGlobalNamespace();
|
|
34
|
+
const b = (blockSuffix = "") => {
|
|
35
|
+
const ns = vue.unref(namespace);
|
|
36
|
+
return blockSuffix ? `${ns}-${block}-${blockSuffix}` : `${ns}-${block}`;
|
|
37
|
+
};
|
|
38
|
+
const e = (element) => {
|
|
39
|
+
return element ? `${b()}__${element}` : "";
|
|
40
|
+
};
|
|
41
|
+
const m = (modifier) => {
|
|
42
|
+
return modifier ? `${b()}--${modifier}` : "";
|
|
43
|
+
};
|
|
44
|
+
const bem = (blockSuffix, element, modifier) => {
|
|
45
|
+
let cls = b(blockSuffix);
|
|
46
|
+
if (element) cls += `__${element}`;
|
|
47
|
+
if (modifier) cls += `--${modifier}`;
|
|
48
|
+
return cls;
|
|
49
|
+
};
|
|
50
|
+
const em = (element, modifier) => {
|
|
51
|
+
return element && modifier ? `${b()}__${element}--${modifier}` : "";
|
|
52
|
+
};
|
|
53
|
+
function is(state, value) {
|
|
54
|
+
if (arguments.length === 1) {
|
|
55
|
+
return `${statePrefix}${state}`;
|
|
56
|
+
}
|
|
57
|
+
return value ? `${statePrefix}${state}` : "";
|
|
58
|
+
}
|
|
59
|
+
const cssVar = (name) => {
|
|
60
|
+
return `--${vue.unref(namespace)}-${block}-${name}`;
|
|
61
|
+
};
|
|
62
|
+
const cssVarObj = (vars) => {
|
|
63
|
+
const obj = {};
|
|
64
|
+
Object.entries(vars).forEach(([key, value]) => {
|
|
65
|
+
obj[cssVar(key)] = value;
|
|
66
|
+
});
|
|
67
|
+
return obj;
|
|
68
|
+
};
|
|
69
|
+
const cssVarBlock = (name) => {
|
|
70
|
+
return `--${vue.unref(namespace)}-${name}`;
|
|
71
|
+
};
|
|
72
|
+
const cssVarBlockObj = (vars) => {
|
|
73
|
+
const obj = {};
|
|
74
|
+
Object.entries(vars).forEach(([key, value]) => {
|
|
75
|
+
obj[cssVarBlock(key)] = value;
|
|
76
|
+
});
|
|
77
|
+
return obj;
|
|
78
|
+
};
|
|
79
|
+
return {
|
|
80
|
+
namespace,
|
|
81
|
+
b,
|
|
82
|
+
e,
|
|
83
|
+
m,
|
|
84
|
+
bem,
|
|
85
|
+
em,
|
|
86
|
+
is,
|
|
87
|
+
cssVar,
|
|
88
|
+
cssVarObj,
|
|
89
|
+
cssVarBlock,
|
|
90
|
+
cssVarBlockObj
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const defaultInitialZIndex = 2e3;
|
|
95
|
+
const zIndexContextKey = Symbol("zIndexContextKey");
|
|
96
|
+
const zIndexCounterKey = Symbol("zIndexCounterKey");
|
|
97
|
+
const getNextZIndex = () => {
|
|
98
|
+
if (typeof window !== "undefined") {
|
|
99
|
+
const windowContext = window;
|
|
100
|
+
if (windowContext.__YH_Z_INDEX__ === void 0) {
|
|
101
|
+
windowContext.__YH_Z_INDEX__ = defaultInitialZIndex;
|
|
102
|
+
}
|
|
103
|
+
return ++windowContext.__YH_Z_INDEX__;
|
|
104
|
+
}
|
|
105
|
+
return defaultInitialZIndex;
|
|
106
|
+
};
|
|
107
|
+
const resetZIndex = (value = defaultInitialZIndex) => {
|
|
108
|
+
if (typeof window !== "undefined") {
|
|
109
|
+
window.__YH_Z_INDEX__ = value;
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
const createZIndexCounter = (initialValue = defaultInitialZIndex) => {
|
|
113
|
+
return { current: initialValue };
|
|
114
|
+
};
|
|
115
|
+
const useZIndex = (zIndexOverrides) => {
|
|
116
|
+
const injectedZIndex = vue.inject(zIndexContextKey, void 0);
|
|
117
|
+
const appCounter = vue.inject(zIndexCounterKey, null);
|
|
118
|
+
const initialZIndex = vue.computed(() => {
|
|
119
|
+
const override = vue.unref(zIndexOverrides);
|
|
120
|
+
return override ?? vue.unref(injectedZIndex) ?? defaultInitialZIndex;
|
|
121
|
+
});
|
|
122
|
+
const currentZIndex = vue.computed(() => initialZIndex.value);
|
|
123
|
+
const nextZIndex = () => {
|
|
124
|
+
const override = vue.unref(zIndexOverrides);
|
|
125
|
+
if (override !== void 0) return override;
|
|
126
|
+
if (appCounter) {
|
|
127
|
+
return ++appCounter.current;
|
|
128
|
+
}
|
|
129
|
+
return getNextZIndex();
|
|
130
|
+
};
|
|
131
|
+
return {
|
|
132
|
+
initialZIndex,
|
|
133
|
+
currentZIndex,
|
|
134
|
+
nextZIndex
|
|
135
|
+
};
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
const configProviderContextKey = Symbol(
|
|
139
|
+
"configProviderContextKey"
|
|
140
|
+
);
|
|
141
|
+
const useConfig = () => {
|
|
142
|
+
const configRef = vue.inject(configProviderContextKey, null);
|
|
143
|
+
const globalSize = vue.computed(() => {
|
|
144
|
+
const config = vue.unref(configRef);
|
|
145
|
+
return config?.size || "default";
|
|
146
|
+
});
|
|
147
|
+
const globalZIndex = vue.computed(() => {
|
|
148
|
+
const config = vue.unref(configRef);
|
|
149
|
+
return config?.zIndex || 2e3;
|
|
150
|
+
});
|
|
151
|
+
const globalLocale = vue.computed(() => {
|
|
152
|
+
const config = vue.unref(configRef);
|
|
153
|
+
return config?.locale;
|
|
154
|
+
});
|
|
155
|
+
return {
|
|
156
|
+
config: configRef,
|
|
157
|
+
globalSize,
|
|
158
|
+
globalZIndex,
|
|
159
|
+
globalLocale
|
|
160
|
+
};
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
const dayjs = _dayjs__namespace.default || _dayjs__namespace;
|
|
164
|
+
const loadedLocales = /* @__PURE__ */ new Set(["en", "zh-cn", "zh-tw", "ja", "ko"]);
|
|
165
|
+
const localeMapping = {
|
|
166
|
+
"zh-cn": "zh-cn",
|
|
167
|
+
"zh-tw": "zh-tw",
|
|
168
|
+
"zh-hk": "zh-hk",
|
|
169
|
+
"zh-mo": "zh-tw",
|
|
170
|
+
// 澳门使用繁体
|
|
171
|
+
en: "en",
|
|
172
|
+
ja: "ja",
|
|
173
|
+
ko: "ko",
|
|
174
|
+
de: "de",
|
|
175
|
+
fr: "fr",
|
|
176
|
+
es: "es",
|
|
177
|
+
pt: "pt",
|
|
178
|
+
"pt-br": "pt-br",
|
|
179
|
+
ru: "ru",
|
|
180
|
+
ar: "ar",
|
|
181
|
+
"ar-eg": "ar",
|
|
182
|
+
tr: "tr",
|
|
183
|
+
it: "it",
|
|
184
|
+
nl: "nl",
|
|
185
|
+
pl: "pl",
|
|
186
|
+
th: "th",
|
|
187
|
+
vi: "vi",
|
|
188
|
+
id: "id",
|
|
189
|
+
ms: "ms",
|
|
190
|
+
da: "da",
|
|
191
|
+
sv: "sv",
|
|
192
|
+
fi: "fi",
|
|
193
|
+
no: "nb",
|
|
194
|
+
"nb-NO": "nb",
|
|
195
|
+
cs: "cs",
|
|
196
|
+
sk: "sk",
|
|
197
|
+
uk: "uk",
|
|
198
|
+
hu: "hu",
|
|
199
|
+
ro: "ro",
|
|
200
|
+
bg: "bg",
|
|
201
|
+
az: "az",
|
|
202
|
+
fa: "fa",
|
|
203
|
+
hi: "hi",
|
|
204
|
+
pa: "pa-in",
|
|
205
|
+
el: "el",
|
|
206
|
+
ca: "ca",
|
|
207
|
+
tk: "tk",
|
|
208
|
+
ta: "ta",
|
|
209
|
+
lv: "lv",
|
|
210
|
+
af: "af",
|
|
211
|
+
et: "et",
|
|
212
|
+
sl: "sl",
|
|
213
|
+
he: "he",
|
|
214
|
+
lo: "lo",
|
|
215
|
+
lt: "lt",
|
|
216
|
+
mn: "mn",
|
|
217
|
+
kk: "kk",
|
|
218
|
+
ku: "ku",
|
|
219
|
+
ckb: "ku",
|
|
220
|
+
"ug-cn": "ug-cn",
|
|
221
|
+
km: "km",
|
|
222
|
+
sr: "sr",
|
|
223
|
+
eu: "eu",
|
|
224
|
+
ky: "ky",
|
|
225
|
+
"hy-am": "hy-am",
|
|
226
|
+
hr: "hr",
|
|
227
|
+
eo: "eo",
|
|
228
|
+
bn: "bn",
|
|
229
|
+
mg: "mg",
|
|
230
|
+
sw: "sw",
|
|
231
|
+
"uz-uz": "uz",
|
|
232
|
+
my: "my",
|
|
233
|
+
te: "te"
|
|
234
|
+
};
|
|
235
|
+
const getDayjsLocale = (localeCode) => {
|
|
236
|
+
return localeMapping[localeCode] || "en";
|
|
237
|
+
};
|
|
238
|
+
const setDayjsLocale = async (localeCode) => {
|
|
239
|
+
const dayjsLocale = getDayjsLocale(localeCode);
|
|
240
|
+
if (loadedLocales.has(dayjsLocale)) {
|
|
241
|
+
dayjs.locale(dayjsLocale);
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
try {
|
|
245
|
+
await import(`../../../../node_modules/dayjs/locale/${dayjsLocale}.js`);
|
|
246
|
+
loadedLocales.add(dayjsLocale);
|
|
247
|
+
dayjs.locale(dayjsLocale);
|
|
248
|
+
} catch {
|
|
249
|
+
console.warn(`[yh-ui] Failed to load dayjs locale: ${dayjsLocale}, falling back to 'en'`);
|
|
250
|
+
dayjs.locale("en");
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
const setDayjsLocaleSync = (localeCode) => {
|
|
254
|
+
const dayjsLocale = getDayjsLocale(localeCode);
|
|
255
|
+
if (loadedLocales.has(dayjsLocale)) {
|
|
256
|
+
dayjs.locale(dayjsLocale);
|
|
257
|
+
} else {
|
|
258
|
+
dayjs.locale("en");
|
|
259
|
+
setDayjsLocale(localeCode);
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
const updateDayjsMonths = (localeCode, months) => {
|
|
263
|
+
const dayjsLocale = getDayjsLocale(localeCode);
|
|
264
|
+
const monthsArray = [
|
|
265
|
+
months.jan,
|
|
266
|
+
months.feb,
|
|
267
|
+
months.mar,
|
|
268
|
+
months.apr,
|
|
269
|
+
months.may,
|
|
270
|
+
months.jun,
|
|
271
|
+
months.jul,
|
|
272
|
+
months.aug,
|
|
273
|
+
months.sep,
|
|
274
|
+
months.oct,
|
|
275
|
+
months.nov,
|
|
276
|
+
months.dec
|
|
277
|
+
];
|
|
278
|
+
try {
|
|
279
|
+
if (dayjs.updateLocale) {
|
|
280
|
+
dayjs.updateLocale(dayjsLocale, {
|
|
281
|
+
months: monthsArray,
|
|
282
|
+
monthsShort: monthsArray
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
} catch {
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
const useLocale = (localeOverrides) => {
|
|
290
|
+
const { globalLocale } = useConfig();
|
|
291
|
+
const locale$1 = vue.computed(() => {
|
|
292
|
+
return vue.unref(localeOverrides) ?? vue.unref(globalLocale) ?? locale.zhCn;
|
|
293
|
+
});
|
|
294
|
+
const lang = vue.computed(() => locale$1.value.name);
|
|
295
|
+
vue.watch(
|
|
296
|
+
lang,
|
|
297
|
+
(newLang) => {
|
|
298
|
+
setDayjsLocale(newLang);
|
|
299
|
+
},
|
|
300
|
+
{ immediate: true }
|
|
301
|
+
);
|
|
302
|
+
const t = (path, options) => {
|
|
303
|
+
const keys = path.split(".");
|
|
304
|
+
let result = locale$1.value.yh;
|
|
305
|
+
for (const key of keys) {
|
|
306
|
+
if (result && typeof result === "object") {
|
|
307
|
+
result = result[key];
|
|
308
|
+
} else {
|
|
309
|
+
result = void 0;
|
|
310
|
+
}
|
|
311
|
+
if (result === void 0) return path;
|
|
312
|
+
}
|
|
313
|
+
if (typeof result !== "string") return path;
|
|
314
|
+
if (options) {
|
|
315
|
+
return result.replace(/\{(\w+)\}/g, (_match, key) => {
|
|
316
|
+
const val = options[key];
|
|
317
|
+
return val !== void 0 ? String(val) : `{${key}}`;
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
return result;
|
|
321
|
+
};
|
|
322
|
+
const tRaw = (path) => {
|
|
323
|
+
const keys = path.split(".");
|
|
324
|
+
let result = locale$1.value.yh;
|
|
325
|
+
for (const key of keys) {
|
|
326
|
+
if (result && typeof result === "object") {
|
|
327
|
+
result = result[key];
|
|
328
|
+
} else {
|
|
329
|
+
result = void 0;
|
|
330
|
+
}
|
|
331
|
+
if (result === void 0) return path;
|
|
332
|
+
}
|
|
333
|
+
return result;
|
|
334
|
+
};
|
|
335
|
+
return {
|
|
336
|
+
locale: locale$1,
|
|
337
|
+
lang,
|
|
338
|
+
t,
|
|
339
|
+
tRaw
|
|
340
|
+
};
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
const idInjectionKey = Symbol("idInjectionKey");
|
|
344
|
+
const useId = (idOverrides) => {
|
|
345
|
+
const injectedId = vue.inject(idInjectionKey, void 0);
|
|
346
|
+
const nativeId = vue.useId();
|
|
347
|
+
const id = vue.computed(() => {
|
|
348
|
+
const override = vue.unref(idOverrides);
|
|
349
|
+
if (override) return override;
|
|
350
|
+
const injected = vue.unref(injectedId);
|
|
351
|
+
if (injected) return injected;
|
|
352
|
+
return nativeId;
|
|
353
|
+
});
|
|
354
|
+
return id;
|
|
355
|
+
};
|
|
356
|
+
const useIdInjection = () => {
|
|
357
|
+
return {
|
|
358
|
+
prefix: vue.computed(() => `yh-${Date.now()}`),
|
|
359
|
+
current: 0
|
|
360
|
+
// No longer using counter
|
|
361
|
+
};
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
const FormContextKey = Symbol("FormContextKey");
|
|
365
|
+
const FormItemContextKey = Symbol("FormItemContextKey");
|
|
366
|
+
const useFormItem = () => {
|
|
367
|
+
const form = vue.inject(FormContextKey, void 0);
|
|
368
|
+
const formItem = vue.inject(FormItemContextKey, void 0);
|
|
369
|
+
return {
|
|
370
|
+
form,
|
|
371
|
+
formItem,
|
|
372
|
+
// 触发校验
|
|
373
|
+
validate: (trigger) => {
|
|
374
|
+
if (formItem) {
|
|
375
|
+
return formItem.validate(trigger).catch(() => {
|
|
376
|
+
return false;
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
return Promise.resolve(true);
|
|
380
|
+
}
|
|
381
|
+
};
|
|
382
|
+
};
|
|
383
|
+
|
|
384
|
+
function useVirtualScroll(options) {
|
|
385
|
+
const { itemHeight, containerHeight, overscan = 3 } = options;
|
|
386
|
+
const containerRef = vue.ref(null);
|
|
387
|
+
const scrollTop = vue.ref(0);
|
|
388
|
+
const itemsRef = vue.computed(() => {
|
|
389
|
+
const items = options.items;
|
|
390
|
+
return Array.isArray(items) ? items : items.value;
|
|
391
|
+
});
|
|
392
|
+
const totalHeight = vue.computed(() => itemsRef.value.length * itemHeight);
|
|
393
|
+
const visibleCount = vue.computed(() => Math.ceil(containerHeight / itemHeight));
|
|
394
|
+
const startIndex = vue.computed(() => {
|
|
395
|
+
const items = itemsRef.value;
|
|
396
|
+
if (items.length === 0) return 0;
|
|
397
|
+
const start = Math.floor(scrollTop.value / itemHeight);
|
|
398
|
+
return Math.max(0, start - overscan);
|
|
399
|
+
});
|
|
400
|
+
const endIndex = vue.computed(() => {
|
|
401
|
+
const items = itemsRef.value;
|
|
402
|
+
if (items.length === 0) return 0;
|
|
403
|
+
const start = Math.floor(scrollTop.value / itemHeight);
|
|
404
|
+
const end = start + visibleCount.value;
|
|
405
|
+
return Math.min(items.length, end + overscan);
|
|
406
|
+
});
|
|
407
|
+
const visibleItems = vue.computed(() => {
|
|
408
|
+
const items = itemsRef.value;
|
|
409
|
+
if (items.length === 0) return [];
|
|
410
|
+
return items.slice(startIndex.value, endIndex.value);
|
|
411
|
+
});
|
|
412
|
+
const offsetY = vue.computed(() => startIndex.value * itemHeight);
|
|
413
|
+
const onScroll = (event) => {
|
|
414
|
+
const target = event.target;
|
|
415
|
+
scrollTop.value = target.scrollTop;
|
|
416
|
+
};
|
|
417
|
+
const scrollToIndex = (index) => {
|
|
418
|
+
if (containerRef.value) {
|
|
419
|
+
const targetScrollTop = index * itemHeight;
|
|
420
|
+
containerRef.value.scrollTop = targetScrollTop;
|
|
421
|
+
scrollTop.value = targetScrollTop;
|
|
422
|
+
}
|
|
423
|
+
};
|
|
424
|
+
return {
|
|
425
|
+
visibleItems,
|
|
426
|
+
totalHeight,
|
|
427
|
+
offsetY,
|
|
428
|
+
startIndex: vue.computed(() => startIndex.value),
|
|
429
|
+
endIndex: vue.computed(() => endIndex.value),
|
|
430
|
+
onScroll,
|
|
431
|
+
scrollToIndex,
|
|
432
|
+
containerRef
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
function useCache(key, fetcher) {
|
|
437
|
+
const data = vue.shallowRef(null);
|
|
438
|
+
const execute = async () => {
|
|
439
|
+
try {
|
|
440
|
+
data.value = await fetcher();
|
|
441
|
+
} catch (err) {
|
|
442
|
+
console.error(`[YH-UI] Cache fetcher error for key ${key}:`, err);
|
|
443
|
+
}
|
|
444
|
+
};
|
|
445
|
+
return {
|
|
446
|
+
data,
|
|
447
|
+
execute
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
function useEventListener(target, event, handler, options) {
|
|
452
|
+
if (typeof window === "undefined") return;
|
|
453
|
+
const getTarget = () => {
|
|
454
|
+
if (typeof target === "function") {
|
|
455
|
+
return target();
|
|
456
|
+
}
|
|
457
|
+
return vue.unref(target);
|
|
458
|
+
};
|
|
459
|
+
const add = () => {
|
|
460
|
+
const el = getTarget();
|
|
461
|
+
if (el) {
|
|
462
|
+
el.addEventListener(event, handler, options);
|
|
463
|
+
}
|
|
464
|
+
};
|
|
465
|
+
const remove = () => {
|
|
466
|
+
const el = getTarget();
|
|
467
|
+
if (el) {
|
|
468
|
+
el.removeEventListener(event, handler, options);
|
|
469
|
+
}
|
|
470
|
+
};
|
|
471
|
+
vue.onMounted(add);
|
|
472
|
+
vue.onBeforeUnmount(remove);
|
|
473
|
+
if (vue.isRef(target)) {
|
|
474
|
+
vue.watch(target, (newVal, oldVal) => {
|
|
475
|
+
if (oldVal) {
|
|
476
|
+
oldVal.removeEventListener(event, handler, options);
|
|
477
|
+
}
|
|
478
|
+
if (newVal) {
|
|
479
|
+
newVal.addEventListener(event, handler, options);
|
|
480
|
+
}
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
const useScrollLock = (trigger) => {
|
|
486
|
+
const isLocked = vue.ref(false);
|
|
487
|
+
let initialHtmlStyle = { overflow: "", paddingRight: "" };
|
|
488
|
+
let initialBodyStyle = { overflow: "", paddingRight: "" };
|
|
489
|
+
const getScrollbarWidth = () => {
|
|
490
|
+
return window.innerWidth - document.documentElement.clientWidth;
|
|
491
|
+
};
|
|
492
|
+
const lock = () => {
|
|
493
|
+
if (isLocked.value) return;
|
|
494
|
+
const width = getScrollbarWidth();
|
|
495
|
+
const html = document.documentElement;
|
|
496
|
+
const body = document.body;
|
|
497
|
+
initialHtmlStyle = {
|
|
498
|
+
overflow: html.style.overflow,
|
|
499
|
+
paddingRight: html.style.paddingRight
|
|
500
|
+
};
|
|
501
|
+
initialBodyStyle = {
|
|
502
|
+
overflow: body.style.overflow,
|
|
503
|
+
paddingRight: body.style.paddingRight
|
|
504
|
+
};
|
|
505
|
+
if (width > 0) {
|
|
506
|
+
const scrollbarWidth = `${width}px`;
|
|
507
|
+
html.style.setProperty("--yh-scrollbar-width", scrollbarWidth);
|
|
508
|
+
const computedBodyPadding = window.getComputedStyle(body).paddingRight;
|
|
509
|
+
body.style.paddingRight = `calc(${computedBodyPadding} + ${scrollbarWidth})`;
|
|
510
|
+
}
|
|
511
|
+
html.style.overflow = "hidden";
|
|
512
|
+
body.style.overflow = "hidden";
|
|
513
|
+
html.classList.add("yh-popup-parent--hidden");
|
|
514
|
+
isLocked.value = true;
|
|
515
|
+
};
|
|
516
|
+
const unlock = () => {
|
|
517
|
+
if (!isLocked.value) return;
|
|
518
|
+
const html = document.documentElement;
|
|
519
|
+
const body = document.body;
|
|
520
|
+
html.style.overflow = initialHtmlStyle.overflow;
|
|
521
|
+
html.style.paddingRight = initialHtmlStyle.paddingRight;
|
|
522
|
+
body.style.overflow = initialBodyStyle.overflow;
|
|
523
|
+
body.style.paddingRight = initialBodyStyle.paddingRight;
|
|
524
|
+
html.classList.remove("yh-popup-parent--hidden");
|
|
525
|
+
setTimeout(() => {
|
|
526
|
+
if (!html.classList.contains("yh-popup-parent--hidden")) {
|
|
527
|
+
html.style.removeProperty("--yh-scrollbar-width");
|
|
528
|
+
}
|
|
529
|
+
}, 400);
|
|
530
|
+
isLocked.value = false;
|
|
531
|
+
};
|
|
532
|
+
vue.watch(trigger, (val) => {
|
|
533
|
+
if (val) {
|
|
534
|
+
lock();
|
|
535
|
+
} else {
|
|
536
|
+
unlock();
|
|
537
|
+
}
|
|
538
|
+
});
|
|
539
|
+
vue.onUnmounted(unlock);
|
|
540
|
+
return {
|
|
541
|
+
isLocked
|
|
542
|
+
};
|
|
543
|
+
};
|
|
544
|
+
|
|
545
|
+
function useClickOutside(target, handler) {
|
|
546
|
+
if (typeof window === "undefined") return;
|
|
547
|
+
const listener = (event) => {
|
|
548
|
+
const el = vue.unref(target);
|
|
549
|
+
if (!el) return;
|
|
550
|
+
const path = event.composedPath();
|
|
551
|
+
if (path.includes(el)) return;
|
|
552
|
+
handler(event);
|
|
553
|
+
};
|
|
554
|
+
useEventListener(window, "mousedown", listener, true);
|
|
555
|
+
useEventListener(window, "touchstart", listener, true);
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
exports.FormContextKey = FormContextKey;
|
|
559
|
+
exports.FormItemContextKey = FormItemContextKey;
|
|
560
|
+
exports.configProviderContextKey = configProviderContextKey;
|
|
561
|
+
exports.createZIndexCounter = createZIndexCounter;
|
|
562
|
+
exports.defaultNamespace = defaultNamespace;
|
|
563
|
+
exports.getDayjsLocale = getDayjsLocale;
|
|
564
|
+
exports.getNextZIndex = getNextZIndex;
|
|
565
|
+
exports.idInjectionKey = idInjectionKey;
|
|
566
|
+
exports.namespaceContextKey = namespaceContextKey;
|
|
567
|
+
exports.resetZIndex = resetZIndex;
|
|
568
|
+
exports.setDayjsLocale = setDayjsLocale;
|
|
569
|
+
exports.setDayjsLocaleSync = setDayjsLocaleSync;
|
|
570
|
+
exports.updateDayjsMonths = updateDayjsMonths;
|
|
571
|
+
exports.useCache = useCache;
|
|
572
|
+
exports.useClickOutside = useClickOutside;
|
|
573
|
+
exports.useConfig = useConfig;
|
|
574
|
+
exports.useEventListener = useEventListener;
|
|
575
|
+
exports.useFormItem = useFormItem;
|
|
576
|
+
exports.useGlobalNamespace = useGlobalNamespace;
|
|
577
|
+
exports.useId = useId;
|
|
578
|
+
exports.useIdInjection = useIdInjection;
|
|
579
|
+
exports.useLocale = useLocale;
|
|
580
|
+
exports.useNamespace = useNamespace;
|
|
581
|
+
exports.useScrollLock = useScrollLock;
|
|
582
|
+
exports.useVirtualScroll = useVirtualScroll;
|
|
583
|
+
exports.useZIndex = useZIndex;
|
|
584
|
+
exports.zIndexContextKey = zIndexContextKey;
|
|
585
|
+
exports.zIndexCounterKey = zIndexCounterKey;
|