// One badge shape for every identity the popup shows: an account, a // publication, a post, an author, or a labeler. The kind changes the media // shape and glyph, never the structure, so a new surface can add an identity // without inventing a second visual language. // // Identity kinds deliberately stay neutral. Color is already the severity // channel for status pills and moderation labels; spending it on "this is a // publication" would make the two vocabularies fight. The media shape carries // the kind (round for people, square for publications and posts, a tag for // labelers), and the role chip names the two people cases that can be // confused: "Author" and "This site". import { TONE_ICONS, failedIconUrls } from './dom' export type BadgeKind = 'account' | 'publication' | 'post' | 'author' | 'labeler' export type BadgeSize = 'sm' | 'md' export type BadgeTone = 'neutral' | 'warn' | 'alert' | 'error' export interface BadgeOptions { kind: BadgeKind label: string /** The second line: a handle, host, or one-word source. */ meta?: string /** A role chip after the identity: "Author", "This site". */ role?: string iconUrl?: string /** Makes the whole badge a link. */ href?: string | null size?: BadgeSize /** Severity for moderation labels; identity badges stay neutral. */ tone?: BadgeTone /** No border or surface; for a badge that sits inside a card. */ ghost?: boolean /** Additional classes for compatibility or a caller's hook. */ className?: string /** Full text for hover, when the visible label is abbreviated. */ title?: string /** Give the label element an id when a card fills it in place. */ labelId?: string } const KIND_NAMES: Record = { account: 'Account', publication: 'Publication', post: 'Post', author: 'Author', labeler: 'Labeler', } /** Media glyphs for kinds that are not pictures of a person or a publication. */ const GLYPHS: Partial> = { post: '', labeler: '', } /** The classes a badge of this kind/size/tone carries. Pure and testable. */ export function badgeClass(options: { kind: BadgeKind size?: BadgeSize tone?: BadgeTone ghost?: boolean className?: string }): string { return [ 'badge', `badge--${options.kind}`, `badge--${options.size ?? 'md'}`, options.tone && options.tone !== 'neutral' ? `badge--${options.tone}` : '', options.ghost ? 'badge--ghost' : '', options.className ?? '', ] .filter(Boolean) .join(' ') } /** A neutral letter tile when an identity has no picture. */ export function badgeMonogram(label: string): string { return [...label.trim().replace(/^@/, '')][0]?.toUpperCase() ?? 'ยท' } /** The kind's name, for the badge's tooltip and assistive text. */ export function badgeKindLabel(kind: BadgeKind): string { return KIND_NAMES[kind] } /** * Fill an icon slot with a picture, or a monogram when there is none (or it * failed). Shared with the cards that fill their badge in place. */ export function applyBadgeIcon( img: HTMLImageElement, mono: HTMLElement, label: string, iconUrl: string | undefined, ): void { mono.textContent = badgeMonogram(label) const showMono = () => { img.hidden = true mono.hidden = false } if (!iconUrl || failedIconUrls.has(iconUrl)) { showMono() return } img.onerror = () => { console.debug('[substandard] badge icon failed to load', iconUrl) failedIconUrls.add(iconUrl) showMono() } if (img.src !== iconUrl) img.src = iconUrl img.hidden = false mono.hidden = true } /** * Build one badge. The element is a link when `href` is a linkable web URL, * otherwise a plain inline container. */ export function badge(options: BadgeOptions): HTMLElement { const { kind, label, meta, role, iconUrl, href, title, labelId } = options const el = document.createElement(href ? 'a' : 'div') el.className = badgeClass(options) el.dataset.kind = kind if (title) el.title = title if (href) { el.setAttribute('href', href) el.setAttribute('target', '_blank') el.setAttribute('rel', 'noopener noreferrer') } const media = document.createElement('span') media.className = 'badge-media' // A label's severity is a shape, not just a hue: the same triangle/diamond // vocabulary the status pills use. A neutral labeler badge is a tag. const severity = kind === 'labeler' && options.tone && options.tone !== 'neutral' ? TONE_ICONS[options.tone] : undefined const glyph = severity ?? GLYPHS[kind] if (glyph) { media.classList.add('badge-media--glyph') media.innerHTML = glyph } else { const img = document.createElement('img') img.className = 'badge-icon' img.alt = '' img.hidden = true const mono = document.createElement('span') mono.className = 'badge-icon badge-icon--mono' mono.setAttribute('aria-hidden', 'true') mono.hidden = true media.append(img, mono) applyBadgeIcon(img, mono, label, iconUrl) } const body = document.createElement('span') body.className = 'badge-body' if (role) { const roleEl = document.createElement('span') roleEl.className = 'badge-role' roleEl.textContent = role body.append(roleEl) } const line = document.createElement('span') line.className = 'badge-line' const name = document.createElement('span') name.className = 'badge-label' if (labelId) name.id = labelId name.textContent = label line.append(name) body.append(line) if (meta) { const sub = document.createElement('span') sub.className = 'badge-meta' sub.textContent = meta body.append(sub) } el.append(media, body) return el }