@geektr.cloud/acrux-ui 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 (156) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +21 -2
  3. package/package.json +70 -3
  4. package/src/components/acrux-ui/actions/ConfirmPopover.vue +107 -0
  5. package/src/components/acrux-ui/actions/FormShell.vue +96 -0
  6. package/src/components/acrux-ui/actions/RemovalButton.vue +65 -0
  7. package/src/components/acrux-ui/actions/Stepper.vue +116 -0
  8. package/src/components/acrux-ui/actions/form.ts +36 -0
  9. package/src/components/acrux-ui/actions/index.ts +70 -0
  10. package/src/components/acrux-ui/actions/stepper.ts +65 -0
  11. package/src/components/acrux-ui/actions/utils.ts +138 -0
  12. package/src/components/acrux-ui/base/CopyBtn.vue +85 -0
  13. package/src/components/acrux-ui/base/EmptyValue.vue +25 -0
  14. package/src/components/acrux-ui/base/Icon.vue +44 -0
  15. package/src/components/acrux-ui/base/IconBtn.vue +51 -0
  16. package/src/components/acrux-ui/base/Text.vue +85 -0
  17. package/src/components/acrux-ui/base/VSeparator.vue +21 -0
  18. package/src/components/acrux-ui/base/async.ts +35 -0
  19. package/src/components/acrux-ui/base/index.ts +7 -0
  20. package/src/components/acrux-ui/display/AsyncError.vue +46 -0
  21. package/src/components/acrux-ui/display/CodeBlock.vue +91 -0
  22. package/src/components/acrux-ui/display/CopyTag.vue +51 -0
  23. package/src/components/acrux-ui/display/DataView.ts +47 -0
  24. package/src/components/acrux-ui/display/DateFormatter.vue +61 -0
  25. package/src/components/acrux-ui/display/Disclosure.vue +45 -0
  26. package/src/components/acrux-ui/display/Link.vue +81 -0
  27. package/src/components/acrux-ui/display/MultiLine.vue +29 -0
  28. package/src/components/acrux-ui/display/Notice.vue +71 -0
  29. package/src/components/acrux-ui/display/Pem.vue +165 -0
  30. package/src/components/acrux-ui/display/QrBtn.vue +117 -0
  31. package/src/components/acrux-ui/display/QrModal.vue +76 -0
  32. package/src/components/acrux-ui/display/Route.vue +52 -0
  33. package/src/components/acrux-ui/display/Token.vue +106 -0
  34. package/src/components/acrux-ui/display/UUID.vue +46 -0
  35. package/src/components/acrux-ui/display/index.ts +17 -0
  36. package/src/components/acrux-ui/display/pem.ts +100 -0
  37. package/src/components/acrux-ui/display/qr.ts +20 -0
  38. package/src/components/acrux-ui/fields/EntitySelect.vue +253 -0
  39. package/src/components/acrux-ui/fields/FieldError.vue +11 -0
  40. package/src/components/acrux-ui/fields/JsonTextArea.vue +105 -0
  41. package/src/components/acrux-ui/fields/MultiLineInput.vue +145 -0
  42. package/src/components/acrux-ui/fields/Select.vue +106 -0
  43. package/src/components/acrux-ui/fields/fieldAccessibility.ts +38 -0
  44. package/src/components/acrux-ui/fields/index.ts +4 -0
  45. package/src/components/acrux-ui/page/BackButton.vue +70 -0
  46. package/src/components/acrux-ui/page/PageDetail.vue +125 -0
  47. package/src/components/acrux-ui/page/PageEntry.vue +130 -0
  48. package/src/components/acrux-ui/page/PageState.vue +108 -0
  49. package/src/components/acrux-ui/page/index.ts +3 -0
  50. package/src/components/acrux-ui/page/pageState.ts +18 -0
  51. package/src/components/ui/alert/Alert.vue +17 -0
  52. package/src/components/ui/alert/AlertAction.vue +17 -0
  53. package/src/components/ui/alert/AlertDescription.vue +22 -0
  54. package/src/components/ui/alert/AlertTitle.vue +22 -0
  55. package/src/components/ui/alert/index.ts +25 -0
  56. package/src/components/ui/badge/Badge.vue +29 -0
  57. package/src/components/ui/badge/index.ts +25 -0
  58. package/src/components/ui/button/Button.vue +31 -0
  59. package/src/components/ui/button/index.ts +39 -0
  60. package/src/components/ui/card/Card.vue +29 -0
  61. package/src/components/ui/card/CardAction.vue +17 -0
  62. package/src/components/ui/card/CardContent.vue +14 -0
  63. package/src/components/ui/card/CardDescription.vue +14 -0
  64. package/src/components/ui/card/CardFooter.vue +17 -0
  65. package/src/components/ui/card/CardHeader.vue +22 -0
  66. package/src/components/ui/card/CardTitle.vue +14 -0
  67. package/src/components/ui/card/index.ts +7 -0
  68. package/src/components/ui/collapsible/Collapsible.vue +15 -0
  69. package/src/components/ui/collapsible/CollapsibleContent.vue +12 -0
  70. package/src/components/ui/collapsible/CollapsibleTrigger.vue +12 -0
  71. package/src/components/ui/collapsible/index.ts +3 -0
  72. package/src/components/ui/combobox/Combobox.vue +15 -0
  73. package/src/components/ui/combobox/ComboboxAnchor.vue +19 -0
  74. package/src/components/ui/combobox/ComboboxEmpty.vue +26 -0
  75. package/src/components/ui/combobox/ComboboxInput.vue +40 -0
  76. package/src/components/ui/combobox/ComboboxItem.vue +29 -0
  77. package/src/components/ui/combobox/ComboboxList.vue +38 -0
  78. package/src/components/ui/combobox/ComboboxViewport.vue +28 -0
  79. package/src/components/ui/combobox/index.ts +9 -0
  80. package/src/components/ui/empty/Empty.vue +22 -0
  81. package/src/components/ui/empty/EmptyContent.vue +17 -0
  82. package/src/components/ui/empty/EmptyDescription.vue +22 -0
  83. package/src/components/ui/empty/EmptyHeader.vue +14 -0
  84. package/src/components/ui/empty/EmptyMedia.vue +22 -0
  85. package/src/components/ui/empty/EmptyTitle.vue +14 -0
  86. package/src/components/ui/empty/index.ts +26 -0
  87. package/src/components/ui/field/Field.vue +27 -0
  88. package/src/components/ui/field/FieldContent.vue +17 -0
  89. package/src/components/ui/field/FieldDescription.vue +24 -0
  90. package/src/components/ui/field/FieldError.vue +50 -0
  91. package/src/components/ui/field/FieldGroup.vue +22 -0
  92. package/src/components/ui/field/FieldLabel.vue +24 -0
  93. package/src/components/ui/field/FieldLegend.vue +24 -0
  94. package/src/components/ui/field/FieldSeparator.vue +26 -0
  95. package/src/components/ui/field/FieldSet.vue +22 -0
  96. package/src/components/ui/field/FieldTitle.vue +22 -0
  97. package/src/components/ui/field/index.ts +30 -0
  98. package/src/components/ui/input/Input.vue +33 -0
  99. package/src/components/ui/input/index.ts +1 -0
  100. package/src/components/ui/input-group/InputGroup.vue +23 -0
  101. package/src/components/ui/input-group/InputGroupAddon.vue +39 -0
  102. package/src/components/ui/input-group/InputGroupButton.vue +22 -0
  103. package/src/components/ui/input-group/InputGroupInput.vue +21 -0
  104. package/src/components/ui/input-group/InputGroupText.vue +21 -0
  105. package/src/components/ui/input-group/InputGroupTextarea.vue +21 -0
  106. package/src/components/ui/input-group/index.ts +53 -0
  107. package/src/components/ui/label/Label.vue +26 -0
  108. package/src/components/ui/label/index.ts +1 -0
  109. package/src/components/ui/popover/Popover.vue +15 -0
  110. package/src/components/ui/popover/PopoverAnchor.vue +12 -0
  111. package/src/components/ui/popover/PopoverContent.vue +38 -0
  112. package/src/components/ui/popover/PopoverDescription.vue +14 -0
  113. package/src/components/ui/popover/PopoverHeader.vue +14 -0
  114. package/src/components/ui/popover/PopoverTitle.vue +14 -0
  115. package/src/components/ui/popover/PopoverTrigger.vue +12 -0
  116. package/src/components/ui/popover/index.ts +7 -0
  117. package/src/components/ui/select/Select.vue +15 -0
  118. package/src/components/ui/select/SelectContent.vue +53 -0
  119. package/src/components/ui/select/SelectGroup.vue +17 -0
  120. package/src/components/ui/select/SelectItem.vue +40 -0
  121. package/src/components/ui/select/SelectItemText.vue +12 -0
  122. package/src/components/ui/select/SelectLabel.vue +14 -0
  123. package/src/components/ui/select/SelectScrollDownButton.vue +32 -0
  124. package/src/components/ui/select/SelectScrollUpButton.vue +32 -0
  125. package/src/components/ui/select/SelectSeparator.vue +19 -0
  126. package/src/components/ui/select/SelectTrigger.vue +36 -0
  127. package/src/components/ui/select/SelectValue.vue +12 -0
  128. package/src/components/ui/select/index.ts +11 -0
  129. package/src/components/ui/separator/Separator.vue +27 -0
  130. package/src/components/ui/separator/index.ts +1 -0
  131. package/src/components/ui/skeleton/Skeleton.vue +14 -0
  132. package/src/components/ui/skeleton/index.ts +1 -0
  133. package/src/components/ui/spinner/Spinner.vue +14 -0
  134. package/src/components/ui/spinner/index.ts +1 -0
  135. package/src/components/ui/switch/Switch.vue +47 -0
  136. package/src/components/ui/switch/index.ts +1 -0
  137. package/src/components/ui/table/Table.vue +24 -0
  138. package/src/components/ui/table/TableBody.vue +14 -0
  139. package/src/components/ui/table/TableCaption.vue +14 -0
  140. package/src/components/ui/table/TableCell.vue +17 -0
  141. package/src/components/ui/table/TableEmpty.vue +32 -0
  142. package/src/components/ui/table/TableFooter.vue +14 -0
  143. package/src/components/ui/table/TableHead.vue +29 -0
  144. package/src/components/ui/table/TableHeader.vue +14 -0
  145. package/src/components/ui/table/TableRow.vue +22 -0
  146. package/src/components/ui/table/index.ts +9 -0
  147. package/src/components/ui/tags-input/TagsInput.vue +31 -0
  148. package/src/components/ui/tags-input/TagsInputInput.vue +20 -0
  149. package/src/components/ui/tags-input/TagsInputItem.vue +28 -0
  150. package/src/components/ui/tags-input/TagsInputItemDelete.vue +23 -0
  151. package/src/components/ui/tags-input/TagsInputItemText.vue +17 -0
  152. package/src/components/ui/tags-input/index.ts +5 -0
  153. package/src/components/ui/textarea/Textarea.vue +33 -0
  154. package/src/components/ui/textarea/index.ts +1 -0
  155. package/src/index.ts +5 -0
  156. package/src/testing/renderer.ts +99 -0
@@ -0,0 +1,138 @@
1
+ import type { Component, ComputedOptions, MethodOptions } from "vue";
2
+ import { defineAsyncComponent, defineComponent, h, nextTick, onUnmounted, ref, useId } from "vue";
3
+ import { useModal, VueFinalModal } from "vue-final-modal";
4
+ import { Card, CardContent } from "../../ui/card";
5
+ import { createAsyncLoaderRetry, Text } from "../base";
6
+ import { AsyncError } from "../display";
7
+
8
+ /** 表单组件必须接收的 id(undefined 表示新建)。 */
9
+ export interface FormModelProps {
10
+ id: string | undefined;
11
+ }
12
+
13
+ /** 表单组件必须通过 close 事件交出关闭意图,模态不自行导航。 */
14
+ export type FormModelEmits = { close: [] };
15
+
16
+ export type FormModelComponent = Component<
17
+ FormModelProps,
18
+ Record<string, unknown>,
19
+ Record<string, unknown>,
20
+ ComputedOptions,
21
+ MethodOptions,
22
+ FormModelEmits
23
+ >;
24
+
25
+ /** 异步表单加载器;调用方用 `() => import("./XxxEditor.vue")` 避免静态引入。 */
26
+ export type FormModelLoader = () => Promise<{ default: FormModelComponent }>;
27
+
28
+ export interface UseFormModelOptions {
29
+ /** 可见标题;同时作为模态的 aria-labelledby 目标。 */
30
+ title?: string;
31
+ /** 无可见标题时的可访问名称,默认“表单”。 */
32
+ ariaLabel?: string;
33
+ /** 异步表单加载中的可见状态文案。 */
34
+ loadingText?: string;
35
+ /** 异步表单加载失败的标题。 */
36
+ errorText?: string;
37
+ /** 重新执行异步表单 loader 的按钮文案。 */
38
+ retryLabel?: string;
39
+ /** 模态关闭后回调;调用方可在此刷新详情等数据。 */
40
+ onClosed?: () => void;
41
+ }
42
+
43
+ export const useFormModel = (loader: FormModelLoader, options: UseFormModelOptions = {}) => {
44
+ const id = ref<string>();
45
+ const titleId = `acrux-form-title-${useId()}`;
46
+ const asyncRetry = createAsyncLoaderRetry();
47
+ const loadingText = () => options.loadingText ?? "表单加载中…";
48
+ const errorText = () => options.errorText ?? "表单加载失败";
49
+ const retryLabel = () => options.retryLabel ?? "重试";
50
+ const AsyncForm = asyncRetry.withRetryBoundary(
51
+ defineAsyncComponent({
52
+ loader: async () => (await loader()).default,
53
+ delay: 0,
54
+ loadingComponent: defineComponent({
55
+ name: "FormModelLoading",
56
+ inheritAttrs: false,
57
+ setup() {
58
+ return () => h(Text, { variant: "muted", role: "status", class: "py-4" }, loadingText());
59
+ },
60
+ }),
61
+ errorComponent: defineComponent({
62
+ name: "FormModelError",
63
+ inheritAttrs: false,
64
+ props: { error: { type: null, required: true } },
65
+ setup(props) {
66
+ return () =>
67
+ h(AsyncError, {
68
+ error: props.error,
69
+ title: errorText(),
70
+ retryLabel: retryLabel(),
71
+ onRetry: asyncRetry.retry,
72
+ });
73
+ },
74
+ }),
75
+ onError: asyncRetry.onError,
76
+ }),
77
+ );
78
+
79
+ const component = defineComponent({
80
+ setup() {
81
+ return () =>
82
+ h(
83
+ VueFinalModal,
84
+ {
85
+ class: "flex overflow-y-auto py-[10vh]",
86
+ contentClass: "m-auto flex w-full flex-col max-w-5xl px-4",
87
+ focusTrap: { returnFocusOnDeactivate: true },
88
+ escToClose: true,
89
+ zIndexFn: ({ index }) => 50 + index,
90
+ "aria-labelledby": options.title ? titleId : undefined,
91
+ "aria-label": options.title ? undefined : (options.ariaLabel ?? "表单"),
92
+ },
93
+ {
94
+ default: () =>
95
+ h(Card, null, {
96
+ default: () =>
97
+ h(
98
+ CardContent,
99
+ { class: "flex flex-col gap-3" },
100
+ {
101
+ default: () => [
102
+ options.title ? h("h2", { id: titleId, class: "text-sm font-semibold" }, options.title) : null,
103
+ h(AsyncForm, { id: id.value, onClose: handleClose }),
104
+ ],
105
+ },
106
+ ),
107
+ }),
108
+ },
109
+ );
110
+ },
111
+ });
112
+
113
+ const { open, close, destroy } = useModal({ component });
114
+
115
+ const handleClose = () => {
116
+ void close().then(() => options.onClosed?.());
117
+ };
118
+
119
+ onUnmounted(destroy);
120
+
121
+ const create = () => {
122
+ close();
123
+ nextTick(() => {
124
+ id.value = undefined;
125
+ open();
126
+ });
127
+ };
128
+
129
+ const update = (_id: string) => {
130
+ close();
131
+ nextTick(() => {
132
+ id.value = _id;
133
+ open();
134
+ });
135
+ };
136
+
137
+ return { create, update };
138
+ };
@@ -0,0 +1,85 @@
1
+ <script setup lang="ts">
2
+ import type { HTMLAttributes } from "vue";
3
+ import { nextTick, onBeforeUnmount, ref } from "vue";
4
+ import { cn } from "cn";
5
+ import { Check, Copy } from "@lucide/vue";
6
+ import IconBtn from "./IconBtn.vue";
7
+
8
+ defineOptions({ name: "CopyBtn", inheritAttrs: false });
9
+
10
+ const props = withDefaults(
11
+ defineProps<{
12
+ value: string;
13
+ label?: string;
14
+ copiedText?: string;
15
+ failedText?: string;
16
+ disabled?: boolean;
17
+ class?: HTMLAttributes["class"];
18
+ }>(),
19
+ {
20
+ label: "复制",
21
+ copiedText: "已复制",
22
+ failedText: "复制失败",
23
+ disabled: false,
24
+ class: undefined,
25
+ },
26
+ );
27
+
28
+ type CopyStatus = "idle" | "copied" | "error";
29
+
30
+ const status = ref<CopyStatus>("idle");
31
+ const message = ref("");
32
+
33
+ let timer: ReturnType<typeof setTimeout> | undefined;
34
+
35
+ const clearTimer = () => {
36
+ if (timer !== undefined) {
37
+ clearTimeout(timer);
38
+ timer = undefined;
39
+ }
40
+ };
41
+
42
+ const setStatus = (next: Exclude<CopyStatus, "idle">) => {
43
+ clearTimer();
44
+ status.value = next;
45
+ // 先清空再写入,重复点击时 live region 内容仍会变化,辅助技术可以再次播报。
46
+ message.value = "";
47
+ void nextTick(() => {
48
+ message.value = next === "error" ? props.failedText : props.copiedText;
49
+ });
50
+ timer = setTimeout(() => {
51
+ status.value = "idle";
52
+ message.value = "";
53
+ timer = undefined;
54
+ }, 2000);
55
+ };
56
+
57
+ const onCopy = async () => {
58
+ if (props.disabled || !props.value) return;
59
+ try {
60
+ await navigator.clipboard.writeText(props.value);
61
+ setStatus("copied");
62
+ } catch {
63
+ setStatus("error");
64
+ }
65
+ };
66
+
67
+ onBeforeUnmount(clearTimer);
68
+ </script>
69
+
70
+ <template>
71
+ <span data-slot="copy-btn" :class="cn('inline-flex items-center gap-1', props.class)">
72
+ <IconBtn
73
+ v-bind="$attrs"
74
+ :icon="status === 'copied' ? Check : Copy"
75
+ :aria-label="label"
76
+ :disabled="disabled || !value"
77
+ size="icon-xs"
78
+ class="shrink-0"
79
+ @click="onCopy"
80
+ />
81
+ <span v-if="message" role="status" :class="status === 'error' ? 'text-destructive text-xs' : 'sr-only'">
82
+ {{ message }}
83
+ </span>
84
+ </span>
85
+ </template>
@@ -0,0 +1,25 @@
1
+ <script setup lang="ts">
2
+ import type { HTMLAttributes } from "vue";
3
+ import { cn } from "cn";
4
+
5
+ defineOptions({ name: "EmptyValue", inheritAttrs: false });
6
+
7
+ const props = withDefaults(
8
+ defineProps<{
9
+ class?: HTMLAttributes["class"];
10
+ }>(),
11
+ {
12
+ class: undefined,
13
+ },
14
+ );
15
+ </script>
16
+
17
+ <template>
18
+ <span
19
+ v-bind="$attrs"
20
+ data-slot="empty-value"
21
+ :class="cn('select-none cursor-not-allowed text-muted-foreground', props.class)"
22
+ >
23
+ <slot>—</slot>
24
+ </span>
25
+ </template>
@@ -0,0 +1,44 @@
1
+ <!-- eslint-disable vue/multi-word-component-names -->
2
+ <script setup lang="ts">
3
+ import type { Component, HTMLAttributes } from "vue";
4
+ import { computed, useAttrs } from "vue";
5
+ import { cn } from "cn";
6
+
7
+ defineOptions({ name: "Icon" });
8
+
9
+ const props = withDefaults(
10
+ defineProps<{
11
+ icon: Component;
12
+ size?: "xs" | "sm" | "md" | "lg";
13
+ class?: HTMLAttributes["class"];
14
+ }>(),
15
+ {
16
+ size: "sm",
17
+ class: undefined,
18
+ },
19
+ );
20
+
21
+ const sizeClass: Record<"xs" | "sm" | "md" | "lg", string> = {
22
+ xs: "size-3",
23
+ sm: "size-3.5",
24
+ md: "size-4",
25
+ lg: "size-5",
26
+ };
27
+
28
+ const attrs = useAttrs();
29
+
30
+ // 默认是装饰图标;调用方提供 aria-label / aria-labelledby 时升级为 informative image。
31
+ const labelled = computed(() => Boolean(attrs["aria-label"] || attrs["aria-labelledby"]));
32
+
33
+ const iconClass = computed(() => cn("shrink-0", sizeClass[props.size], props.class));
34
+ </script>
35
+
36
+ <template>
37
+ <component
38
+ :is="icon"
39
+ data-slot="icon"
40
+ :role="labelled ? 'img' : undefined"
41
+ :aria-hidden="labelled ? undefined : 'true'"
42
+ :class="iconClass"
43
+ />
44
+ </template>
@@ -0,0 +1,51 @@
1
+ <script setup lang="ts">
2
+ import type { AsTag } from "reka-ui";
3
+ import type { Component, HTMLAttributes } from "vue";
4
+ import { computed } from "vue";
5
+ import { cn } from "cn";
6
+ import { Button, type ButtonVariants } from "../../ui/button";
7
+ import Icon from "./Icon.vue";
8
+
9
+ defineOptions({ name: "IconBtn" });
10
+
11
+ const props = withDefaults(
12
+ defineProps<{
13
+ as?: AsTag | Component;
14
+ asChild?: boolean;
15
+ icon?: Component;
16
+ variant?: ButtonVariants["variant"];
17
+ size?: ButtonVariants["size"];
18
+ disabled?: boolean;
19
+ title?: string;
20
+ class?: HTMLAttributes["class"];
21
+ }>(),
22
+ {
23
+ as: "button",
24
+ variant: "ghost",
25
+ size: "icon",
26
+ icon: undefined,
27
+ title: undefined,
28
+ class: undefined,
29
+ },
30
+ );
31
+
32
+ const isNativeButton = computed(() => !props.asChild && props.as === "button");
33
+ </script>
34
+
35
+ <template>
36
+ <Button
37
+ :as="as"
38
+ :as-child="asChild"
39
+ :variant="variant"
40
+ :size="size"
41
+ :disabled="disabled"
42
+ :title="title"
43
+ :type="isNativeButton ? 'button' : undefined"
44
+ data-slot="icon-btn"
45
+ :class="cn('cursor-pointer', props.class)"
46
+ >
47
+ <slot>
48
+ <Icon v-if="icon" :icon="icon" />
49
+ </slot>
50
+ </Button>
51
+ </template>
@@ -0,0 +1,85 @@
1
+ <!-- eslint-disable vue/multi-word-component-names, vue/no-reserved-component-names -->
2
+ <script setup lang="ts">
3
+ import type { HTMLAttributes } from "vue";
4
+ import { cn } from "cn";
5
+
6
+ defineOptions({ name: "Text", inheritAttrs: false });
7
+
8
+ type TextVariant =
9
+ | "h1"
10
+ | "h2"
11
+ | "h3"
12
+ | "h4"
13
+ | "section"
14
+ | "subsection"
15
+ | "body"
16
+ | "lead"
17
+ | "large"
18
+ | "small"
19
+ | "muted"
20
+ | "blockquote"
21
+ | "list"
22
+ | "inlineCode";
23
+
24
+ // eslint-disable-next-line no-undef -- DOM lib supplies this global type.
25
+ type NativeTag = keyof HTMLElementTagNameMap;
26
+
27
+ const props = withDefaults(
28
+ defineProps<{
29
+ variant?: TextVariant;
30
+ as?: NativeTag;
31
+ class?: HTMLAttributes["class"];
32
+ }>(),
33
+ {
34
+ variant: "body",
35
+ as: undefined,
36
+ class: undefined,
37
+ },
38
+ );
39
+
40
+ const variantTags: Record<TextVariant, NativeTag> = {
41
+ h1: "h1",
42
+ h2: "h2",
43
+ h3: "h3",
44
+ h4: "h4",
45
+ section: "h2",
46
+ subsection: "h3",
47
+ body: "p",
48
+ lead: "p",
49
+ large: "div",
50
+ small: "small",
51
+ muted: "p",
52
+ blockquote: "blockquote",
53
+ list: "ul",
54
+ inlineCode: "code",
55
+ };
56
+
57
+ // shadcn-vue Typography 的语义变体,移除大段 margin、h2 边框与居中,收敛到紧凑字号。
58
+ const variantClasses: Record<TextVariant, string> = {
59
+ h1: "text-2xl font-semibold tracking-tight text-balance",
60
+ h2: "text-xl font-semibold tracking-tight",
61
+ h3: "text-lg font-semibold tracking-tight",
62
+ h4: "text-base font-semibold tracking-tight",
63
+ section: "text-sm font-medium tracking-tight",
64
+ subsection: "text-xs font-medium tracking-tight",
65
+ body: "text-xs leading-relaxed",
66
+ lead: "text-sm text-muted-foreground",
67
+ large: "text-sm font-semibold",
68
+ small: "text-xs font-medium leading-none",
69
+ muted: "text-xs leading-relaxed text-muted-foreground",
70
+ blockquote: "border-l-2 pl-4 text-xs leading-relaxed italic",
71
+ list: "ml-4 list-disc text-xs leading-relaxed [&>li]:mt-1",
72
+ inlineCode: "relative rounded-none bg-muted px-1 py-0.5 font-mono text-xs font-semibold",
73
+ };
74
+ </script>
75
+
76
+ <template>
77
+ <component
78
+ :is="as ?? variantTags[variant]"
79
+ data-slot="text"
80
+ v-bind="$attrs"
81
+ :class="cn(variantClasses[variant], props.class)"
82
+ >
83
+ <slot />
84
+ </component>
85
+ </template>
@@ -0,0 +1,21 @@
1
+ <script setup lang="ts">
2
+ import type { HTMLAttributes } from "vue";
3
+ import { cn } from "cn";
4
+ import { Separator } from "../../ui/separator";
5
+
6
+ defineOptions({ name: "VSeparator", inheritAttrs: false });
7
+
8
+ const props = defineProps<{
9
+ class?: HTMLAttributes["class"];
10
+ }>();
11
+ </script>
12
+
13
+ <template>
14
+ <Separator
15
+ v-bind="$attrs"
16
+ as="span"
17
+ orientation="vertical"
18
+ data-slot="v-separator"
19
+ :class="cn('mx-1', props.class)"
20
+ />
21
+ </template>
@@ -0,0 +1,35 @@
1
+ import type { Component } from "vue";
2
+ import { defineComponent, h, ref } from "vue";
3
+
4
+ /** 为 defineAsyncComponent 提供可见错误所需的重试边界。 */
5
+ export const createAsyncLoaderRetry = () => {
6
+ const boundaryKey = ref(0);
7
+ let retryLoader: (() => void) | undefined;
8
+
9
+ const retry = () => {
10
+ const load = retryLoader;
11
+ if (!load) return;
12
+ // 防止在边界更新前重复点击;只重跑 loader,不刷新宿主页面。
13
+ retryLoader = undefined;
14
+ load();
15
+ boundaryKey.value += 1;
16
+ };
17
+
18
+ const onError = (_error: Error, load: () => void, fail: () => void) => {
19
+ retryLoader = load;
20
+ // Vue 3.5 的 errorComponent 只接收 error;fail 后由共享错误 UI 调用 retry。
21
+ fail();
22
+ };
23
+
24
+ const withRetryBoundary = <T extends Component>(component: T): T =>
25
+ // attrs 与 slots 原样透传,因此保留原组件的调用方类型。
26
+ defineComponent({
27
+ name: "AsyncLoaderRetryBoundary",
28
+ inheritAttrs: false,
29
+ setup(_, { attrs, slots }) {
30
+ return () => h(component, { ...attrs, key: boundaryKey.value }, slots);
31
+ },
32
+ }) as T;
33
+
34
+ return { retry, onError, withRetryBoundary };
35
+ };
@@ -0,0 +1,7 @@
1
+ export { createAsyncLoaderRetry } from "./async";
2
+ export { default as CopyBtn } from "./CopyBtn.vue";
3
+ export { default as EmptyValue } from "./EmptyValue.vue";
4
+ export { default as Icon } from "./Icon.vue";
5
+ export { default as IconBtn } from "./IconBtn.vue";
6
+ export { default as Text } from "./Text.vue";
7
+ export { default as VSeparator } from "./VSeparator.vue";
@@ -0,0 +1,46 @@
1
+ <script setup lang="ts">
2
+ import type { HTMLAttributes } from "vue";
3
+ import { computed } from "vue";
4
+ import { RotateCcw } from "@lucide/vue";
5
+ import { cn } from "cn";
6
+ import { Button } from "../../ui/button";
7
+ import Notice from "./Notice.vue";
8
+
9
+ defineOptions({ name: "AsyncError", inheritAttrs: false });
10
+
11
+ const props = withDefaults(
12
+ defineProps<{
13
+ error?: unknown;
14
+ title?: string;
15
+ retryLabel?: string;
16
+ class?: HTMLAttributes["class"];
17
+ }>(),
18
+ {
19
+ error: undefined,
20
+ title: "加载失败",
21
+ retryLabel: "重试",
22
+ class: undefined,
23
+ },
24
+ );
25
+
26
+ const emit = defineEmits<{
27
+ retry: [];
28
+ }>();
29
+
30
+ const errorMessage = computed(() => {
31
+ if (props.error === undefined || props.error === null) return "";
32
+ return props.error instanceof Error ? props.error.message : String(props.error);
33
+ });
34
+ </script>
35
+
36
+ <template>
37
+ <Notice v-bind="$attrs" tone="error" data-slot="async-error" :title="title" :class="cn('min-w-0', props.class)">
38
+ <template v-if="errorMessage" #default>{{ errorMessage }}</template>
39
+ <template #actions>
40
+ <Button type="button" variant="secondary" size="sm" @click="emit('retry')">
41
+ <RotateCcw aria-hidden="true" />
42
+ {{ retryLabel }}
43
+ </Button>
44
+ </template>
45
+ </Notice>
46
+ </template>
@@ -0,0 +1,91 @@
1
+ <script setup lang="ts">
2
+ import type { HTMLAttributes } from "vue";
3
+ import { computed, ref, useId } from "vue";
4
+ import { cn } from "cn";
5
+ import { ChevronDown, ChevronRight } from "@lucide/vue";
6
+ import { CopyBtn, EmptyValue, IconBtn } from "../base";
7
+
8
+ defineOptions({ name: "CodeBlock", inheritAttrs: false });
9
+
10
+ const props = withDefaults(
11
+ defineProps<{
12
+ value?: string | null;
13
+ label?: string;
14
+ copy?: boolean;
15
+ collapsible?: boolean;
16
+ defaultExpanded?: boolean;
17
+ wrap?: boolean;
18
+ maxHeight?: string;
19
+ copyLabel?: string;
20
+ expandLabel?: string;
21
+ collapseLabel?: string;
22
+ class?: HTMLAttributes["class"];
23
+ }>(),
24
+ {
25
+ value: undefined,
26
+ label: undefined,
27
+ copy: true,
28
+ collapsible: false,
29
+ defaultExpanded: false,
30
+ wrap: true,
31
+ maxHeight: "12rem",
32
+ copyLabel: undefined,
33
+ expandLabel: undefined,
34
+ collapseLabel: undefined,
35
+ class: undefined,
36
+ },
37
+ );
38
+
39
+ const contentId = useId();
40
+ const expanded = ref(props.defaultExpanded);
41
+
42
+ const open = computed(() => !props.collapsible || expanded.value);
43
+ const hasValue = computed(() => Boolean(props.value));
44
+ const overlayCopy = computed(() => !props.collapsible && !props.label && props.copy && hasValue.value);
45
+
46
+ const toggleLabel = computed(() => {
47
+ const action = expanded.value ? "收起" : "展开";
48
+ const fallback = props.label ? `${action} ${props.label}` : `${action}代码`;
49
+ return (expanded.value ? props.collapseLabel : props.expandLabel) ?? fallback;
50
+ });
51
+
52
+ const copyAccessibleLabel = computed(() => props.copyLabel ?? (props.label ? `复制 ${props.label}` : "复制代码"));
53
+ </script>
54
+
55
+ <template>
56
+ <div v-bind="$attrs" data-slot="code-block" :class="cn('relative flex min-w-0 flex-col gap-1 text-xs', props.class)">
57
+ <div v-if="!overlayCopy && (collapsible || label || (copy && hasValue))" class="flex min-w-0 items-center gap-1">
58
+ <IconBtn
59
+ v-if="collapsible"
60
+ :icon="expanded ? ChevronDown : ChevronRight"
61
+ size="icon-xs"
62
+ :aria-label="toggleLabel"
63
+ :aria-expanded="expanded"
64
+ :aria-controls="open ? contentId : undefined"
65
+ class="shrink-0"
66
+ @click="expanded = !expanded"
67
+ />
68
+ <span v-if="label" class="min-w-0 truncate text-muted-foreground">{{ label }}</span>
69
+ <CopyBtn v-if="copy && hasValue" :value="value ?? ''" :label="copyAccessibleLabel" class="ml-auto shrink-0" />
70
+ </div>
71
+ <CopyBtn
72
+ v-if="overlayCopy"
73
+ :value="value ?? ''"
74
+ :label="copyAccessibleLabel"
75
+ class="absolute top-1 right-1 z-10 bg-muted"
76
+ />
77
+ <pre
78
+ v-if="open && hasValue"
79
+ :id="contentId"
80
+ tabindex="0"
81
+ :style="{ maxHeight }"
82
+ :class="
83
+ cn(
84
+ 'overflow-auto border border-border bg-muted p-2 text-xs focus-visible:ring-2 focus-visible:ring-ring/50 focus-visible:outline-none',
85
+ overlayCopy && 'pr-10',
86
+ )
87
+ "
88
+ ><code :class="wrap ? 'whitespace-pre-wrap break-all' : 'whitespace-pre'">{{ value }}</code></pre>
89
+ <EmptyValue v-else-if="open" :id="contentId" />
90
+ </div>
91
+ </template>
@@ -0,0 +1,51 @@
1
+ <script setup lang="ts">
2
+ import type { HTMLAttributes } from "vue";
3
+ import { computed } from "vue";
4
+ import { cn } from "cn";
5
+ import { Badge, type BadgeVariants } from "../../ui/badge";
6
+ import { CopyBtn, EmptyValue, Text, VSeparator } from "../base";
7
+
8
+ defineOptions({ name: "CopyTag", inheritAttrs: false });
9
+
10
+ const props = withDefaults(
11
+ defineProps<{
12
+ label?: string;
13
+ value?: string;
14
+ variant?: BadgeVariants["variant"] | "raw";
15
+ copyLabel?: string;
16
+ copiedText?: string;
17
+ copyFailedText?: string;
18
+ class?: HTMLAttributes["class"];
19
+ }>(),
20
+ {
21
+ label: undefined,
22
+ value: undefined,
23
+ variant: "raw",
24
+ copyLabel: "复制标签",
25
+ copiedText: "已复制",
26
+ copyFailedText: "复制失败",
27
+ class: undefined,
28
+ },
29
+ );
30
+
31
+ const text = computed(() => props.label ?? props.value ?? "");
32
+ const isRaw = computed(() => props.variant === "raw");
33
+ const badgeVariant = computed(() => (props.variant === "raw" ? undefined : props.variant));
34
+ </script>
35
+
36
+ <template>
37
+ <span v-bind="$attrs" data-slot="copy-tag" :class="cn('inline-flex min-w-0 items-center gap-1', props.class)">
38
+ <Badge v-if="!isRaw" as="span" :variant="badgeVariant" class="min-w-0">
39
+ <Text v-if="text" variant="inlineCode" class="truncate">{{ text }}</Text>
40
+ <EmptyValue v-else />
41
+ </Badge>
42
+ <template v-else>
43
+ <Text v-if="text" variant="inlineCode" class="truncate">{{ text }}</Text>
44
+ <EmptyValue v-else />
45
+ </template>
46
+ <template v-if="value">
47
+ <VSeparator />
48
+ <CopyBtn :value="value" :label="copyLabel" :copied-text="copiedText" :failed-text="copyFailedText" />
49
+ </template>
50
+ </span>
51
+ </template>