@molecule/app-safe-link-angular

Feature · safe-link · App (browser) · v1.1.2 · Apache-2.0

SafeLink Angular router wrapper that appends a #top fragment when target matches current path. Avoids dead-link probes that return no URL/DOM mutation on same-path clicks.

npm install @molecule/app-safe-link-angular

npm · Source on GitHub

How it works

@molecule/app-safe-link-angular is a ready-made safe-link 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 { SafeLink } from '@molecule/app-safe-link-angular'

@Component({
  selector: 'app-nav',
  standalone: true,
  imports: [SafeLink],
  template: `<a mol-safe-link to="/dashboard">Dashboard</a>`,
})
export class NavComponent {}

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

Reference

@molecule/app-safe-link-angular — an in-app anchor that appends a #top fragment when the destination matches the current path, so a same-path click still produces an observable URL change. Useful for nav primitives exercised by behavioural verifiers / e2e probes. Angular counterpart of @molecule/app-safe-link-react: the same props (React names) and the same DOM for the same input.

Note: "safe" refers to dead-link-probe safety only — this adds NO security behavior (no rel="noopener", no URL allowlisting). All other attributes pass through unchanged.

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 { SafeLink } from '@molecule/app-safe-link-angular'

@Component({
  selector: 'app-nav',
  standalone: true,
  imports: [SafeLink],
  template: `<a mol-safe-link to="/dashboard">Dashboard</a>`,
})
export class NavComponent {}

Type

feature

Installation

npm install @molecule/app-safe-link-angular @angular/common @angular/core @angular/platform-browser @molecule/app-angular @molecule/app-routing @molecule/app-ui @molecule/app-ui-angular

API

Interfaces

SafeLinkProps

Props accepted by SafeLink — the same props @molecule/app-safe-link-react accepts. The link's content is projected content in Angular (an input cannot carry a node), so children is typed unknown: pass it in the host template, not as a value.

interface SafeLinkProps {
  /**
   * Destination: a string path only (`/settings`, `/blog/`), as with react-router's
   * `<Link to>` (no partial-Path objects). Rendered as the anchor's `href`, with a
   * `#top` fragment appended when it matches the current path.
   */
  to?: string
  /**
   * Replace the current history entry instead of pushing one. When omitted, a
   * link to the location already shown replaces it and any other link pushes,
   * as with react-router's `<Link>`.
   */
  replace?: boolean
  /** State stored with the new history entry (read back with `useLocation()`). */
  state?: unknown
  /** Extra classes on the anchor. */
  className?: string
  /** The link's content, projected as the element's default content. */
  children?: unknown
  /** The anchor's click listener; the in-app navigation runs after it, like react-router's `<Link onClick>`. */
  onClick?: unknown
}

Classes

SafeLink component — the Angular counterpart of @molecule/app-safe-link-react's SafeLink: the same props (React names) and the same DOM for the same input.

<SafeLink> renders an in-app anchor with react-router <Link>'s click rules (followRouterLink from @molecule/app-routing, the rule the binding's Link follows), but if its destination matches the current path it appends a #top fragment so clicking still produces an observable URL change.

Avoids "dead-link" probes that return no URL/DOM mutation when a same-path link is clicked — useful for nav primitives that get exercised by behavioural verifiers / e2e probes.

The host IS the <a> (a[mol-safe-link]), matching React's root exactly. The link's content is projected (the element's default content), and every other attribute (id, target, rel, aria-*, data-*) is written on the host anchor as usual.

@Component({
  selector: 'a[mol-safe-link]',
  standalone: true,
  inputs: ['to', 'replace', 'state', 'className'],
  host: {
    '[attr.href]': 'target()',
    '[class]': 'className',
  },
  template: `<ng-content></ng-content>`,
})
export class SafeLink implements OnInit {
  /** The destination: a string app path (`/settings`, `/blog/`). */
  to = ''
  /** Replace the current history entry instead of pushing one. */
  replace?: boolean
  /** State stored with the new history entry. */
  state?: unknown
  /** Extra classes on the anchor. */
  className?: string

  private readonly location = useLocation()
  private readonly router = useRouterInstance()
  private readonly element = inject<ElementRef<HTMLAnchorElement>>(ElementRef)
  private readonly renderer = inject(Renderer2)
  private readonly destroyRef = inject(DestroyRef)

  /**
   * The anchor's destination with the same-path `#top` fragment applied (a
   * destination matching the current path — or the current path plus a
   * trailing slash — gets `#top`, its own trailing slash stripped first).
   * @returns The rendered `href`.
   */
  target(): string {
    const to = this.to
    const pathname = this.location().pathname
    const samePath = pathname === to || pathname + '/' === to
    return samePath ? `${to.replace(/\/$/, '')}#top` : to
  }

  /**
   * Starts handling clicks. The listener is added here, after the host
   * element's own `(click)` bindings were created, so those run first and can
   * prevent the navigation (react-router calls `onClick` first too).
   */
  ngOnInit(): void {
    const stop = this.renderer.listen(this.element.nativeElement, 'click', (event: MouseEvent) => {
      followRouterLink(event, this.target(), this.router, {
        replace: this.replace,
        state: this.state,
      })
    })
    this.destroyRef.onDestroy(stop)
  }
}

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.11.0 || ^4.0.0
  • @molecule/app-routing ^1.3.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-routing

  • @molecule/app-ui

  • @molecule/app-ui-angular

  • Must render inside the molecule router (provideRouter / provideMolecule({ router })) — useLocation() throws otherwise. The click rules are followRouterLink from @molecule/app-routing, the rule the binding's Link follows (react-router <Link>'s contract). Do not use in apps on a different router.

  • to accepts a string path only (no partial-Path objects); the link's content is the element's default projection.

  • Use the NAMED import (import { SafeLink } from …); the package barrel does not re-export the file's default export.

  • Internal routes only — for external URLs use a plain <a> (with rel="noopener noreferrer" when target="_blank").