import { Component, ChangeDetectionStrategy, ChangeDetectorRef, PLATFORM_ID, afterNextRender, inject } from '@angular/core'; import { isPlatformBrowser } from '@angular/common'; import { donateColors, donateState, clampPercent } from './donate-colors'; /** The site's Ko-fi page (https://ko-fi.com/). Empty hides the button. */ export const KOFI_PAGE = 'johnmcm'; /** * The header's Donate button: "Donate 37%", the share of this month's goal * raised so far (from /api/donations/progress, which resets on the 1st), in * a colour from red at 0% to green at 100%. It opens the Ko-fi page in a new * tab. Until the figure arrives, if it cannot, and while it is 0% (nothing * given yet this month), it reads plain "Donate" in the site's own colours * rather than a red "Donate 0%" (donateState); its width is reserved for * "Donate 100%" so the header never shifts when the number appears. */ @Component({ selector: 'app-donate-button', changeDetection: ChangeDetectionStrategy.Eager, template: ` @if (page) { } `, styles: ` :host { display: inline-flex; } .donate-button { display: inline-flex; align-items: center; justify-content: center; min-width: 8.6em; padding: 4px 12px; border: 1px solid; border-radius: 999px; font-weight: 700; font-size: .9rem; line-height: 1.4; text-decoration: none; white-space: nowrap; font-variant-numeric: tabular-nums; /* "Donate 40%" is capitals and digits only, but the line still keeps room for descenders, so the letters sat a pixel or two high. Trimmed to cap height and baseline, the flex centring centres the letters themselves; the fixed height keeps the button its size. Browsers without text-box (Firefox for now) keep the old look. */ box-sizing: border-box; height: 30px; border-color: var(--brand); color: var(--brand); background: transparent; transition: filter .15s; } /* text-box only trims a block's own lines, so the label is a block inside the flex button. Chrome then snaps the baseline up a fraction of a pixel; half a pixel down evens it out (measured at 1x to 2x: within one pixel everywhere, where it sat up to three high). */ .label { display: block; text-box: trim-both cap alphabetic; position: relative; top: .5px; } .donate-button:hover, .donate-button:focus-visible { filter: brightness(1.08); text-decoration: none; } .donate-button:focus-visible { outline: 2px solid currentColor; outline-offset: 2px; } `, }) export class DonateButtonComponent { readonly page = KOFI_PAGE; percent: number | null = null; colors = donateColors(0); state = donateState(null); private readonly cdr = inject(ChangeDetectorRef); constructor() { if (!isPlatformBrowser(inject(PLATFORM_ID)) || !this.page) return; afterNextRender(() => void this.load()); } private async load(): Promise { try { const r = await fetch('/api/donations/progress', { headers: { Accept: 'application/json' } }); if (!r.ok) return; const body = (await r.json()) as { percent: number | null }; if (typeof body.percent !== 'number') return; this.percent = clampPercent(body.percent); this.colors = donateColors(this.percent); this.state = donateState(this.percent); this.cdr.detectChanges(); } catch { // Offline or the API is down: the button stays plain "Donate". } } }