Files
advent-of-slop-02-ugly-sweater/src/badge.ts
T
2026-11-30 15:22:03 +01:00

40 lines
1.2 KiB
TypeScript

// <advent-badge day="1">: the link in the top right corner, back to the door of the day this was made for.
// It draws itself in a shadow root, so its styles and the page's never touch.
const site = 'https://advent-of-slop.quesma.com';
const css = `
a {
position: fixed;
top: 0;
right: 0;
z-index: 2147483647;
padding: 0.35rem 0.7rem;
border-bottom-left-radius: 0.5rem;
background: #b8232f;
color: #f6ecd6;
font: 700 0.8125rem/1.2 system-ui, sans-serif;
text-decoration: none;
}
a:hover {
background: #0f3b2d;
}
`;
class AdventBadge extends HTMLElement {
connectedCallback(): void {
const day = Number(this.getAttribute('day'));
if (!Number.isInteger(day) || day < 1 || day > 24) {
throw new Error(`<advent-badge> needs day="1" to day="24", got ${this.getAttribute('day')}`);
}
const style = document.createElement('style');
style.textContent = css;
const link = document.createElement('a');
link.href = `${site}/day/${day}/`;
link.textContent = 'Advent of AI Slop';
link.title = `Made for day ${day} of Advent of AI Slop`;
this.attachShadow({ mode: 'open' }).append(style, link);
}
}
customElements.define('advent-badge', AdventBadge);