@molecule/app-bias-indicator-angular
v1.1.2
Published
Political-bias / source-credibility indicator (left/right scale + reliability tier) for news article headers
Maintainers
Readme
@molecule/app-bias-indicator-angular
Angular political-bias / source-credibility indicator.
Exports <BiasIndicator> — left/right scale with marker plus optional
reliability dot/chip — for news-aggregator article headers and dense
article lists (compact variant). It is the Angular counterpart of
@molecule/app-bias-indicator-react: same props 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.tsJSDoc, not this file.
Quick Start
import { Component } from '@angular/core'
import { BiasIndicator } from '@molecule/app-bias-indicator-angular'
@Component({
selector: 'app-article-header',
standalone: true,
imports: [BiasIndicator],
template: `
<mol-bias-indicator [bias]="-0.4" [reliability]="0.8">
<span slot="sourceLabel">Reuters</span>
</mol-bias-indicator>
`,
})
export class ArticleHeader {}Type
feature
Installation
npm install @molecule/app-bias-indicator-angular @angular/common @angular/core @angular/platform-browser @molecule/app-angular @molecule/app-ui @molecule/app-ui-angularAPI
Interfaces
BiasIndicatorProps
Props accepted by BiasIndicator — the same props
@molecule/app-bias-indicator-react accepts. React's node prop
(sourceLabel) is projected content in Angular (an input cannot carry a
node), so it is typed unknown: pass it in the host template as
[slot=sourceLabel] content, not as a value.
interface BiasIndicatorProps {
/**
* Bias scalar from `-1` (far left) through `0` (center) to `+1`
* (far right). Values are clamped before rendering.
*/
bias: number
/**
* Optional source-credibility / reliability score from `0` (lowest /
* disputed) to `1` (highest). When omitted, no reliability indicator
* is rendered.
*/
reliability?: number
/**
* Compact variant collapses the scale to a single coloured dot —
* useful for dense lists. Defaults to `false`.
*/
compact?: boolean
/**
* Optional source name (e.g. `'Reuters'`). Renders as a small caption —
* projected content selected by `[slot=sourceLabel]`.
*/
sourceLabel?: unknown
/** Extra classes to merge onto the root element. */
className?: string
/** `data-mol-id` for AI-agent / E2E selectors. */
dataMolId?: string
}Types
ReliabilityTier
Reliability tier — drives the colour + label of the optional dot/chip.
Mapped from a 0..1 reliability score by reliabilityToTier.
type ReliabilityTier = 'high' | 'medium' | 'low' | 'disputed'BiasBucket
Bias bucket — drives the bias-marker hue and the screen-reader label.
Derived from a -1..1 bias scalar by biasToBucket.
type BiasBucket = 'far-left' | 'left' | 'center' | 'right' | 'far-right'Classes
BiasIndicator
BiasIndicator component.
Political-bias / source-credibility indicator: a horizontal -1..+1 scale
with a coloured marker at bias, an optional reliability dot/chip below,
and a compact single-dot variant for dense lists. React's root element flips
between a <div> and the compact <span>; the branch React renders stands
in front of the host's place and the host itself leaves the page
(placeElementBefore + setElementPresent).
React's sourceLabel node prop is projected content in Angular: pass it as
[slot=sourceLabel] content, and the caption element exists only when
something was projected — the same rule React follows with
sourceLabel !== undefined.
@Component({
selector: 'mol-bias-indicator',
standalone: true,
hostDirectives: [MolPlacement],
inputs: ['bias', 'reliability', 'compact', 'className', 'dataMolId'],
// prettier-ignore
template: `<div #fullRoot [attr.data-mol-id]="containerId()" [attr.data-bias-bucket]="bucket()" [attr.data-reliability-tier]="tier()" [class]="rootClasses()"><span #fullSource data-mol-id="bias-indicator-source" [class]="sourceClasses()"><ng-content select="[slot=sourceLabel]"></ng-content></span><div role="img" [attr.aria-label]="biasLabel()" data-mol-id="bias-indicator-scale" [class]="scaleClasses()" [style]="scaleStyle()"><span data-mol-id="bias-indicator-marker" aria-hidden="true" [class]="markerClasses()" [style]="markerStyle()"></span></div><div [class]="rowClasses()"><span data-mol-id="bias-indicator-bias-label" [class]="biasLabelClasses()">{{ biasLabel() }}</span>@if (tier()) {<span data-mol-id="bias-indicator-reliability" [attr.data-reliability-tier]="tier()" [class]="chipClasses()"><span aria-hidden="true" [class]="dotClasses()" [style]="tierDotStyle()"></span><span>{{ tierLabel() }}</span></span>}</div></div>
<span #compactRoot role="img" [attr.aria-label]="compactAriaLabel()" [attr.data-mol-id]="containerId()" [attr.data-bias-bucket]="bucket()" [attr.data-reliability-tier]="tier()" [class]="compactClasses()"><span aria-hidden="true" [class]="dotClasses()" [style]="dotStyle()"></span><span #compactSource [class]="compactSourceClasses()"><ng-content select="[slot=sourceLabel]"></ng-content></span></span>`,
})
export class BiasIndicator {
/** Bias scalar (-1 far left … +1 far right). */
bias!: number
/** Optional 0..1 reliability score. */
reliability?: number
/** Render only the coloured dot. */
compact?: boolean
/** Extra classes on the root element. */
className?: string
/** `data-mol-id` for AI-agent / E2E selectors. */
dataMolId?: string
/** The non-compact root (`<div>`). */
@ViewChild('fullRoot') private fullRoot?: ElementRef<HTMLElement>
/** The non-compact source caption (React renders it only when present). */
@ViewChild('fullSource') private fullSource?: ElementRef<HTMLElement>
/** The compact root (`<span>`). */
@ViewChild('compactRoot') private compactRoot?: ElementRef<HTMLElement>
/** The compact source caption. */
@ViewChild('compactSource') private compactSource?: ElementRef<HTMLElement>
/** The i18n handle (locale-reactive), read in an injection context. */
private readonly i18n = useTranslation()
/** The component's host element (React renders no element of its own). */
private readonly host = inject(ElementRef<HTMLElement>)
private projectedSource: HTMLElement | null | undefined
constructor() {
usePlacement((hasContent) => this.reconcile(hasContent))
}
/** The current class map. */
cm(): UIClassMap {
return getClassMap()
}
/** The current bias bucket. */
bucket(): BiasBucket {
return biasToBucket(this.bias)
}
/** The translated bias label. */
biasLabel(): string {
const bucket = this.bucket()
return this.i18n.t(BIAS_KEY[bucket], {}, { defaultValue: BIAS_FALLBACK[bucket] })
}
/** The current reliability tier, or null when no score was given. */
tier(): ReliabilityTier | null {
return this.reliability !== undefined ? reliabilityToTier(this.reliability) : null
}
/** The translated reliability label, or null without a tier. */
tierLabel(): string | null {
const tier = this.tier()
return tier ? this.i18n.t(TIER_KEY[tier], {}, { defaultValue: TIER_FALLBACK[tier] }) : null
}
/** The `data-mol-id` on the root (React's default parameter). */
containerId(): string {
return this.dataMolId ?? 'bias-indicator'
}
/** The compact root's accessible label (bias, plus the tier when set). */
compactAriaLabel(): string {
const label = this.biasLabel()
const tier = this.tierLabel()
return tier ? `${label} — ${tier}` : label
}
/** The compact root's class list. */
compactClasses(): string {
const cm = this.cm()
return cm.cn(cm.flex({ align: 'center', gap: 'xs' }), this.className)
}
/** The non-compact root's class list. */
rootClasses(): string {
const cm = this.cm()
return cm.cn(cm.flex({ direction: 'col', gap: 'xs' }), this.className)
}
dotClasses(): string {
const cm = this.cm()
return cm.cn(cm.w(2), cm.h(2), cm.roundedFull, cm.displayInlineBlock)
}
compactSourceClasses(): string {
const cm = this.cm()
return cm.cn(cm.textSize('xs'), cm.textMuted)
}
/** The non-compact source caption's class list. */
sourceClasses(): string {
const cm = this.cm()
return cm.cn(cm.textSize('xs'), cm.fontWeight('semibold'))
}
/** The scale track's class list. */
scaleClasses(): string {
const cm = this.cm()
return cm.cn(cm.position('relative'), cm.h(2))
}
/** The marker's class list. */
markerClasses(): string {
const cm = this.cm()
return cm.cn(cm.position('absolute'), cm.roundedFull)
}
/** The label row's class list. */
rowClasses(): string {
const cm = this.cm()
return cm.flex({ align: 'center', justify: 'between', gap: 'xs' })
}
/** The bias label's class list. */
biasLabelClasses(): string {
return this.cm().cn(this.cm().textSize('xs'))
}
/** The reliability chip's class list. */
chipClasses(): string {
const cm = this.cm()
return cm.cn(cm.flex({ align: 'center', gap: 'xs' }), cm.textSize('xs'))
}
/** The bias dot's inline style. */
dotStyle(): string {
return `background: ${bucketColor(this.bucket())}`
}
/** The reliability dot's inline style. */
tierDotStyle(): string {
const tier = this.tier()
return `background: ${tier ? tierColor(tier) : ''}`
}
/** The scale track's inline style. */
scaleStyle(): string {
return `width: 100%; min-width: 80px; border-radius: 999px; background: ${SCALE_GRADIENT}; opacity: 0.45`
}
/** The marker's inline style, positioned at `biasToPercent(bias)%`. */
markerStyle(): string {
return `left: ${biasToPercent(this.bias)}%; top: 50%; transform: translate(-50%, -50%); width: 12px; height: 12px; background: ${bucketColor(this.bucket())}; border: 2px solid var(--color-surface, #ffffff); box-shadow: 0 0 0 1px var(--color-border, rgba(0,0,0,0.15))`
}
private reconcile(hasContent: SlotReader): void {
const full = this.fullRoot?.nativeElement
const compact = this.compactRoot?.nativeElement
if (!full || !compact) return
const fullSource = this.fullSource?.nativeElement ?? null
const compactSource = this.compactSource?.nativeElement ?? null
// Angular projected the caption into one of the two capture points; find
// it before any move, while its nodes are still its own children. Until
// something is projected the search runs again, so content a consumer adds
// later is found where Angular projected it.
if (this.projectedSource === undefined || this.projectedSource === null) {
this.projectedSource =
[fullSource, compactSource].find((el) => el !== null && hasContent(el)) ?? null
}
const compactShown = this.compact === true
const active = compactShown ? compact : full
const inactive = compactShown ? full : compact
const activeSource = compactShown ? compactSource : fullSource
// Render the caption's content in the branch React renders it in.
if (this.projectedSource && activeSource) {
placeContent(this.projectedSource, activeSource)
}
// The caption element renders only when something was projected.
const shown = hasContent(activeSource)
setElementPresent(fullSource, !compactShown && shown)
setElementPresent(compactSource, compactShown && shown)
setElementPresent(active, true)
// place and the host leaves the page.
placeElementBefore(active, this.host.nativeElement)
setElementPresent(inactive, false)
setElementPresent(this.host.nativeElement, false)
}
}(Member comments are shortened to their first sentence, and the 5 longest are left out to fit; the full declaration is in node_modules/@molecule/app-bias-indicator-angular/dist/bias-indicator.component.d.ts.)
Functions
clamp(value, min, max)
Clamp a number into the inclusive [min, max] range.
function clamp(value: number, min: number, max: number): numbervalue— Value to clamp.min— Lower bound.max— Upper bound.
Returns: The clamped value.
biasToBucket(bias)
Map a -1..1 bias scalar into a coarse five-bucket label used for
accessibility and colouring. Boundaries: <= -0.6 far-left,
<= -0.2 left, < 0.2 center, < 0.6 right, otherwise far-right.
function biasToBucket(bias: number): BiasBucketbias— Bias scalar (clamped to[-1, 1]first).
Returns: The bias bucket.
reliabilityToTier(reliability)
Map a 0..1 reliability score into a four-tier label. Boundaries:
>= 0.75 high, >= 0.5 medium, >= 0.25 low, otherwise disputed.
function reliabilityToTier(reliability: number): ReliabilityTierreliability— Reliability scalar (clamped to[0, 1]first).
Returns: The reliability tier.
biasToPercent(bias)
Convert a -1..1 bias value into a 0..100 percent offset for
absolute positioning along the scale track.
function biasToPercent(bias: number): numberbias— Bias scalar (clamped to[-1, 1]first).
Returns: The percent offset from the left edge of the track.
bucketColor(bucket)
Resolve the marker colour for a given bias bucket. Uses semantic CSS custom properties so swapping themes / styling libraries does not require touching this component.
function bucketColor(bucket: BiasBucket): stringbucket— Bias bucket.
Returns: A var(--…) CSS color string.
tierColor(tier)
Resolve the dot colour for a given reliability tier.
function tierColor(tier: ReliabilityTier): stringtier— Reliability tier.
Returns: A var(--…) CSS color string.
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-angularRequires a bonded ClassMap and an i18n provider (the component reads
useTranslation()), exactly like the React package.React's
sourceLabelnode prop is projected content: pass it as[slot=sourceLabel]content. The caption element renders only when something was projected.Colours come from the per-app semantic CSS custom properties, so themes flow through without code changes.
Translations
Translation strings are provided by @molecule/app-locales-bias-indicator.
