feat: add CloseButton Avatar and Skeleton components

This commit is contained in:
maoyingjie
2026-10-21 17:06:53 +08:00
parent 56b1ef11d8
commit 3d05fea7bd
60 changed files with 3906 additions and 57 deletions
@@ -0,0 +1,51 @@
<script setup lang="ts">
import { computed, inject, provide, useAttrs } from "vue";
import { AvatarRoot } from "reka-ui";
import { avatarVariants, type AvatarVariants } from "@heroui-vue/styles";
import { avatarContextKey, avatarGroupContextKey } from "./context";
defineOptions({ inheritAttrs: false });
const props = defineProps<{
color?: AvatarVariants["color"];
size?: AvatarVariants["size"];
variant?: AvatarVariants["variant"];
/** Marks a direct AvatarGroup child (see AVATAR_GROUP_CHILD). */
__avatar_group_child?: boolean;
}>();
const attrs = useAttrs();
const groupCtx = inject(avatarGroupContextKey, undefined);
const isGroupChild = computed(() => props.__avatar_group_child === true);
const dataSlot = computed(() =>
typeof attrs["data-slot"] === "string" ? attrs["data-slot"] : "avatar",
);
const finalSize = computed(
() => props.size ?? (isGroupChild.value ? groupCtx?.value.size : undefined),
);
const finalColor = computed(
() => props.color ?? (isGroupChild.value ? groupCtx?.value.color : undefined),
);
const finalVariant = computed(
() =>
props.variant ?? (isGroupChild.value ? groupCtx?.value.variant : undefined),
);
const slots = computed(() =>
avatarVariants({
color: finalColor.value,
size: finalSize.value,
variant: finalVariant.value,
}),
);
provide(avatarContextKey, { slots });
</script>
<template>
<AvatarRoot v-bind="attrs" :class="slots.base()" :data-slot="dataSlot">
<slot />
</AvatarRoot>
</template>
@@ -0,0 +1,46 @@
<script setup lang="ts">
import { inject, useAttrs } from "vue";
import { AvatarFallback } from "reka-ui";
import { avatarVariants, type AvatarVariants } from "@heroui-vue/styles";
import { avatarContextKey } from "./context";
defineOptions({ inheritAttrs: false });
const props = defineProps<{
delayMs?: number;
color?: AvatarVariants["color"];
}>();
const attrs = useAttrs();
const ctx = inject(avatarContextKey, undefined);
// Built during render (not cached): key additions/removals on $attrs must not
// depend on computed invalidation of an initially empty key set.
function restAttrs() {
const rest: Record<string, unknown> = {};
for (const key of Object.keys(attrs)) {
if (key !== "class") rest[key] = attrs[key];
}
return rest;
}
// Single tv slot call so a local `color` replaces the inherited variant
// instead of concatenating a second `--color` class (upstream composeSlotClassName).
function fallbackClass() {
const slotArgs: Record<string, unknown> = { class: attrs.class };
if (props.color !== undefined) slotArgs.color = props.color;
const slot = ctx?.slots.value.fallback ?? avatarVariants().fallback;
return slot(slotArgs);
}
</script>
<template>
<AvatarFallback
v-bind="restAttrs()"
:delay-ms="delayMs"
:class="fallbackClass()"
data-slot="avatar-fallback"
>
<slot />
</AvatarFallback>
</template>
@@ -0,0 +1,150 @@
<script lang="ts">
import {
Comment,
Fragment,
Text,
cloneVNode,
computed,
defineComponent,
h,
isVNode,
provide,
useAttrs,
type PropType,
type VNode,
type VNodeChild,
} from "vue";
import {
avatarGroupVariants,
type AvatarGroupVariants,
type AvatarVariants,
} from "@heroui-vue/styles";
import { avatarGroupContextKey } from "./context";
import AvatarGroupCount from "./AvatarGroupCount.vue";
function isCountNode(node: VNode): boolean {
const type = node.type as { name?: string; __name?: string } | string;
if (typeof type !== "object" || type === null) return false;
return type.name === "AvatarGroupCount" || type.__name === "AvatarGroupCount";
}
/** Keep only member-like elements; skip comments, text and other non-members. */
function flattenChildren(nodes: VNodeChild[], out: VNode[] = []): VNode[] {
for (const child of nodes) {
if (Array.isArray(child)) {
flattenChildren(child, out);
continue;
}
if (!isVNode(child)) continue;
if (child.type === Comment || child.type === Text) continue;
if (child.type === Fragment && Array.isArray(child.children)) {
flattenChildren(child.children as VNodeChild[], out);
continue;
}
out.push(child);
}
return out;
}
export default defineComponent({
name: "AvatarGroup",
inheritAttrs: false,
props: {
color: {
type: String as PropType<AvatarVariants["color"]>,
default: undefined,
},
size: {
type: String as PropType<AvatarVariants["size"]>,
default: "md",
},
variant: {
type: String as PropType<AvatarVariants["variant"]>,
default: undefined,
},
max: { type: Number, default: undefined },
isGrid: { type: Boolean, default: false },
overlap: {
type: String as PropType<AvatarGroupVariants["overlap"]>,
default: "clip",
},
},
setup(props, { attrs, slots }) {
const variantSlots = computed(() =>
avatarGroupVariants({
isGrid: props.isGrid,
overlap: props.overlap,
}),
);
const countClass = computed(() => variantSlots.value.count());
const groupContext = computed(() => ({
size: props.size,
color: props.color,
variant: props.variant,
countClass: countClass.value,
}));
provide(avatarGroupContextKey, groupContext);
/**
* Re-read the default slot on every render. Slot-function identity is not a
* reactive dependency, so caching in `computed` shows stale members after the
* parent swaps the slot closure. Returning vnodes as `h()` children (not via
* `<component :is="vnode">`) keeps patches in sync with the latest props.
*/
function render() {
const flat = flattenChildren((slots.default?.() ?? []) as VNodeChild[]);
const avatarNodes: VNode[] = [];
const countNodes: VNode[] = [];
for (const node of flat) {
if (isCountNode(node)) countNodes.push(node);
else avatarNodes.push(node);
}
const visible =
props.max == null
? avatarNodes
: avatarNodes.slice(0, Math.max(0, props.max as number));
const remaining =
props.max == null
? 0
: Math.max(0, avatarNodes.length - visible.length);
const children: VNode[] = visible.map((child) =>
cloneVNode(child, { __avatar_group_child: true }),
);
const hasExplicitCount = countNodes.length > 0;
if (hasExplicitCount) {
children.push(...countNodes);
} else if (remaining > 0) {
children.push(
h(
AvatarGroupCount,
{ key: "avatar-group-auto-count" },
{
default: () => `+${remaining}`,
},
),
);
}
return h(
"div",
{
...attrs,
// Merge caller class with component classes (object spread would drop it).
class: [variantSlots.value.base(), attrs.class],
"data-slot": "avatar-group",
},
children,
);
}
return render;
},
});
</script>
@@ -0,0 +1,56 @@
<script setup lang="ts">
import { computed, inject, useAttrs } from "vue";
import { avatarGroupVariants, type AvatarVariants } from "@heroui-vue/styles";
import Avatar from "./Avatar.vue";
import AvatarFallback from "./AvatarFallback.vue";
import { avatarGroupContextKey } from "./context";
defineOptions({
inheritAttrs: false,
name: "AvatarGroupCount",
});
const props = defineProps<{
color?: AvatarVariants["color"];
size?: AvatarVariants["size"];
variant?: AvatarVariants["variant"];
}>();
const attrs = useAttrs();
const group = inject(avatarGroupContextKey, undefined);
const finalSize = computed(() => props.size ?? group?.value.size);
const finalColor = computed(() => props.color ?? group?.value.color);
const finalVariant = computed(() => props.variant ?? group?.value.variant);
// Built during render (not cached): key additions/removals on $attrs must not
// depend on computed invalidation of an initially empty key set.
function restAttrs() {
const rest: Record<string, unknown> = {};
for (const key of Object.keys(attrs)) {
if (key !== "class") rest[key] = attrs[key];
}
return rest;
}
function countClass() {
const base = group?.value.countClass ?? avatarGroupVariants().count();
return [base, attrs.class];
}
</script>
<template>
<Avatar
v-bind="restAttrs()"
:color="finalColor"
:size="finalSize"
:variant="finalVariant"
:__avatar_group_child="true"
:class="countClass()"
data-slot="avatar-group-count"
>
<AvatarFallback>
<slot />
</AvatarFallback>
</Avatar>
</template>
@@ -0,0 +1,40 @@
<script setup lang="ts">
import { computed, inject, useAttrs } from "vue";
import { AvatarImage } from "reka-ui";
import { avatarContextKey } from "./context";
defineOptions({ inheritAttrs: false });
defineProps<{
src: string;
alt?: string;
asChild?: boolean;
crossOrigin?: "" | "anonymous" | "use-credentials";
loading?: "eager" | "lazy";
sizes?: string;
srcSet?: string;
referrerPolicy?: string;
}>();
const attrs = useAttrs();
const ctx = inject(avatarContextKey, undefined);
const imageClass = computed(
() => ctx?.slots.value.image() ?? "hv-avatar__image",
);
</script>
<template>
<AvatarImage
v-bind="attrs"
:src="src"
:alt="alt"
:as-child="asChild"
:crossorigin="crossOrigin"
:loading="loading"
:sizes="sizes"
:srcset="srcSet"
:referrerpolicy="referrerPolicy"
:class="imageClass"
data-slot="avatar-image"
/>
</template>
@@ -0,0 +1,27 @@
import type { ComputedRef, InjectionKey } from "vue";
import type { AvatarVariants, avatarVariants } from "@heroui-vue/styles";
/** Marks direct children of AvatarGroup so nested Avatars do not inherit group props. */
export const AVATAR_GROUP_CHILD = "__avatar_group_child" as const;
export type AvatarSlots = ReturnType<typeof avatarVariants>;
export interface AvatarPartsContext {
slots: ComputedRef<AvatarSlots>;
}
export const avatarContextKey: InjectionKey<AvatarPartsContext> =
Symbol("HeroUIVue.Avatar");
export interface AvatarGroupContextValue {
size?: AvatarVariants["size"];
color?: AvatarVariants["color"];
variant?: AvatarVariants["variant"];
countClass: string;
}
export type AvatarGroupContext = ComputedRef<AvatarGroupContextValue>;
export const avatarGroupContextKey: InjectionKey<AvatarGroupContext> = Symbol(
"HeroUIVue.AvatarGroup",
);
@@ -0,0 +1,119 @@
<script setup lang="ts">
import { computed, ref, useAttrs, watch } from "vue";
import {
closeButtonVariants,
type CloseButtonVariants,
} from "@heroui-vue/styles";
defineOptions({ inheritAttrs: false });
const props = withDefaults(
defineProps<{
variant?: CloseButtonVariants["variant"];
isDisabled?: boolean;
disabled?: boolean;
}>(),
{ variant: "default" },
);
const attrs = useAttrs();
const disabled = computed(() => props.isDisabled || props.disabled);
const ariaLabel = computed(() =>
typeof attrs["aria-label"] === "string" ? attrs["aria-label"] : "Close",
);
const hovered = ref(false);
const pressed = ref(false);
const focused = ref(false);
const focusVisible = ref(false);
watch(
disabled,
(isDisabled) => {
if (isDisabled) {
hovered.value = false;
pressed.value = false;
}
},
{ flush: "sync" },
);
function guard(e: MouseEvent) {
if (disabled.value) {
e.preventDefault();
e.stopImmediatePropagation();
}
}
function onKeydownGuard(e: KeyboardEvent) {
if (disabled.value) {
e.preventDefault();
e.stopImmediatePropagation();
return;
}
if (e.key === " " || e.key === "Enter") pressed.value = true;
}
function focus(e: FocusEvent) {
focused.value = true;
focusVisible.value = (e.target as HTMLElement).matches(":focus-visible");
}
</script>
<template>
<button
v-bind="attrs"
:type="'button'"
:class="closeButtonVariants({ variant })"
:disabled="disabled"
:aria-disabled="disabled || undefined"
:aria-label="ariaLabel"
:data-hovered="(!disabled && hovered) || undefined"
:data-pressed="(!disabled && pressed) || undefined"
:data-focused="focused || undefined"
:data-focus-visible="focusVisible || undefined"
data-slot="close-button"
@click.capture="guard"
@keydown.capture="onKeydownGuard"
@pointerenter="hovered = !disabled"
@pointerleave="
hovered = false;
pressed = false;
"
@pointerdown="pressed = !disabled"
@pointerup="pressed = false"
@keyup="pressed = false"
@focus="focus"
@blur="
focused = false;
focusVisible = false;
"
>
<slot
:is-hovered="hovered"
:is-pressed="pressed"
:is-focused="focused"
:is-focus-visible="focusVisible"
:is-disabled="disabled"
>
<!-- Default close mark from the fixed baseline CloseIcon. -->
<svg
aria-hidden="true"
data-slot="close-button-icon"
fill="none"
height="16"
role="presentation"
viewBox="0 0 16 16"
width="16"
xmlns="http://www.w3.org/2000/svg"
>
<path
clip-rule="evenodd"
d="M3.47 3.47a.75.75 0 0 1 1.06 0L8 6.94l3.47-3.47a.75.75 0 1 1 1.06 1.06L9.06 8l3.47 3.47a.75.75 0 1 1-1.06 1.06L8 9.06l-3.47 3.47a.75.75 0 0 1-1.06-1.06L6.94 8 3.47 4.53a.75.75 0 0 1 0-1.06Z"
fill="currentColor"
fill-rule="evenodd"
/>
</svg>
</slot>
</button>
</template>
@@ -0,0 +1,49 @@
<script setup lang="ts">
import { computed, onMounted, ref, useAttrs } from "vue";
import { skeletonVariants, type SkeletonVariants } from "@heroui-vue/styles";
defineOptions({ inheritAttrs: false });
const props = defineProps<{
animationType?: SkeletonVariants["animationType"];
}>();
const attrs = useAttrs();
const cssAnimationType = ref<SkeletonVariants["animationType"]>();
function readCssVariable() {
if (typeof document === "undefined") return undefined;
try {
const value = getComputedStyle(document.documentElement)
.getPropertyValue("--skeleton-animation")
.trim();
if (value === "shimmer" || value === "pulse" || value === "none") {
return value;
}
return undefined;
} catch {
return undefined;
}
}
const resolvedAnimationType = computed<SkeletonVariants["animationType"]>(
() => props.animationType ?? cssAnimationType.value ?? "shimmer",
);
const baseClass = computed(() =>
skeletonVariants({ animationType: resolvedAnimationType.value }).base(),
);
onMounted(() => {
if (props.animationType === undefined) {
cssAnimationType.value = readCssVariable();
}
});
</script>
<template>
<div v-bind="attrs" :class="baseClass" data-slot="skeleton">
<slot />
</div>
</template>
+32
View File
@@ -22,6 +22,10 @@ export {
linkVariants,
spinnerVariants,
textAreaVariants,
closeButtonVariants,
avatarVariants,
avatarGroupVariants,
skeletonVariants,
type ButtonVariants,
type CardVariants,
type InputVariants,
@@ -30,6 +34,10 @@ export {
type LinkVariants,
type SpinnerVariants,
type TextAreaVariants,
type CloseButtonVariants,
type AvatarVariants,
type AvatarGroupVariants,
type SkeletonVariants,
} from "@heroui-vue/styles";
export type {
ButtonProps,
@@ -43,6 +51,13 @@ export type {
LinkIconProps,
SpinnerProps,
TextAreaProps,
CloseButtonProps,
AvatarProps,
AvatarImageProps,
AvatarFallbackProps,
AvatarGroupProps,
AvatarGroupCountProps,
SkeletonProps,
} from "./types";
export { tv, cn, type VariantProps } from "@heroui-vue/styles";
@@ -59,3 +74,20 @@ export { default as Link } from "./components/link/Link.vue";
export { default as LinkIcon } from "./components/link/LinkIcon.vue";
export { default as Spinner } from "./components/spinner/Spinner.vue";
export { default as TextArea } from "./components/textarea/TextArea.vue";
export { default as CloseButton } from "./components/close-button/CloseButton.vue";
export { default as Avatar } from "./components/avatar/Avatar.vue";
export { default as AvatarImage } from "./components/avatar/AvatarImage.vue";
export { default as AvatarFallback } from "./components/avatar/AvatarFallback.vue";
export { default as AvatarGroup } from "./components/avatar/AvatarGroup.vue";
export { default as AvatarGroupCount } from "./components/avatar/AvatarGroupCount.vue";
export {
AVATAR_GROUP_CHILD,
avatarContextKey,
avatarGroupContextKey,
type AvatarGroupContext,
type AvatarGroupContextValue,
type AvatarPartsContext,
type AvatarSlots,
} from "./components/avatar/context";
export { default as Skeleton } from "./components/skeleton/Skeleton.vue";
+16
View File
@@ -7,6 +7,13 @@ import type Link from "./components/link/Link.vue";
import type LinkIcon from "./components/link/LinkIcon.vue";
import type Spinner from "./components/spinner/Spinner.vue";
import type TextArea from "./components/textarea/TextArea.vue";
import type CloseButton from "./components/close-button/CloseButton.vue";
import type Avatar from "./components/avatar/Avatar.vue";
import type AvatarImage from "./components/avatar/AvatarImage.vue";
import type AvatarFallback from "./components/avatar/AvatarFallback.vue";
import type AvatarGroup from "./components/avatar/AvatarGroup.vue";
import type AvatarGroupCount from "./components/avatar/AvatarGroupCount.vue";
import type Skeleton from "./components/skeleton/Skeleton.vue";
export type ButtonProps = InstanceType<typeof Button>["$props"];
export type CardProps = InstanceType<typeof Card>["$props"];
@@ -17,6 +24,15 @@ export type LinkProps = InstanceType<typeof Link>["$props"];
export type LinkIconProps = InstanceType<typeof LinkIcon>["$props"];
export type SpinnerProps = InstanceType<typeof Spinner>["$props"];
export type TextAreaProps = InstanceType<typeof TextArea>["$props"];
export type CloseButtonProps = InstanceType<typeof CloseButton>["$props"];
export type AvatarProps = InstanceType<typeof Avatar>["$props"];
export type AvatarImageProps = InstanceType<typeof AvatarImage>["$props"];
export type AvatarFallbackProps = InstanceType<typeof AvatarFallback>["$props"];
export type AvatarGroupProps = InstanceType<typeof AvatarGroup>["$props"];
export type AvatarGroupCountProps = InstanceType<
typeof AvatarGroupCount
>["$props"];
export type SkeletonProps = InstanceType<typeof Skeleton>["$props"];
import type SwitchGroup from "./components/switch/SwitchGroup.vue";
import type Label from "./components/label/Label.vue";