@molecule/app-comparison-row-angular

Feature · comparison-row · App (browser) · v1.1.4 · Apache-2.0

Period-over-period stat comparison ("$X vs $Y", with delta)

npm install @molecule/app-comparison-row-angular

npm · Source on GitHub

How it works

@molecule/app-comparison-row-angular is a ready-made comparison-row 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 { ComparisonRow } from '@molecule/app-comparison-row-angular'

@Component({
  selector: 'app-stat',
  standalone: true,
  imports: [ComparisonRow],
  template: `
    <div
      mol-comparison-row
      label="Revenue"
      current="$24,800"
      previous="$21,300"
      [deltaPct]="16.4"
      periodLabel="vs. last month"
    ></div>
  `,
})
export class Stat {}

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

Reference

Angular period-over-period stat comparison row — the counterpart of @molecule/app-comparison-row-react, with the same props and the same DOM for the same input.

Exports <ComparisonRow>: current value, optional previous value, and a coloured delta% chip for dashboards, report summaries and finance overviews.

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 { ComparisonRow } from '@molecule/app-comparison-row-angular'

@Component({
  selector: 'app-stat',
  standalone: true,
  imports: [ComparisonRow],
  template: `
    <div
      mol-comparison-row
      label="Revenue"
      current="$24,800"
      previous="$21,300"
      [deltaPct]="16.4"
      periodLabel="vs. last month"
    ></div>
  `,
})
export class Stat {}

Type

feature

Installation

npm install @molecule/app-comparison-row-angular @angular/common @angular/core @angular/platform-browser @molecule/app-angular @molecule/app-ui @molecule/app-ui-angular

API

Interfaces

ComparisonRowProps

Props accepted by ComparisonRow — the same props @molecule/app-comparison-row-react accepts. Node-valued props are projected in Angular (an input cannot carry a node), so they are typed unknown: pass a string or number value through the input, and element content in the host template as [slot=<name>] content.

interface ComparisonRowProps {
  /** Label for the metric (e.g. "Revenue"). */
  label?: unknown
  /** Current period value (formatted). */
  current?: unknown
  /** Previous period value (formatted). */
  previous?: unknown
  /** Numeric delta% — drives direction + color when present. */
  deltaPct?: number
  /** Custom delta formatter: a template receiving the delta as `$implicit` / `a0`. */
  formatDelta?: TemplateRef<unknown> | null
  /** Optional sub-text ("vs. last week"). */
  periodLabel?: unknown
  /** Extra classes. */
  className?: string
}

Classes

ComparisonRow

ComparisonRow component.

Period-over-period stat comparison row — current value, optional previous value, and a coloured delta% chip. The host IS the root <div> (div[mol-comparison-row]), matching React's root exactly. The delta's tone class and the layout and type classes come from the ClassMap bond. Same props, same DOM as @molecule/app-comparison-row-react's ComparisonRow. Node-valued label, current, previous and periodLabel content is projected ([slot=<name>]); a string or number input renders as text. A custom formatDelta is a template receiving the delta as $implicit / a0; without one the default arrow + percentage renders.

@Component({
  selector: 'div[mol-comparison-row]',
  standalone: true,
  imports: [NgTemplateOutlet],
  inputs: ['label', 'current', 'previous', 'deltaPct', 'formatDelta', 'periodLabel', 'className'],
  hostDirectives: [MolPlacement],
  host: {
    '[class]': 'classes()',
  },
  // prettier-ignore
  template: `<div [class]="stackClass()">
      <span [class]="labelClass()">@if (hasLabel) {<ng-container>{{ label }}</ng-container>}<ng-content select="[slot=label]" /></span><span #periodSpan [class]="cm().textSize('xs')">@if (hasPeriodLabel) {<ng-container>{{ periodLabel }}</ng-container>}<ng-content select="[slot=periodLabel]" /></span>
    </div>
    <div [class]="valuesClass()">
      <span [class]="currentClass()">@if (hasCurrent) {<ng-container>{{ current }}</ng-container>}<ng-content select="[slot=current]" /></span><span #previousSpan [class]="cm().cn(cm().textSize('xs'))" [style.text-decoration]="'line-through'" [style.opacity]="0.6">@if (hasPrevious) {<ng-container>{{ previous }}</ng-container>}<ng-content select="[slot=previous]" /></span>@if (hasDelta) {<span [class]="deltaClass()">@if (formatDelta) {<ng-container [ngTemplateOutlet]="formatDelta" [ngTemplateOutletContext]="deltaContext()" />}@else {<ng-container>{{ arrow() }} {{ pct() }}%</ng-container>}</span>}
    </div>`,
})
export class ComparisonRow {
  /** Label for the metric (e.g. "Revenue"): text, or `[slot=label]` content. */
  label?: unknown
  /** Current period value (formatted): text, or `[slot=current]` content. */
  current?: unknown
  /** Previous period value (formatted): text, or `[slot=previous]` content. */
  previous?: unknown
  /** Numeric delta% — drives direction + color when present. */
  deltaPct?: number
  /** Custom delta formatter template (receives the delta as `$implicit` / `a0`). */
  formatDelta?: TemplateRef<unknown> | null
  /** Optional sub-text ("vs. last week"): text, or `[slot=periodLabel]` content. */
  periodLabel?: unknown
  /** Extra classes. */
  className?: string

  /** The period-label span (always in the label stack; detached while it has nothing to render). */
  @ViewChild('periodSpan', { static: true }) private periodSpan?: ElementRef<HTMLSpanElement>
  /** The previous-value span (always in the values row; detached while it has nothing to render). */
  @ViewChild('previousSpan', { static: true }) private previousSpan?: ElementRef<HTMLSpanElement>

  /**
   * Keeps each optional span on the page only while it renders a text input or
   * projected content, re-decided whenever either changes (a slot taken away
   * and given back included).
   */
  constructor() {
    usePlacement((hasContent) => {
      const period = this.periodSpan?.nativeElement
      setElementPresent(period, this.hasPeriodLabel || (period ? hasContent(period) : false))
      const previous = this.previousSpan?.nativeElement
      setElementPresent(previous, this.hasPrevious || (previous ? hasContent(previous) : false))
    })
  }

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

  /**
   * Computes the root class list.
   * @returns The class string.
   */
  classes(): string {
    return this.cm().cn(
      this.cm().flex({ align: 'baseline', justify: 'between', gap: 'md' }),
      this.className,
    )
  }

  /**
   * The left stack's class.
   * @returns The class string.
   */
  stackClass(): string {
    return this.cm().stack(0)
  }

  /**
   * The label span's class.
   * @returns The class string.
   */
  labelClass(): string {
    return this.cm().cn(this.cm().textSize('xs'), this.cm().fontWeight('semibold'))
  }

  /**
   * The values row's class.
   * @returns The class string.
   */
  valuesClass(): string {
    return this.cm().flex({ align: 'baseline', gap: 'sm' })
  }

  /**
   * The current-value span's class.
   * @returns The class string.
   */
  currentClass(): string {
    return this.cm().cn(this.cm().textSize('lg'), this.cm().fontWeight('bold'))
  }

  /**
   * The delta chip's class, with the tone of the current direction.
   * @returns The class string.
   */
  deltaClass(): string {
    return this.cm().cn(
      this.cm().textSize('xs'),
      this.cm().fontWeight('semibold'),
      this.cm().textTone(TONE(this.deltaPct ?? 0)),
    )
  }

  /**
   * Whether a text label input is present (a projected label is not).
   * @returns True when the label input renders as text.
   */
  get hasLabel(): boolean {
    return this.label !== undefined && this.label !== null
  }

  /**
   * Whether a text current-value input is present.
   * @returns True when the current input renders as text.
   */
  get hasCurrent(): boolean {
    return this.current !== undefined && this.current !== null
  }

  /**
   * Whether the previous value renders as text: React's
   * `previous !== undefined`.
   * @returns True when the struck-through value shows.
   */
  get hasPrevious(): boolean {
    return this.previous !== undefined
  }

  /**
   * Whether the period label renders as text: React's `periodLabel && …`
   * truthiness.
   * @returns True when the sub-text shows.
   */
  get hasPeriodLabel(): boolean {
    return this.periodLabel !== undefined && this.periodLabel !== null && this.periodLabel !== ''
  }

  /**
   * Whether the delta chip renders.
   * @returns True when `deltaPct` is set.
   */
  get hasDelta(): boolean {
    return this.deltaPct !== undefined
  }

  /**
   * The delta's direction arrow.
   * @returns `'▲'`, `'▼'` or `'–'`.
   */
  arrow(): string {
    return ARROW(this.deltaPct ?? 0)
  }

  /**
   * The delta's magnitude, one decimal place (React's
   * `Math.abs(deltaPct).toFixed(1)`).
   * @returns The formatted magnitude.
   */
  pct(): string {
    return this.deltaPct === undefined ? '' : Math.abs(this.deltaPct).toFixed(1)
  }

  /**
   * The template outlet context for a custom formatter.
   * @returns The delta as `$implicit` and `a0`.
   */
  deltaContext(): Record<string, unknown> {
    const delta = this.deltaPct
    return { $implicit: delta, a0: delta, a1: undefined, a2: 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-ui ^1.4.0
  • @molecule/app-ui-angular ^1.1.0

Runtime Dependencies

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

The delta chip colors by SIGN with fixed semantics: positive = green, negative = red (hardcoded hexes applied inline, not theme tokens). There is no "down-is-good" inversion — for metrics like churn or costs, negate deltaPct (and restate the sign in formatDelta, which controls the text only, never the color). label / current / previous / periodLabel are consumer-provided values — pass pre-formatted, translated values; node content is projected as [slot=<name>] content instead of an input. A custom formatDelta is an ng-template receiving the delta as $implicit / a0.