@molecule/app-listing-card-angular

Feature · listing-card · App (browser) · v1.2.3 · Apache-2.0

Angular listing/product-card primitives with media, body, actions slots

npm install @molecule/app-listing-card-angular

npm · Source on GitHub

How it works

@molecule/app-listing-card-angular is a ready-made listing-card 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 {
  ListingCard,
  ListingCardActions,
  ListingCardBody,
  ListingCardMedia,
  ListingGrid,
} from '@molecule/app-listing-card-angular'

interface Listing {
  id: string
  name: string
  location: string
  imageUrl: string
  price: number
}

@Component({
  selector: 'app-listings',
  standalone: true,
  imports: [ListingCard, ListingCardActions, ListingCardBody, ListingCardMedia, ListingGrid],
  template: `
    <div mol-listing-grid [columns]="3">
      <div mol-listing-card [onClick]="open(item.id)">
        <div mol-listing-card-media [src]="item.imageUrl" aspect="4/3" [alt]="item.name"></div>
        <div
          mol-listing-card-body
          [title]="item.name"
          [subtitle]="item.location"
          [price]="'$' + item.price + '/night'"
        ></div>
        <div mol-listing-card-actions>
          <button type="button" (click)="$event.stopPropagation(); save(item.id)">Save</button>
        </div>
      </div>
    </div>
  `,
})
export class Listings {
  items: Listing[] = []
  item: Listing = {
    id: 'l1',
    name: 'Sea-view loft',
    location: 'Lisbon',
    imageUrl: '/listings/loft.jpg',
    price: 120,
  }
  open(id: string): () => void {
    return () => console.log('open listing', id)
  }
  readonly save = (id: string): void => {
    console.log('save listing', id)
  }
}

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

Reference

Angular product / property / listing card primitives.

Exports:

  • <ListingCard> — outer Card shell (div[mol-listing-card]). Props: toggle? (a counted server-backed action, rendered after the content), onClick?, className?, dataMolId?; children are projected content.
  • <ListingCardMedia> — top image slot (div[mol-listing-card-media]). Props: src?, alt?, node children (custom media node), aspect? ('1/1' | '4/3' | '16/9' | '3/2', default '4/3', applied as an inline aspect-ratio style), overlay? ([slot=overlay], absolutely positioned over the media), className?.
  • <ListingCardBody> — stacked text (div[mol-listing-card-body]). Props: title, subtitle?, price?, meta?, className?.
  • <ListingCardActions> — action row (div[mol-listing-card-actions]). Props: node children, layout? ('horizontal' default | 'stacked'), className?.
  • <ListingGrid> — grid container (div[mol-listing-grid]). Props: node children, columns? (1–6, default 3), gap?, className?.

This is the Angular counterpart of @molecule/app-listing-card-react: the same exports, the same props (React names) and the same DOM for the same input. Node props are projected content — [slot=<name>] for the named ones and default content for children.

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 {
  ListingCard,
  ListingCardActions,
  ListingCardBody,
  ListingCardMedia,
  ListingGrid,
} from '@molecule/app-listing-card-angular'

interface Listing {
  id: string
  name: string
  location: string
  imageUrl: string
  price: number
}

@Component({
  selector: 'app-listings',
  standalone: true,
  imports: [ListingCard, ListingCardActions, ListingCardBody, ListingCardMedia, ListingGrid],
  template: `
    <div mol-listing-grid [columns]="3">
      <div mol-listing-card [onClick]="open(item.id)">
        <div mol-listing-card-media [src]="item.imageUrl" aspect="4/3" [alt]="item.name"></div>
        <div
          mol-listing-card-body
          [title]="item.name"
          [subtitle]="item.location"
          [price]="'$' + item.price + '/night'"
        ></div>
        <div mol-listing-card-actions>
          <button type="button" (click)="$event.stopPropagation(); save(item.id)">Save</button>
        </div>
      </div>
    </div>
  `,
})
export class Listings {
  items: Listing[] = []
  item: Listing = {
    id: 'l1',
    name: 'Sea-view loft',
    location: 'Lisbon',
    imageUrl: '/listings/loft.jpg',
    price: 120,
  }
  open(id: string): () => void {
    return () => console.log('open listing', id)
  }
  readonly save = (id: string): void => {
    console.log('save listing', id)
  }
}

Type

feature

Installation

npm install @molecule/app-listing-card-angular @angular/common @angular/core @angular/platform-browser @molecule/app-angular @molecule/app-ui @molecule/app-ui-angular

API

Interfaces

ListingCardToggleProps

A server-backed card action with the total number of people who selected it.

interface ListingCardToggleProps {
  /** The pressed state and count from the same loaded record. */
  value: { pressed: boolean; count: number }
  /** Saves the desired state and returns the server-confirmed state and count together. */
  commit: (desired: boolean) => Promise<ListingCardToggleProps['value']>
  /** Label when the displayed value is true. */
  activeLabel: string | number
  /** Label when the displayed value is false. */
  inactiveLabel: string | number
  /** Receives a failed save after the toggle put the last confirmed state and count back. */
  onError?: (error: unknown) => void
  /** Overrides the button's default `data-mol-id`. */
  dataMolId?: string
  /** Button variant (defaults to `'solid'` while pressed, `'outline'` otherwise). */
  variant?: ButtonVariant
  /** Button color. */
  color?: ColorVariant
  /** Button size. */
  size?: ButtonSize
  /** Whether the button stretches to fill its container. */
  fullWidth?: boolean
  /** Disables the button (permissions, never a pending save). */
  disabled?: boolean
  /** Extra classes on the button. */
  className?: string
  /** `data-testid` for the button. */
  testId?: string
  /** `data-mol-id` automation id; wins over `dataMolId` (React's precedence). */
  automationId?: string
}

ListingCardProps

Props accepted by ListingCard — the same props @molecule/app-listing-card-react accepts. Node props (children) are projected content in Angular (an input cannot carry a node), so they are typed unknown: pass them in the host template, not as values.

interface ListingCardProps {
  /** Slots — projected as the element's default content. */
  children?: unknown
  /** Renders a counted native toggle after the content, outside any links in the content. */
  toggle?: ListingCardToggleProps
  /** Called on any click on the card (React's `onClick`, with the click event). */
  onClick?: (event: MouseEvent) => void
  /** Extra classes on the outer Card. */
  className?: string
  /** `data-mol-id` for AI-agent selectors. */
  dataMolId?: string
}

ListingCardActionsProps

Props for ListingCardActions.

interface ListingCardActionsProps {
  /** Action content — buttons, favorite toggle, etc. Projected content. */
  children?: unknown
  /** Layout — `'horizontal'` (default) fills width, `'stacked'` is vertical. */
  layout?: 'horizontal' | 'stacked'
  /** Extra classes. */
  className?: string
}

ListingCardBodyProps

Props for ListingCardBody. Node rows are projected content ([slot=<name>]) or text inputs.

interface ListingCardBodyProps {
  /** Primary title / name, a text input or `[slot=title]` content. */
  title?: unknown
  /** Secondary line (subtitle, location, category), a text input or `[slot=subtitle]` content. */
  subtitle?: unknown
  /** Price / metric line, a text input or `[slot=price]` content. */
  price?: unknown
  /** Optional extras row (rating, availability, badges), a text input or `[slot=meta]` content. */
  meta?: unknown
  /** Extra classes. */
  className?: string
}

ListingCardMediaProps

Props for ListingCardMedia.

interface ListingCardMediaProps {
  /** Image URL. */
  src?: string
  /** Image alternative text (defaults to an empty string). */
  alt?: string
  /** Override with a node (video, carousel, svg), projected as the default content. */
  children?: unknown
  /** Aspect ratio (`'1/1' | '4/3' | '16/9' | '3/2'`), applied as an inline `aspect-ratio` style. Defaults to `'4/3'`. */
  aspect?: '1/1' | '4/3' | '16/9' | '3/2'
  /** Optional overlay node (badge, favorite heart), projected as `[slot=overlay]` content. */
  overlay?: unknown
  /** Extra classes. */
  className?: string
}

ListingGridProps

Props for ListingGrid.

interface ListingGridProps {
  /** Grid content — the cards. Projected content. */
  children?: unknown
  /** Column count, fixed at every viewport width. Defaults to 3. */
  columns?: 1 | 2 | 3 | 4 | 5 | 6
  /** Gap between cards. Defaults to `'md'`. */
  gap?: 'xs' | 'sm' | 'md' | 'lg'
  /** Extra classes. */
  className?: string
}

Classes

ListingCardActions

ListingCardActions component.

Bottom action row of a listing card. The host IS the root div (div[mol-listing-card-actions]), matching React's root exactly; the action content is the element's default projected content.

@Component({
  selector: 'div[mol-listing-card-actions]',
  standalone: true,
  inputs: ['layout', 'className'],
  host: {
    '[class]': 'classes()',
  },
  template: '<ng-content></ng-content>',
})
export class ListingCardActions {
  /** Layout — `'horizontal'` (default) fills width, `'stacked'` is vertical. */
  layout?: ListingCardActionsProps['layout']
  /** Extra classes. */
  className?: string

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

  /**
   * Computes the row class list (React's flex + padding tokens plus
   * `className`).
   * @returns The class string.
   */
  classes(): string {
    const cm = this.cm()
    return cm.cn(
      cm.flex({
        direction: this.layout === 'stacked' ? 'col' : 'row',
        gap: 'sm',
        align: 'stretch',
      }),
      cm.sp('p', 3),
      this.className,
    )
  }
}

ListingCardBody

ListingCardBody component.

Body of a listing card — title / subtitle / price / meta rows stacked. The host IS the root div (div[mol-listing-card-body]), matching React's root exactly. Node rows are projected content ([slot=<name>]) or plain text inputs; an optional row renders only while it has a text input or projected content, as React's {subtitle && <p>…} does, and follows content a consumer projects later.

@Component({
  selector: 'div[mol-listing-card-body]',
  standalone: true,
  hostDirectives: [MolPlacement],
  inputs: ['title', 'subtitle', 'price', 'meta', 'className'],
  host: {
    '[class]': 'classes()',
  },
  // prettier-ignore
  template: `<h3 [class]="titleClasses()">@if (hasTitle()) {{{ title }}}<ng-content select="[slot=title]"></ng-content></h3><p #subtitleEl [class]="subtitleClasses()">@if (hasSubtitle()) {{{ subtitle }}}<ng-content select="[slot=subtitle]"></ng-content></p><p #priceEl [class]="priceClasses()">@if (hasPrice()) {{{ price }}}<ng-content select="[slot=price]"></ng-content></p><div #metaEl [class]="metaClasses()">@if (hasMeta()) {{{ meta }}}<ng-content select="[slot=meta]"></ng-content></div>`,
})
export class ListingCardBody {
  /** Primary title / name — a text input, or `[slot=title]` projected content. */
  title?: unknown
  /** Secondary line — a text input, or `[slot=subtitle]` projected content. */
  subtitle?: unknown
  /** Price / metric line — a text input, or `[slot=price]` projected content. */
  price?: unknown
  /** Optional extras row — a text input, or `[slot=meta]` projected content. */
  meta?: unknown
  /** Extra classes. */
  className?: string

  /** The optional subtitle paragraph (a text input or projected content lands here). */
  @ViewChild('subtitleEl', { static: true }) private subtitleEl?: ElementRef<HTMLElement>
  /** The optional price paragraph. */
  @ViewChild('priceEl', { static: true }) private priceEl?: ElementRef<HTMLElement>
  /** The optional meta row. */
  @ViewChild('metaEl', { static: true }) private metaEl?: ElementRef<HTMLElement>

  /**
   * Shows each optional row while it has a text input or projected content
   * (React's `{subtitle && <p>…}`); a row stays in the template, so content a
   * consumer projects later renders in it.
   */
  constructor() {
    usePlacement((hasContent) => this.syncRows(hasContent))
  }

  /**
   * Applies the rows' presence.
   *
   * @param hasContent - Reads whether a slot has content.
   */
  private syncRows(hasContent: SlotReader): void {
    const row = (el: HTMLElement | undefined, text: boolean): void =>
      setElementPresent(el, text || (el !== undefined && hasContent(el)))
    row(this.subtitleEl?.nativeElement, this.hasSubtitle())
    row(this.priceEl?.nativeElement, this.hasPrice())
    row(this.metaEl?.nativeElement, this.hasMeta())
  }

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

  /**
   * The root class list (React's `cm.cn(cm.stack(1), cm.sp('p', 3), className)`).
   * @returns The class string.
   */
  classes(): string {
    const cm = this.cm()
    return cm.cn(cm.stack(1), cm.sp('p', 3), this.className)
  }

  /**
   * The title heading's class list (React's
   * `cm.cn(cm.textSize('base'), cm.fontWeight('semibold'))`).
   * @returns The class string.
   */
  titleClasses(): string {
    const cm = this.cm()
    return cm.cn(cm.textSize('base'), cm.fontWeight('semibold'))
  }

  /**
   * The subtitle paragraph's class list (React's `cm.textSize('sm')`).
   * @returns The class string.
   */
  subtitleClasses(): string {
    return this.cm().textSize('sm')
  }

  /**
   * The price paragraph's class list (React's
   * `cm.cn(cm.textSize('lg'), cm.fontWeight('bold'))`).
   * @returns The class string.
   */
  priceClasses(): string {
    const cm = this.cm()
    return cm.cn(cm.textSize('lg'), cm.fontWeight('bold'))
  }

  /**
   * The meta row's class list (React's
   * `cm.flex({ align: 'center', gap: 'sm', wrap: 'wrap' })`).
   * @returns The class string.
   */
  metaClasses(): string {
    return this.cm().flex({ align: 'center', gap: 'sm', wrap: 'wrap' })
  }

  /**
   * Whether a text title input renders (a projected title is not text).
   * @returns True when the title renders as text.
   */
  hasTitle(): boolean {
    return hasText(this.title)
  }

  /**
   * Whether a text subtitle input renders (React's `subtitle && …`).
   * @returns True when the subtitle renders as text.
   */
  hasSubtitle(): boolean {
    return hasText(this.subtitle)
  }

  /**
   * Whether a text price input renders (React's `price && …`).
   * @returns True when the price renders as text.
   */
  hasPrice(): boolean {
    return hasText(this.price)
  }

  /**
   * Whether a text meta input renders (React's `meta && …`).
   * @returns True when the meta renders as text.
   */
  hasMeta(): boolean {
    return hasText(this.meta)
  }
}

ListingCardMedia

ListingCardMedia component.

Media slot at the top of a listing card. The host IS the root div (div[mol-listing-card-media]), matching React's root exactly: the ClassMap classes and the inline aspect-ratio live on it. The caller's node is the default projected content and replaces the <img> fallback; overlay is projected as [slot=overlay] into the absolutely-positioned inset-0 layer.

@Component({
  selector: 'div[mol-listing-card-media]',
  standalone: true,
  hostDirectives: [MolPlacement],
  imports: [NgTemplateOutlet],
  inputs: ['src', 'alt', 'aspect', 'overlay', 'className'],
  host: {
    '[class]': 'classes()',
    '[style.aspect-ratio]': 'aspectText()',
  },
  // prettier-ignore
  template: `<img #img [attr.alt]="alt ?? ''" style="width: 100%; height: 100%; object-fit: cover" /><span #childrenSlot><ng-content></ng-content></span><div #overlayEl [class]="overlayClasses()">@if (overlayTemplate(); as overlayTpl) {<ng-container [ngTemplateOutlet]="overlayTpl" />}<ng-content select="[slot=overlay]"></ng-content></div>`,
})
export class ListingCardMedia {
  /** Image URL. */
  src?: string
  /** Image alternative text (defaults to an empty string). */
  alt?: string
  /** Aspect ratio (defaults to `'4/3'` at read time). */
  aspect?: ListingCardMediaProps['aspect']
  /** Overlay node — `[slot=overlay]` projected content, or a node template input. */
  overlay?: unknown
  /** Extra classes. */
  className?: string

  /** The `<img>` fallback (in the page, with its source, only while React renders it). */
  @ViewChild('img', { static: true }) private img?: ElementRef<HTMLImageElement>
  /** The capture of the caller's node; its content renders in its place. */
  @ViewChild('childrenSlot', { static: true }) private childrenSlot?: ElementRef<HTMLElement>
  /** The overlay wrapper (in the page only while there is an overlay). */
  @ViewChild('overlayEl', { static: true }) private overlayEl?: ElementRef<HTMLElement>

  /**
   * Renders React's `children ?? (src && <img>)` and `{overlay && <div>…}`,
   * following projected content that arrives or leaves after the first render.
   */
  constructor() {
    usePlacement((hasContent) => this.sync(hasContent))
  }

  /**
   * Applies the image, children and overlay presence.
   *
   * @param hasContent - Reads whether a slot has content.
   */
  private sync(hasContent: SlotReader): void {
    const children = this.childrenSlot?.nativeElement
    placeContentBefore(children, children)
    setElementPresent(children, false)
    const img = this.img?.nativeElement
    const showImage = !hasContent(children) && !!this.src
    // The source is set only while the <img> shows: React creates no <img> while children
    // replace it, so the browser requests no image.
    if (showImage) img?.setAttribute('src', this.src!)
    else img?.removeAttribute('src')
    setElementPresent(img, showImage)
    const overlay = this.overlayEl?.nativeElement
    setElementPresent(overlay, this.overlayTemplate() !== null || hasContent(overlay))
  }

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

  /**
   * The root class list (React's `cm.cn(cm.position('relative'), className)`).
   * @returns The class string.
   */
  classes(): string {
    return this.cm().cn(this.cm().position('relative'), this.className)
  }

  /**
   * The overlay layer's class list (React's
   * `cm.cn(cm.position('absolute'), cm.inset0)`).
   * @returns The class string.
   */
  overlayClasses(): string {
    return this.cm().cn(this.cm().position('absolute'), this.cm().inset0)
  }

  /**
   * The aspect ratio (React's `aspect.replace('/', ' / ')`).
   * @returns The CSS aspect-ratio value.
   */
  aspectText(): string {
    return (this.aspect ?? '4/3').replace('/', ' / ')
  }

  /**
   * The overlay node template, when the overlay arrived as a node-valued prop.
   * @returns The template, or null when none was passed.
   */
  overlayTemplate(): TemplateRef<unknown> | null {
    return isTemplate(this.overlay) ? this.overlay : null
  }
}

ListingCard

ListingCard component.

Product / property / listing card shell. The host IS the Card div (div[mol-listing-card]), matching React's root exactly: the Card's ClassMap classes (cm.card({ variant: 'elevated' }) + cm.cardPadding('md')

  • cm.cursorPointer when a click handler is supplied + className), data-mol-id and the click handler live on it. The children are projected content, stacked vertically in the inner div (cm.stack(0)).

A supplied toggle renders the counted card action after the content, outside any links in it — React's <ListingCardActions><ListingCardToggle> pair, in the same DOM order.

@Component({
  selector: 'div[mol-listing-card]',
  standalone: true,
  imports: [ListingCardActions, ListingCardToggle],
  inputs: ['toggle', 'onClick', 'className', 'dataMolId'],
  host: {
    '[class]': 'classes()',
    '[attr.data-mol-id]': 'dataMolId ?? null',
    '(click)': 'handleClick($event)',
  },
  // prettier-ignore
  template: `<div [class]="stackClasses()"><ng-content></ng-content>@if (toggle) {<div mol-listing-card-actions><button mol-listing-card-toggle [value]="toggle.value" [commit]="toggle.commit" [activeLabel]="toggle.activeLabel" [inactiveLabel]="toggle.inactiveLabel" [onError]="toggle.onError" [dataMolId]="toggle.dataMolId" [variant]="toggle.variant" [color]="toggle.color" [size]="toggle.size" [fullWidth]="toggle.fullWidth ?? false" [disabled]="toggle.disabled ?? false" [className]="toggle.className" [testId]="toggle.testId" [automationId]="toggle.automationId"></button></div>}</div>`,
})
export class ListingCard {
  /** Renders a counted native toggle after the content, outside any links in the content. */
  toggle?: ListingCardProps['toggle']
  /** Click handler; receives the DOM click event (React's `onClick`). */
  onClick?: ListingCardProps['onClick']
  /** Extra classes on the outer Card. */
  className?: string
  /** `data-mol-id` for AI-agent selectors. */
  dataMolId?: string

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

  /**
   * Computes the root class list (React's Card classes, with React's
   * `cm.cursorPointer` for a clickable card and the caller's `className`).
   * @returns The class string.
   */
  classes(): string {
    const cm = this.cm()
    return cm.cn(
      cm.card({ variant: 'elevated' }),
      cm.cardPadding('md'),
      cm.cn(this.onClick ? cm.cursorPointer : undefined, this.className),
    )
  }

  /**
   * The inner stack's class list (React's `cm.stack(0)`).
   * @returns The class string.
   */
  stackClasses(): string {
    return this.cm().stack(0)
  }

  /**
   * Forwards the click to the React-named handler with the native event.
   * @param event - The native click event.
   */
  handleClick(event: MouseEvent): void {
    this.onClick?.(event)
  }
}

ListingGrid

ListingGrid component.

Grid for listing cards — a fixed columns-column grid at every viewport width. The host IS the root div (div[mol-listing-grid]), matching React's root exactly; the cards are the element's default projected content.

@Component({
  selector: 'div[mol-listing-grid]',
  standalone: true,
  inputs: ['columns', 'gap', 'className'],
  host: {
    '[class]': 'classes()',
  },
  template: '<ng-content></ng-content>',
})
export class ListingGrid {
  /** Column count, fixed at every viewport width (defaults to `3` at read time). */
  columns?: ListingGridProps['columns']
  /** Gap between cards (defaults to `'md'` at read time). */
  gap?: ListingGridProps['gap']
  /** Extra classes. */
  className?: string

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

  /**
   * Computes the grid class list (React's `cm.grid({ cols, gap })` plus the
   * extra `className`).
   * @returns The class string.
   */
  classes(): string {
    return this.cm().cn(
      this.cm().grid({ cols: this.columns ?? 3, gap: this.gap ?? 'md' }),
      this.className,
    )
  }
}

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.0.1
  • @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

  • ListingCard's (click) handler is attached to the WHOLE card — clicks on buttons inside <ListingCardActions> bubble into it. Call event.stopPropagation() in every action handler (as in the example) or the card navigation fires too.

  • ListingGrid renders a fixed columns-column grid (via cm.grid); override className for a custom grid.

  • overlay content is rendered inside an absolutely-positioned inset-0 layer; give interactive overlays their own pointer handling and stopPropagation.

  • Styling resolves through getClassMap(); the shell reproduces <Card>'s classes from @molecule/app-ui — wire a ClassMap bond first.