@molecule/app-announcement-bar-angular

Feature · announcement-bar · App (browser) · v1.1.2 · Apache-2.0

Persistent banner for announcements, promos, feature callouts, with dismiss + CTA

npm install @molecule/app-announcement-bar-angular

npm · Source on GitHub

How it works

@molecule/app-announcement-bar-angular is a ready-made announcement-bar 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 { AnnouncementBar } from '@molecule/app-announcement-bar-angular'

@Component({
  selector: 'app-page',
  standalone: true,
  imports: [AnnouncementBar],
  template: `
    <div
      mol-announcement-bar
      [kind]="'promo'"
      [icon]="'🎉'"
      [action]="{ label: 'Learn more', href: '/pricing' }"
      [onDismiss]="() => onDismissed()"
      [dataMolId]="'promo-bar'"
    >
      New Pro plan — 3 months free for early adopters.
    </div>
  `,
})
export class Page {
  onDismissed(): void {
    console.log('dismissed')
  }
}

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

Reference

Angular announcement / promo bar.

Exports <AnnouncementBar> — persistent top-of-page banner with icon, message, optional action (link or button), and optional dismiss (×). Long-lived and prominent, unlike a Toast; carries an action slot + dismiss, unlike an Alert. It is the Angular counterpart of @molecule/app-announcement-bar-react: same props (React names), same DOM for the same input, same ClassMap tokens and i18n keys.

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 { AnnouncementBar } from '@molecule/app-announcement-bar-angular'

@Component({
  selector: 'app-page',
  standalone: true,
  imports: [AnnouncementBar],
  template: `
    <div
      mol-announcement-bar
      [kind]="'promo'"
      [icon]="'🎉'"
      [action]="{ label: 'Learn more', href: '/pricing' }"
      [onDismiss]="() => onDismissed()"
      [dataMolId]="'promo-bar'"
    >
      New Pro plan — 3 months free for early adopters.
    </div>
  `,
})
export class Page {
  onDismissed(): void {
    console.log('dismissed')
  }
}

Type

feature

Installation

npm install @molecule/app-announcement-bar-angular @angular/common @angular/core @angular/platform-browser @molecule/app-angular @molecule/app-ui @molecule/app-ui-angular

API

Interfaces

AnnouncementBarProps

Props accepted by the AnnouncementBar component.

The message is projected content (<ng-content>), the icon is either the icon input (text or a template ref) or a [slot=icon] projection, and onDismiss is the onDismiss input.

interface AnnouncementBarProps {
  /** Optional leading icon — text, a template ref, or `[slot=icon]` content. */
  icon?: TemplateRef<unknown> | string | null
  /**
   * Optional call-to-action (link or button). Node props are templated
   * content in Angular, so the label is typed `unknown`: pass a plain string
   * or an `<ng-template>` ref.
   */
  action?: {
    /** The action's label — text or a template ref. */
    label: unknown
    /** Rendering as a link: the action is an `<a href>` instead of a `<button>`. */
    href?: string
    /**
     * Called when the action button is clicked (ignored when `href` is set).
     * React's DOM click handler receives the click event, so the callback may
     * take it.
     */
    onClick?: (event?: MouseEvent) => void
  } | null
  /** Semantic kind — emitted as `data-kind` on the root (style via CSS/`className`). Defaults to `'info'`. */
  kind?: AnnouncementKind
  /** Show a dismiss (×) button. Defaults to true. */
  dismissible?: boolean
  /** Called when the bar is dismissed. */
  onDismiss?: () => void
  /** Controlled visibility — when provided, overrides internal state. */
  visible?: boolean
  /** Extra classes. */
  className?: string
  /** `data-mol-id` for AI-agent selectors. */
  dataMolId?: string
}

Types

AnnouncementKind

Semantic kind, exposed as a data-kind attribute for per-kind styling (no built-in style change).

type AnnouncementKind = 'info' | 'success' | 'warning' | 'error' | 'promo'

Classes

AnnouncementBar

AnnouncementBar component.

Persistent announcement banner — product updates, promos, outage notices, feature callouts. Long-lived and prominent, unlike a Toast; carries an action + dismiss, unlike an Alert. The host IS the root <div> (div[mol-announcement-bar]), matching React's root exactly: role, data-kind and data-mol-id live on it.

React renders null while the bar is hidden — Angular's host element always exists, so useHostPresence takes it out of the page in that state (no element is left behind, matching React's null) and puts it back when the controlled visible input or the internal dismissal state shows it again.

kind is exposed as data-kind only; it does not change the bar's colors by itself. The dismiss label comes from useTranslation() (announcement.dismiss), read reactively so a locale switch refreshes it.

@Component({
  selector: 'div[mol-announcement-bar]',
  standalone: true,
  imports: [NgTemplateOutlet],
  inputs: [
    'icon',
    'action',
    'kind',
    'dismissible',
    'onDismiss',
    'visible',
    'className',
    'dataMolId',
  ],
  host: {
    role: 'status',
    '[attr.data-mol-id]': 'dataMolId ?? null',
    '[attr.data-kind]': 'kind ?? "info"',
    '[class]': 'classes()',
  },
  template: `<div [class]="rowClasses()">
      <ng-content select="[slot=icon]"></ng-content>
      @if (iconTemplate(); as tpl) {
        <ng-container [ngTemplateOutlet]="tpl" />
      } @else if (hasIconText()) {
        <ng-container>{{ textOf(icon) }}</ng-container>
      }
      <span [class]="messageClasses()"><ng-content></ng-content></span>
    </div>
    <div [class]="rowClasses()">
      @if (action?.href) {
        <a [attr.href]="action.href" [class]="actionClasses()">
          @if (labelTemplate(); as tpl) {
            <ng-container [ngTemplateOutlet]="tpl" />
          } @else {
            <ng-container>{{ textOf(action.label) }}</ng-container>
          }
        </a>
      } @else if (action) {
        <button type="button" (click)="runAction($event)" [class]="actionClasses()">
          @if (labelTemplate(); as tpl) {
            <ng-container [ngTemplateOutlet]="tpl" />
          } @else {
            <ng-container>{{ textOf(action.label) }}</ng-container>
          }
        </button>
      }
      @if (dismissible ?? true) {
        <button
          type="button"
          (click)="dismiss()"
          [attr.aria-label]="dismissLabel()"
          [class]="dismissClasses()"
        >
          ×
        </button>
      }
    </div>`,
})
export class AnnouncementBar {
  /** Optional leading icon: text, a template ref, or `[slot=icon]` content. */
  icon?: TemplateRef<unknown> | string | null
  /** Optional call-to-action (link or button). */
  action?: NonNullable<AnnouncementBarProps['action']> | null
  /** Semantic kind (defaults to `'info'` at read time). */
  kind?: AnnouncementKind
  /** Show a dismiss (×) button (defaults to true at read time). */
  dismissible?: boolean
  /** Called when the bar is dismissed (React's `onDismiss`; takes no arguments). */
  onDismiss?: () => void
  /** Controlled visibility — when provided, overrides internal state. */
  visible?: boolean
  /** Extra classes. */
  className?: string
  /** `data-mol-id` for AI-agent selectors. */
  dataMolId?: string

  /**
   * Reactive translation, read in the injection context. Its `t` reads the
   * locale, so the view re-renders the text on a locale switch (the static `t`
   * does not).
   */
  private readonly i18n = useTranslation()
  /** Whether the bar is visible when `visible` is not given (React's `useState`). */
  private readonly internalVisible = signal(true)

  constructor() {
    // Leaves no element in the page while the bar is hidden (React renders null).
    useHostPresence(() => this.isVisible())
  }

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

  /**
   * The wrapper class list (React's `cm.cn(cm.flex({…}), cm.sp('px', 4), cm.sp('py', 2), className)`).
   * @returns The class string.
   */
  classes(): string {
    return this.cm().cn(
      this.cm().flex({ align: 'center', justify: 'between', gap: 'md', wrap: 'wrap' }),
      this.cm().sp('px', 4),
      this.cm().sp('py', 2),
      this.className,
    )
  }

  /**
   * The inner rows' class (React's `cm.flex({ align: 'center', gap: 'sm' })`).
   * @returns The class string.
   */
  rowClasses(): string {
    return this.cm().flex({ align: 'center', gap: 'sm' })
  }

  /**
   * The message span's class (React's `cm.textSize('sm')`).
   * @returns The class string.
   */
  messageClasses(): string {
    return this.cm().textSize('sm')
  }

  /**
   * The action's class list (React's
   * `cm.cn(cm.textSize('sm'), cm.fontWeight('semibold'), cm.link)`).
   * @returns The class string.
   */
  actionClasses(): string {
    return this.cm().cn(this.cm().textSize('sm'), this.cm().fontWeight('semibold'), this.cm().link)
  }

  /**
   * The dismiss button's class list (React's
   * `cm.cn(cm.w(6), cm.h(6), cm.flex({ align: 'center', justify: 'center' }))`).
   * @returns The class string.
   */
  dismissClasses(): string {
    return this.cm().cn(
      this.cm().w(6),
      this.cm().h(6),
      this.cm().flex({ align: 'center', justify: 'center' }),
    )
  }

  /**
   * The text fallback used when a value is not a template.
   *
   * @param value - The prop value.
   * @returns The text to render.
   */
  textOf(value: unknown): string {
    return textOf(value)
  }

  /**
   * The icon as a template, when the caller passed a node.
   * @returns The icon template, or null.
   */
  iconTemplate(): TemplateRef<unknown> | null {
    return isTemplate(this.icon) ? this.icon : null
  }

  /**
   * Whether the icon input renders as text (React renders any non-empty
   * string node; `''` and `null` render nothing).
   * @returns True when the icon text renders.
   */
  hasIconText(): boolean {
    return typeof this.icon === 'string' ? this.icon !== '' : typeof this.icon === 'number'
  }

  /**
   * The action label as a template, when the caller passed a node.
   * @returns The label template, or null.
   */
  labelTemplate(): TemplateRef<unknown> | null {
    return isTemplate(this.action?.label) ? this.action.label : null
  }

  /**
   * The dismiss button's accessible label (React's `announcement.dismiss`).
   * @returns The label text.
   */
  dismissLabel(): string {
    return this.i18n.t('announcement.dismiss', {}, { defaultValue: 'Dismiss' })
  }

  /**
   * Whether the bar renders (React's `visible ?? internalVisible`).
   * @returns True while visible.
   */
  isVisible(): boolean {
    return this.visible ?? this.internalVisible()
  }

  /**
   * Hides the bar (internal state, unless `visible` controls it) and fires
   * `onDismiss` — React's `dismiss`.
   */
  dismiss(): void {
    if (this.visible === undefined) this.internalVisible.set(false)
    this.onDismiss?.()
  }

  /**
   * Calls the action's `onClick` with the click event, as React's DOM handler
   * does.
   *
   * @param event - The native click event.
   */
  runAction(event: MouseEvent): void {
    this.action?.onClick?.(event)
  }
}

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.12.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

kind is exposed as a data-kind attribute on the root — it does NOT change the bar's colors by itself; style per-kind via className or a [data-kind="…"] selector. Dismissal is uncontrolled by default (internal state; the bar stays hidden until remount) — bind visible to control it, e.g. to persist dismissal per user. dismissible defaults to true. There is no default data-mol-id; bind dataMolId so agents/E2E can target the bar. The message is projected content and the icon is either the icon input (text or an <ng-template> ref) or [slot=icon] content. The dismiss label comes from the companion @molecule/app-locales-announcement-bar locale bond.

Translations

Translation strings are provided by @molecule/app-locales-announcement-bar.