@molecule/app-settings-actions-bar-angular

Feature · settings-actions-bar · App (browser) · v1.1.3 · Apache-2.0

Sticky Save/Cancel bar with "Saved" timestamp + loading state for settings and form pages

npm install @molecule/app-settings-actions-bar-angular

npm · Source on GitHub

How it works

@molecule/app-settings-actions-bar-angular is a ready-made settings-actions-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, signal } from '@angular/core'
import { SettingsActionsBar } from '@molecule/app-settings-actions-bar-angular'

@Component({
  selector: 'app-profile-actions',
  standalone: true,
  imports: [SettingsActionsBar],
  template: `
    <div
      mol-settings-actions-bar
      [onSave]="save"
      [onCancel]="reset"
      [loading]="saving()"
      [savedAt]="savedAt()"
    ></div>
  `,
})
export class ProfileActions {
  saving = signal(false)
  savedAt = signal<number | null>(null)
  save = async (): Promise<void> => {
    this.saving.set(true)
    try {
      await this.api.saveProfile()
      this.savedAt.set(Date.now())
    } finally {
      this.saving.set(false)
    }
  }
  reset = (): void => this.form.reset()
}

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

Reference

Angular sticky Save/Cancel bar with "Saved" timestamp + loading state for settings and form pages.

Exports SettingsActionsBar. Props: onSave (required; sync or async), onCancel? (Cancel hidden when omitted), loading? (disables Save and shows "Saving…"), disabled?, savedAt? (epoch ms → "Saved 3m ago"), error? (inline text, or [slot=error] content), sticky? (default true), leading? ([slot=leading] content before the status), className?.

This is the Angular counterpart of @molecule/app-settings-actions-bar-react: the same props (React names) and the same DOM for the same input.

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, signal } from '@angular/core'
import { SettingsActionsBar } from '@molecule/app-settings-actions-bar-angular'

@Component({
  selector: 'app-profile-actions',
  standalone: true,
  imports: [SettingsActionsBar],
  template: `
    <div
      mol-settings-actions-bar
      [onSave]="save"
      [onCancel]="reset"
      [loading]="saving()"
      [savedAt]="savedAt()"
    ></div>
  `,
})
export class ProfileActions {
  saving = signal(false)
  savedAt = signal<number | null>(null)
  save = async (): Promise<void> => {
    this.saving.set(true)
    try {
      await this.api.saveProfile()
      this.savedAt.set(Date.now())
    } finally {
      this.saving.set(false)
    }
  }
  reset = (): void => this.form.reset()
}

Type

feature

Installation

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

API

Interfaces

SettingsActionsBarProps

Props accepted by SettingsActionsBar — the same props @molecule/app-settings-actions-bar-react accepts. Node-valued props (error, leading) are projected content in Angular (an input cannot carry a node), so they are typed unknown: pass them in the host template ([slot=error] / [slot=leading]), not as values.

interface SettingsActionsBarProps {
  /** Called when Save is clicked. */
  onSave: () => void | Promise<void>
  /**
   * Called when Cancel is clicked (with the click event, as React's
   * `onClick={onCancel}` forwards it). Hides Cancel if omitted.
   */
  onCancel?: (event?: MouseEvent) => void
  /** When true, the Save button disables and shows a loading label. */
  loading?: boolean
  /** When true, the Save button is disabled without a loading label. */
  disabled?: boolean
  /** When set, shows a "Saved" badge with the relative time. Epoch ms. */
  savedAt?: number | null
  /** Error text rendered inline (a string input; nodes are `[slot=error]` content). */
  error?: unknown
  /** Sticky-to-bottom. Defaults to true. */
  sticky?: boolean
  /** Content before the buttons (projected as `[slot=leading]`). */
  leading?: unknown
  /** Extra classes. */
  className?: string
}

Classes

SettingsActionsBar

SettingsActionsBar component.

Sticky bottom / inline Save-Cancel bar for settings and forms. The host IS React's root <div> (div[mol-settings-actions-bar]), with the bar's class list and sticky inline style bound on the host.

Features:

  • Primary Save button with loading/disabled state.
  • Optional Cancel button (hidden when onCancel is omitted).
  • "Saved Xm ago" status when savedAt is provided.
  • Inline error text (a string input, or projected [slot=error] content).
  • Optional leading slot ([slot=leading]) for custom status badges.

The inline error span is an always-mounted capture point that setElementPresent takes out whenever React renders no error — the projected nodes need it in the template, but React's DOM has no such element.

@Component({
  selector: 'div[mol-settings-actions-bar]',
  standalone: true,
  hostDirectives: [MolPlacement],
  imports: [Button],
  inputs: [
    'onSave',
    'onCancel',
    'loading',
    'disabled',
    'savedAt',
    'error',
    'sticky',
    'leading',
    'className',
  ],
  host: {
    '[class]': 'classes()',
    '[style]': 'inlineStyleText()',
  },
  template: `<div [class]="cm().flex({ align: 'center', gap: 'sm' })">
      {{ leadingText() }}<ng-content select="[slot=leading]"></ng-content>
      @if (savedText() !== null) {
        <span [class]="savedClasses()">{{ savedText() }}</span>
      }
      <span #errorSlot [class]="cm().textSize('sm')"
        >{{ errorText() }}<ng-content select="[slot=error]"></ng-content
      ></span>
    </div>
    <div [class]="cm().flex({ align: 'center', gap: 'sm' })">
      @if (onCancel) {
        <button
          mol-button
          [variant]="'ghost'"
          [disabled]="loading === true"
          (click)="handleCancel($event)"
        >
          {{ t('settingsActions.cancel', {}, { defaultValue: 'Cancel' }) }}
        </button>
      }
      <button
        mol-button
        [variant]="'solid'"
        [color]="'primary'"
        [disabled]="isSaveDisabled()"
        (click)="handleSave()"
      >
        {{ saveLabel() }}
      </button>
    </div>`,
})
export class SettingsActionsBar {
  /** Called when Save is clicked. */
  onSave?: () => void | Promise<void>
  /** Called when Cancel is clicked; receives the DOM click event (React's `onClick`). */
  onCancel?: (event?: MouseEvent) => void
  /** When true, the Save button disables and shows a loading label. */
  loading?: boolean
  /** When true, the Save button is disabled without a loading label. */
  disabled?: boolean
  /** When set, shows a "Saved" badge with the relative time. Epoch ms. */
  savedAt?: number | null
  /** Inline error: a string/number renders as text; nodes are `[slot=error]` content. */
  error?: unknown
  /** Sticky-to-bottom (React's default `true`). */
  sticky?: boolean
  /** Content before the buttons (projected as `[slot=leading]`). */
  leading?: unknown
  /** Extra classes. */
  className?: string

  /** The reactive translation, read in the injection context. */
  private readonly i18n = useTranslation()

  /** The translation function for the template. */
  protected t = this.i18n.t

  constructor() {
    usePlacement((hasContent) => this.reconcile(hasContent))
  }

  /** The error span's capture wrapper (always mounted; detached when unused). */
  @ViewChild('errorSlot') errorSlot?: ElementRef<HTMLSpanElement>

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

  /**
   * Computes the bar's class list (React's `cm.cn(cm.surface, cm.borderT, …)`),
   * with `className` last.
   * @returns The class string.
   */
  classes(): string {
    return this.cm().cn(
      // A real theme surface + top border so the (sticky) bar is opaque and
      // page content no longer shows through it while scrolling. Both are
      // theme tokens, never hardcoded colors, so light/dark stay correct.
      this.cm().surface,
      this.cm().borderT,
      this.cm().flex({ align: 'center', justify: 'between', gap: 'md', wrap: 'wrap' }),
      this.cm().sp('px', 4),
      this.cm().sp('py', 3),
      this.className,
    )
  }

  /**
   * The bar's inline style text (React's `style={sticky ? {…} : undefined}`);
   * null when `sticky` is false, so no `style` attribute renders.
   * @returns The style text, or null.
   */
  inlineStyleText(): string | null {
    return this.sticky === false ? null : STICKY_STYLE
  }

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

  /**
   * The "Saved …" badge text, or null when React renders no badge
   * (`savedAt === null || savedAt === undefined`; an epoch-ms 0 still shows).
   * @returns The badge text, or null.
   */
  savedText(): string | null {
    const savedAt = this.savedAt
    if (savedAt === null || savedAt === undefined) return null
    return this.t(
      'settingsActions.saved',
      { at: formatRelativeShort(savedAt) },
      {
        defaultValue: 'Saved {{at}}',
      },
    )
  }

  /**
   * The error input's text (React's `{error}` for a text child: strings and
   * numbers render; a node arrives as `[slot=error]` content instead).
   * @returns The text, or `''` for a node error.
   */
  errorText(): string {
    return typeof this.error === 'string' || typeof this.error === 'number'
      ? String(this.error)
      : ''
  }

  /**
   * The leading input's text (React's `{leading}` for a text child).
   * @returns The text, or `''` for a node leading value.
   */
  leadingText(): string {
    return typeof this.leading === 'string' || typeof this.leading === 'number'
      ? String(this.leading)
      : ''
  }

  /**
   * The Save button's disabled state (React's `loading || disabled`).
   * @returns Whether the Save button is disabled.
   */
  isSaveDisabled(): boolean {
    return this.loading === true || this.disabled === true
  }

  /**
   * The Save button's label (React's `loading ? 'Saving…' : 'Save changes'`).
   * @returns The translated label.
   */
  saveLabel(): string {
    return this.loading
      ? this.t('settingsActions.saving', {}, { defaultValue: 'Saving…' })
      : this.t('settingsActions.save', {}, { defaultValue: 'Save changes' })
  }

  /**
   * Runs Save the way React's `() => void onSave()` does: no arguments.
   */
  handleSave(): void {
    void this.onSave?.()
  }

  /**
   * Forwards Cancel the way React's `onClick={onCancel}` does: with the event.
   *
   * @param event - The DOM click event.
   */
  handleCancel(event: MouseEvent): void {
    this.onCancel?.(event)
  }

  /**
   * Applies React's error branch: the span stays in the DOM only while there is
   * an error input or a projected error node. The `{{ errorText() }}` text node
   * is the wrapper's own first child and is skipped.
   *
   * @param hasContent - Reads whether a slot has content.
   */
  private reconcile(hasContent: SlotReader): void {
    const el = this.errorSlot?.nativeElement
    if (!el) return
    setElementPresent(el, this.errorText().length > 0 || hasContent(el, 1))
  }
}

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

  • The bar ships with an opaque theme surface (cm.surface) + top border (cm.borderT), so when sticky is on, page content no longer shows through it while scrolling. Pass className to layer on more styling.

  • The "Saved …" time is English-shorthand ("3m ago"), computed once per render — it does not tick while mounted; re-render (or bump savedAt) to refresh it.

  • error renders as plain unstyled text — style/color the node yourself.

  • Requires a bonded ClassMap and i18n provider — rendering throws otherwise. Translations: @molecule/app-locales-settings-actions-bar.

Translations

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