@agile-plus/headless 0.0.0-stage → 0.1.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.
Files changed (196) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +150 -2
  3. package/dist/index.cjs +2610 -0
  4. package/dist/index.cjs.map +1 -0
  5. package/dist/index.d.cts +30 -0
  6. package/dist/index.d.ts +30 -0
  7. package/dist/index.js +2631 -0
  8. package/dist/index.js.map +1 -0
  9. package/dist/types-2ROaIfZ9.d.cts +57 -0
  10. package/dist/types-2ROaIfZ9.d.ts +57 -0
  11. package/dist/types-C9nl8qeg.d.cts +100 -0
  12. package/dist/types-C9nl8qeg.d.ts +100 -0
  13. package/dist/types-DM4s_xZa.d.cts +128 -0
  14. package/dist/types-DM4s_xZa.d.ts +128 -0
  15. package/dist/useCheckbox/index.cjs +255 -0
  16. package/dist/useCheckbox/index.cjs.map +1 -0
  17. package/dist/useCheckbox/index.d.cts +145 -0
  18. package/dist/useCheckbox/index.d.ts +145 -0
  19. package/dist/useCheckbox/index.js +232 -0
  20. package/dist/useCheckbox/index.js.map +1 -0
  21. package/dist/useCheckboxGroup/index.cjs +100 -0
  22. package/dist/useCheckboxGroup/index.cjs.map +1 -0
  23. package/dist/useCheckboxGroup/index.d.cts +102 -0
  24. package/dist/useCheckboxGroup/index.d.ts +102 -0
  25. package/dist/useCheckboxGroup/index.js +73 -0
  26. package/dist/useCheckboxGroup/index.js.map +1 -0
  27. package/dist/useCollapsible/index.cjs +151 -0
  28. package/dist/useCollapsible/index.cjs.map +1 -0
  29. package/dist/useCollapsible/index.d.cts +106 -0
  30. package/dist/useCollapsible/index.d.ts +106 -0
  31. package/dist/useCollapsible/index.js +129 -0
  32. package/dist/useCollapsible/index.js.map +1 -0
  33. package/dist/useDialog/index.cjs +294 -0
  34. package/dist/useDialog/index.cjs.map +1 -0
  35. package/dist/useDialog/index.d.cts +105 -0
  36. package/dist/useDialog/index.d.ts +105 -0
  37. package/dist/useDialog/index.js +267 -0
  38. package/dist/useDialog/index.js.map +1 -0
  39. package/dist/useForm/index.cjs +123 -0
  40. package/dist/useForm/index.cjs.map +1 -0
  41. package/dist/useForm/index.d.cts +22 -0
  42. package/dist/useForm/index.d.ts +22 -0
  43. package/dist/useForm/index.js +96 -0
  44. package/dist/useForm/index.js.map +1 -0
  45. package/dist/useFormControl/index.cjs +162 -0
  46. package/dist/useFormControl/index.cjs.map +1 -0
  47. package/dist/useFormControl/index.d.cts +116 -0
  48. package/dist/useFormControl/index.d.ts +116 -0
  49. package/dist/useFormControl/index.js +143 -0
  50. package/dist/useFormControl/index.js.map +1 -0
  51. package/dist/useFormField/index.cjs +45 -0
  52. package/dist/useFormField/index.cjs.map +1 -0
  53. package/dist/useFormField/index.d.cts +22 -0
  54. package/dist/useFormField/index.d.ts +22 -0
  55. package/dist/useFormField/index.js +18 -0
  56. package/dist/useFormField/index.js.map +1 -0
  57. package/dist/useFormMessage/index.cjs +77 -0
  58. package/dist/useFormMessage/index.cjs.map +1 -0
  59. package/dist/useFormMessage/index.d.cts +80 -0
  60. package/dist/useFormMessage/index.d.ts +80 -0
  61. package/dist/useFormMessage/index.js +54 -0
  62. package/dist/useFormMessage/index.js.map +1 -0
  63. package/dist/usePresence/index.cjs +117 -0
  64. package/dist/usePresence/index.cjs.map +1 -0
  65. package/dist/usePresence/index.d.cts +52 -0
  66. package/dist/usePresence/index.d.ts +52 -0
  67. package/dist/usePresence/index.js +90 -0
  68. package/dist/usePresence/index.js.map +1 -0
  69. package/dist/useRadioGroup/index.cjs +197 -0
  70. package/dist/useRadioGroup/index.cjs.map +1 -0
  71. package/dist/useRadioGroup/index.d.cts +162 -0
  72. package/dist/useRadioGroup/index.d.ts +162 -0
  73. package/dist/useRadioGroup/index.js +170 -0
  74. package/dist/useRadioGroup/index.js.map +1 -0
  75. package/dist/useScrollArea/index.cjs +309 -0
  76. package/dist/useScrollArea/index.cjs.map +1 -0
  77. package/dist/useScrollArea/index.d.cts +127 -0
  78. package/dist/useScrollArea/index.d.ts +127 -0
  79. package/dist/useScrollArea/index.js +287 -0
  80. package/dist/useScrollArea/index.js.map +1 -0
  81. package/dist/useSlider/index.cjs +417 -0
  82. package/dist/useSlider/index.cjs.map +1 -0
  83. package/dist/useSlider/index.d.cts +215 -0
  84. package/dist/useSlider/index.d.ts +215 -0
  85. package/dist/useSlider/index.js +401 -0
  86. package/dist/useSlider/index.js.map +1 -0
  87. package/dist/useSwitch/index.cjs +264 -0
  88. package/dist/useSwitch/index.cjs.map +1 -0
  89. package/dist/useSwitch/index.d.cts +110 -0
  90. package/dist/useSwitch/index.d.ts +110 -0
  91. package/dist/useSwitch/index.js +237 -0
  92. package/dist/useSwitch/index.js.map +1 -0
  93. package/dist/useTabs/index.cjs +153 -0
  94. package/dist/useTabs/index.cjs.map +1 -0
  95. package/dist/useTabs/index.d.cts +84 -0
  96. package/dist/useTabs/index.d.ts +84 -0
  97. package/dist/useTabs/index.js +126 -0
  98. package/dist/useTabs/index.js.map +1 -0
  99. package/dist/useToast/index.cjs +375 -0
  100. package/dist/useToast/index.cjs.map +1 -0
  101. package/dist/useToast/index.d.cts +148 -0
  102. package/dist/useToast/index.d.ts +148 -0
  103. package/dist/useToast/index.js +358 -0
  104. package/dist/useToast/index.js.map +1 -0
  105. package/dist/useToastProvider/index.cjs +118 -0
  106. package/dist/useToastProvider/index.cjs.map +1 -0
  107. package/dist/useToastProvider/index.d.cts +25 -0
  108. package/dist/useToastProvider/index.d.ts +25 -0
  109. package/dist/useToastProvider/index.js +96 -0
  110. package/dist/useToastProvider/index.js.map +1 -0
  111. package/dist/useToastViewport/index.cjs +227 -0
  112. package/dist/useToastViewport/index.cjs.map +1 -0
  113. package/dist/useToastViewport/index.d.cts +130 -0
  114. package/dist/useToastViewport/index.d.ts +130 -0
  115. package/dist/useToastViewport/index.js +207 -0
  116. package/dist/useToastViewport/index.js.map +1 -0
  117. package/dist/useToggle/index.cjs +86 -0
  118. package/dist/useToggle/index.cjs.map +1 -0
  119. package/dist/useToggle/index.d.cts +66 -0
  120. package/dist/useToggle/index.d.ts +66 -0
  121. package/dist/useToggle/index.js +59 -0
  122. package/dist/useToggle/index.js.map +1 -0
  123. package/dist/useToggleGroup/index.cjs +227 -0
  124. package/dist/useToggleGroup/index.cjs.map +1 -0
  125. package/dist/useToggleGroup/index.d.cts +159 -0
  126. package/dist/useToggleGroup/index.d.ts +159 -0
  127. package/dist/useToggleGroup/index.js +207 -0
  128. package/dist/useToggleGroup/index.js.map +1 -0
  129. package/package.json +163 -3
  130. package/src/index.ts +37 -0
  131. package/src/useCheckbox/index.ts +3 -0
  132. package/src/useCheckbox/types.ts +150 -0
  133. package/src/useCheckbox/useCheckbox.ts +141 -0
  134. package/src/useCheckboxGroup/index.ts +3 -0
  135. package/src/useCheckboxGroup/types.ts +104 -0
  136. package/src/useCheckboxGroup/useCheckboxGroup.ts +79 -0
  137. package/src/useCollapsible/index.ts +3 -0
  138. package/src/useCollapsible/types.ts +103 -0
  139. package/src/useCollapsible/useCollapsible.ts +160 -0
  140. package/src/useDialog/index.ts +3 -0
  141. package/src/useDialog/types.ts +109 -0
  142. package/src/useDialog/useDialog.ts +181 -0
  143. package/src/useForm/index.ts +9 -0
  144. package/src/useForm/types.ts +149 -0
  145. package/src/useForm/useForm.ts +156 -0
  146. package/src/useFormControl/index.ts +3 -0
  147. package/src/useFormControl/types.ts +101 -0
  148. package/src/useFormControl/useFormControl.ts +181 -0
  149. package/src/useFormField/index.ts +3 -0
  150. package/src/useFormField/types.ts +65 -0
  151. package/src/useFormField/useFormField.ts +46 -0
  152. package/src/useFormMessage/index.ts +3 -0
  153. package/src/useFormMessage/types.ts +64 -0
  154. package/src/useFormMessage/useFormMessage.ts +116 -0
  155. package/src/usePresence/index.ts +3 -0
  156. package/src/usePresence/types.ts +38 -0
  157. package/src/usePresence/usePresence.ts +168 -0
  158. package/src/useRadioGroup/index.ts +8 -0
  159. package/src/useRadioGroup/types.ts +173 -0
  160. package/src/useRadioGroup/useRadioGroup.ts +204 -0
  161. package/src/useScrollArea/index.ts +3 -0
  162. package/src/useScrollArea/types.ts +141 -0
  163. package/src/useScrollArea/useScrollArea.ts +360 -0
  164. package/src/useSlider/index.ts +3 -0
  165. package/src/useSlider/types.ts +231 -0
  166. package/src/useSlider/useSlider.ts +486 -0
  167. package/src/useSwitch/index.ts +3 -0
  168. package/src/useSwitch/types.ts +109 -0
  169. package/src/useSwitch/useSwitch.ts +144 -0
  170. package/src/useTabs/index.ts +3 -0
  171. package/src/useTabs/types.ts +83 -0
  172. package/src/useTabs/useTabs.ts +196 -0
  173. package/src/useToast/index.ts +3 -0
  174. package/src/useToast/types.ts +167 -0
  175. package/src/useToast/useToast.ts +378 -0
  176. package/src/useToastProvider/index.ts +3 -0
  177. package/src/useToastProvider/types.ts +121 -0
  178. package/src/useToastProvider/useToastProvider.ts +115 -0
  179. package/src/useToastViewport/index.ts +3 -0
  180. package/src/useToastViewport/types.ts +131 -0
  181. package/src/useToastViewport/useToastViewport.ts +319 -0
  182. package/src/useToggle/index.ts +3 -0
  183. package/src/useToggle/types.ts +59 -0
  184. package/src/useToggle/useToggle.ts +57 -0
  185. package/src/useToggleGroup/index.ts +3 -0
  186. package/src/useToggleGroup/types.ts +171 -0
  187. package/src/useToggleGroup/useToggleGroup.ts +260 -0
  188. package/src/utils/env.ts +26 -0
  189. package/src/utils/useBubbleInput.ts +168 -0
  190. package/src/utils/useCollection.ts +92 -0
  191. package/src/utils/useControllable.ts +96 -0
  192. package/src/utils/useFocusTrap.ts +160 -0
  193. package/src/utils/useFormResetListener.ts +80 -0
  194. package/src/utils/usePointerDrag.ts +101 -0
  195. package/src/utils/useResizeObserver.ts +60 -0
  196. package/src/utils/useScrollLock.ts +95 -0
@@ -0,0 +1,22 @@
1
+ import { U as UseFormFieldOptions, a as UseFormFieldReturn } from '../types-2ROaIfZ9.cjs';
2
+ import { c as UseFormReturn } from '../types-DM4s_xZa.cjs';
3
+ import 'vue';
4
+ import '@agile-plus/primitives/form';
5
+
6
+ /**
7
+ * 字段实例:一个 id、一个名字,加上从表单注册表里读到的校验结果。
8
+ *
9
+ * 由 FormField 创建并向下提供,Label 与 Control 复用同一个实例,因此三处的
10
+ * `data-valid` / `data-invalid` 永远来自同一次判定,不会出现「文案说无效、控件还标着有效」。
11
+ *
12
+ * 这里不持有任何校验状态:判定结果写在表单的注册表里,字段只是按 name 去读。
13
+ * 之所以不干脆把快照放在字段上,是因为 `Message` 可以脱离 Field 只靠 name 挂进来,
14
+ * 状态放在字段上会让这种用法静默失效。
15
+ *
16
+ * @param options 字段名、服务端标记与可选 id。
17
+ * @param form 所在表单的注册表。
18
+ * @returns 字段状态与属性表。
19
+ */
20
+ declare function useFormField(options: UseFormFieldOptions, form: UseFormReturn): UseFormFieldReturn;
21
+
22
+ export { UseFormFieldOptions, UseFormFieldReturn, useFormField };
@@ -0,0 +1,22 @@
1
+ import { U as UseFormFieldOptions, a as UseFormFieldReturn } from '../types-2ROaIfZ9.js';
2
+ import { c as UseFormReturn } from '../types-DM4s_xZa.js';
3
+ import 'vue';
4
+ import '@agile-plus/primitives/form';
5
+
6
+ /**
7
+ * 字段实例:一个 id、一个名字,加上从表单注册表里读到的校验结果。
8
+ *
9
+ * 由 FormField 创建并向下提供,Label 与 Control 复用同一个实例,因此三处的
10
+ * `data-valid` / `data-invalid` 永远来自同一次判定,不会出现「文案说无效、控件还标着有效」。
11
+ *
12
+ * 这里不持有任何校验状态:判定结果写在表单的注册表里,字段只是按 name 去读。
13
+ * 之所以不干脆把快照放在字段上,是因为 `Message` 可以脱离 Field 只靠 name 挂进来,
14
+ * 状态放在字段上会让这种用法静默失效。
15
+ *
16
+ * @param options 字段名、服务端标记与可选 id。
17
+ * @param form 所在表单的注册表。
18
+ * @returns 字段状态与属性表。
19
+ */
20
+ declare function useFormField(options: UseFormFieldOptions, form: UseFormReturn): UseFormFieldReturn;
21
+
22
+ export { UseFormFieldOptions, UseFormFieldReturn, useFormField };
@@ -0,0 +1,18 @@
1
+ // src/useFormField/useFormField.ts
2
+ import { computed, toValue, useId } from "vue";
3
+ import { getFieldStateAttrs } from "@agile-plus/primitives/form";
4
+ function useFormField(options, form) {
5
+ const uid = useId();
6
+ const name = computed(() => toValue(options.name));
7
+ const id = computed(() => toValue(options.id) ?? `field-${uid}`);
8
+ const serverInvalid = computed(() => toValue(options.serverInvalid) === true);
9
+ const validity = computed(() => form.getFieldValidity(name.value));
10
+ function getStateAttrs() {
11
+ return getFieldStateAttrs({ validity: validity.value, serverInvalid: serverInvalid.value });
12
+ }
13
+ return { id, name, serverInvalid, validity, getStateAttrs };
14
+ }
15
+ export {
16
+ useFormField
17
+ };
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/useFormField/useFormField.ts"],"sourcesContent":["import { computed, toValue, useId } from 'vue';\n\nimport { getFieldStateAttrs } from '@agile-plus/primitives/form';\n\nimport type { UseFormReturn } from '../useForm';\n\nimport type { UseFormFieldOptions, UseFormFieldReturn } from './types';\n\n/**\n * 字段实例:一个 id、一个名字,加上从表单注册表里读到的校验结果。\n *\n * 由 FormField 创建并向下提供,Label 与 Control 复用同一个实例,因此三处的\n * `data-valid` / `data-invalid` 永远来自同一次判定,不会出现「文案说无效、控件还标着有效」。\n *\n * 这里不持有任何校验状态:判定结果写在表单的注册表里,字段只是按 name 去读。\n * 之所以不干脆把快照放在字段上,是因为 `Message` 可以脱离 Field 只靠 name 挂进来,\n * 状态放在字段上会让这种用法静默失效。\n *\n * @param options 字段名、服务端标记与可选 id。\n * @param form 所在表单的注册表。\n * @returns 字段状态与属性表。\n */\nexport function useFormField(\n options: UseFormFieldOptions,\n form: UseFormReturn\n): UseFormFieldReturn {\n /**\n * useId 在 SSR 下与服务端渲染结果一致,不会造成注水不匹配;\n * 加前缀是为了不与消费者手写的 id 撞名。\n */\n const uid = useId();\n\n const name = computed(() => toValue(options.name));\n\n const id = computed(() => toValue(options.id) ?? `field-${uid}`);\n\n const serverInvalid = computed(() => toValue(options.serverInvalid) === true);\n\n const validity = computed(() => form.getFieldValidity(name.value));\n\n function getStateAttrs(): Record<string, unknown> {\n return getFieldStateAttrs({ validity: validity.value, serverInvalid: serverInvalid.value });\n }\n\n return { id, name, serverInvalid, validity, getStateAttrs };\n}\n"],"mappings":";AAAA,SAAS,UAAU,SAAS,aAAa;AAEzC,SAAS,0BAA0B;AAoB5B,SAAS,aACd,SACA,MACoB;AAKpB,QAAM,MAAM,MAAM;AAElB,QAAM,OAAO,SAAS,MAAM,QAAQ,QAAQ,IAAI,CAAC;AAEjD,QAAM,KAAK,SAAS,MAAM,QAAQ,QAAQ,EAAE,KAAK,SAAS,GAAG,EAAE;AAE/D,QAAM,gBAAgB,SAAS,MAAM,QAAQ,QAAQ,aAAa,MAAM,IAAI;AAE5E,QAAM,WAAW,SAAS,MAAM,KAAK,iBAAiB,KAAK,KAAK,CAAC;AAEjE,WAAS,gBAAyC;AAChD,WAAO,mBAAmB,EAAE,UAAU,SAAS,OAAO,eAAe,cAAc,MAAM,CAAC;AAAA,EAC5F;AAEA,SAAO,EAAE,IAAI,MAAM,eAAe,UAAU,cAAc;AAC5D;","names":[]}
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/useFormMessage/index.ts
21
+ var useFormMessage_exports = {};
22
+ __export(useFormMessage_exports, {
23
+ useFormMessage: () => useFormMessage
24
+ });
25
+ module.exports = __toCommonJS(useFormMessage_exports);
26
+
27
+ // src/useFormMessage/useFormMessage.ts
28
+ var import_vue = require("vue");
29
+ var import_form = require("@agile-plus/primitives/form");
30
+ function resolveMatch(match) {
31
+ if (match === void 0) return void 0;
32
+ return (0, import_vue.isRef)(match) ? match.value : match;
33
+ }
34
+ function useFormMessage(options, form, field) {
35
+ const name = (0, import_vue.toValue)(options.name) ?? field?.name.value;
36
+ if (!name) {
37
+ throw new Error("useFormMessage \u9700\u8981 name\uFF1A\u8BF7\u628A\u5B83\u653E\u8FDB useFormField\uFF0C\u6216\u663E\u5F0F\u63D0\u4F9B name\u3002");
38
+ }
39
+ const uid = (0, import_vue.useId)();
40
+ const id = `message-${uid}`;
41
+ const match = (0, import_vue.computed)(() => resolveMatch(options.match));
42
+ const matches = (0, import_vue.computed)(
43
+ () => (0, import_form.getMessageMatch)({
44
+ match: match.value,
45
+ validity: form.getFieldValidity(name),
46
+ customErrors: form.getFieldCustomErrors(name),
47
+ messageId: id,
48
+ forceMatch: (0, import_vue.toValue)(options.forceMatch) === true
49
+ })
50
+ );
51
+ (0, import_vue.watchEffect)((onCleanup) => {
52
+ if (!matches.value) return;
53
+ form.addFieldMessageId(name, id);
54
+ onCleanup(() => form.removeFieldMessageId(name, id));
55
+ });
56
+ (0, import_vue.watchEffect)((onCleanup) => {
57
+ const current = match.value;
58
+ if (typeof current !== "function") return;
59
+ form.addFieldMatcher(name, { id, match: current });
60
+ onCleanup(() => form.removeFieldMatcher(name, id));
61
+ });
62
+ function getDefaultText() {
63
+ const current = match.value;
64
+ if (typeof current !== "string") return import_form.DEFAULT_INVALID_MESSAGE;
65
+ return import_form.BUILT_IN_VALIDITY_MESSAGES[current] ?? import_form.DEFAULT_INVALID_MESSAGE;
66
+ }
67
+ (0, import_vue.onScopeDispose)(() => {
68
+ form.removeFieldMessageId(name, id);
69
+ form.removeFieldMatcher(name, id);
70
+ });
71
+ return { id, name: (0, import_vue.computed)(() => name), matches, getDefaultText };
72
+ }
73
+ // Annotate the CommonJS export names for ESM import in node:
74
+ 0 && (module.exports = {
75
+ useFormMessage
76
+ });
77
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/useFormMessage/index.ts","../../src/useFormMessage/useFormMessage.ts"],"sourcesContent":["export type { UseFormMessageOptions, UseFormMessageReturn } from './types';\n\nexport { useFormMessage } from './useFormMessage';\n","import { computed, isRef, onScopeDispose, toValue, useId, watchEffect } from 'vue';\n\nimport {\n BUILT_IN_VALIDITY_MESSAGES,\n DEFAULT_INVALID_MESSAGE,\n getMessageMatch,\n} from '@agile-plus/primitives/form';\n\nimport type { CustomMatcher, ValidityMatcher } from '@agile-plus/primitives/form';\n\nimport type { Ref } from 'vue';\n\nimport type { UseFormReturn } from '../useForm';\n\nimport type { UseFormFieldReturn } from '../useFormField';\n\nimport type { UseFormMessageOptions, UseFormMessageReturn } from './types';\n\n/**\n * 取出 match 的当前值。\n *\n * 这里不能用 `toValue`:函数本身就是这条 API 的合法取值(自定义匹配器),而 `toValue`\n * 会把函数当成 getter 调用一次,于是匹配器在毫无参数的情况下被执行。因此只接受「值」\n * 或「Ref」两种形态。\n */\nfunction resolveMatch(\n match:\n ValidityMatcher | CustomMatcher | Ref<ValidityMatcher | CustomMatcher | undefined> | undefined\n): ValidityMatcher | CustomMatcher | undefined {\n if (match === undefined) return undefined;\n\n return isRef(match) ? match.value : match;\n}\n\n/**\n * 一条错误文案的命中判定与登记。\n *\n * 两个登记动作决定了这条文案能被控件「看见」:\n *\n * 1. **id 进字段的说明列表**:控件的 `aria-describedby` 只包含当前正在显示的文案,\n * 没显示的不能占位——否则读屏会念出一串空引用。\n * 2. **函数式 match 进字段的匹配器清单**:匹配结果由控件在校验那一刻统一跑,\n * 文案自己不去读控件的值。这样才能拿到跨字段比较所需的同一份 `FormData`,\n * 也保证「原生错误优先」这条规则只实现一遍。\n *\n * 登记随实例的销毁自动撤销,所以文案可以随 `v-if` 反复出现消失,注册表不会积累脏条目。\n *\n * @param options 命中条件、forceMatch 与字段名。\n * @param form 所在表单的注册表。\n * @param field 所在字段的实例,可省略。\n * @returns id、是否显示与缺省文案。\n * @throws 既不在字段内、又没有显式 name 时抛出。\n */\nexport function useFormMessage(\n options: UseFormMessageOptions,\n form: UseFormReturn,\n field?: UseFormFieldReturn\n): UseFormMessageReturn {\n const name = toValue(options.name) ?? field?.name.value;\n\n if (!name) {\n throw new Error('useFormMessage 需要 name:请把它放进 useFormField,或显式提供 name。');\n }\n\n const uid = useId();\n\n const id = `message-${uid}`;\n\n const match = computed(() => resolveMatch(options.match));\n\n const matches = computed(() =>\n getMessageMatch({\n match: match.value as ValidityMatcher | CustomMatcher | undefined,\n validity: form.getFieldValidity(name),\n customErrors: form.getFieldCustomErrors(name),\n messageId: id,\n forceMatch: toValue(options.forceMatch) === true,\n })\n );\n\n // 只有正在显示的文案才进 aria-describedby;隐藏中的 forceMatch 文案同样算显示。\n watchEffect((onCleanup) => {\n if (!matches.value) return;\n\n form.addFieldMessageId(name, id);\n onCleanup(() => form.removeFieldMessageId(name, id));\n });\n\n // 函数式 match 注册成匹配器。异步与否在这里不区分:区分发生在控件校验的那一刻,\n // 因为只有它拿得到当前值与 FormData。\n watchEffect((onCleanup) => {\n const current = match.value;\n\n if (typeof current !== 'function') return;\n\n form.addFieldMatcher(name, { id, match: current });\n onCleanup(() => form.removeFieldMatcher(name, id));\n });\n\n function getDefaultText(): string {\n const current = match.value;\n\n // 函数式与未提供 match 都没有可推导的原文,只能给通用文案;\n // 字符串 match 才有内置文案表。\n if (typeof current !== 'string') return DEFAULT_INVALID_MESSAGE;\n\n return BUILT_IN_VALIDITY_MESSAGES[current] ?? DEFAULT_INVALID_MESSAGE;\n }\n\n onScopeDispose(() => {\n form.removeFieldMessageId(name, id);\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O,uCAA2B,OAAO,KAAK;AAAA,EAChD;AAEA,iCAAe,MAAM;AACnB,SAAK,qBAAqB,MAAM,EAAE;AAClC,SAAK,mBAAmB,MAAM,EAAE;AAAA,EAClC,CAAC;AAED,SAAO,EAAE,IAAI,UAAM,qBAAS,MAAM,IAAI,GAAG,SAAS,eAAe;AACnE;","names":[]}
@@ -0,0 +1,80 @@
1
+ import { Ref, MaybeRefOrGetter, ComputedRef } from 'vue';
2
+ import { ValidityMatcher, CustomMatcher } from '@agile-plus/primitives/form';
3
+ import { c as UseFormReturn } from '../types-DM4s_xZa.cjs';
4
+ import { a as UseFormFieldReturn } from '../types-2ROaIfZ9.cjs';
5
+
6
+ /**
7
+ * useFormMessage 的配置项。
8
+ */
9
+ interface UseFormMessageOptions {
10
+ /**
11
+ * 命中的条件。
12
+ *
13
+ * - 不传:只要渲染就显示,用于服务端错误等与原生状态无关的文案。
14
+ * - 字符串:命中对应的内置校验状态,例如 `valueMissing`、`typeMismatch`。
15
+ * - 函数:自定义匹配,收到当前值与整份 `FormData`,可返回 Promise。
16
+ *
17
+ * 只接受「值」或「Ref」,不接受 getter:函数本身就是这里的合法取值,`toValue` 会把它
18
+ * 当成 getter 先调用一次,等于在没有任何参数的情况下跑了一遍匹配器。
19
+ *
20
+ * @default undefined
21
+ */
22
+ match?: ValidityMatcher | CustomMatcher | Ref<ValidityMatcher | CustomMatcher | undefined>;
23
+ /**
24
+ * 无条件命中。
25
+ *
26
+ * 存在的理由只有一个:文案需要常驻 DOM 才能做进出场动画。平时不要用,否则错误文案
27
+ * 会一直显示。
28
+ *
29
+ * @default false
30
+ */
31
+ forceMatch?: MaybeRefOrGetter<boolean | undefined>;
32
+ /**
33
+ * 归属的字段名。
34
+ *
35
+ * 在 FormField 内可省略;脱离 Field 时必须给,这是把文案挂到正确字段上的唯一依据。
36
+ *
37
+ * @default undefined
38
+ */
39
+ name?: MaybeRefOrGetter<string | undefined>;
40
+ }
41
+ /**
42
+ * useFormMessage 的返回值。
43
+ */
44
+ interface UseFormMessageReturn {
45
+ /** 文案元素 id,同时是自定义匹配器在错误表里的键 */
46
+ id: string;
47
+ /** 归属的字段名 */
48
+ name: ComputedRef<string>;
49
+ /** 当前是否应当显示 */
50
+ matches: ComputedRef<boolean>;
51
+ /**
52
+ * 缺省文案:内置状态对应的英文原文,没有对应项时回落到通用文案。
53
+ *
54
+ * @returns 文案字符串。
55
+ */
56
+ getDefaultText(): string;
57
+ }
58
+
59
+ /**
60
+ * 一条错误文案的命中判定与登记。
61
+ *
62
+ * 两个登记动作决定了这条文案能被控件「看见」:
63
+ *
64
+ * 1. **id 进字段的说明列表**:控件的 `aria-describedby` 只包含当前正在显示的文案,
65
+ * 没显示的不能占位——否则读屏会念出一串空引用。
66
+ * 2. **函数式 match 进字段的匹配器清单**:匹配结果由控件在校验那一刻统一跑,
67
+ * 文案自己不去读控件的值。这样才能拿到跨字段比较所需的同一份 `FormData`,
68
+ * 也保证「原生错误优先」这条规则只实现一遍。
69
+ *
70
+ * 登记随实例的销毁自动撤销,所以文案可以随 `v-if` 反复出现消失,注册表不会积累脏条目。
71
+ *
72
+ * @param options 命中条件、forceMatch 与字段名。
73
+ * @param form 所在表单的注册表。
74
+ * @param field 所在字段的实例,可省略。
75
+ * @returns id、是否显示与缺省文案。
76
+ * @throws 既不在字段内、又没有显式 name 时抛出。
77
+ */
78
+ declare function useFormMessage(options: UseFormMessageOptions, form: UseFormReturn, field?: UseFormFieldReturn): UseFormMessageReturn;
79
+
80
+ export { type UseFormMessageOptions, type UseFormMessageReturn, useFormMessage };
@@ -0,0 +1,80 @@
1
+ import { Ref, MaybeRefOrGetter, ComputedRef } from 'vue';
2
+ import { ValidityMatcher, CustomMatcher } from '@agile-plus/primitives/form';
3
+ import { c as UseFormReturn } from '../types-DM4s_xZa.js';
4
+ import { a as UseFormFieldReturn } from '../types-2ROaIfZ9.js';
5
+
6
+ /**
7
+ * useFormMessage 的配置项。
8
+ */
9
+ interface UseFormMessageOptions {
10
+ /**
11
+ * 命中的条件。
12
+ *
13
+ * - 不传:只要渲染就显示,用于服务端错误等与原生状态无关的文案。
14
+ * - 字符串:命中对应的内置校验状态,例如 `valueMissing`、`typeMismatch`。
15
+ * - 函数:自定义匹配,收到当前值与整份 `FormData`,可返回 Promise。
16
+ *
17
+ * 只接受「值」或「Ref」,不接受 getter:函数本身就是这里的合法取值,`toValue` 会把它
18
+ * 当成 getter 先调用一次,等于在没有任何参数的情况下跑了一遍匹配器。
19
+ *
20
+ * @default undefined
21
+ */
22
+ match?: ValidityMatcher | CustomMatcher | Ref<ValidityMatcher | CustomMatcher | undefined>;
23
+ /**
24
+ * 无条件命中。
25
+ *
26
+ * 存在的理由只有一个:文案需要常驻 DOM 才能做进出场动画。平时不要用,否则错误文案
27
+ * 会一直显示。
28
+ *
29
+ * @default false
30
+ */
31
+ forceMatch?: MaybeRefOrGetter<boolean | undefined>;
32
+ /**
33
+ * 归属的字段名。
34
+ *
35
+ * 在 FormField 内可省略;脱离 Field 时必须给,这是把文案挂到正确字段上的唯一依据。
36
+ *
37
+ * @default undefined
38
+ */
39
+ name?: MaybeRefOrGetter<string | undefined>;
40
+ }
41
+ /**
42
+ * useFormMessage 的返回值。
43
+ */
44
+ interface UseFormMessageReturn {
45
+ /** 文案元素 id,同时是自定义匹配器在错误表里的键 */
46
+ id: string;
47
+ /** 归属的字段名 */
48
+ name: ComputedRef<string>;
49
+ /** 当前是否应当显示 */
50
+ matches: ComputedRef<boolean>;
51
+ /**
52
+ * 缺省文案:内置状态对应的英文原文,没有对应项时回落到通用文案。
53
+ *
54
+ * @returns 文案字符串。
55
+ */
56
+ getDefaultText(): string;
57
+ }
58
+
59
+ /**
60
+ * 一条错误文案的命中判定与登记。
61
+ *
62
+ * 两个登记动作决定了这条文案能被控件「看见」:
63
+ *
64
+ * 1. **id 进字段的说明列表**:控件的 `aria-describedby` 只包含当前正在显示的文案,
65
+ * 没显示的不能占位——否则读屏会念出一串空引用。
66
+ * 2. **函数式 match 进字段的匹配器清单**:匹配结果由控件在校验那一刻统一跑,
67
+ * 文案自己不去读控件的值。这样才能拿到跨字段比较所需的同一份 `FormData`,
68
+ * 也保证「原生错误优先」这条规则只实现一遍。
69
+ *
70
+ * 登记随实例的销毁自动撤销,所以文案可以随 `v-if` 反复出现消失,注册表不会积累脏条目。
71
+ *
72
+ * @param options 命中条件、forceMatch 与字段名。
73
+ * @param form 所在表单的注册表。
74
+ * @param field 所在字段的实例,可省略。
75
+ * @returns id、是否显示与缺省文案。
76
+ * @throws 既不在字段内、又没有显式 name 时抛出。
77
+ */
78
+ declare function useFormMessage(options: UseFormMessageOptions, form: UseFormReturn, field?: UseFormFieldReturn): UseFormMessageReturn;
79
+
80
+ export { type UseFormMessageOptions, type UseFormMessageReturn, useFormMessage };
@@ -0,0 +1,54 @@
1
+ // src/useFormMessage/useFormMessage.ts
2
+ import { computed, isRef, onScopeDispose, toValue, useId, watchEffect } from "vue";
3
+ import {
4
+ BUILT_IN_VALIDITY_MESSAGES,
5
+ DEFAULT_INVALID_MESSAGE,
6
+ getMessageMatch
7
+ } from "@agile-plus/primitives/form";
8
+ function resolveMatch(match) {
9
+ if (match === void 0) return void 0;
10
+ return isRef(match) ? match.value : match;
11
+ }
12
+ function useFormMessage(options, form, field) {
13
+ const name = toValue(options.name) ?? field?.name.value;
14
+ if (!name) {
15
+ throw new Error("useFormMessage \u9700\u8981 name\uFF1A\u8BF7\u628A\u5B83\u653E\u8FDB useFormField\uFF0C\u6216\u663E\u5F0F\u63D0\u4F9B name\u3002");
16
+ }
17
+ const uid = useId();
18
+ const id = `message-${uid}`;
19
+ const match = computed(() => resolveMatch(options.match));
20
+ const matches = computed(
21
+ () => getMessageMatch({
22
+ match: match.value,
23
+ validity: form.getFieldValidity(name),
24
+ customErrors: form.getFieldCustomErrors(name),
25
+ messageId: id,
26
+ forceMatch: toValue(options.forceMatch) === true
27
+ })
28
+ );
29
+ watchEffect((onCleanup) => {
30
+ if (!matches.value) return;
31
+ form.addFieldMessageId(name, id);
32
+ onCleanup(() => form.removeFieldMessageId(name, id));
33
+ });
34
+ watchEffect((onCleanup) => {
35
+ const current = match.value;
36
+ if (typeof current !== "function") return;
37
+ form.addFieldMatcher(name, { id, match: current });
38
+ onCleanup(() => form.removeFieldMatcher(name, id));
39
+ });
40
+ function getDefaultText() {
41
+ const current = match.value;
42
+ if (typeof current !== "string") return DEFAULT_INVALID_MESSAGE;
43
+ return BUILT_IN_VALIDITY_MESSAGES[current] ?? DEFAULT_INVALID_MESSAGE;
44
+ }
45
+ onScopeDispose(() => {
46
+ form.removeFieldMessageId(name, id);
47
+ form.removeFieldMatcher(name, id);
48
+ });
49
+ return { id, name: computed(() => name), matches, getDefaultText };
50
+ }
51
+ export {
52
+ useFormMessage
53
+ };
54
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/useFormMessage/useFormMessage.ts"],"sourcesContent":["import { computed, isRef, onScopeDispose, toValue, useId, watchEffect } from 'vue';\n\nimport {\n BUILT_IN_VALIDITY_MESSAGES,\n DEFAULT_INVALID_MESSAGE,\n getMessageMatch,\n} from '@agile-plus/primitives/form';\n\nimport type { CustomMatcher, ValidityMatcher } from '@agile-plus/primitives/form';\n\nimport type { Ref } from 'vue';\n\nimport type { UseFormReturn } from '../useForm';\n\nimport type { UseFormFieldReturn } from '../useFormField';\n\nimport type { UseFormMessageOptions, UseFormMessageReturn } from './types';\n\n/**\n * 取出 match 的当前值。\n *\n * 这里不能用 `toValue`:函数本身就是这条 API 的合法取值(自定义匹配器),而 `toValue`\n * 会把函数当成 getter 调用一次,于是匹配器在毫无参数的情况下被执行。因此只接受「值」\n * 或「Ref」两种形态。\n */\nfunction resolveMatch(\n match:\n ValidityMatcher | CustomMatcher | Ref<ValidityMatcher | CustomMatcher | undefined> | undefined\n): ValidityMatcher | CustomMatcher | undefined {\n if (match === undefined) return undefined;\n\n return isRef(match) ? match.value : match;\n}\n\n/**\n * 一条错误文案的命中判定与登记。\n *\n * 两个登记动作决定了这条文案能被控件「看见」:\n *\n * 1. **id 进字段的说明列表**:控件的 `aria-describedby` 只包含当前正在显示的文案,\n * 没显示的不能占位——否则读屏会念出一串空引用。\n * 2. **函数式 match 进字段的匹配器清单**:匹配结果由控件在校验那一刻统一跑,\n * 文案自己不去读控件的值。这样才能拿到跨字段比较所需的同一份 `FormData`,\n * 也保证「原生错误优先」这条规则只实现一遍。\n *\n * 登记随实例的销毁自动撤销,所以文案可以随 `v-if` 反复出现消失,注册表不会积累脏条目。\n *\n * @param options 命中条件、forceMatch 与字段名。\n * @param form 所在表单的注册表。\n * @param field 所在字段的实例,可省略。\n * @returns id、是否显示与缺省文案。\n * @throws 既不在字段内、又没有显式 name 时抛出。\n */\nexport function useFormMessage(\n options: UseFormMessageOptions,\n form: UseFormReturn,\n field?: UseFormFieldReturn\n): UseFormMessageReturn {\n const name = toValue(options.name) ?? field?.name.value;\n\n if (!name) {\n throw new Error('useFormMessage 需要 name:请把它放进 useFormField,或显式提供 name。');\n }\n\n const uid = useId();\n\n const id = `message-${uid}`;\n\n const match = computed(() => resolveMatch(options.match));\n\n const matches = computed(() =>\n getMessageMatch({\n match: match.value as ValidityMatcher | CustomMatcher | undefined,\n validity: form.getFieldValidity(name),\n customErrors: form.getFieldCustomErrors(name),\n messageId: id,\n forceMatch: toValue(options.forceMatch) === true,\n })\n );\n\n // 只有正在显示的文案才进 aria-describedby;隐藏中的 forceMatch 文案同样算显示。\n watchEffect((onCleanup) => {\n if (!matches.value) return;\n\n form.addFieldMessageId(name, id);\n onCleanup(() => form.removeFieldMessageId(name, id));\n });\n\n // 函数式 match 注册成匹配器。异步与否在这里不区分:区分发生在控件校验的那一刻,\n // 因为只有它拿得到当前值与 FormData。\n watchEffect((onCleanup) => {\n const current = match.value;\n\n if (typeof current !== 'function') return;\n\n form.addFieldMatcher(name, { id, match: current });\n onCleanup(() => form.removeFieldMatcher(name, id));\n });\n\n function getDefaultText(): string {\n const current = match.value;\n\n // 函数式与未提供 match 都没有可推导的原文,只能给通用文案;\n // 字符串 match 才有内置文案表。\n if (typeof current !== 'string') return DEFAULT_INVALID_MESSAGE;\n\n return BUILT_IN_VALIDITY_MESSAGES[current] ?? DEFAULT_INVALID_MESSAGE;\n }\n\n onScopeDispose(() => {\n form.removeFieldMessageId(name, id);\n form.removeFieldMatcher(name, id);\n });\n\n return { id, name: computed(() => name), matches, getDefaultText };\n}\n"],"mappings":";AAAA,SAAS,UAAU,OAAO,gBAAgB,SAAS,OAAO,mBAAmB;AAE7E;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAmBP,SAAS,aACP,OAE6C;AAC7C,MAAI,UAAU,OAAW,QAAO;AAEhC,SAAO,MAAM,KAAK,IAAI,MAAM,QAAQ;AACtC;AAqBO,SAAS,eACd,SACA,MACA,OACsB;AACtB,QAAM,OAAO,QAAQ,QAAQ,IAAI,KAAK,OAAO,KAAK;AAElD,MAAI,CAAC,MAAM;AACT,UAAM,IAAI,MAAM,kIAAuD;AAAA,EACzE;AAEA,QAAM,MAAM,MAAM;AAElB,QAAM,KAAK,WAAW,GAAG;AAEzB,QAAM,QAAQ,SAAS,MAAM,aAAa,QAAQ,KAAK,CAAC;AAExD,QAAM,UAAU;AAAA,IAAS,MACvB,gBAAgB;AAAA,MACd,OAAO,MAAM;AAAA,MACb,UAAU,KAAK,iBAAiB,IAAI;AAAA,MACpC,cAAc,KAAK,qBAAqB,IAAI;AAAA,MAC5C,WAAW;AAAA,MACX,YAAY,QAAQ,QAAQ,UAAU,MAAM;AAAA,IAC9C,CAAC;AAAA,EACH;AAGA,cAAY,CAAC,cAAc;AACzB,QAAI,CAAC,QAAQ,MAAO;AAEpB,SAAK,kBAAkB,MAAM,EAAE;AAC/B,cAAU,MAAM,KAAK,qBAAqB,MAAM,EAAE,CAAC;AAAA,EACrD,CAAC;AAID,cAAY,CAAC,cAAc;AACzB,UAAM,UAAU,MAAM;AAEtB,QAAI,OAAO,YAAY,WAAY;AAEnC,SAAK,gBAAgB,MAAM,EAAE,IAAI,OAAO,QAAQ,CAAC;AACjD,cAAU,MAAM,KAAK,mBAAmB,MAAM,EAAE,CAAC;AAAA,EACnD,CAAC;AAED,WAAS,iBAAyB;AAChC,UAAM,UAAU,MAAM;AAItB,QAAI,OAAO,YAAY,SAAU,QAAO;AAExC,WAAO,2BAA2B,OAAO,KAAK;AAAA,EAChD;AAEA,iBAAe,MAAM;AACnB,SAAK,qBAAqB,MAAM,EAAE;AAClC,SAAK,mBAAmB,MAAM,EAAE;AAAA,EAClC,CAAC;AAED,SAAO,EAAE,IAAI,MAAM,SAAS,MAAM,IAAI,GAAG,SAAS,eAAe;AACnE;","names":[]}
@@ -0,0 +1,117 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/usePresence/index.ts
21
+ var usePresence_exports = {};
22
+ __export(usePresence_exports, {
23
+ usePresence: () => usePresence
24
+ });
25
+ module.exports = __toCommonJS(usePresence_exports);
26
+
27
+ // src/usePresence/usePresence.ts
28
+ var import_vue = require("vue");
29
+ var import_presence = require("@agile-plus/primitives/presence");
30
+ function usePresence(options) {
31
+ const state = (0, import_vue.ref)((0, import_vue.toValue)(options.present) ? "mounted" : "unmounted");
32
+ const element = (0, import_vue.ref)(null);
33
+ const isPresent = (0, import_vue.computed)(() => (0, import_presence.isPresencePresent)(state.value));
34
+ let previousAnimationName = "none";
35
+ let fillModeTimer = 0;
36
+ function readStyles() {
37
+ if (!element.value) return null;
38
+ return element.value.ownerDocument.defaultView?.getComputedStyle(element.value) ?? null;
39
+ }
40
+ function readAnimationName() {
41
+ return readStyles()?.animationName || "none";
42
+ }
43
+ function send(event) {
44
+ const next = (0, import_presence.sendPresenceEvent)(state.value, event);
45
+ if (next === state.value) return;
46
+ state.value = next;
47
+ previousAnimationName = next === "mounted" ? readAnimationName() : "none";
48
+ }
49
+ function handleAnimationStart(event) {
50
+ if (event.target !== element.value) return;
51
+ previousAnimationName = readAnimationName();
52
+ }
53
+ function handleAnimationFinished(event) {
54
+ const node = element.value;
55
+ if (!node || event.target !== node) return;
56
+ const currentAnimationName = readAnimationName();
57
+ if (!currentAnimationName.includes(event.animationName)) return;
58
+ send("ANIMATION_END");
59
+ if ((0, import_vue.toValue)(options.present)) return;
60
+ const previousFillMode = node.style.animationFillMode;
61
+ node.style.animationFillMode = "forwards";
62
+ fillModeTimer = window.setTimeout(() => {
63
+ if (node.style.animationFillMode === "forwards") {
64
+ node.style.animationFillMode = previousFillMode;
65
+ }
66
+ });
67
+ }
68
+ function setElement(node) {
69
+ element.value = node instanceof HTMLElement ? node : null;
70
+ }
71
+ (0, import_vue.watch)(element, (node, previousNode) => {
72
+ window.clearTimeout(fillModeTimer);
73
+ previousNode?.removeEventListener("animationstart", handleAnimationStart);
74
+ previousNode?.removeEventListener("animationcancel", handleAnimationFinished);
75
+ previousNode?.removeEventListener("animationend", handleAnimationFinished);
76
+ if (!node) {
77
+ send("ANIMATION_END");
78
+ return;
79
+ }
80
+ if (state.value === "mounted") previousAnimationName = readAnimationName();
81
+ node.addEventListener("animationstart", handleAnimationStart);
82
+ node.addEventListener("animationcancel", handleAnimationFinished);
83
+ node.addEventListener("animationend", handleAnimationFinished);
84
+ });
85
+ (0, import_vue.watch)(
86
+ () => (0, import_vue.toValue)(options.present),
87
+ (present, wasPresent) => {
88
+ if (present) {
89
+ send("MOUNT");
90
+ return;
91
+ }
92
+ const currentAnimationName = readAnimationName();
93
+ const hasExitAnimation = currentAnimationName !== "none" && readStyles()?.display !== "none";
94
+ const isAnimating = previousAnimationName !== currentAnimationName;
95
+ if (!wasPresent || !hasExitAnimation || !isAnimating) {
96
+ send("UNMOUNT");
97
+ return;
98
+ }
99
+ send("ANIMATION_OUT");
100
+ },
101
+ { flush: "post", immediate: true }
102
+ );
103
+ (0, import_vue.onScopeDispose)(() => {
104
+ window.clearTimeout(fillModeTimer);
105
+ const node = element.value;
106
+ if (!node) return;
107
+ node.removeEventListener("animationstart", handleAnimationStart);
108
+ node.removeEventListener("animationcancel", handleAnimationFinished);
109
+ node.removeEventListener("animationend", handleAnimationFinished);
110
+ });
111
+ return { isPresent, element, setElement };
112
+ }
113
+ // Annotate the CommonJS export names for ESM import in node:
114
+ 0 && (module.exports = {
115
+ usePresence
116
+ });
117
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/usePresence/index.ts","../../src/usePresence/usePresence.ts"],"sourcesContent":["export { usePresence } from './usePresence';\n\nexport type { UsePresenceOptions, UsePresenceReturn } from './types';\n","import { computed, onScopeDispose, ref, toValue, watch } from 'vue';\n\nimport { isPresencePresent, sendPresenceEvent } from '@agile-plus/primitives/presence';\n\nimport type { PresenceEvent, PresenceState } from '@agile-plus/primitives/presence';\n\nimport type { UsePresenceOptions, UsePresenceReturn } from './types';\n\n/**\n * Presence:把「应当存在」翻译成「是否留在 DOM 中」。\n *\n * 状态、事件与转移表都在 primitives/presence,本 composable 只负责两件事:判断\n * 「此刻有没有退出动画在跑」,以及把动画事件桥接回状态机。\n *\n * 判定依赖 `animationstart` / `animationend` / `animationcancel` 与 `animation-name`\n * 的变化,这一点与 Radix 一致。**因此本库组件的进出场必须写成 @keyframes 动画**:\n * transition 被打断时没有对应事件,用它做退出动画会把元素永久留在 DOM 里。\n *\n * @param options Presence 配置。\n * @returns 渲染开关与元素绑定入口。\n */\nexport function usePresence(options: UsePresenceOptions): UsePresenceReturn {\n const state = ref<PresenceState>(toValue(options.present) ? 'mounted' : 'unmounted');\n const element = ref<HTMLElement | null>(null);\n const isPresent = computed(() => isPresencePresent(state.value));\n\n let previousAnimationName = 'none';\n\n let fillModeTimer = 0;\n\n /**\n * 即时读取计算样式。\n *\n * Radix 缓存 getComputedStyle 的返回对象,但那是为了绕开 React 兄弟副作用污染\n * body 样式后强制重算的问题;这里只在 present 变化与动画结束时读取,两个时机都\n * 是低频,直接读取更可靠:缓存对象在 jsdom 下不会随内联样式更新,会让退出动画的\n * 判定无法在单元测试里覆盖。\n */\n function readStyles(): CSSStyleDeclaration | null {\n if (!element.value) return null;\n\n return element.value.ownerDocument.defaultView?.getComputedStyle(element.value) ?? null;\n }\n\n function readAnimationName(): string {\n return readStyles()?.animationName || 'none';\n }\n\n /**\n * 推进状态机并同步动画名基线。\n *\n * 进入 mounted 时把基线设为当前动画名,退出时才能比对出差异;其它状态重置为\n * 'none',与 Radix 在 passive effect 里的处理一致。\n */\n function send(event: PresenceEvent): void {\n const next = sendPresenceEvent(state.value, event);\n\n if (next === state.value) return;\n\n state.value = next;\n previousAnimationName = next === 'mounted' ? readAnimationName() : 'none';\n }\n\n function handleAnimationStart(event: AnimationEvent): void {\n if (event.target !== element.value) return;\n\n previousAnimationName = readAnimationName();\n }\n\n function handleAnimationFinished(event: AnimationEvent): void {\n const node = element.value;\n\n if (!node || event.target !== node) return;\n\n const currentAnimationName = readAnimationName();\n\n // 只认当前生效的动画:进入动画被打断同样派发 animationcancel,\n // 那时不能把挂起中的退出当成结束。\n if (!currentAnimationName.includes(event.animationName)) return;\n\n send('ANIMATION_END');\n\n if (toValue(options.present)) return;\n\n /**\n * 动画结束到节点移除之间仍有一帧可见内容,会闪一下。\n *\n * 把 fill-mode 强制成 forwards 让节点保持最后一个关键帧的样式,之后再还原\n * (有些消费者会保留元素)。时机必须是 setTimeout 而不是 rAF——rAF 仍在同一帧\n * 内,闪屏照旧,这是 Radix 注释里明确写过的坑。\n */\n const previousFillMode = node.style.animationFillMode;\n\n node.style.animationFillMode = 'forwards';\n\n fillModeTimer = window.setTimeout(() => {\n if (node.style.animationFillMode === 'forwards') {\n node.style.animationFillMode = previousFillMode;\n }\n });\n }\n\n function setElement(node: unknown): void {\n element.value = node instanceof HTMLElement ? node : null;\n }\n\n watch(element, (node, previousNode) => {\n window.clearTimeout(fillModeTimer);\n\n previousNode?.removeEventListener('animationstart', handleAnimationStart);\n previousNode?.removeEventListener('animationcancel', handleAnimationFinished);\n previousNode?.removeEventListener('animationend', handleAnimationFinished);\n\n if (!node) {\n // 节点被外部提前移除(父级 v-if 变化等),不会有动画事件回来,\n // 直接按结束处理,避免卡在 unmountSuspended。\n send('ANIMATION_END');\n\n return;\n }\n\n if (state.value === 'mounted') previousAnimationName = readAnimationName();\n\n node.addEventListener('animationstart', handleAnimationStart);\n node.addEventListener('animationcancel', handleAnimationFinished);\n node.addEventListener('animationend', handleAnimationFinished);\n });\n\n // flush: 'post' 让回调在 DOM 补丁之后执行,此时 data-state 已经落到元素上,\n // 读到的 animation-name 才反映退出动画是否真的开始。\n watch(\n () => toValue(options.present),\n (present, wasPresent) => {\n if (present) {\n send('MOUNT');\n\n return;\n }\n\n const currentAnimationName = readAnimationName();\n const hasExitAnimation = currentAnimationName !== 'none' && readStyles()?.display !== 'none';\n const isAnimating = previousAnimationName !== currentAnimationName;\n\n if (!wasPresent || !hasExitAnimation || !isAnimating) {\n send('UNMOUNT');\n\n return;\n }\n\n send('ANIMATION_OUT');\n },\n { flush: 'post', immediate: true }\n );\n\n onScopeDispose(() => {\n window.clearTimeout(fillModeTimer);\n\n const node = element.value;\n\n if (!node) return;\n\n node.removeEventListener('animationstart', handleAnimationStart);\n node.removeEventListener('animationcancel', handleAnimationFinished);\n node.removeEventListener('animationend', handleAnimationFinished);\n });\n\n return { isPresent, element, setElement };\n}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efOrGetter, ComputedRef, Ref } from 'vue';
2
+
3
+ /**
4
+ * usePresence 的配置项。
5
+ */
6
+ interface UsePresenceOptions {
7
+ /**
8
+ * 元素是否应当存在于 DOM 中。
9
+ *
10
+ * 用 MaybeRefOrGetter 而不是裸 boolean:组件封装层传入的是响应式 props,裸值会在
11
+ * 展开时固化成快照,外部再次打开时 Presence 收不到变化、元素永远不会重新挂载。
12
+ */
13
+ present: MaybeRefOrGetter<boolean>;
14
+ }
15
+ /**
16
+ * usePresence 的返回值。
17
+ *
18
+ * 契约与库内其它 composable 一致:这里不返回可 v-bind 的 ARIA 属性,因为 Presence
19
+ * 不产生任何属性,只决定渲染与否。
20
+ */
21
+ interface UsePresenceReturn {
22
+ /** 是否应当渲染元素;退出动画期间仍为 true */
23
+ isPresent: ComputedRef<boolean>;
24
+ /** 当前被观测的元素,未挂载时为 null */
25
+ element: Ref<HTMLElement | null>;
26
+ /**
27
+ * 绑定元素的入口,传给模板的 `:ref`。
28
+ *
29
+ * 用函数而不是直接把 Ref 交给模板:模板上的 `:ref` 需要同时能写内部状态和
30
+ * 处理卸载时的置空,函数入口是 Vue 明确支持的写法。
31
+ *
32
+ * @param node 元素实例,卸载时为 null。
33
+ */
34
+ setElement(node: unknown): void;
35
+ }
36
+
37
+ /**
38
+ * Presence:把「应当存在」翻译成「是否留在 DOM 中」。
39
+ *
40
+ * 状态、事件与转移表都在 primitives/presence,本 composable 只负责两件事:判断
41
+ * 「此刻有没有退出动画在跑」,以及把动画事件桥接回状态机。
42
+ *
43
+ * 判定依赖 `animationstart` / `animationend` / `animationcancel` 与 `animation-name`
44
+ * 的变化,这一点与 Radix 一致。**因此本库组件的进出场必须写成 @keyframes 动画**:
45
+ * transition 被打断时没有对应事件,用它做退出动画会把元素永久留在 DOM 里。
46
+ *
47
+ * @param options Presence 配置。
48
+ * @returns 渲染开关与元素绑定入口。
49
+ */
50
+ declare function usePresence(options: UsePresenceOptions): UsePresenceReturn;
51
+
52
+ export { type UsePresenceOptions, type UsePresenceReturn, usePresence };
@@ -0,0 +1,52 @@
1
+ import { MaybeRefOrGetter, ComputedRef, Ref } from 'vue';
2
+
3
+ /**
4
+ * usePresence 的配置项。
5
+ */
6
+ interface UsePresenceOptions {
7
+ /**
8
+ * 元素是否应当存在于 DOM 中。
9
+ *
10
+ * 用 MaybeRefOrGetter 而不是裸 boolean:组件封装层传入的是响应式 props,裸值会在
11
+ * 展开时固化成快照,外部再次打开时 Presence 收不到变化、元素永远不会重新挂载。
12
+ */
13
+ present: MaybeRefOrGetter<boolean>;
14
+ }
15
+ /**
16
+ * usePresence 的返回值。
17
+ *
18
+ * 契约与库内其它 composable 一致:这里不返回可 v-bind 的 ARIA 属性,因为 Presence
19
+ * 不产生任何属性,只决定渲染与否。
20
+ */
21
+ interface UsePresenceReturn {
22
+ /** 是否应当渲染元素;退出动画期间仍为 true */
23
+ isPresent: ComputedRef<boolean>;
24
+ /** 当前被观测的元素,未挂载时为 null */
25
+ element: Ref<HTMLElement | null>;
26
+ /**
27
+ * 绑定元素的入口,传给模板的 `:ref`。
28
+ *
29
+ * 用函数而不是直接把 Ref 交给模板:模板上的 `:ref` 需要同时能写内部状态和
30
+ * 处理卸载时的置空,函数入口是 Vue 明确支持的写法。
31
+ *
32
+ * @param node 元素实例,卸载时为 null。
33
+ */
34
+ setElement(node: unknown): void;
35
+ }
36
+
37
+ /**
38
+ * Presence:把「应当存在」翻译成「是否留在 DOM 中」。
39
+ *
40
+ * 状态、事件与转移表都在 primitives/presence,本 composable 只负责两件事:判断
41
+ * 「此刻有没有退出动画在跑」,以及把动画事件桥接回状态机。
42
+ *
43
+ * 判定依赖 `animationstart` / `animationend` / `animationcancel` 与 `animation-name`
44
+ * 的变化,这一点与 Radix 一致。**因此本库组件的进出场必须写成 @keyframes 动画**:
45
+ * transition 被打断时没有对应事件,用它做退出动画会把元素永久留在 DOM 里。
46
+ *
47
+ * @param options Presence 配置。
48
+ * @returns 渲染开关与元素绑定入口。
49
+ */
50
+ declare function usePresence(options: UsePresenceOptions): UsePresenceReturn;
51
+
52
+ export { type UsePresenceOptions, type UsePresenceReturn, usePresence };