feat: add Badge and Alert components

This commit is contained in:
maoyingjie
2026-10-21 21:32:40 +08:00
parent 3515a47f5e
commit 4f43789f60
41 changed files with 2422 additions and 17 deletions
+127
View File
@@ -0,0 +1,127 @@
<script lang="ts">
import {
Comment,
Fragment,
Text,
computed,
defineComponent,
h,
isVNode,
provide,
useAttrs,
type PropType,
type VNodeChild,
} from "vue";
import { badgeVariants, type BadgeVariants } from "@heroui-vue/styles";
import { badgeContextKey } from "./context";
import BadgeLabel from "./BadgeLabel.vue";
function flattenChildren(
nodes: VNodeChild[],
out: VNodeChild[] = [],
): VNodeChild[] {
for (const child of nodes) {
if (Array.isArray(child)) {
flattenChildren(child, out);
continue;
}
if (
isVNode(child) &&
child.type === Fragment &&
Array.isArray(child.children)
) {
flattenChildren(child.children as VNodeChild[], out);
continue;
}
out.push(child);
}
return out;
}
function isIgnorable(node: VNodeChild): boolean {
if (node == null || node === false || node === true) return true;
if (typeof node === "string") return node.trim() === "";
if (typeof node === "number") return false;
if (!isVNode(node)) return true;
if (node.type === Comment) return true;
if (node.type === Text && String(node.children ?? "").trim() === "")
return true;
return false;
}
/** True when the slot is only plain text/number content (auto-wrapped in BadgeLabel). */
function isPlainTextSlot(nodes: VNodeChild[]): boolean {
const meaningful = flattenChildren(nodes).filter(
(node) => !isIgnorable(node),
);
if (meaningful.length === 0) return false;
return meaningful.every((node) => {
if (typeof node === "number" || typeof node === "string") return true;
return isVNode(node) && node.type === Text;
});
}
export default defineComponent({
name: "Badge",
inheritAttrs: false,
props: {
color: {
type: String as PropType<BadgeVariants["color"]>,
default: "default",
},
placement: {
type: String as PropType<BadgeVariants["placement"]>,
default: "top-right",
},
size: {
type: String as PropType<BadgeVariants["size"]>,
default: "md",
},
variant: {
type: String as PropType<BadgeVariants["variant"]>,
default: "primary",
},
},
setup(props, { attrs, slots }) {
const variantSlots = computed(() =>
badgeVariants({
color: props.color,
placement: props.placement,
size: props.size,
variant: props.variant,
}),
);
provide(badgeContextKey, { slots: variantSlots });
/**
* Re-read the default slot on every render so text ↔ composed content
* switches and parent slot-swap updates stay in sync.
*/
function render() {
const children = (slots.default?.() ?? []) as VNodeChild[];
const content: VNodeChild[] = isPlainTextSlot(children)
? [
h(
BadgeLabel,
{ key: "badge-auto-label" },
{ default: () => children },
),
]
: children;
return h(
"span",
{
...attrs,
class: [variantSlots.value.base(), attrs.class],
"data-slot": "badge",
},
content,
);
}
return render;
},
});
</script>
@@ -0,0 +1,18 @@
<script setup lang="ts">
import { useAttrs } from "vue";
import { badgeVariants } from "@heroui-vue/styles";
defineOptions({ inheritAttrs: false });
const attrs = useAttrs();
</script>
<template>
<span
v-bind="attrs"
:class="badgeVariants().anchor()"
data-slot="badge-anchor"
>
<slot />
</span>
</template>
@@ -0,0 +1,19 @@
<script setup lang="ts">
import { inject, useAttrs } from "vue";
import { badgeContextKey } from "./context";
defineOptions({ inheritAttrs: false });
const attrs = useAttrs();
const ctx = inject(badgeContextKey, undefined);
</script>
<template>
<span
v-bind="attrs"
:class="ctx?.slots.value.label()"
data-slot="badge-label"
>
<slot />
</span>
</template>
@@ -0,0 +1,11 @@
import type { ComputedRef, InjectionKey } from "vue";
import type { badgeVariants } from "@heroui-vue/styles";
export type BadgeSlots = ReturnType<typeof badgeVariants>;
export interface BadgeContextValue {
slots: ComputedRef<BadgeSlots>;
}
export const badgeContextKey: InjectionKey<BadgeContextValue> =
Symbol("HeroUIVue.Badge");