feat: add CloseButton Avatar and Skeleton components
This commit is contained in:
@@ -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",
|
||||
);
|
||||
Reference in New Issue
Block a user