@molecule/app-avatar-stack-angular

Feature · avatar-stack · App (browser) · v1.2.4 · Apache-2.0

Angular stacked avatars and user-chip component

npm install @molecule/app-avatar-stack-angular

npm · Source on GitHub

How it works

@molecule/app-avatar-stack-angular is a ready-made avatar-stack feature for the app (browser) side. It composes the core interfaces it needs, so it works with whichever providers your app has bonded.

import { Component } from '@angular/core'
import { AvatarStack, UserChip } from '@molecule/app-avatar-stack-angular'

@Component({
  selector: 'app-assignees',
  standalone: true,
  imports: [AvatarStack, UserChip],
  template: `
    <div mol-avatar-stack [people]="people" [max]="3" size="sm"></div>
    <div mol-user-chip name="Alice Kim" src="/avatars/alice.jpg">
      <span slot="subtitle">Admin</span>
    </div>
    <div
      mol-user-chip
      name="Alice Kim"
      href="/people/alice"
      data-person="alice"
      [nameLinkProps]="{ className: 'person-name', target: '_blank', rel: 'noreferrer' }"
    ></div>
  `,
})
export class Assignees {
  people = [
    { name: 'Alice Kim', src: '/avatars/alice.jpg' },
    { name: 'Bob Lee' },
    { name: 'Carol Díaz', src: '/avatars/carol.jpg' },
  ]
}

Works with: @molecule/app-angular, @molecule/app-i18n, @molecule/app-ui, @molecule/app-ui-angular

Reference

Angular avatar-stack and user-chip components — the counterpart of @molecule/app-avatar-stack-react: the same props and the same DOM for the same input.

Exports:

  • <div mol-avatar-stack> — horizontal stack of up to max avatars with a trailing "+N" overflow chip.
  • <div mol-user-chip> — avatar + name + optional subtitle row for dropdowns, mention pickers, and row-level user references.

Both render on top of <div mol-avatar> from @molecule/app-ui-angular, so avatar fallbacks (text initials, color hash) come from there.

Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit src/index.ts JSDoc, not this file.

Quick Start

import { Component } from '@angular/core'
import { AvatarStack, UserChip } from '@molecule/app-avatar-stack-angular'

@Component({
  selector: 'app-assignees',
  standalone: true,
  imports: [AvatarStack, UserChip],
  template: `
    <div mol-avatar-stack [people]="people" [max]="3" size="sm"></div>
    <div mol-user-chip name="Alice Kim" src="/avatars/alice.jpg">
      <span slot="subtitle">Admin</span>
    </div>
    <div
      mol-user-chip
      name="Alice Kim"
      href="/people/alice"
      data-person="alice"
      [nameLinkProps]="{ className: 'person-name', target: '_blank', rel: 'noreferrer' }"
    ></div>
  `,
})
export class Assignees {
  people = [
    { name: 'Alice Kim', src: '/avatars/alice.jpg' },
    { name: 'Bob Lee' },
    { name: 'Carol Díaz', src: '/avatars/carol.jpg' },
  ]
}

Type

feature

Installation

npm install @molecule/app-avatar-stack-angular @angular/common @angular/core @angular/platform-browser @molecule/app-angular @molecule/app-i18n @molecule/app-ui @molecule/app-ui-angular

API

Interfaces

AvatarStackPerson

Represents a single person entry rendered inside an AvatarStack.

interface AvatarStackPerson {
  /** Display name (also used to generate the text fallback). */
  name: string
  /** Optional avatar image URL. */
  src?: string
  /** Optional alt text — defaults to `name`. */
  alt?: string
}

AvatarStackProps

Props accepted by AvatarStack — the same props @molecule/app-avatar-stack-react accepts. The list itself is the people input, so every prop is bindable.

interface AvatarStackProps {
  /** People to render. */
  people: AvatarStackPerson[]
  /** Maximum visible avatars. Remaining are summarised in an "overflow" chip. */
  max?: number
  /** Avatar size preset. */
  size?: 'xs' | 'sm' | 'md' | 'lg'
  /** Extra classes on the outer stack. */
  className?: string
}

UserChipProps

Props accepted by UserChip — the same props @molecule/app-avatar-stack-react accepts. Node props (subtitle, trailing) are projected content in Angular (an input cannot carry a node), so they are typed unknown: pass them in the host template, projected as [slot=subtitle] / [slot=trailing].

interface UserChipProps {
  /** Display name. */
  name: string
  /** Profile destination. Only the displayed name becomes a link. */
  href?: string
  /** Attributes and events for the canonical name link. */
  nameLinkProps?: UserChipNameLinkProps
  /** Optional avatar image URL. */
  src?: string
  /** Optional secondary line (role / email / handle), projected as `[slot=subtitle]` content. */
  subtitle?: unknown
  /** Optional trailing content (action button, status dot, etc.), projected as `[slot=trailing]` content. */
  trailing?: unknown
  /** Avatar size preset. */
  size?: 'xs' | 'sm' | 'md' | 'lg'
  /** Extra classes on the outer row. */
  className?: string
}

Classes

AvatarStack

AvatarStack component.

Horizontal stack of overlapping avatars (assignees, attendees, etc.). Renders up to max avatars; any remaining are summarised as +N in a trailing chip, whose accessible name (avatarStack.more) and visible prefix (avatarStack.overflowPrefix) come from useTranslation(), so the chip follows the active locale. The host IS the outer <div> (div[mol-avatar-stack]), matching React's root exactly.

@Component({
  selector: 'div[mol-avatar-stack]',
  standalone: true,
  imports: [Avatar],
  inputs: ['people', 'max', 'size', 'className'],
  host: {
    '[class]': 'classes()',
  },
  // prettier-ignore
  template: `@for (person of visible(); track $index; let index = $index) {
    <span [style.margin-left]="index === 0 ? null : overlapMargin">
      <div mol-avatar [src]="person.src" [alt]="person.alt ?? person.name" [name]="person.name" [size]="avatarSize()"></div>
    </span>
  }@if (overflow() > 0) {
    <span [class]="overflowClasses()" [style.margin-left]="overlapMargin" [attr.aria-label]="overflowLabel()">{{ overflowText() }}</span>
  }`,
})
export class AvatarStack {
  /** People to render. */
  people: AvatarStackPerson[] = []
  /** Maximum visible avatars (defaults to `4` at read time). */
  max?: number
  /** Avatar size preset (defaults to `'sm'` at read time). */
  size?: 'xs' | 'sm' | 'md' | 'lg'
  /** Extra classes on the outer stack. */
  className?: string

  /** The overlap's negative left margin. */
  readonly overlapMargin = OVERLAP_MARGIN

  /**
   * Reactive translation, read in the injection context. Its `t` reads the
   * locale, so the view re-renders the chip's two strings on a locale switch.
   */
  private readonly i18n = useTranslation()

  /**
   * The current class map.
   * @returns The UIClassMap.
   */
  cm(): UIClassMap {
    return getClassMap()
  }

  /**
   * The avatar size passed to every `<div mol-avatar>` (React's
   * `size = 'sm'` default parameter).
   * @returns The size preset.
   */
  avatarSize(): 'xs' | 'sm' | 'md' | 'lg' {
    return this.size ?? 'sm'
  }

  /**
   * The avatars that fit within `max` (React's `people.slice(0, max)`).
   * @returns The visible people.
   */
  visible(): AvatarStackPerson[] {
    return this.people.slice(0, this.max ?? 4)
  }

  /**
   * How many people the overflow chip summarises (React's
   * `Math.max(0, people.length - visible.length)`).
   * @returns The overflow count.
   */
  overflow(): number {
    return Math.max(0, this.people.length - this.visible().length)
  }

  /**
   * The overflow chip's accessible name (React's
   * `t('avatarStack.more', { count: overflow }, { defaultValue: '+{{count}} more' })`).
   * @returns The translated label.
   */
  overflowLabel(): string {
    return this.i18n.t(
      'avatarStack.more',
      { count: this.overflow() },
      { defaultValue: '+{{count}} more' },
    )
  }

  /**
   * The overflow chip's visible text: the translated prefix and the count
   * (React's `{t('avatarStack.overflowPrefix', undefined, …)}{overflow}`).
   * @returns The prefix followed by the overflow count.
   */
  overflowText(): string {
    return (
      this.i18n.t('avatarStack.overflowPrefix', undefined, { defaultValue: '+' }) + this.overflow()
    )
  }

  /**
   * Computes the outer stack's class list (`cm.flex({ align: 'center' })` +
   * `className`).
   * @returns The class string.
   */
  classes(): string {
    return this.cm().cn(this.cm().flex({ align: 'center' }), this.className)
  }

  /**
   * Computes the overflow chip's class list (`cm.flex({ align: 'center',
   * justify: 'center' })` + `cm.roundedFull` + `cm.textSize('xs')` +
   * `cm.fontWeight('medium')`).
   * @returns The class string.
   */
  overflowClasses(): string {
    const cm = this.cm()
    return cm.cn(
      cm.flex({ align: 'center', justify: 'center' }),
      cm.roundedFull,
      cm.textSize('xs'),
      cm.fontWeight('medium'),
    )
  }
}

UserChip

UserChip component.

Avatar + name + optional subtitle row — useful in dropdowns, mention pickers, assignment popovers, and row-level user references. The host IS the outer <div> (div[mol-user-chip]), matching React's root exactly. The node props are projected slots: subtitle renders inside its own <span>, trailing renders bare at the end of the row. The subtitle span renders optimistically so a projection can land, then withdraws at view init when neither a text input nor a projection is present — React renders no span at all for an absent or empty subtitle.

@Component({
  selector: 'div[mol-user-chip]',
  standalone: true,
  hostDirectives: [MolPlacement],
  imports: [Avatar],
  inputs: ['name', 'href', 'nameLinkProps', 'src', 'subtitle', 'trailing', 'size', 'className'],
  host: {
    '[class]': 'classes()',
  },
  // prettier-ignore
  template: `@if (href === undefined) {<div mol-avatar [src]="src" [alt]="name" [name]="name" [size]="avatarSize()"></div>}
    @else {<div aria-hidden="true"><div mol-avatar [src]="src" [alt]="name" [name]="name" [size]="avatarSize()"></div></div>}
    <div [class]="bodyClasses()">
      @if (href === undefined) {<span [class]="nameClasses()">{{ name }}</span>}
      @else {<a #nameLink data-mol-id="user-chip-profile" [href]="href" [class]="nameClasses(true)" [attr.style]="nameLinkStyle()">{{ name }}</a>}
      <span #subtitleEl [class]="cm().textSize('xs')">@if (hasTextSubtitle()) {<ng-container>{{ subtitle }}</ng-container>}<ng-content select="[slot=subtitle]"></ng-content></span>
    </div>
    <ng-content select="[slot=trailing]"></ng-content>`,
})
export class UserChip implements AfterViewChecked, OnDestroy {
  /** Display name. */
  name = ''
  /** Profile destination. The avatar stays outside the single name link. */
  href?: string
  /** Attributes, classes and event handlers for the canonical name link. */
  nameLinkProps?: UserChipProps['nameLinkProps']
  /** Optional avatar image URL. */
  src?: string
  /** Optional secondary line: text, or `[slot=subtitle]` content. */
  subtitle?: unknown
  /** Optional trailing content: projected `[slot=trailing]` content. */
  trailing?: unknown
  /** Avatar size preset (defaults to `'sm'` at read time). */
  size?: 'xs' | 'sm' | 'md' | 'lg'
  /** Extra classes on the outer row. */
  className?: string

  /** The subtitle span (a text input or projected content lands here). */
  @ViewChild('subtitleEl', { static: true }) private subtitleEl?: ElementRef<HTMLSpanElement>

  /** The name anchor, present only while href is defined. */
  @ViewChild('nameLink') private nameLink?: ElementRef<HTMLAnchorElement>

  private readonly renderer = inject(Renderer2)
  private renderedNameLink?: HTMLAnchorElement
  private nameLinkWriter?: ElementPropsWriter

  /**
   * Renders the subtitle span only while it has a text input or projected
   * content (React's `{subtitle && <span>…}`), including content a consumer
   * projects later.
   */
  constructor() {
    usePlacement((hasContent) => {
      const subtitle = this.subtitleEl?.nativeElement
      setElementPresent(subtitle, this.hasTextSubtitle() || (!!subtitle && hasContent(subtitle)))
    })
  }

  /**
   * The current class map.
   * @returns The UIClassMap.
   */
  cm(): UIClassMap {
    return getClassMap()
  }

  /**
   * The avatar size passed to `<div mol-avatar>` (React's `size = 'sm'`
   * default parameter).
   * @returns The size preset.
   */
  avatarSize(): 'xs' | 'sm' | 'md' | 'lg' {
    return this.size ?? 'sm'
  }

  /**
   * Whether the subtitle input itself carries text (React's `{subtitle}` for
   * a string child; a node arrives as `[slot=subtitle]` content instead).
   * @returns True for a string or number subtitle.
   */
  hasTextSubtitle(): boolean {
    return typeof this.subtitle === 'string' || typeof this.subtitle === 'number'
  }

  /**
   * Computes the outer row's class list (`cm.flex({ align: 'center', gap:
   * 'sm' })` + `className`).
   * @returns The class string.
   */
  classes(): string {
    return this.cm().cn(this.cm().flex({ align: 'center', gap: 'sm' }), this.className)
  }

  /**
   * Computes the name/subtitle column's class list (`cm.flex1` +
   * `cm.stack(0)`).
   * @returns The class string.
   */
  bodyClasses(): string {
    return this.cm().cn(this.cm().flex1, this.cm().stack(0))
  }

  /**
   * Computes the name span's class list (`cm.textSize('sm')` +
   * `cm.fontWeight('medium')`).
   * @param linked - Whether to include the name-link classes.
   * @returns The class string.
   */
  nameClasses(linked = false): string {
    return this.cm().cn(
      this.cm().textSize('sm'),
      this.cm().fontWeight('medium'),
      linked && this.nameLinkProps?.className,
      linked && this.nameLinkProps?.class,
    )
  }

  /**
   * Serializes the link's inline style with the shared React-compatible utility.
   * @returns The style attribute, or null when absent.
   */
  nameLinkStyle(): string | null {
    return styleText(this.nameLinkProps?.style)
  }

  /** Applies the link attribute input to the anchor, including changes and removals. */
  ngAfterViewChecked(): void {
    const link = this.nameLink?.nativeElement
    if (link !== this.renderedNameLink) {
      this.nameLinkWriter?.destroy()
      this.nameLinkWriter = undefined
      this.renderedNameLink = link
    }
    if (!link) return
    this.nameLinkWriter ??= new ElementPropsWriter(this.renderer, link)
    // The profile id is the default: a caller's own `data-mol-id` wins, as
    // React's spread order does.
    this.nameLinkWriter.write(
      { 'data-mol-id': 'user-chip-profile', ...this.nameLinkProps },
      OWNED_NAME_LINK_KEYS,
    )
  }

  /** Removes link listeners when the chip unmounts. */
  ngOnDestroy(): void {
    this.nameLinkWriter?.destroy()
    this.nameLinkWriter = undefined
  }
}

Injection Notes

Requirements

Peer dependencies:

  • @angular/common ^22.0.0
  • @angular/core ^22.0.0
  • @angular/platform-browser ^22.0.0
  • @molecule/app-angular ^3.13.0 || ^4.0.0
  • @molecule/app-i18n ^1.0.1
  • @molecule/app-ui ^1.0.1
  • @molecule/app-ui-angular ^1.3.0

Runtime Dependencies

  • @angular/common
  • @angular/core
  • @angular/platform-browser
  • @molecule/app-angular
  • @molecule/app-i18n
  • @molecule/app-ui
  • @molecule/app-ui-angular

UserChip with href renders one profile link on the displayed name, with a decorative avatar outside it. Subtitle and trailing projections remain outside the link. The nameLinkProps input forwards anchor attributes and events (including onClick for app routing); className or class adds link classes. The host div is the root: bind ordinary attributes and events directly on it. Without href, the plain avatar/name DOM is unchanged and nameLinkProps is ignored. An empty-string href still renders a link.

The overflow chip announces "+N more" — this is the only user-visible text. Both of its strings come from useTranslation() in @molecule/app-angular: the chip's accessible name is the avatarStack.more key and the sign before its count is avatarStack.overflowPrefix. Add the companion bond @molecule/app-locales-avatar-stack to translate them; without a translation the English defaults render. Avatar fallbacks (initials, color hash) come from <div mol-avatar> in @molecule/app-ui-angular. The overlap is an inline negative left margin (margin-left: -0.5rem), not a ClassMap class: the abstract spacing scale is non-negative, so a negative margin is one of the sanctioned inline-style cases. Node props are projected content: [slot=subtitle] and [slot=trailing].

Translations

Translation strings are provided by @molecule/app-locales-avatar-stack.