[CL-1091] feat: Add a status slot to the avatar (#24602)

Consumers project a bit-berry, which the avatar sizes and places at the top end corner. The
avatar is masked around it instead of drawing a ring, so the gap works on any background.
This commit is contained in:
Bryan Cunningham
2026-11-11 12:26:16 -05:00
committed by GitHub
parent f82b5ea6ea
commit a21f43157b
4 changed files with 109 additions and 8 deletions

View File

@@ -1,7 +1,8 @@
<!-- Cut the status gap out of the avatar instead of drawing a ring, so it works on any background -->
<svg
xmlns="http://www.w3.org/2000/svg"
pointer-events="none"
class="tw-rounded-full tw-size-full tw-min-w-full tw-bg-[--avatar-bg] group-aria-disabled/avatar:tw-bg-bg-inactive [&:is(button:not([aria-disabled='true'])_*)]:group-hover/avatar:tw-bg-[--avatar-bg-hover] [&:is(button:not([aria-disabled='true'])_*)]:group-focus-visible/avatar:tw-bg-[--avatar-bg-hover]"
class="tw-rounded-full tw-size-full tw-min-w-full tw-bg-[--avatar-bg] [--avatar-status-x:calc(100%_-_var(--avatar-status-r))] rtl:[--avatar-status-x:var(--avatar-status-r)] group-has-[[slot=status]]/avatar:[mask-image:radial-gradient(circle_at_var(--avatar-status-x)_var(--avatar-status-r),transparent_var(--avatar-status-cutout),#000_calc(var(--avatar-status-cutout)_+_0.5px))] group-aria-disabled/avatar:tw-bg-bg-inactive [&:is(button:not([aria-disabled='true'])_*)]:group-hover/avatar:tw-bg-[--avatar-bg-hover] [&:is(button:not([aria-disabled='true'])_*)]:group-focus-visible/avatar:tw-bg-[--avatar-bg-hover]"
attr.viewBox="0 0 {{ svgSize }} {{ svgSize }}"
>
<title>{{ title() || text() }}</title>
@@ -21,3 +22,9 @@
{{ displayChars() }}
</text>
</svg>
<!-- Overrides the berry's own status size to match the avatar -->
<span
class="tw-absolute tw-top-0 tw-end-0 tw-flex tw-size-[calc(var(--avatar-status-r)*2)] [&>bit-berry]:tw-size-full"
>
<ng-content select="[slot=status]" />
</span>

Before

Width:  |  Height:  |  Size: 1007 B

After

Width:  |  Height:  |  Size: 1.6 KiB

View File

@@ -17,11 +17,36 @@ import { AvatarColor, AvatarDefaultColors, getAvatarDefaultColor } from "./avata
export type AvatarSize = "2xl" | "xl" | "lg" | "base" | "sm";
const sizeClasses: Record<AvatarSize, string[]> = {
"2xl": ["tw-size-16", "tw-min-w-16"],
xl: ["tw-size-14", "tw-min-w-14"],
lg: ["tw-size-11", "tw-min-w-11"],
base: ["tw-size-8", "tw-min-w-8"],
sm: ["tw-size-6", "tw-min-w-6"],
"2xl": [
"tw-size-16",
"tw-min-w-16",
"[--avatar-status-r:0.5rem]",
"[--avatar-status-cutout:0.625rem]",
],
xl: [
"tw-size-14",
"tw-min-w-14",
"[--avatar-status-r:0.375rem]",
"[--avatar-status-cutout:0.5rem]",
],
lg: [
"tw-size-11",
"tw-min-w-11",
"[--avatar-status-r:0.3125rem]",
"[--avatar-status-cutout:0.4375rem]",
],
base: [
"tw-size-8",
"tw-min-w-8",
"[--avatar-status-r:0.25rem]",
"[--avatar-status-cutout:0.3125rem]",
],
sm: [
"tw-size-6",
"tw-min-w-6",
"[--avatar-status-r:0.1875rem]",
"[--avatar-status-cutout:0.25rem]",
],
};
/**
@@ -76,7 +101,7 @@ export function isAvatarColor(color: string | undefined): color is AvatarColor {
changeDetection: ChangeDetectionStrategy.OnPush,
host: {
class:
"tw-inline-block tw-leading-[0px] focus-visible:tw-outline-none tw-rounded-full focus-visible:tw-ring-2 focus-visible:tw-ring-offset-1 focus-visible:tw-ring-border-focus !focus-visible:tw-border-[transparent] focus-visible:tw-z-10 tw-group/avatar aria-disabled:tw-cursor-not-allowed [&.tw-test-hover_svg]:tw-bg-[--avatar-bg-hover] [&.tw-test-focus-visible_svg]:tw-bg-[--avatar-bg-hover]",
"tw-relative tw-inline-block tw-leading-[0px] focus-visible:tw-outline-none tw-rounded-full focus-visible:tw-ring-2 focus-visible:tw-ring-offset-1 focus-visible:tw-ring-border-focus !focus-visible:tw-border-[transparent] focus-visible:tw-z-10 tw-group/avatar aria-disabled:tw-cursor-not-allowed [&.tw-test-hover_svg]:tw-bg-[--avatar-bg-hover] [&.tw-test-focus-visible_svg]:tw-bg-[--avatar-bg-hover]",
"[class]": "sizeClass()",
"[style.--avatar-bg]": "avatarColors().bg",
"[style.--avatar-bg-hover]": "avatarColors().bgHover",

View File

@@ -69,6 +69,27 @@ The avatar color can be set 3 ways. The color is generated using the following o
<Canvas of={stories.ColorByText} />
## Status
Project a status [berry](?path=/docs/component-library-berry--docs) into the `status` slot, choosing
the variant that fits the status. The avatar sizes it and places it in the top end corner.
```html
<bit-avatar text="Walt Walterson" size="2xl">
<bit-berry
slot="status"
type="status"
variant="success"
role="img"
[attr.aria-label]="'online' | i18n"
></bit-berry>
</bit-avatar>
```
<Canvas of={stories.Status} />
## Accessibility
Avatar background color should have 3.1:1 contrast with its background.
Give the status indicator an accessible name, since color alone doesn't convey its meaning.

View File

@@ -1,12 +1,19 @@
import { Meta, StoryObj } from "@storybook/angular";
import { Meta, moduleMetadata, StoryObj } from "@storybook/angular";
import { formatArgsForCodeSnippet } from "@bitwarden/storybook";
import { BerryComponent } from "../berry";
import { AvatarComponent } from "./avatar.component";
export default {
title: "Component Library/Avatar",
component: AvatarComponent,
decorators: [
moduleMetadata({
imports: [BerryComponent],
}),
],
args: {
id: undefined,
text: "Walt Walterson",
@@ -181,6 +188,47 @@ export const CustomColor: Story = {
},
};
export const Status: Story = {
render: (args) => {
return {
props: args,
template: /*html*/ `
<div class="tw-flex tw-gap-4 tw-mb-10">
@for (size of ["sm", "base", "lg", "xl", "2xl"]; track size) {
<bit-avatar [color]="'teal'" [text]="'Walt Walterson'" [size]="size">
<bit-berry slot="status" type="status" variant="success" role="img" aria-label="Online"></bit-berry>
</bit-avatar>
}
</div>
<span class="tw-font-bold">On different backgrounds</span>
<div class="tw-flex tw-gap-4 tw-mb-10">
@for (bg of ["tw-bg-bg-primary", "tw-bg-bg-secondary", "tw-bg-bg-brand-soft", "tw-bg-bg-dark"]; track bg) {
<div class="tw-p-3 tw-rounded" [class]="bg">
<bit-avatar [color]="'teal'" [text]="'Walt Walterson'" [size]="'2xl'">
<bit-berry slot="status" type="status" variant="success" role="img" aria-label="Online"></bit-berry>
</bit-avatar>
</div>
}
</div>
<span class="tw-font-bold">Interactive</span>
<div class="tw-flex tw-gap-4">
<button bit-avatar [color]="'teal'" [text]="'Walt Walterson'" [size]="'2xl'">
<bit-berry slot="status" type="status" variant="success" role="img" aria-label="Online"></bit-berry>
</button>
<button bit-avatar [color]="'teal'" [text]="'Walt Walterson'" [size]="'2xl'" class="tw-test-hover">
<bit-berry slot="status" type="status" variant="success" role="img" aria-label="Online"></bit-berry>
</button>
<button bit-avatar [color]="'teal'" [text]="'Walt Walterson'" [size]="'2xl'" class="tw-test-focus-visible">
<bit-berry slot="status" type="status" variant="success" role="img" aria-label="Online"></bit-berry>
</button>
</div>
`,
};
},
};
export const InteractionStates: Story = {
render: (args) => {
return {