<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Andrzej Stacherski</title>
    <description>The latest articles on DEV Community by Andrzej Stacherski (@andrzej_stacherski).</description>
    <link>https://dev.to/andrzej_stacherski</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4239092%2F8cca02d5-691f-4b44-bd19-2fec13080e34.png</url>
      <title>DEV Community: Andrzej Stacherski</title>
      <link>https://dev.to/andrzej_stacherski</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/andrzej_stacherski"/>
    <language>en</language>
    <item>
      <title>An AI agent for an 11ty site, grounded in llms-full.txt</title>
      <dc:creator>Andrzej Stacherski</dc:creator>
      <pubDate>Sun, 15 Nov 2026 21:21:41 +0000</pubDate>
      <link>https://dev.to/andrzej_stacherski/an-ai-agent-for-an-11ty-site-grounded-in-llms-fulltxt-4jdn</link>
      <guid>https://dev.to/andrzej_stacherski/an-ai-agent-for-an-11ty-site-grounded-in-llms-fulltxt-4jdn</guid>
      <description>&lt;p&gt;We run the website of a mental-health practice in Wrocław, Poland. It is a static &lt;a href="https://www.11ty.dev/" rel="noopener noreferrer"&gt;Eleventy&lt;/a&gt; site on Netlify, and visitors mostly ask the same practical questions: who treats children, how much is the first consultation, where are you, can I book online?&lt;/p&gt;

&lt;p&gt;All of those answers already exist on the site, as structured data. So we built &lt;strong&gt;Zapytaj PSYCHE&lt;/strong&gt; ("Ask PSYCHE"): a small assistant that answers only from the site's own content, runs on Gemini (we started on the free tier and moved to a small paid one, see below), and needs no vector database, no framework and no server of our own.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Try it live:&lt;/strong&gt; open &lt;a href="https://psyche.wroclaw.pl/?utm_source=dev.to&amp;amp;utm_medium=article&amp;amp;utm_campaign=zapytaj-psyche-article"&gt;psyche.wroclaw.pl&lt;/a&gt; and press the &lt;strong&gt;Zapytaj PSYCHE&lt;/strong&gt; button at the bottom right. The assistant answers in Polish only, so try questions like "Kto przyjmuje dzieci?" (who sees children?) or "Ile kosztuje pierwsza konsultacja?" (how much is the first consultation?). The knowledge file it reads is public too: &lt;a href="https://psyche.wroclaw.pl/llms-full.txt?utm_source=dev.to&amp;amp;utm_medium=article&amp;amp;utm_campaign=zapytaj-psyche-article"&gt;llms-full.txt&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This post walks through the setup and the problems we hit.&lt;/p&gt;

&lt;h2&gt;
  
  
  The idea: the site publishes its own knowledge file
&lt;/h2&gt;

&lt;p&gt;Instead of embeddings, we render one plain-text file at build time: &lt;code&gt;/llms-full.txt&lt;/code&gt;. The same Eleventy data that builds the pages (services, team, prices, locations) also writes this file, so the assistant can never know something the site doesn't.&lt;/p&gt;

&lt;p&gt;Each service looks roughly like this in the file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;### Psycholog Dziecięcy Wrocław

Short description...

- Strona: https://psyche.wroclaw.pl/oferta/psycholog-dzieciecy-wroclaw/
- Dla kogo: dzieci, młodzież
- Specjaliści: Paulina Armatys, ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The file is about 115 KB, which fits comfortably in Gemini's context window. That makes retrieval a non-problem: we send the whole file with every question. A 10-minute in-memory cache in the function keeps it from being fetched each time.&lt;/p&gt;

&lt;p&gt;The pattern is simple: &lt;strong&gt;the pages and the agent share one source of truth.&lt;/strong&gt; When a price changes in a JSON file, the page, the knowledge file and the agent all change on the next deploy.&lt;/p&gt;

&lt;h2&gt;
  
  
  The backend: one Netlify Function
&lt;/h2&gt;

&lt;p&gt;The whole backend is one file, &lt;code&gt;netlify/functions/zapytaj.mjs&lt;/code&gt;, exposed at &lt;code&gt;/api/zapytaj&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'/api/zapytaj'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The request flow:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Check the &lt;code&gt;Origin&lt;/code&gt; header (only our own site may call it).&lt;/li&gt;
&lt;li&gt;Check rate limits (more on that below).&lt;/li&gt;
&lt;li&gt;Load the knowledge file (cached).&lt;/li&gt;
&lt;li&gt;Call Gemini's &lt;code&gt;generateContent&lt;/code&gt; REST endpoint with a system instruction and the last five messages.&lt;/li&gt;
&lt;li&gt;Parse, validate and clean the answer, then return &lt;code&gt;{ answer, links }&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The Gemini call asks for JSON and keeps the model cheap and predictable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="n"&gt;generationConfig&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;temperature&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;maxOutputTokens&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2500&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;responseMimeType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'application/json'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;thinkingConfig&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;thinkingLevel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'low'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The API key lives in a Netlify environment variable and never reaches the browser.&lt;/p&gt;

&lt;h2&gt;
  
  
  The system prompt does the policy work
&lt;/h2&gt;

&lt;p&gt;For a health-related site, what the agent must &lt;em&gt;not&lt;/em&gt; do matters more than what it can do. The prompt (in Polish, summarised here) says:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;answer only from the knowledge base, and say so when it isn't there;&lt;/li&gt;
&lt;li&gt;give concrete data: price, duration, address, specialist names;&lt;/li&gt;
&lt;li&gt;never diagnose, advise treatment or promise results;&lt;/li&gt;
&lt;li&gt;in a crisis, give only emergency numbers and nothing else;&lt;/li&gt;
&lt;li&gt;ask visitors not to share personal or health data;&lt;/li&gt;
&lt;li&gt;refuse unrelated questions politely, and ignore instructions hidden in questions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A prompt is a request, not a guarantee, so the important guarantees are in code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Guarantee 1: the model can only link to pages that exist
&lt;/h2&gt;

&lt;p&gt;The model returns names of specialists and services as markdown links, plus a short list of other pages. Models invent URLs, so the function builds an &lt;strong&gt;address index&lt;/strong&gt; from the knowledge file when it loads it: service pages, specialist pages, locations, booking links.&lt;/p&gt;

&lt;p&gt;Every URL in the answer is checked against that index. Anything not in it is turned back into plain text. Specialist names are also auto-linked in any grammatical form (Polish inflects names: &lt;em&gt;Pauliny Armatys&lt;/em&gt;, &lt;em&gt;Paulinie Armatys&lt;/em&gt;), using a stem-based pattern that requires both first and last name to match, so "Jacek Kowalski" is never linked to "Jan Kowalski".&lt;/p&gt;

&lt;p&gt;The browser applies a second filter: only &lt;code&gt;psyche.wroclaw.pl&lt;/code&gt; and &lt;code&gt;booksy.com&lt;/code&gt; addresses become links, answers are built with DOM calls and &lt;code&gt;textContent&lt;/code&gt; (never &lt;code&gt;innerHTML&lt;/code&gt;), and every link gets UTM parameters so we can see agent-driven visits in analytics.&lt;/p&gt;

&lt;h2&gt;
  
  
  Guarantee 2: limits and privacy
&lt;/h2&gt;

&lt;p&gt;Quotas and spending caps are one limit, and a public endpoint invites abuse, so we add our own:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;8 questions per IP hash per hour and 300 per day for everybody, counted in Netlify Blobs;&lt;/li&gt;
&lt;li&gt;the IP is hashed before use, and &lt;strong&gt;question text is never stored or logged&lt;/strong&gt;. Only counters exist;&lt;/li&gt;
&lt;li&gt;on failure the function returns Gemini's error status and a short snippet of the error, never the question.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The widget also tells visitors up front that questions go to Google Gemini, and where to call in an emergency.&lt;/p&gt;

&lt;h2&gt;
  
  
  The widget
&lt;/h2&gt;

&lt;p&gt;The front end is vanilla JavaScript and one CSS file, included on every page:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a launcher button next to the site's points pill, and a panel above it;&lt;/li&gt;
&lt;li&gt;an "enlarge" button that fills the viewport, and automatic full screen on phones;&lt;/li&gt;
&lt;li&gt;the conversation sits at the bottom like a normal chat, and the intro with the data-protection note and emergency numbers is part of the scrollable area;&lt;/li&gt;
&lt;li&gt;a small animated spark while waiting for the answer.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Cards instead of just links
&lt;/h3&gt;

&lt;p&gt;When an answer mentions specialists or services, the widget shows them as cards under the text: a round photo and name for specialists, an icon and name for services. Each type is a horizontally scrolling strip with &lt;code&gt;scroll-snap&lt;/code&gt;, with cards of fixed width, so a wider chat simply shows more of them.&lt;/p&gt;

&lt;p&gt;The card data comes from a static &lt;code&gt;/zapytaj-cards.json&lt;/code&gt;, generated at build time from the same Eleventy data. Specialist photos are resized to 200 px WebP (about 5 KB each) using &lt;code&gt;@11ty/eleventy-img&lt;/code&gt;, because the originals are far too heavy for a chat bubble. The widget matches the URLs in the answer against that file, so the &lt;strong&gt;server does not need to know anything about cards&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The harness: keeping the data right and the cost bounded
&lt;/h2&gt;

&lt;p&gt;The model is the easy part. Most of the work was the scaffolding around it, and it falls into two groups.&lt;/p&gt;

&lt;h3&gt;
  
  
  The data harness: the model never gets the last word
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Layer&lt;/th&gt;
&lt;th&gt;What it does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;One source of truth&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;llms-full.txt&lt;/code&gt; is generated from the same Eleventy data as the pages, so the agent cannot know more or less than the site.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Whole file in the prompt&lt;/td&gt;
&lt;td&gt;No retrieval step that can miss a price or a name.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Low temperature, JSON output&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;temperature: 0.2&lt;/code&gt; and &lt;code&gt;responseMimeType: 'application/json'&lt;/code&gt; keep answers short and parseable.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Address index&lt;/td&gt;
&lt;td&gt;Every URL in an answer must exist in the knowledge file, otherwise it becomes plain text.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Name linking&lt;/td&gt;
&lt;td&gt;Specialist names are linked in any grammatical form, with strict first-and-last-name matching.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Tolerant parser&lt;/td&gt;
&lt;td&gt;Broken or cut-off JSON is recovered into plain text instead of being shown raw.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Refusals in the prompt&lt;/td&gt;
&lt;td&gt;No diagnoses, no efficacy claims, emergency numbers only in a crisis, off-topic questions declined.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Browser-side filter&lt;/td&gt;
&lt;td&gt;Only our own hosts become links, and nothing is ever inserted as HTML.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The principle: the prompt asks, the code enforces.&lt;/p&gt;

&lt;h3&gt;
  
  
  The cost harness: every knob that sets the bill
&lt;/h3&gt;

&lt;p&gt;The expensive part of this design is that every question carries the whole knowledge file, so cost per question is mostly &lt;em&gt;input tokens&lt;/em&gt;. We pull on every lever we have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Spending cap at the provider:&lt;/strong&gt; the paid project has a monthly cap of 100 PLN, with auto top-up when the balance falls under 1 PLN.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A daily cap for everybody:&lt;/strong&gt; 300 questions a day, whatever the cap at the provider says.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A per-visitor cap:&lt;/strong&gt; 8 questions per hour per hashed IP.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cheap requests:&lt;/strong&gt; &lt;code&gt;thinkingLevel: 'low'&lt;/code&gt;, &lt;code&gt;maxOutputTokens: 2500&lt;/code&gt; as a ceiling, a 400-character question limit, and only the last five messages as history.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No repeated downloads:&lt;/strong&gt; the knowledge file is cached in the function for 10 minutes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Only our site can call it:&lt;/strong&gt; an &lt;code&gt;Origin&lt;/code&gt; check keeps other websites from using the endpoint.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cheap failure:&lt;/strong&gt; if a model is overloaded, the function tries one fallback model, then gives up with a clear error instead of retrying in a loop.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The harness we have not built yet
&lt;/h3&gt;

&lt;p&gt;Two things are missing, and we would add them next:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;A test harness for answers.&lt;/strong&gt; A file of about thirty real questions with expected facts (price, specialist, booking path, refusal) that runs against the endpoint after every change to the prompt or the data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cost telemetry.&lt;/strong&gt; Gemini returns token counts with each response. Logging only those numbers, never the question, would show cost per question and make the caps tunable from evidence instead of guesses.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Both fit the privacy rule we started with: counters yes, visitor text never.&lt;/p&gt;

&lt;h2&gt;
  
  
  Things that went wrong
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Truncated JSON.&lt;/strong&gt; A question like "which specialists treat children?" produced a long list. With &lt;code&gt;maxOutputTokens&lt;/code&gt; at 900 the JSON was cut off mid-array, &lt;code&gt;JSON.parse&lt;/code&gt; failed, and visitors saw the raw JSON with &lt;code&gt;ł&lt;/code&gt; escapes. Fixes: a bigger limit, plus a parser that recovers the &lt;code&gt;"answer"&lt;/code&gt; string from broken JSON and decodes its escapes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Global CSS leaking into the widget.&lt;/strong&gt; The site's own &lt;code&gt;p&lt;/code&gt; and &lt;code&gt;li&lt;/code&gt; sizes were larger than the widget's text, so answers and the link list looked huge next to the example chips. The widget now sets &lt;code&gt;font-size: inherit&lt;/code&gt; on those elements explicitly. Any widget injected into a site with global element styles needs this.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;An arbitrary cap.&lt;/strong&gt; We limited cards to nine per strip "to be safe", and a 16-person answer silently lost seven of them. The cap was pointless, so we removed it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quota errors, and the free tier was not enough.&lt;/strong&gt; The first symptom was a generic "unavailable". Returning the upstream error detail made it obvious: the model's free-tier quota was exhausted. Sending a 115 KB knowledge file with every question uses far more input tokens than a short chat, so the free tier does not go far, even at our small traffic.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it costs
&lt;/h2&gt;

&lt;p&gt;We moved the API key to a paid project on &lt;strong&gt;Tier 1&lt;/strong&gt;, with guard rails:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a &lt;strong&gt;monthly spending cap of 100 PLN&lt;/strong&gt; (roughly 25 USD), so a bug or abuse cannot produce a surprise bill;&lt;/li&gt;
&lt;li&gt;an initial top-up of &lt;strong&gt;20 PLN&lt;/strong&gt;, with &lt;strong&gt;auto top-up when the balance falls below 1 PLN&lt;/strong&gt;;&lt;/li&gt;
&lt;li&gt;our own limits in the function (8 questions per hour per IP hash, 300 per day) stay in place as a second line of defence.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At this traffic, the 20 PLN top-up goes a long way. If it ever gets tight, there are two cheap levers: a smaller or cheaper model, and Gemini's context caching for the knowledge file, which we have not needed yet.&lt;/p&gt;

&lt;h2&gt;
  
  
  Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;If your site already has structured data, &lt;strong&gt;render it to one text file and put the whole file in the prompt&lt;/strong&gt;. It beats retrieval at this scale.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Validate what the model returns in code&lt;/strong&gt;, especially URLs. Treat the prompt as a hint.&lt;/li&gt;
&lt;li&gt;Build the harness before you tune the prompt: one set of checks for the data the model returns, one set of limits for what it can cost.&lt;/li&gt;
&lt;li&gt;Keep the backend to one function, and let the browser do presentation work such as cards from a static file.&lt;/li&gt;
&lt;li&gt;Decide what the agent must refuse before you decide what it can answer.&lt;/li&gt;
&lt;li&gt;Plan for the free tier to run out when the prompt carries your whole knowledge base. A capped paid tier with a small auto top-up is a safe next step.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The whole thing is one function, one CSS file, one script and two small data files.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>webdev</category>
      <category>netlify</category>
      <category>11ty</category>
    </item>
    <item>
      <title>A web component without Shadow DOM: build one with plain JavaScript</title>
      <dc:creator>Andrzej Stacherski</dc:creator>
      <pubDate>Wed, 11 Nov 2026 13:00:27 +0000</pubDate>
      <link>https://dev.to/andrzej_stacherski/a-web-component-without-shadow-dom-build-one-with-plain-javascript-14e4</link>
      <guid>https://dev.to/andrzej_stacherski/a-web-component-without-shadow-dom-build-one-with-plain-javascript-14e4</guid>
      <description>&lt;p&gt;&lt;strong&gt;Build a custom element with plain JavaScript and plain CSS: lifecycle, properties vs attributes, and the two traps that break most first components.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Most web component tutorials open with Shadow DOM, templates and slots. You can skip all of that and still get a useful, reusable component. Here is a card element in vanilla JavaScript. It works with your normal CSS and needs no build step.&lt;/p&gt;

&lt;p&gt;The goal is to turn this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;card&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'article'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;card&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;className&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'card'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// ...a dozen lines of createElement and append&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;into this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;card&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'ml-card'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;card&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fact&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fact&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;card&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;topic&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;feed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;card&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And in HTML, you can write &lt;code&gt;&amp;lt;ml-card hint&amp;gt;&amp;lt;/ml-card&amp;gt;&lt;/code&gt; as if browsers had always shipped a card tag.&lt;/p&gt;

&lt;p&gt;Two details in that tag are worth explaining up front:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;ml-&lt;/code&gt;&lt;/strong&gt; is a prefix I use on every component. It stands for &lt;strong&gt;Microlearn&lt;/strong&gt;, the name of the app I'm building. The prefix is what keeps my tags from clashing with other people's code or with future HTML elements.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;hint&lt;/code&gt;&lt;/strong&gt; is a boolean attribute I invented for this card. It switches on a small "Scroll for the next one ↓" line at the bottom of the card. It works like the built-in &lt;code&gt;disabled&lt;/code&gt; or &lt;code&gt;checked&lt;/code&gt;: if the attribute is present, the hint shows; if it's absent, the hint stays hidden. I put it only on the first card of the feed, so new users learn they can scroll, and the rest of the cards stay clean. We'll see how the component reads it further down.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The smallest custom element
&lt;/h2&gt;

&lt;p&gt;A &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_custom_elements" rel="noopener noreferrer"&gt;custom element&lt;/a&gt; is a class that extends &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement" rel="noopener noreferrer"&gt;&lt;code&gt;HTMLElement&lt;/code&gt;&lt;/a&gt;, registered under a tag name:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MlCard&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;HTMLElement&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;connectedCallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Hello from a card'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;customElements&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;define&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'ml-card'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;MlCard&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three things to know:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The tag name must contain a hyphen.&lt;/strong&gt; &lt;code&gt;ml-card&lt;/code&gt; works, &lt;code&gt;card&lt;/code&gt; does not. The hyphen is how the browser tells your tags from its own, now and in the future. That's why I start every name with &lt;code&gt;ml-&lt;/code&gt; (Microlearn).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;connectedCallback&lt;/code&gt;&lt;/strong&gt; is called by the browser when the element is added to the page.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/CustomElementRegistry/define" rel="noopener noreferrer"&gt;&lt;code&gt;customElements.define&lt;/code&gt;&lt;/a&gt;&lt;/strong&gt; registers the tag. Every &lt;code&gt;&amp;lt;ml-card&amp;gt;&lt;/code&gt;, existing or added later, becomes an instance of your class.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Build once, render many times
&lt;/h2&gt;

&lt;p&gt;Here is the real component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MlCard&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;HTMLElement&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;observedAttributes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'hint'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  #&lt;span class="n"&gt;fact&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// the fact to show: { id, t, f, m }&lt;/span&gt;
  #&lt;span class="n"&gt;topic&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// the topic it belongs to: { label, hue }&lt;/span&gt;

  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ready&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;connectedCallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ready&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;#&lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;#&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;attributeChangedCallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ready&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;#&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;set&lt;/span&gt; &lt;span class="nf"&gt;fact&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;#&lt;span class="n"&gt;fact&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;#&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="nf"&gt;fact&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;#&lt;span class="n"&gt;fact&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;set&lt;/span&gt; &lt;span class="nf"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;#&lt;span class="n"&gt;topic&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;#&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

  #&lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sb"&gt;`
      &amp;lt;p class="ml-card__topic"&amp;gt;&amp;lt;/p&amp;gt;
      &amp;lt;h2 class="ml-card__fact"&amp;gt;&amp;lt;/h2&amp;gt;
      &amp;lt;p class="ml-card__more"&amp;gt;&amp;lt;/p&amp;gt;
      &amp;lt;p class="ml-card__hint" hidden&amp;gt;Scroll for the next one ↓&amp;lt;/p&amp;gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ready&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  #&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Guard: stop if the markup isn't built yet, or the data hasn't arrived.&lt;/span&gt;
    &lt;span class="c1"&gt;// This is what makes it safe to call #render at any moment.&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ready&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;#&lt;span class="n"&gt;fact&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;#&lt;span class="n"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Set a CSS custom property (--h) on the card, so the stylesheet can&lt;/span&gt;
    &lt;span class="c1"&gt;// use the topic's hue for colours (e.g. hsl(var(--h) 70% 50%)).&lt;/span&gt;
    &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'--h'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;#&lt;span class="n"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Fill in the topic label, e.g. "Space". textContent inserts plain&lt;/span&gt;
    &lt;span class="c1"&gt;// text, never HTML, so the data can't inject markup.&lt;/span&gt;
    &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'.ml-card__topic'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;#&lt;span class="n"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Fill in the headline: the fact itself (field "f").&lt;/span&gt;
    &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'.ml-card__fact'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;#&lt;span class="n"&gt;fact&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Fill in the extra detail under the headline (field "m", for "more").&lt;/span&gt;
    &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'.ml-card__more'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;#&lt;span class="n"&gt;fact&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Show the hint only if the card has the `hint` attribute.&lt;/span&gt;
    &lt;span class="c1"&gt;// hasAttribute returns true/false; "hidden" is the opposite, hence the !.&lt;/span&gt;
    &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'.ml-card__hint'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hidden&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hasAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'hint'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customElements&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'ml-card'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customElements&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;define&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'ml-card'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;MlCard&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same shape works for any component:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The constructor only sets defaults.&lt;/strong&gt; Don't read attributes or touch children there. The element may not be in the page yet.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;connectedCallback&lt;/code&gt; builds the markup once.&lt;/strong&gt; The &lt;code&gt;ready&lt;/code&gt; flag matters: the callback runs again every time the element is moved or reconnected, and without the flag you would build twice.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;#render&lt;/code&gt; is safe to call at any moment.&lt;/strong&gt; It returns early if there is nothing to draw yet, and calling it five times gives the same result.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data is poured in with &lt;code&gt;textContent&lt;/code&gt;&lt;/strong&gt;, never placed inside the &lt;code&gt;innerHTML&lt;/code&gt; string. The template is fixed text you wrote, so it is safe.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The &lt;code&gt;#&lt;/code&gt; names are &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_elements" rel="noopener noreferrer"&gt;private class members&lt;/a&gt;. Only code inside the class can use them, which keeps the public surface small: &lt;code&gt;fact&lt;/code&gt;, &lt;code&gt;topic&lt;/code&gt; and the &lt;code&gt;hint&lt;/code&gt; attribute.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are &lt;code&gt;#fact&lt;/code&gt; and &lt;code&gt;#topic&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;They are the card's two pieces of data, stored in private fields. Each starts as &lt;code&gt;null&lt;/code&gt; ("nothing yet") and is filled in from outside.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;#fact&lt;/code&gt;&lt;/strong&gt; is one entry from the app's list of facts, an object with four keys:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'b0'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'space'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'A day on Venus is longer than its year.'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'Venus turns once every 243 Earth days...'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;id&lt;/code&gt; identifies the fact, &lt;code&gt;t&lt;/code&gt; is its topic key, &lt;code&gt;f&lt;/code&gt; is the headline and &lt;code&gt;m&lt;/code&gt; is the longer explanation.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;#topic&lt;/code&gt;&lt;/strong&gt; describes the topic that fact belongs to. It holds a display name and a colour:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'Space'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;230&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The app keeps a lookup table of topics, and the calling code picks the right one with &lt;code&gt;TOPICS[fact.t]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Why two fields and not one? The fact says &lt;em&gt;what&lt;/em&gt; to show, and the topic says &lt;em&gt;how it looks&lt;/em&gt; (its label and colour). Many facts share one topic, so the topic is stored once and passed in separately.&lt;/p&gt;

&lt;p&gt;Why private? Because the outside world never touches the fields directly. It goes through the public setters, &lt;code&gt;card.fact = ...&lt;/code&gt; and &lt;code&gt;card.topic = ...&lt;/code&gt;, and each setter stores the value and calls &lt;code&gt;#render&lt;/code&gt;. That is how the card redraws itself whenever new data arrives. The &lt;code&gt;fact&lt;/code&gt; getter exists so other code can ask the card which fact it holds. There is no &lt;code&gt;topic&lt;/code&gt; getter because nothing needed one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Properties or attributes?
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Input&lt;/th&gt;
&lt;th&gt;Use&lt;/th&gt;
&lt;th&gt;Why&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;A flag or short text (&lt;code&gt;hint&lt;/code&gt;, &lt;code&gt;size="large"&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;Attribute&lt;/td&gt;
&lt;td&gt;Visible in HTML, usable from a CMS, styleable with &lt;code&gt;[hint]&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;An object or array&lt;/td&gt;
&lt;td&gt;Property&lt;/td&gt;
&lt;td&gt;Attributes can only hold strings&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;hint&lt;/code&gt; is the attribute case. Inside &lt;code&gt;#render&lt;/code&gt;, this line does the work:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'.ml-card__hint'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hidden&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hasAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'hint'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It checks whether the &lt;code&gt;hint&lt;/code&gt; attribute is present and shows or hides the hint paragraph to match. For the card to redraw when the attribute changes later, list it in &lt;code&gt;static observedAttributes&lt;/code&gt;. After that, &lt;code&gt;card.toggleAttribute('hint')&lt;/code&gt; updates the card on its own, with no extra JavaScript on your side. In the feed, I use &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/toggleAttribute" rel="noopener noreferrer"&gt;&lt;code&gt;toggleAttribute('hint', index === 0)&lt;/code&gt;&lt;/a&gt; so only the first card gets it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Trap 1: the blank card
&lt;/h2&gt;

&lt;p&gt;Look at the order of events in the calling code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;card&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'ml-card'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;card&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fact&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fact&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;         &lt;span class="c1"&gt;// setter runs, but there is no markup yet&lt;/span&gt;
&lt;span class="n"&gt;card&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;topic&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;       &lt;span class="c1"&gt;// same&lt;/span&gt;
&lt;span class="n"&gt;feed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;card&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;        &lt;span class="c1"&gt;// now connectedCallback builds the markup&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both setters run before the card is in the page. The guard on the first line of &lt;code&gt;#render&lt;/code&gt; makes them harmless: they store the value and return. When &lt;code&gt;connectedCallback&lt;/code&gt; fires, &lt;code&gt;#render&lt;/code&gt; picks up what was stored. Without the guard you get blank cards or errors. If a card renders blank, check the order of events first.&lt;/p&gt;

&lt;h2&gt;
  
  
  Trap 2: the collapsed card
&lt;/h2&gt;

&lt;p&gt;A custom element is &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/display" rel="noopener noreferrer"&gt;&lt;code&gt;display: inline&lt;/code&gt;&lt;/a&gt; by default, like a &lt;code&gt;&amp;lt;span&amp;gt;&lt;/code&gt;, and inline elements ignore &lt;code&gt;height&lt;/code&gt;. Forget this and your cards shrink to the height of their text.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;ml-card&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100dvh&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Whenever you register a tag, decide what &lt;code&gt;display&lt;/code&gt; it should have and write that rule first.&lt;/p&gt;

&lt;h2&gt;
  
  
  A related gotcha: define twice
&lt;/h2&gt;

&lt;p&gt;Calling &lt;code&gt;customElements.define&lt;/code&gt; twice for the same name throws. In your own page that never happens. In a CMS, where a theme and a module may both load the same script, it happens all the time. The &lt;code&gt;get&lt;/code&gt; check before &lt;code&gt;define&lt;/code&gt; makes loading the script twice harmless.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why no Shadow DOM?
&lt;/h2&gt;

&lt;p&gt;An honest trade-off.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What you give up:&lt;/strong&gt; real style isolation. Another stylesheet could restyle your card by accident. The prefix (&lt;code&gt;ml-card__topic&lt;/code&gt;, not &lt;code&gt;.topic&lt;/code&gt;) is your only protection, so use it everywhere.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What you gain:&lt;/strong&gt; your normal CSS, design tokens and dark mode just work. The inspector shows plain HTML. Components drop into a CMS and inherit the surrounding page's styles, which is often exactly what you want.&lt;/p&gt;

&lt;p&gt;For an app or design system you control, light DOM is the shorter road. Shadow DOM earns its effort when you ship a widget into pages you don't own.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;

&lt;p&gt;Add a &lt;code&gt;compact&lt;/code&gt; attribute that makes the headline smaller. You need one entry in &lt;code&gt;observedAttributes&lt;/code&gt; and one CSS rule: &lt;code&gt;ml-card[compact] .ml-card__fact&lt;/code&gt;. Notice that the attribute does its work through CSS alone.&lt;/p&gt;




&lt;p&gt;This component comes from a small book I'm writing, where you build a full "one fact per screen" learning app with vanilla JavaScript and web components, chapter by chapter. Chapter 1 is free to read: &lt;a href="https://onescreenatatime.netlify.app/book/chapter-1/" rel="noopener noreferrer"&gt;One Screen at a Time&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>webcomponents</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
