@molecule/app-price-range-slider-angular

Feature · price-range-slider · App (browser) · v1.1.4 · Apache-2.0

Single- or dual-handle range slider with formatted labels

npm install @molecule/app-price-range-slider-angular

npm · Source on GitHub

How it works

@molecule/app-price-range-slider-angular is a ready-made price-range-slider 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 { PriceRangeSlider } from '@molecule/app-price-range-slider-angular'

@Component({
  selector: 'price-filter',
  standalone: true,
  imports: [PriceRangeSlider],
  template: `
    <div
      mol-price-range-slider
      [min]="0"
      [max]="1000"
      [value]="[50, 500]"
      [step]="10"
      [onChange]="setRange"
    >
      <strong slot="label">Price range</strong>
    </div>
  `,
})
export class PriceFilter {
  readonly setRange = (value: [number, number]): void => {
    console.log('price range', value)
  }
}

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

Reference

Angular numeric min/max range picker — the Angular counterpart of @molecule/app-price-range-slider-react: the same props and the same DOM for the same input.

Exports <PriceRangeSlider> — TWO native <input type="range"> controls rendered side by side (min slider + max slider) with formatted endpoint labels. The handles clamp against each other so low <= high always holds. It is not a single-track dual-thumb slider.

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 { PriceRangeSlider } from '@molecule/app-price-range-slider-angular'

@Component({
  selector: 'price-filter',
  standalone: true,
  imports: [PriceRangeSlider],
  template: `
    <div
      mol-price-range-slider
      [min]="0"
      [max]="1000"
      [value]="[50, 500]"
      [step]="10"
      [onChange]="setRange"
    >
      <strong slot="label">Price range</strong>
    </div>
  `,
})
export class PriceFilter {
  readonly setRange = (value: [number, number]): void => {
    console.log('price range', value)
  }
}

Type

feature

Installation

npm install @molecule/app-price-range-slider-angular @angular/common @angular/core @angular/platform-browser @molecule/app-angular @molecule/app-ui @molecule/app-ui-angular

API

Interfaces

PriceRangeSliderProps

Props accepted by PriceRangeSlider — the same props @molecule/app-price-range-slider-react accepts. Node props (label) 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 PriceRangeSliderProps {
  /** Minimum possible value. */
  min: number
  /** Maximum possible value. */
  max: number
  /** Current [low, high] tuple. If only one value is relevant, set low === min. */
  value: [number, number]
  /** Called whenever either handle moves. */
  onChange: (value: [number, number]) => void
  /** Step increment. Defaults to 1. */
  step?: number
  /** Called to format each endpoint label — defaults to USD currency formatting. */
  formatValue?: (n: number) => string
  /** Optional label above, projected as `[slot=label]` content (or a string input). */
  label?: unknown
  /** Extra classes. */
  className?: string
}

Classes

PriceRangeSlider

PriceRangeSlider component.

Dual-handle numeric range slider using two native <input type="range"> controls. Apps supply min/max/step and a formatValue to render the endpoint labels (typical use: currency-formatted prices). The handles clamp against each other so low <= high always holds. The host IS the outer <div> (div[mol-price-range-slider]), matching React's root exactly. The optional label is projected [slot=label] content (or a string input).

Angular projection needs an always-mounted <ng-content> capture point, so the label wrapper lives in the template unconditionally and PriceRangeSlider.reconcile reconciles it against React's DOM (which omits the <span> entirely when there is no label).

@Component({
  selector: 'div[mol-price-range-slider]',
  standalone: true,
  hostDirectives: [MolPlacement],
  imports: [],
  inputs: ['min', 'max', 'value', 'step', 'formatValue', 'label', 'className', 'onChange'],
  host: {
    '[class]': 'classes()',
  },
  template: `<span #labelWrap [class]="labelClasses()"
      ><ng-content select="[slot=label]"></ng-content>{{ labelText() }}</span
    >
    <div [class]="rowClasses()">
      <span [class]="endpointClasses()">{{ fmt(low()) }}</span
      ><input
        #minInput
        type="range"
        [attr.aria-label]="minLabel()"
        [attr.min]="min"
        [attr.max]="max"
        [attr.step]="stepValue()"
        [attr.value]="low()"
        (input)="onMinInput($event)"
        [class]="endpointInputClasses()"
      /><input
        #maxInput
        type="range"
        [attr.aria-label]="maxLabel()"
        [attr.min]="min"
        [attr.max]="max"
        [attr.step]="stepValue()"
        [attr.value]="high()"
        (input)="onMaxInput($event)"
        [class]="endpointInputClasses()"
      /><span [class]="endpointClasses()">{{ fmt(high()) }}</span>
    </div>`,
})
export class PriceRangeSlider implements AfterViewInit, DoCheck {
  /** Minimum possible value. */
  min?: number
  /** Maximum possible value. */
  max?: number
  /** Current [low, high] tuple. */
  value?: [number, number]
  /** Step increment (defaults to `'1'` at read time). */
  step?: number
  /** Called to format each endpoint label — defaults to USD currency formatting. */
  formatValue?: (n: number) => string
  /** Optional label above: a string input, or projected `[slot=label]` content. */
  label?: unknown
  /** Extra classes. */
  className?: string
  /** Called whenever either handle moves. */
  onChange?: (value: [number, number]) => void

  /** The label capture wrapper, detached when there is no label. */
  @ViewChild('labelWrap') labelWrap?: ElementRef<HTMLElement>

  /** The minimum handle's input (found by template ref, never by translated label). */
  @ViewChild('minInput') minInput?: ElementRef<HTMLInputElement>

  /** The maximum handle's input. */
  @ViewChild('maxInput') maxInput?: ElementRef<HTMLInputElement>

  /** The i18n provider, read at label time so the labels follow a locale switch. */
  private readonly i18n = useTranslation()

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

  /**
   * Computes the wrapper class list (`cm.stack(2)` + `className`).
   * @returns The class string.
   */
  classes(): string {
    return this.cm().cn(this.cm().stack(2), this.className)
  }

  /**
   * Computes the label class list (`cm.textSize('xs')` + `cm.fontWeight('semibold')`).
   * @returns The class string.
   */
  labelClasses(): string {
    return this.cm().cn(this.cm().textSize('xs'), this.cm().fontWeight('semibold'))
  }

  /**
   * Computes the slider row class (`cm.flex({ align: 'center', gap: 'sm' })`).
   * @returns The class string.
   */
  rowClasses(): string {
    return this.cm().flex({ align: 'center', gap: 'sm' })
  }

  /**
   * Computes an endpoint label's class (`cm.textSize('sm')`, through `cn` so
   * an `[class]` binding never receives a non-string member).
   * @returns The class string.
   */
  endpointClasses(): string {
    return this.cm().cn(this.cm().textSize('sm'))
  }

  /**
   * Computes a range input's class (`cm.flex1`, through `cn` for the same
   * reason).
   * @returns The class string.
   */
  endpointInputClasses(): string {
    return this.cm().cn(this.cm().flex1)
  }

  /**
   * The step in effect (React's default parameter: an `undefined` binding must
   * not erase the `1` default).
   * @returns The step.
   */
  stepValue(): number {
    return this.step ?? 1
  }

  /**
   * The low endpoint of the current value tuple.
   * @returns The low value.
   */
  low(): number {
    return this.value?.[0] ?? 0
  }

  /**
   * The high endpoint of the current value tuple.
   * @returns The high value.
   */
  high(): number {
    return this.value?.[1] ?? 0
  }

  /**
   * Formats an endpoint label: the caller's `formatValue`, else the USD
   * default (`$1,000`).
   * @param n - The endpoint value.
   * @returns The formatted label.
   */
  fmt(n: number): string {
    return this.formatValue ? this.formatValue(n) : `$${n.toLocaleString()}`
  }

  /**
   * The minimum handle's label, through `t()` so it follows the locale.
   * @returns The translated label.
   */
  minLabel(): string {
    return this.i18n.t('priceRangeSlider.aria.minimum', undefined, { defaultValue: 'Minimum' })
  }

  /**
   * The maximum handle's label, through `t()` so it follows the locale.
   * @returns The translated label.
   */
  maxLabel(): string {
    return this.i18n.t('priceRangeSlider.aria.maximum', undefined, { defaultValue: 'Maximum' })
  }

  /**
   * The label input's text (a string input renders as text; a projected node
   * label renders through the capture point instead).
   * @returns The label text, or `''` when absent.
   */
  labelText(): string {
    return typeof this.label === 'string' ? this.label : ''
  }

  /** Registers the label wrapper's placement decision. */
  constructor() {
    usePlacement((hasContent) => this.reconcile(hasContent))
  }

  /** Puts the inputs' live values on React's controlled values (view queries resolve at view init). */
  ngAfterViewInit(): void {
    this.syncInputs()
  }

  /** Re-syncs the inputs' live values on later passes. */
  ngDoCheck(): void {
    this.syncInputs()
  }

  /**
   * Keeps the two inputs' live values on React's controlled values. The
   * attribute binding alone does not re-write the live property after a user
   * edit, and React re-writes the property on every re-render.
   */
  private syncInputs(): void {
    const minEl = this.minInput?.nativeElement
    const maxEl = this.maxInput?.nativeElement
    if (minEl && minEl.value !== String(this.low())) minEl.value = String(this.low())
    if (maxEl && maxEl.value !== String(this.high())) maxEl.value = String(this.high())
  }

  /**
   * Applies React's condition: no `<span>` at all when there is no label.
   *
   * @param hasContent - Reads whether a slot has content.
   */
  private reconcile(hasContent: SlotReader): void {
    const wrap = this.labelWrap?.nativeElement
    if (!wrap) return
    setElementPresent(wrap, this.hasLabel() || hasContent(wrap))
  }

  /**
   * Whether a string label input is present (a projected node label is not).
   * @returns True when the label input renders as text.
   */
  private hasLabel(): boolean {
    return this.label !== undefined && this.label !== null && this.label !== ''
  }

  /**
   * Handles the minimum handle's move (React's `onChange` fires on the native
   * `input` event): clamps against `high`, publishes the tuple, then restores
   * the controlled value — React restores a controlled input right after the
   * change handler returns.
   *
   * @param event - The input event.
   */
  onMinInput(event: Event): void {
    const element = event.target as HTMLInputElement
    this.onChange?.([Math.min(Number(element.value), this.high()), this.high()])
    element.value = String(this.low())
  }

  /**
   * Handles the maximum handle's move: clamps against `low`, publishes the
   * tuple, then restores the controlled value.
   *
   * @param event - The input event.
   */
  onMaxInput(event: Event): void {
    const element = event.target as HTMLInputElement
    this.onChange?.([this.low(), Math.max(Number(element.value), this.low())])
    element.value = String(this.high())
  }
}

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 default formatValue renders US-style dollars ($1,000) — always pass your own formatter for non-USD apps. The two sliders' aria-labels ("Minimum"/"Maximum") translate through t() (priceRangeSlider.aria.minimum/priceRangeSlider.aria.maximum), so they follow the active locale. Requires a wired ClassMap bond (getClassMap()). The optional label is projected [slot=label] content (or a string label input).

Translations

Translation strings are provided by @molecule/app-locales-price-range-slider.