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
@@ -0,0 +1,36 @@
<script setup lang="ts">
import { computed, provide, useAttrs } from "vue";
import {
alertVariants,
type AlertVariants,
type SurfaceVariants,
} from "@heroui-vue/styles";
import { surfaceContextKey } from "../surface/context";
import { alertContextKey, type AlertStatus } from "./context";
defineOptions({ inheritAttrs: false });
const props = withDefaults(
defineProps<{
status?: AlertVariants["status"];
}>(),
{ status: "default" },
);
const attrs = useAttrs();
const status = computed<AlertStatus>(() => props.status ?? "default");
const slots = computed(() => alertVariants({ status: status.value }));
provide(alertContextKey, { slots, status });
// Reset nested Surface context so children do not inherit the outer surface.
provide(surfaceContextKey, {
variant: computed<SurfaceVariants["variant"]>(() => "default"),
});
</script>
<template>
<div v-bind="attrs" :class="slots.base()" data-slot="alert-root">
<slot />
</div>
</template>
@@ -0,0 +1,19 @@
<script setup lang="ts">
import { inject, useAttrs } from "vue";
import { alertContextKey } from "./context";
defineOptions({ inheritAttrs: false });
const attrs = useAttrs();
const ctx = inject(alertContextKey, undefined);
</script>
<template>
<div
v-bind="attrs"
:class="ctx?.slots.value.content()"
data-slot="alert-content"
>
<slot />
</div>
</template>
@@ -0,0 +1,58 @@
<script lang="ts">
import { computed, defineComponent, h, useAttrs, type PropType } from "vue";
import type { AlertStatus } from "./context";
/* Adapted from HeroUI v3, ce90ea3 icons.tsx (Info/Success/Warning/Danger).
* Copyright 2025 NextUI Inc. See third-party/HEROUI-LICENSE. */
const iconPaths: Record<AlertStatus, string> = {
default:
"M8 13.5a5.5 5.5 0 1 0 0-11a5.5 5.5 0 0 0 0 11M8 15A7 7 0 1 0 8 1a7 7 0 0 0 0 14m1-9.5a1 1 0 1 1-2 0a1 1 0 0 1 2 0m-.25 3a.75.75 0 0 0-1.5 0V11a.75.75 0 0 0 1.5 0z",
accent:
"M8 13.5a5.5 5.5 0 1 0 0-11a5.5 5.5 0 0 0 0 11M8 15A7 7 0 1 0 8 1a7 7 0 0 0 0 14m1-9.5a1 1 0 1 1-2 0a1 1 0 0 1 2 0m-.25 3a.75.75 0 0 0-1.5 0V11a.75.75 0 0 0 1.5 0z",
success:
"M13.5 8a5.5 5.5 0 1 1-11 0a5.5 5.5 0 0 1 11 0M15 8A7 7 0 1 1 1 8a7 7 0 0 1 14 0m-3.9-1.55a.75.75 0 1 0-1.2-.9L7.419 8.858L6.03 7.47a.75.75 0 0 0-1.06 1.06l2 2a.75.75 0 0 0 1.13-.08z",
warning:
"M7.134 2.994L2.217 11.5a1 1 0 0 0 .866 1.5h9.834a1 1 0 0 0 .866-1.5L8.866 2.993a1 1 0 0 0-1.732 0m3.03-.75c-.962-1.665-3.366-1.665-4.329 0L.918 10.749c-.963 1.666.24 3.751 2.165 3.751h9.834c1.925 0 3.128-2.085 2.164-3.751zM8 5a.75.75 0 0 1 .75.75v2a.75.75 0 0 1-1.5 0v-2A.75.75 0 0 1 8 5m1 5.75a1 1 0 1 1-2 0a1 1 0 0 1 2 0",
danger:
"M8 13.5a5.5 5.5 0 1 0 0-11a5.5 5.5 0 0 0 0 11M8 15A7 7 0 1 0 8 1a7 7 0 0 0 0 14m1-4.5a1 1 0 1 1-2 0a1 1 0 0 1 2 0M8.75 5a.75.75 0 0 0-1.5 0v2.5a.75.75 0 0 0 1.5 0z",
};
export default defineComponent({
name: "AlertDefaultIcon",
inheritAttrs: false,
props: {
status: {
type: String as PropType<AlertStatus>,
default: "default",
},
},
setup(props) {
const attrs = useAttrs();
const path = computed(() => iconPaths[props.status] ?? iconPaths.default);
return () =>
h(
"svg",
{
...attrs,
"aria-hidden": "true",
"data-slot": "alert-default-icon",
fill: "none",
height: 16,
role: "presentation",
viewBox: "0 0 16 16",
width: 16,
xmlns: "http://www.w3.org/2000/svg",
},
[
h("path", {
"clip-rule": "evenodd",
d: path.value,
fill: "currentColor",
"fill-rule": "evenodd",
}),
],
);
},
});
</script>
@@ -0,0 +1,19 @@
<script setup lang="ts">
import { inject, useAttrs } from "vue";
import { alertContextKey } from "./context";
defineOptions({ inheritAttrs: false });
const attrs = useAttrs();
const ctx = inject(alertContextKey, undefined);
</script>
<template>
<span
v-bind="attrs"
:class="ctx?.slots.value.description()"
data-slot="alert-description"
>
<slot />
</span>
</template>
@@ -0,0 +1,88 @@
<script lang="ts">
import {
Comment,
Fragment,
Text,
defineComponent,
h,
inject,
isVNode,
useAttrs,
type VNodeChild,
} from "vue";
import { alertContextKey } from "./context";
import AlertDefaultIcon from "./AlertDefaultIcon.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 isMeaningful(node: VNodeChild): boolean {
if (node == null || node === false || node === true) return false;
if (typeof node === "string") return node.trim() !== "";
if (typeof node === "number") return true;
if (!isVNode(node)) return true;
if (node.type === Comment) return false;
if (node.type === Text) return String(node.children ?? "").trim() !== "";
return true;
}
export default defineComponent({
name: "AlertIndicator",
inheritAttrs: false,
setup(_, { attrs, slots }) {
const ctx = inject(alertContextKey, undefined);
/**
* Empty/conditional slot falls back to the status default icon.
* Re-read the slot every render so restore/replacement stays in sync.
*/
function render() {
const children = flattenChildren(
(slots.default?.() ?? []) as VNodeChild[],
);
const content = children.filter(isMeaningful);
const body: VNodeChild[] =
content.length > 0
? children
: [
h(AlertDefaultIcon, {
key: "alert-default-icon",
status: ctx?.status.value,
}),
];
return h(
"div",
{
...attrs,
// Merge caller class with component classes (object spread would drop it).
class: [ctx?.slots.value.indicator(), attrs.class],
"data-slot": "alert-indicator",
},
body,
);
}
return render;
},
});
</script>
@@ -0,0 +1,15 @@
<script setup lang="ts">
import { inject, useAttrs } from "vue";
import { alertContextKey } from "./context";
defineOptions({ inheritAttrs: false });
const attrs = useAttrs();
const ctx = inject(alertContextKey, undefined);
</script>
<template>
<p v-bind="attrs" :class="ctx?.slots.value.title()" data-slot="alert-title">
<slot />
</p>
</template>
@@ -0,0 +1,14 @@
import type { ComputedRef, InjectionKey } from "vue";
import type { AlertVariants, alertVariants } from "@heroui-vue/styles";
export type AlertSlots = ReturnType<typeof alertVariants>;
export type AlertStatus = NonNullable<AlertVariants["status"]>;
export interface AlertContextValue {
slots: ComputedRef<AlertSlots>;
status: ComputedRef<AlertStatus>;
}
export const alertContextKey: InjectionKey<AlertContextValue> =
Symbol("HeroUIVue.Alert");
+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");
+32
View File
@@ -29,6 +29,8 @@ export {
separatorVariants,
kbdVariants,
chipVariants,
badgeVariants,
alertVariants,
type ButtonVariants,
type CardVariants,
type InputVariants,
@@ -44,6 +46,8 @@ export {
type SeparatorVariants,
type KbdVariants,
type ChipVariants,
type BadgeVariants,
type AlertVariants,
} from "@heroui-vue/styles";
export type {
ButtonProps,
@@ -70,6 +74,14 @@ export type {
KbdContentProps,
ChipProps,
ChipLabelProps,
BadgeProps,
BadgeAnchorProps,
BadgeLabelProps,
AlertProps,
AlertIndicatorProps,
AlertContentProps,
AlertTitleProps,
AlertDescriptionProps,
} from "./types";
export { tv, cn, type VariantProps } from "@heroui-vue/styles";
@@ -125,3 +137,23 @@ export {
type ChipContextValue,
type ChipSlots,
} from "./components/chip/context";
export { default as Badge } from "./components/badge/Badge.vue";
export { default as BadgeAnchor } from "./components/badge/BadgeAnchor.vue";
export { default as BadgeLabel } from "./components/badge/BadgeLabel.vue";
export {
badgeContextKey,
type BadgeContextValue,
type BadgeSlots,
} from "./components/badge/context";
export { default as Alert } from "./components/alert/Alert.vue";
export { default as AlertIndicator } from "./components/alert/AlertIndicator.vue";
export { default as AlertContent } from "./components/alert/AlertContent.vue";
export { default as AlertTitle } from "./components/alert/AlertTitle.vue";
export { default as AlertDescription } from "./components/alert/AlertDescription.vue";
export {
alertContextKey,
type AlertContextValue,
type AlertSlots,
type AlertStatus,
} from "./components/alert/context";
+20
View File
@@ -52,3 +52,23 @@ export type KbdAbbrProps = InstanceType<typeof KbdAbbr>["$props"];
export type KbdContentProps = InstanceType<typeof KbdContent>["$props"];
export type ChipProps = InstanceType<typeof Chip>["$props"];
export type ChipLabelProps = InstanceType<typeof ChipLabel>["$props"];
import type Badge from "./components/badge/Badge.vue";
import type BadgeAnchor from "./components/badge/BadgeAnchor.vue";
import type BadgeLabel from "./components/badge/BadgeLabel.vue";
import type Alert from "./components/alert/Alert.vue";
import type AlertIndicator from "./components/alert/AlertIndicator.vue";
import type AlertContent from "./components/alert/AlertContent.vue";
import type AlertTitle from "./components/alert/AlertTitle.vue";
import type AlertDescription from "./components/alert/AlertDescription.vue";
export type BadgeProps = InstanceType<typeof Badge>["$props"];
export type BadgeAnchorProps = InstanceType<typeof BadgeAnchor>["$props"];
export type BadgeLabelProps = InstanceType<typeof BadgeLabel>["$props"];
export type AlertProps = InstanceType<typeof Alert>["$props"];
export type AlertIndicatorProps = InstanceType<typeof AlertIndicator>["$props"];
export type AlertContentProps = InstanceType<typeof AlertContent>["$props"];
export type AlertTitleProps = InstanceType<typeof AlertTitle>["$props"];
export type AlertDescriptionProps = InstanceType<
typeof AlertDescription
>["$props"];