@molecule/app-sort-picker-angular
Feature · sort-picker · App (browser) · v1.1.3 · Apache-2.0
Standalone single-select sort menu with options and label
npm install @molecule/app-sort-picker-angularHow it works
@molecule/app-sort-picker-angular is a ready-made sort-picker 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 { SortPicker } from '@molecule/app-sort-picker-angular'
@Component({
selector: 'app-list-toolbar',
standalone: true,
imports: [SortPicker],
template: `
<div mol-sort-picker [value]="sort()" [options]="options" [onChange]="setSort"></div>
`,
})
export class ListToolbar {
sort = signal('newest')
options = [
{ value: 'newest', label: 'Newest' },
{ value: 'oldest', label: 'Oldest' },
{ value: 'popular', label: 'Most Popular' },
]
setSort = (value: string): void => this.sort.set(value)
}Works with: @molecule/app-angular, @molecule/app-ui, @molecule/app-ui-angular
Reference
Angular sort-by picker — the counterpart of @molecule/app-sort-picker-react:
the same props and the same DOM for the same input.
Exports <SortPicker> — a single-select wrapper around <Select> from
@molecule/app-ui-angular with a conventional "Sort by" label — and the
SortOption type. Different from <FilterBar>, which manages multiple
filter fields.
Props: value, onChange(value), options ({ value, label }[]),
label, ariaLabel, labelPosition ('inline' | 'above', default
inline), className.
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.tsJSDoc, not this file.
Quick Start
import { Component, signal } from '@angular/core'
import { SortPicker } from '@molecule/app-sort-picker-angular'
@Component({
selector: 'app-list-toolbar',
standalone: true,
imports: [SortPicker],
template: `
<div mol-sort-picker [value]="sort()" [options]="options" [onChange]="setSort"></div>
`,
})
export class ListToolbar {
sort = signal('newest')
options = [
{ value: 'newest', label: 'Newest' },
{ value: 'oldest', label: 'Oldest' },
{ value: 'popular', label: 'Most Popular' },
]
setSort = (value: string): void => this.sort.set(value)
}
Type
feature
Installation
npm install @molecule/app-sort-picker-angular @angular/common @angular/core @angular/platform-browser @molecule/app-angular @molecule/app-ui @molecule/app-ui-angular
API
Interfaces
SortOption
A single option entry for the SortPicker select list.
interface SortOption<T extends string = string> {
value: T
/**
* Option label. A native `<select>`/`<option>` can only display text,
* so this is a plain `string` — it is rendered as the option's text
* child (never `String()`-coerced). For a rich label above/beside the
* control, use the `SortPickerProps.label` prop, which accepts a
* node.
*/
label: string
}
SortPickerProps
Props accepted by SortPicker — the same props the React package accepts.
interface SortPickerProps<T extends string = string> {
/** The currently selected option value. */
value: T
/** Called with the selected value when the user picks an option. */
onChange: (value: T) => void
/** The options to list. */
options: SortOption<T>[]
/**
* Optional label rendered above or to the left of the select. A node
* value is projected content (`[slot=label]`) and renders as nodes — it is
* never stringified; a plain string renders as text. When you pass a node,
* also pass `SortPickerProps.ariaLabel` to give the select an
* accessible name.
*/
label?: unknown
/**
* Accessible name for the underlying `<select>`. Defaults to `label`
* when it is a plain string, otherwise the translated "Sort by".
*/
ariaLabel?: string
/** Where to render the label. Defaults to `'inline'`. */
labelPosition?: 'inline' | 'above'
/** Extra classes. */
className?: string
}
Classes
SortPicker
SortPicker component.
Standalone sort-by selector: a single-select <Select> with a conventional
"Sort by" label, rendered inline (default) or above the control. The host IS
the outer <div> React renders (div[mol-sort-picker]), with the same
classes. A node-valued label is projected content ([slot=label]); a
string label renders as text.
Angular projection needs the <ng-content> capture point mounted, so the
label span always stays in the template and the placement function re-decides
whether to show the projected label or the translated default (React would
never render both).
@Component({
selector: 'div[mol-sort-picker]',
standalone: true,
hostDirectives: [MolPlacement],
imports: [Select],
inputs: ['value', 'onChange', 'options', 'label', 'ariaLabel', 'labelPosition', 'className'],
host: {
'[class]': 'classes()',
},
// prettier-ignore
template: `<span [class]="labelClasses()">{{ labelText() }}<span #labelSlot><ng-content select="[slot=label]" /></span>{{ colon() }}</span><div #selectHost mol-select [options]="options ?? []" [value]="value" (valueChange)="handleValueChange($event)"></div>{{ syncAccessibleName() }}`,
})
export class SortPicker {
/** The currently selected option value. */
value?: string
/** Called with the selected value when the user picks an option. */
onChange?: (value: string) => void
/** The options to list. */
options?: SortOption[]
/** Label: a string rendered as text, or a node projected as `[slot=label]`. */
label?: unknown
/** Accessible name for the underlying `<select>`. */
ariaLabel?: string
/** Where to render the label. Defaults to `'inline'`. */
labelPosition?: 'inline' | 'above'
/** Extra classes. */
className?: string
/** The capture of the `[slot=label]` content, which renders in its place inside the label span. */
@ViewChild('labelSlot', { static: true }) private labelSlot?: ElementRef<HTMLSpanElement>
/** The `Select` host, whose rest attributes belong on its native `<select>`. */
@ViewChild('selectHost', { read: ElementRef }) private selectHost?: ElementRef<HTMLElement>
/**
* Whether a `[slot=label]` projection landed, re-decided as the slot changes.
*/
private readonly labelProjected = signal(false)
/**
* Reactive translation, read in the injection context. Its `t` reads the
* locale, so the view re-renders the text on a locale switch.
*/
private readonly i18n = useTranslation()
/**
* Re-decides the label text: when neither a string input nor projected
* content landed, React would render the translated default, so the
* projection check runs on every slot change (taken away and given back
* included). Also keeps the select's accessible name in step.
*/
constructor() {
usePlacement((hasContent) => {
const slot = this.labelSlot?.nativeElement
this.labelProjected.set(hasContent(slot))
placeContentBefore(slot, slot)
setElementPresent(slot, false)
this.syncAccessibleName()
})
}
/**
* Keeps the accessible name on the native `<select>` where React's `...rest`
* spread puts it — the only element the caller can name.
*
* The render template calls this rather than a change-detection hook: a
* locale switch (and any other signal-driven change) refreshes this view's
* template WITHOUT running view hooks, so a hook would leave the select
* named in the previous language. Called from the template it runs with
* every render, including those, and is idempotent.
*
* @returns An empty string (the interpolation renders nothing).
*/
syncAccessibleName(): string {
const select = this.selectHost?.nativeElement.querySelector('select')
if (select) {
const label = this.selectAriaLabel()
if (select.getAttribute('aria-label') !== label) select.setAttribute('aria-label', label)
}
return ''
}
/**
* The current class map.
* @returns The UIClassMap.
*/
cm(): UIClassMap {
return getClassMap()
}
/**
* Whether the label renders above the select (React's `'above'` layout).
* @returns True for the "above" layout.
*/
isAbove(): boolean {
return this.labelPosition === 'above'
}
/**
* Computes the wrapper class list (React's
* `cm.cn(cm.stack(1) | cm.flex({ align: 'center', gap: 'sm' }), className)`).
* @returns The class string.
*/
classes(): string {
const cm = this.cm()
return this.isAbove()
? cm.cn(cm.stack(1), this.className)
: cm.cn(cm.flex({ align: 'center', gap: 'sm' }), this.className)
}
/**
* Computes the label span's class list.
* @returns The class string.
*/
labelClasses(): string {
const cm = this.cm()
return this.isAbove() ? cm.cn(cm.textSize('xs'), cm.fontWeight('semibold')) : cm.textSize('sm')
}
/**
* The label's leading text: a string `label` input, else the translated
* default when nothing was projected (React's `label ?? defaultLabel`), else
* nothing because the projected `[slot=label]` node renders in its place.
* @returns The label text.
*/
labelText(): string {
if (typeof this.label === 'string') return this.label
return this.labelProjected() ? '' : this.defaultLabel()
}
/**
* The label's trailing colon (React renders `{labelNode}:` inline; the
* "above" layout has no colon).
* @returns `':'` for the inline layout, else an empty string.
*/
colon(): string {
return this.isAbove() ? '' : ':'
}
/**
* The translated default label (React's `t('sort.label', …, 'Sort by')`).
* @returns The label text.
*/
defaultLabel(): string {
return this.i18n.t('sort.label', {}, { defaultValue: 'Sort by' })
}
/**
* The select's accessible name: an explicit `ariaLabel`, else the label when
* it is a string, else the translated default. Never `String()` a node.
* @returns The `aria-label` value.
*/
selectAriaLabel(): string {
return this.ariaLabel ?? (typeof this.label === 'string' ? this.label : this.defaultLabel())
}
/**
* Reports a picked option through the `onChange` callback (React's
* `onValueChange`).
*
* @param next - The selected option value.
*/
handleValueChange(next: string): void {
this.onChange?.(next)
}
}
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 -
Must render inside the app's i18n provider and with a ClassMap bond wired (
useTranslation()/getClassMap()throw otherwise). -
SortOption.labelis a plainstring— a native<select>/<option>can only display text, so it is rendered as the option's text child. -
The outer
labelprop is a string input, or projected content selected by[slot=label]when it is a node, so icons/styled elements display as nodes. When it is a node, passariaLabelto name the select (a node cannot be anaria-label). -
The default "Sort by" label uses the
sort.labeli18n key; passlabelto override.
Translations
Translation strings are provided by @molecule/app-locales-sort-picker.