Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | 7x 7x 12x 12x 12x 12x 12x 12x 12x 11x 11x 1x 1x 1x 1x 11x 11x 11x 30x 11x 11x 11x 1x 1x 11x 24x 11x 11x 11x 11x 11x 12x 12x 12x 12x 12x 60x | import { APP_ORIGIN, fetchPublicSchedule, formatAge, type ScheduleItem } from '../src/shared/public-schedule';
import { EMBED_SOURCE, PROTOCOL_VERSION, type EmbedMessage } from './protocol';
export interface WidgetOptions {
/** Origin serving the Cygnus API. Overridable so the same bundle works in dev and on a preview. */
apiOrigin: string;
/** Cap on how many rows to render. The full PNI schedule is long; a sidebar widget is not. */
limit?: number;
/** Called for every outbound message, so the two variants can differ in transport only. */
emit: (message: EmbedMessage) => void;
}
/**
* The dark palette, in one place: the stylesheet applies it on two conditions (see STYLES).
*
* The dark theme sets its own teal and grey for the title, link and secondary text. It used to
* inherit the light theme's: #186560 measured 2.46:1 on the dark card and #6b7280 3.48:1, where
* 11–15px text needs 4.5:1. axe never saw it, because the shadow root is closed. Now 9.63:1 and
* 6.63:1. The age pills keep their light fill in both themes, where the teal is 6.12:1. The error
* red too: #9b1c1c is 2.06:1 on the dark card, #fca5a5 is 8.86:1.
*/
const DARK_PALETTE =
'--bg: #10201f; --edge: #21403d; --rule: #1b3331; --ink: #e6edf3; --accent: #7dd3c8; --muted: #9ca3af; --error: #fca5a5;';
/**
* Styles live in a template string rather than a stylesheet because the whole point is that they
* never leave the shadow root. There is no build step that could accidentally extract them into a
* global CSS file the host also loads, and no class name that could collide with the host's.
*
* Colours are custom properties on the host, so a theme is one block of values. Which block
* applies:
* - `theme="light"` or `theme="dark"` on the host element: that one, whatever the OS says. A host
* whose page has its own theme switch passes it, because the OS setting is not the page's: on a
* light page in a dark-mode OS, the widget came out as a dark slab.
* - No attribute (or any other value): the visitor's `prefers-color-scheme`, as before the
* attribute existed, so no host that pasted the widget earlier sees a change.
* Custom properties are the one thing `all: initial` leaves alone, and they are set on `:host`
* itself, so nothing on the host page can reach them. (Comments stay out of the string below:
* everything in it ships to every host.)
*/
const STYLES = `
:host {
all: initial;
display: block;
font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
line-height: 1.4;
--bg: #ffffff; --edge: #d9e2ec; --rule: #eef2f6; --ink: #1f2933; --accent: #186560; --muted: #6b7280; --error: #9b1c1c;
color: var(--ink);
}
@media (prefers-color-scheme: dark) {
:host(:not([theme="light"])) { ${DARK_PALETTE} }
}
:host([theme="dark"]) { ${DARK_PALETTE} }
* { box-sizing: border-box; }
.card {
border: 1px solid var(--edge);
border-radius: 12px;
padding: 16px;
background: var(--bg);
max-width: 420px;
}
.title { font-size: 15px; font-weight: 700; margin: 0 0 2px; color: var(--accent); }
.subtitle { font-size: 12px; color: var(--muted); margin: 0 0 12px; }
ul { list-style: none; margin: 0; padding: 0; }
li { display: flex; gap: 10px; padding: 7px 0; border-top: 1px solid var(--rule); }
li:first-child { border-top: none; }
.age {
flex: 0 0 74px;
font-size: 11px;
font-weight: 600;
color: #186560;
background: #e8f5f3;
border-radius: 999px;
padding: 2px 8px;
height: fit-content;
text-align: center;
}
.name { font-size: 13px; font-weight: 600; margin: 0; }
.dose { font-size: 11px; color: var(--muted); margin: 1px 0 0; }
.footer { margin-top: 12px; padding-top: 10px; border-top: 1px solid var(--rule); font-size: 11px; }
a { color: var(--accent); font-weight: 600; text-decoration: none; }
a:hover { text-decoration: underline; }
a:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }
.state { font-size: 12px; color: var(--muted); margin: 0; }
.error { color: var(--error); }
`;
/**
* Renders the public PNI schedule into a shadow root.
*
* Shared by both distribution variants — script tag and iframe — so the two can never drift in what
* they show. What differs is only how `emit` gets a message to the host.
*/
export class VaccineScheduleWidget {
private readonly root: ShadowRoot;
private readonly options: WidgetOptions;
private lastHeight = 0;
private observer: ResizeObserver | null = null;
constructor(host: HTMLElement, options: WidgetOptions) {
this.options = options;
// `closed` is deliberate: the host page cannot reach into this tree, which means it cannot
// restyle it into something misleading, and equally cannot break it by accident.
//
// No `host.shadowRoot ??` guard, because closed mode makes that property permanently null —
// it would read like reuse protection while never once being true. Constructing twice against
// the same host throws, and the callers guard against that themselves.
this.root = host.attachShadow({ mode: 'closed' });
}
async render(): Promise<void> {
// The loading and error lines sit inside the card too. Outside it they were drawn straight on
// the host's background, which the widget cannot see (grey text meant for the dark card, on a
// white page), and the error reported a height of 0, because the height is the card's: an
// iframe host that sizes the frame from it collapsed the message out of sight.
this.paint(`<div class="card"><p class="state">Carregando o calendário vacinal…</p></div>`);
try {
const schedule = await fetchPublicSchedule(this.options.apiOrigin);
this.paintSchedule(schedule);
this.emitSize('ready');
} catch (error) {
const reason = error instanceof Error ? error.message : 'unknown';
this.paint(`<div class="card"><p class="state error">Não foi possível carregar o calendário vacinal.</p></div>`);
this.options.emit({ source: EMBED_SOURCE, version: PROTOCOL_VERSION, type: 'error', reason });
this.emitSize('ready');
}
}
private paintSchedule(schedule: ScheduleItem[]): void {
const limit = this.options.limit ?? 8;
const items = schedule.slice(0, limit);
const rows = items
.map(
(item) => `
<li>
<span class="age">${escapeHtml(formatAge(item.recommendedAgeInMonths))}</span>
<div>
<p class="name">${escapeHtml(item.name)}</p>
<p class="dose">${item.doseNumber}ª dose</p>
</div>
</li>`,
)
.join('');
this.paint(`
<div class="card">
<p class="title">Calendário vacinal</p>
<p class="subtitle">Programa Nacional de Imunizações — primeiras ${items.length} doses</p>
<ul>${rows}</ul>
<p class="footer"><a href="${APP_ORIGIN}" data-navigate>Acompanhar as vacinas do seu bebê →</a></p>
</div>
`);
const link = this.root.querySelector('[data-navigate]');
link?.addEventListener('click', (event) => {
// The host owns navigation. See NavigateMessage in protocol.ts for why the embed never
// navigates the page it is embedded in.
event.preventDefault();
this.options.emit({
source: EMBED_SOURCE,
version: PROTOCOL_VERSION,
type: 'navigate',
url: APP_ORIGIN,
});
});
this.watchSize();
}
private paint(html: string): void {
this.root.innerHTML = `<style>${STYLES}</style>${html}`;
}
private watchSize(): void {
Iif (this.observer || typeof ResizeObserver === 'undefined') return;
const card = this.root.querySelector('.card');
Iif (!card) return;
this.observer = new ResizeObserver(() => this.emitSize('resize'));
this.observer.observe(card);
}
private emitSize(type: 'ready' | 'resize'): void {
const card = this.root.querySelector('.card');
const height = Math.ceil(card?.getBoundingClientRect().height ?? 0);
// Sub-pixel churn would otherwise produce a message per frame while a host animates.
Iif (type === 'resize' && Math.abs(height - this.lastHeight) < 2) return;
this.lastHeight = height;
this.options.emit({ source: EMBED_SOURCE, version: PROTOCOL_VERSION, type, height });
}
}
function escapeHtml(value: string): string {
return value
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"');
}
|