typescript
52 lines · 8 steps
A CSS-driven progress ring in Angular
A standalone Angular component draws an SVG progress ring by pushing computed geometry into CSS custom properties via host bindings.
Explained by
highlit
1import { Component, HostBinding, Input } from '@angular/core';
2
3type ProgressVariant = 'success' | 'warning' | 'danger';
4
5@Component({
6 selector: 'app-progress-ring',
7 standalone: true,
8 template: `
9 <svg viewBox="0 0 120 120">
10 <circle class="track" cx="60" cy="60" r="54" />
11 <circle class="indicator" cx="60" cy="60" r="54" />
12 </svg>
13 <span class="label">{{ value }}%</span>
14 `,
15 styleUrl: './progress-ring.component.css',
16})
17export class ProgressRingComponent {
18 private readonly circumference = 2 * Math.PI * 54;
19
20 @Input() variant: ProgressVariant = 'success';
21
22 @Input({ required: true })
23 set value(percent: number) {
24 this._value = Math.min(100, Math.max(0, percent));
25 }
26 get value(): number {
27 return this._value;
28 }
29 private _value = 0;
30
31 @HostBinding('style.--ring-circumference')
32 get circumferenceVar(): string {
33 return `${this.circumference}px`;
34 }
35
36 @HostBinding('style.--ring-offset')
37 get offsetVar(): string {
38 return `${this.circumference * (1 - this.value / 100)}px`;
39 }
40
41 @HostBinding('style.--ring-color')
42 get colorVar(): string {
43 return `var(--tone-${this.variant})`;
44 }
45
46 @HostBinding('attr.role') readonly role = 'progressbar';
47
48 @HostBinding('attr.aria-valuenow')
49 get ariaValue(): number {
50 return this.value;
51 }
52}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Host bindings that return computed CSS custom properties let the browser handle rendering while the component owns the math.
- 2A setter on an @Input is the natural place to validate and clamp incoming values before they reach the view.
- 3Mirroring visual state into ARIA attributes keeps a custom control accessible to assistive technology.
Related explainers
typescript
import { registerLocaleData } from '@angular/common'; import localeFr from '@angular/common/locales/fr'; import localeFrExtra from '@angular/common/locales/extra/fr'; import localeDe from '@angular/common/locales/de';
Locale-aware bootstrapping in Angular
i18n
localization
dependency-injection
Intermediate
8 steps
typescript
import { Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; import * as Joi from 'joi';
Validating env config at boot in NestJS
configuration
schema-validation
environment-variables
Intermediate
8 steps
typescript
import { Inject, Injectable, Logger } from '@nestjs/common'; import { CACHE_MANAGER } from '@nestjs/cache-manager'; import { Cache } from 'cache-manager'; import { InjectRepository } from '@nestjs/typeorm';
A cache-aside country lookup in NestJS
cache-aside
dependency-injection
batch-lookup
Intermediate
8 steps
typescript
import { Injectable, effect, signal, computed } from '@angular/core'; interface Preferences { theme: 'light' | 'dark';
A signal-based preferences store in Angular
signals
state-management
persistence
Intermediate
7 steps
typescript
import { useEffect, useState } from "react"; interface Section { id: string;
Building a scroll-spy hook in React
custom-hooks
intersectionobserver
dom-observation
Intermediate
8 steps
typescript
import { Injectable, Scope, Inject, NotFoundException } from '@nestjs/common'; import { REQUEST } from '@nestjs/core'; import { Request } from 'express'; import { DataSource } from 'typeorm';
Per-tenant database connections in NestJS
multi-tenancy
connection-pooling
dependency-injection
Advanced
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/a-css-driven-progress-ring-in-angular-explained-typescript-986f/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.