@molecule/app-floating-action-button-angular

Feature · floating-action-button · App (browser) · v1.1.2 · Apache-2.0

Fixed-position FAB with icon, tooltip, link-or-callback

npm install @molecule/app-floating-action-button-angular

npm · Source on GitHub

How it works

@molecule/app-floating-action-button-angular is a ready-made floating-action-button 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 { FloatingActionButton } from '@molecule/app-floating-action-button-angular'

@Component({
  selector: 'app-create-fab',
  standalone: true,
  imports: [FloatingActionButton],
  template: `
    <div
      mol-floating-action-button
      label="Create new item"
      position="bottom-right"
      [onClick]="openCreate"
    >
      <span slot="icon" aria-hidden="true">+</span>
    </div>
  `,
})
export class CreateFab {
  readonly createOpen = signal(false)

  openCreate(): void {
    this.createOpen.set(true)
  }
}

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

Reference

Angular floating action button (FAB) — the counterpart of @molecule/app-floating-action-button-react: the same props and the same DOM for the same input.

Exports <FloatingActionButton> — a fixed-position circular button (or anchor when href is set) with an icon slot, native-title tooltip, and four corner-anchoring positions.

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 { FloatingActionButton } from '@molecule/app-floating-action-button-angular'

@Component({
  selector: 'app-create-fab',
  standalone: true,
  imports: [FloatingActionButton],
  template: `
    <div
      mol-floating-action-button
      label="Create new item"
      position="bottom-right"
      [onClick]="openCreate"
    >
      <span slot="icon" aria-hidden="true">+</span>
    </div>
  `,
})
export class CreateFab {
  readonly createOpen = signal(false)

  openCreate(): void {
    this.createOpen.set(true)
  }
}

Type

feature

Installation

npm install @molecule/app-floating-action-button-angular @angular/common @angular/core @angular/platform-browser @molecule/app-angular @molecule/app-ui @molecule/app-ui-angular

API

Interfaces

FloatingActionButtonProps

Props accepted by FloatingActionButton — the same props @molecule/app-floating-action-button-react accepts. The node prop icon is projected content in Angular (an input cannot carry a node), so it is typed unknown: pass it in the host template as [slot=icon] content, not as a value.

interface FloatingActionButtonProps {
  /** Icon rendered inside the FAB, projected as `[slot=icon]` content. */
  icon?: unknown
  /** Accessible label / tooltip text (usually `t('...')`). */
  label: string
  /** Click handler (mutually exclusive with `href`). */
  onClick?: (event: MouseEvent) => void
  /** Navigation target (mutually exclusive with `onClick`). */
  href?: string
  /** Screen corner for anchoring. Defaults to `'bottom-right'`. */
  position?: 'bottom-right' | 'bottom-left' | 'top-right' | 'top-left'
  /** Visual size. */
  size?: 'sm' | 'md' | 'lg'
  /** Whether to show the label on hover. Defaults to true. */
  showTooltip?: boolean
  /** Extra classes. */
  className?: string
}

Classes

FloatingActionButton

FloatingActionButton component.

Fixed-position circular action button. Renders either an anchor (when href is set) or a button (when onClick is set). Positioning is inline-style so the component works without extra CSS setup, and it ships with a visible default surface (primary gradient + soft elevation, via the gradientPrimary/shadowLifted ClassMap tokens). Pass className to override the surface — caller classes are resolved last by cm.cn (tailwind-merge).

The rendered root is the <a> when href is supplied and the <button> otherwise — a standalone component's host tag cannot change at runtime, so the rendered root stands in front of the host's place and the host itself leaves the page (placeContentBefore + setElementPresent). The projected [slot=icon] content is placed into that root, matching React's bare {icon} position.

@Component({
  selector: 'div[mol-floating-action-button]',
  standalone: true,
  imports: [Link],
  hostDirectives: [MolPlacement],
  inputs: ['icon', 'label', 'onClick', 'href', 'position', 'size', 'showTooltip', 'className'],
  // prettier-ignore
  template: `@if (isLink()) {<a #linkRoot [molLink]="href ?? ''" [attr.aria-label]="ariaLabel()" [attr.title]="titleAttr()" [attr.class]="commonClass()" [style]="rootStyleText()"><span data-icon-anchor></span></a>} @else {<button #buttonRoot type="button" [attr.aria-label]="ariaLabel()" [attr.title]="titleAttr()" [attr.class]="commonClass()" [style]="rootStyleText()" (click)="onClick?.($event)"><span data-icon-anchor></span></button>}<span #iconCapture style="display: none"><ng-content select="[slot=icon]"></ng-content></span>`,
})
export class FloatingActionButton {
  /** Icon — projected `[slot=icon]` content (an input cannot carry a node). */
  icon?: unknown
  /** Accessible label (also the tooltip text and i18n key). */
  label?: string
  /** Click handler — React's `onClick`; receives the click event. */
  onClick?: (event: MouseEvent) => void
  /** Link destination. An app path navigates through the mounted router. */
  href?: string
  /** Screen corner (defaults to `'bottom-right'` at read time). */
  position?: 'bottom-right' | 'bottom-left' | 'top-right' | 'top-left'
  /** Visual size (defaults to `'md'` at read time). */
  size?: 'sm' | 'md' | 'lg'
  /** Whether to show the label on hover (defaults to true at read time). */
  showTooltip?: boolean
  /** Extra classes on the root element. */
  className?: string

  /** The icon slot's capture span (kept out of the DOM; its content is the icon). */
  @ViewChild('iconCapture') iconCapture?: ElementRef<HTMLSpanElement>

  /** The `<a>` root, while `href` is set. */
  @ViewChild('linkRoot') private linkRoot?: ElementRef<HTMLElement>
  /** The `<button>` root, while `href` is not set. */
  @ViewChild('buttonRoot') private buttonRoot?: ElementRef<HTMLElement>

  /** The host element (React renders no element of its own). */
  private readonly el = inject(ElementRef)

  /**
   * Reactive translation, read in the injection context. Its `t` reads the
   * locale, so the label re-renders on a locale switch (the static `t` does not).
   */
  private readonly i18n = useTranslation()

  /** The icon anchor, kept once it leaves the DOM so later content is placed too. */
  private anchor: HTMLElement | null = null

  constructor() {
    // React's root is the `<a>`/`<button>` itself: it renders in front of the host's place
    // and the host leaves the page; the icon content then goes into that root.
    usePlacement(() => {
      const host = this.el.nativeElement as HTMLElement
      placeContentBefore(host, host)
      setElementPresent(host, false)
      this.placeIcon()
    })
  }

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

  /**
   * Whether the root renders as an `<a>` (React's `href ?` branch).
   * @returns True when `href` is set.
   */
  isLink(): boolean {
    return !!this.href
  }

  /**
   * The size in pixels (React's `SIZE_MAP[size] * 4` with `'md'` default).
   * @returns The square size in pixels.
   */
  dim(): number {
    return SIZE_MAP[this.size ?? 'md'] * 4
  }

  /**
   * The root's inline style, exactly as React writes it: the corner offsets,
   * then the square size, then the stacking level.
   * @returns The `style` attribute text.
   */
  rootStyleText(): string {
    const [x, y] = CORNER[this.position ?? 'bottom-right']
    const dim = this.dim()
    return `position: fixed; ${x}: 24px; ${y}: 24px; width: ${dim}px; height: ${dim}px; z-index: 40`
  }

  /**
   * The root's class list (default surface + the caller's extra classes).
   * @returns The class string.
   */
  commonClass(): string {
    const cm = this.cm()
    return cm.cn(
      cm.flex({ align: 'center', justify: 'center' }),
      cm.roundedFull,
      cm.gradientPrimary,
      cm.shadowLifted,
      this.className,
    )
  }

  /**
   * The root's accessible label (React's `t(label, {}, { defaultValue: label })`).
   * @returns The label text.
   */
  ariaLabel(): string {
    const label = this.label ?? ''
    return this.i18n.t(label, {}, { defaultValue: label })
  }

  /**
   * The tooltip `title` attribute, or null when `showTooltip` is false.
   * @returns The title text, or null.
   */
  titleAttr(): string | null {
    return (this.showTooltip ?? true) ? this.ariaLabel() : null
  }

  /**
   * Places the icon slot's content into the current root — React renders
   * `{icon}` bare there. Runs on every placement so a branch switch
   * (button ↔ link) that re-created the root gets the content again. The root is
   * found through its template ref, not the host: the root stands beside the
   * host, not inside it.
   */
  private placeIcon(): void {
    const root = (this.linkRoot?.nativeElement ?? this.buttonRoot?.nativeElement) as
      HTMLElement | undefined
    const anchor = root?.querySelector<HTMLElement>('[data-icon-anchor]') ?? this.anchor
    const capture = this.iconCapture?.nativeElement
    if (!root || !anchor || !capture) return
    this.anchor = anchor
    if (anchor.parentNode === null) setElementPresent(anchor, true)
    const parent = anchor.parentElement
    if (parent) placeContent(capture, parent, anchor)
    setElementPresent(anchor, false)
    setElementPresent(capture, false)
  }
}

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.15.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 FAB ships with a visible default surface — a primary gradient plus soft elevation (the gradientPrimary + shadowLifted ClassMap tokens, which also supply a contrasting on-primary icon color) — so an out-of-the-box FAB is prominent in both light and dark themes with no extra styling. Pass className to override it (e.g. cm.surface); caller classes are merged last (tailwind-merge) so they win over the default.

label doubles as an i18n key: it is resolved through t(label, {}, { defaultValue: label }), so passing a translation key localizes the aria-label / tooltip, and plain English strings still work as their own fallback. The tooltip is the native title attribute (no styled tooltip component).

href and onClick are mutually exclusive — when href is set an <a> renders and onClick is ignored. The icon prop is projected content ([slot=icon]); every other prop keeps its React name.