Something went wrong. Try again.
Browser extension: detect and subscribe to standard.site publications on ATProto substandard.blog
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184// 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<BadgeKind, string> = { 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<Record<BadgeKind, string>> = { post: '<svg viewBox="0 0 16 16" aria-hidden="true"><path d="M4 1.5h5.2L13 5.3v9.2H4z" fill="none" stroke="currentColor" stroke-width="1.3" stroke-linejoin="round"/><path d="M9.2 1.5v3.8H13" fill="none" stroke="currentColor" stroke-width="1.3" stroke-linejoin="round"/><path d="M6 8.2h5M6 10.4h5" stroke="currentColor" stroke-width="1.2" stroke-linecap="round"/></svg>', labeler: '<svg viewBox="0 0 16 16" aria-hidden="true"><path d="M2.2 2.2h5.1l6.5 6.5-5.1 5.1-6.5-6.5z" fill="none" stroke="currentColor" stroke-width="1.3" stroke-linejoin="round"/><circle cx="5.6" cy="5.6" r="1.1" fill="currentColor"/></svg>',}
/** 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}