Files
advent-of-slop-04-flaeskesteg/src/badge.ts
T

57 lines
1.8 KiB
TypeScript

// <advent-badge day="1" name="Living fishtank">: the label in the top right corner, "Advent of AI Slop [Day 1]
// Living fishtank". Its first part links to the calendar, the rest 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 = `
nav {
position: fixed;
top: 0;
right: 0;
z-index: 2147483647;
display: flex;
flex-wrap: wrap;
gap: 0 0.35em;
max-width: 100vw;
box-sizing: border-box;
padding: 0.35rem 0.7rem;
border-bottom-left-radius: 0.5rem;
background: #b8232f;
font: 700 0.8125rem/1.2 system-ui, sans-serif;
}
a {
color: #f6ecd6;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
`;
const link = (href: string, text: string): HTMLAnchorElement => {
const anchor = document.createElement('a');
anchor.href = href;
anchor.textContent = text;
return anchor;
};
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 name = this.getAttribute('name');
if (name === null || name.trim() === '') {
throw new Error('<advent-badge> needs name="...", the title of its day, such as name="Living fishtank"');
}
const style = document.createElement('style');
style.textContent = css;
const label = document.createElement('nav');
label.append(link(`${site}/`, 'Advent of AI Slop'), link(`${site}/day/${day}/`, `[Day ${day}] ${name}`));
this.attachShadow({ mode: 'open' }).append(style, label);
}
}
customElements.define('advent-badge', AdventBadge);