Use it in any framework
@sparkletree/core/elements is a set of custom elements. They do what the
React components do, on any page that can write HTML: plain HTML, Astro,
Svelte, Liquid, Vue. The text you serve inside them is the fallback. It is
there before any script runs, it sets the layout, and it is what a reader
without scripts sees.
A complete page
Section titled “A complete page”<!doctype html><html lang="sv-SE"> <head> <meta charset="utf-8" /> <meta name="sparkletree-key" content="st_pk_live_…" /> <!-- Render-blocking, in <head>: the texts are held before the first paint. --> <script type="module" blocking="render" src="/sparkletree.js"></script> <style> sparkletree-island, sparkletree-fragments { display: block; } sparkletree-live { display: block; aspect-ratio: 16 / 9; } </style> </head> <body> <sparkletree-island campaign="CAMPAIGN_ID"> <p><sparkletree-text field="greeting">God morgon</sparkletree-text></p> <h1><sparkletree-text field="headline">Stoneware for slow mornings</sparkletree-text></h1> <p><sparkletree-text field="body">Fired twice and glazed by hand in our studio.</sparkletree-text></p> <sparkletree-live><span>Not live right now</span></sparkletree-live> </sparkletree-island>
<sparkletree-fragments page="product" intent="convert" campaign="CAMPAIGN_ID"> <h2><sparkletree-text field="name" kind="fact" product="stoneware-mug" fact="name">Stoneware mug</sparkletree-text></h2> <p><sparkletree-text field="pitch" kind="body">A heavy mug that keeps coffee warm.</sparkletree-text></p> <p><sparkletree-text field="price" kind="fact" product="stoneware-mug" fact="price">249,00 kr</sparkletree-text></p> <p><sparkletree-text field="stock" kind="fact" product="stoneware-mug" fact="availability" in-stock="I lager" out-of-stock="Slutsåld">Slutsåld</sparkletree-text></p> <p><sparkletree-text field="promo" kind="slot" slot-name="promoLine">Delivery in two days</sparkletree-text></p> </sparkletree-fragments>
<sparkletree-island campaign="CAMPAIGN_ID" island="cta"> <sparkletree-text field="cta"><a href="/shop/stoneware-mug">Buy the mug</a></sparkletree-text> </sparkletree-island> </body></html>/sparkletree.js is one module:
import { defineSparkletreeElements } from "@sparkletree/core/elements";
defineSparkletreeElements();Build it with your bundler. Without one, serve the package’s dist/ folder
from your site and import dist/elements/index.js from it by a relative
path; the folder has no other dependencies.
What happens on this page:
- The hero island streams live copy for its greeting, headline and body. The CTA island streams its button label, puts the campaign’s checked destination on your link, and records the click. Both islands show the same variant of the campaign.
- The fragments group writes
pitchfor its declared placement, reads the mug’s stated name, price and availability (shown in the page’s words, “I lager” or “Slutsåld”), and reads thepromoLinecustom slot of the variant the hero drew. One hold covers all five texts. - The live slot shows the player when a live channel is running, in the box your CSS reserved.
The same markup in other frameworks
Section titled “The same markup in other frameworks”The markup is the same everywhere. Each snippet below changes only what its
framework requires. Load /sparkletree.js with the same tag in the page
head in every case.
Astro. A script tag with attributes other than src is left as written,
so the render-blocking tag goes in the layout’s <head> with is:inline:
---const { handle } = Astro.props;---<head> <script is:inline type="module" blocking="render" src="/sparkletree.js"></script></head><sparkletree-fragments page="product" intent="convert"> <p><sparkletree-text field="pitch" kind="body">A heavy mug that keeps coffee warm.</sparkletree-text></p> <p><sparkletree-text field="price" kind="fact" product={handle} fact="price">249,00 kr</sparkletree-text></p></sparkletree-fragments>Svelte. Write the elements as they are. Use slot-name, not slot:
Svelte reserves the slot attribute.
<script> let { handle } = $props();</script>
<sparkletree-fragments page="product" intent="convert"> <p><sparkletree-text field="pitch" kind="body">A heavy mug that keeps coffee warm.</sparkletree-text></p> <p><sparkletree-text field="price" kind="fact" product={handle} fact="price">249,00 kr</sparkletree-text></p></sparkletree-fragments>Liquid.
<sparkletree-fragments page="product" intent="convert"> <p><sparkletree-text field="pitch" kind="body">{{ product.description | strip_html | truncate: 120 }}</sparkletree-text></p> <p><sparkletree-text field="price" kind="fact" product="{{ product.handle }}" fact="price">{{ product.price | money }}</sparkletree-text></p></sparkletree-fragments>Vue. Tell Vue the elements are not components:
vue({ template: { compilerOptions: { isCustomElement: (tag) => tag.startsWith("sparkletree-") } } })<template> <sparkletree-fragments page="product" intent="convert"> <p><sparkletree-text field="pitch" kind="body">A heavy mug that keeps coffee warm.</sparkletree-text></p> <p><sparkletree-text field="price" kind="fact" :product="handle" fact="price">249,00 kr</sparkletree-text></p> </sparkletree-fragments></template>This works the same with Vue on the server (Nuxt, for example).
React. In a React app, use the React components.
If you write the elements in JSX instead, write them as they are: React
passes every attribute shown here to the element. In TypeScript, declare
the tags in JSX.IntrinsicElements first.
The elements
Section titled “The elements”<sparkletree-island>
Section titled “<sparkletree-island>”One island stream for the <sparkletree-text field="…"> elements inside it.
| Attribute | |
|---|---|
campaign |
The campaign to play. |
surface |
Use instead of campaign when a screen or placement decides what plays. |
island |
hero (the default: greeting, headline, body) or cta (cta). |
hold-ms |
How long the texts hold, once in view, before the served text is final. Defaults to 2500. 0 turns the hold off. |
pin |
false lets this island draw its own variant instead of sharing the page’s. |
name |
Tells apart two islands of the same campaign on one page. |
Each field of the island takes one text with served text in it. A text for another field, a second text for the same field, or an empty one shows as served, and development builds say so.
In a cta island, the campaign’s destination is checked and set as the
href of the <a> around the text or inside it, and a click on it is
recorded. An island records one page view, and one impression when it has
been seen, with what the visitor was shown.
<sparkletree-fragments>
Section titled “<sparkletree-fragments>”One fragments group for the <sparkletree-text field="…"> elements inside it:
one request, one hold, one draw.
| Attribute | |
|---|---|
page, intent, audience, notes |
The declared context, as in fragments. |
campaign, surface |
The default campaign or surface for slot texts that name none. |
hold-ms |
As on the island. Defaults to 2500. 0 turns the hold off. |
<sparkletree-text>
Section titled “<sparkletree-text>”One text. Its served text is the fallback. When it holds a single element
and no text of its own (<sparkletree-text><a href="/start">Start</a></sparkletree-text>),
the copy is written into that element, so your link or emphasis stays and
only its words change.
| Attribute | |
|---|---|
field |
In an island: greeting, headline, body or cta. In a fragments group: the field’s name, which is its key in the request. |
key |
In a fragments group: an older name for field, still read. When both are set, field wins. Vue and React keep key for themselves and do not write it to the element, so use field. |
kind |
In a fragments group: cta, label, headline, body or empty-state for generated copy (body when absent); fact or slot. |
product, fact |
With kind="fact": the product’s id or handle, and the fact (name, price, amount, currency, availability, sku, description). |
in-stock, out-of-stock |
With fact="availability": your words for each state. The words for the stated state land; without them the served text stays. |
slot-name |
With kind="slot": the custom slot’s name. slot is read too, but some frameworks reserve it. |
campaign, surface |
With kind="slot": where to read the slot, over the group’s. |
A text with no field name, an unknown fact, a slot with no name or no served text takes no part: it shows as served. Two texts with the same field name in one group are one field: they show the same copy.
<sparkletree-fact>
Section titled “<sparkletree-fact>”One stated fact on its own, with no group around it. It is a fragments group of one text, the element itself, so it follows the same rules.
<p>Price: <sparkletree-fact product="stoneware-mug" fact="price">249,00 kr</sparkletree-fact></p><p><sparkletree-fact product="stoneware-mug" fact="availability" in-stock="In stock" out-of-stock="Sold out">In stock</sparkletree-fact></p>| Attribute | |
|---|---|
product |
The product’s id or handle. |
fact |
name, price, amount, currency, availability, sku or description. |
in-stock, out-of-stock |
With fact="availability": your words for each state. Without a label for the stated state the served text stays, so availability needs them. Make the served text the longest label. |
hold-ms |
As on the island. |
Facts that start within the same frame share one facts request: a name, a
price and an availability for one product make one request. price is
formatted for the language of the nearest lang attribute, else the
document’s, else en. See Show prices and other facts.
<sparkletree-live>
Section titled “<sparkletree-live>”The live player’s slot. Inside a <sparkletree-island> it shows the
island’s player. On its own, with a campaign or surface attribute, it
opens a stream of its own for the player only, pinned to the variant the
page already shows for that campaign.
Its children show while there is no player. The SDK builds the iframe
itself from the stream’s checked address and never inserts markup from the
stream. A player for the channel that is already playing leaves the iframe
alone, so it does not reload. A page with a Content Security Policy needs
frame-src https://*.cloudflarestream.com.
Configuration
Section titled “Configuration”Each value is read from the first place that has it:
- a
data-sparkletree-key,data-sparkletree-api-baseordata-sparkletree-organizationattribute on the element or any element around it, for example<body data-sparkletree-key="st_pk_live_…">; - the config passed to
defineSparkletreeElements; - a
<meta name="sparkletree-key" content="st_pk_live_…">tag, orsparkletree-api-baseandsparkletree-organization.
As everywhere in the SDK, an explicit organization and API base win over the key’s own for addressing, and the key is still sent as the credential. An element with no working configuration (no key and no organization with an API base, a malformed key, an insecure API base) keeps its served text, and development builds say what to add.
defineSparkletreeElements({ publishableKey: "st_pk_live_…", // or from the page, as above context: { deviceType: "kiosk" }, // merged over what the browser collects, for island streams mintToken: async ({ context, schema }) => getToken(context, schema), // for fragment writes});Every member is optional: publishableKey, apiBase, organizationId,
context, fetchImpl and mintToken. Call it once, as early as you can.
Calling it again is safe: elements already defined are left alone, and the
first config passed is the one used. Where there is no customElements
(server rendering, a worker) it does nothing and returns false, so the
import is safe in code that also runs on a server.
The island streams send the same signals the React provider collects: time of day, day of week, time zone, language, a mobile or desktop flag and the referring origin. A fragments group sends only its declared context.
When copy lands, and when the served text stays
Section titled “When copy lands, and when the served text stays”- While copy is coming, the texts hold. Each text is hidden with
visibility: hidden, its served text in place, so its space is kept and nothing is blanked. - The hold starts when a text is in view and lasts
hold-ms, 2500 ms by default. It restarts once when the stream proves it is alive (an island) or when the server starts writing copy for this view (a fragments group), never past twice the budget. - Copy lands once, inside the hold. An island’s copy lands whole when the stream finishes; a field still being written when the hold runs out, when the stream breaks, or when it stops getting text, is not shown, and its served text stays. The elements never show typing, so the one case where React replaces visible text (a half line it typed) does not arise here. A fragments group lands when its response arrives. Each text lands only if it fits (see the next section); a text that does not fit keeps its served text.
- When the hold runs out, the served text is the copy, final for the page view. Copy that arrives later is not shown. A fragments request still running is stopped; an island’s stream runs on for the theme, the live player and the destination, but writes no text.
- Text the visitor has seen is never replaced. When an attribute
changes (another
campaign, anotherpage), copy that nobody has seen yet is read again at once. Visible copy changes only when the visitor caused it: callmarkVisitorChange()from@sparkletree/corein the event handler before you change the attribute, and the new copy replaces the old once, whole. Otherwise the change waits until the group is off screen. - A group that is created again in the same page view (a client-side navigation, for example) shows what it showed before, with nothing held.
Why the script must run before the first paint
Section titled “Why the script must run before the first paint”The hold works only on text no one has seen. When the module runs before the
browser first paints, every text is held from the moment it is parsed, and
nothing a visitor reads is ever replaced. That is what the tag in <head>
does:
<script type="module" blocking="render" src="/sparkletree.js"></script>type="module" lets the parser go on while the module loads.
blocking="render" keeps the browser from painting until it has run.
Browsers that do not support blocking ignore it.
When the module runs after the first paint, the elements already on the page are checked as they are defined. If the page has painted and any of a group’s texts is in the viewport, or the page has been scrolled, the visitor may have read them: they do not hold, and the served text is final for that group. A group whose texts are all out of view holds as usual. Nothing ever moves either way; the page just shows fewer adapted texts above the fold.
The fit rule is stricter than in React
Section titled “The fit rule is stricter than in React”The elements write no styles on your markup, so they cannot pin a box the
way React’s FragmentText pins its span to the space the fallback held.
Instead the copy is written, the layout is read in the page’s own layout
engine, and the served text is put back before the browser paints if
anything moved. Copy lands only when all of these stay exactly as the
served text set them:
- the box of the nearest block-level element around the text (the text itself when your CSS made it block-level): all four edges;
- the text’s lines: how many, and each line’s top and height;
- the position of the first content after the text inside that block.
So copy that wraps differently, spills out of a fixed-height box, moves the words after it or changes the width of a block that sizes to its content is refused. Shorter copy that would remove a line is refused too, where React would land it in the space it holds. Write served text that is close to the length of the copy you expect there.
The live slot needs a box from your CSS
Section titled “The live slot needs a box from your CSS”sparkletree-live { display: block; aspect-ratio: 16 / 9; }An unknown element lays out inline, and an inline box has no height for a
player to fill. The element never writes display. When it is defined
before the first paint and your CSS sets no aspect ratio, it writes
aspect-ratio: 16 / 9 inline, which still needs display: block from you.
When a player would mount after the page has painted and the slot has no box
of its own (no height, or an inline box), no player mounts: the slot stays
as served, and development builds name the CSS to add.
What a page script can read
Section titled “What a page script can read”document.addEventListener("sparkletree:settled", (event) => { const { source, variantId } = event.detail; console.log(event.target, source, variantId);});sparkletree:settled is dispatched, bubbling, by an island or fragments
group (and a <sparkletree-fact>) when its copy is final. It goes out after
the current task’s code has run, so a listener added by the same script
still hears it. detail.source is the strongest source among the group’s
texts (generated, cached or static; a stated fact is static), and
detail.variantId is the variant for island copy, null otherwise. From
that moment the group also carries data-st-settled with the same source,
so a script that runs later can check instead of waiting. The constants are
exported as SETTLED_EVENT and SETTLED_ATTRIBUTE.
| Where | Attribute | |
|---|---|---|
| Each text | data-content-source |
static until copy lands, then generated, cached, static or stated. |
| Each text | data-st-reserve, data-holding="true" |
While it holds. |
| Island | data-phase |
The stream’s phase. |
| Island | data-st-slot |
The slot id React stamps for the same island and served text. |
| Group | data-st-settled |
Once its copy is final: the source. |
| Live slot | data-live |
waiting or playing. |
| Live slot | data-live-channel |
The playing channel’s id. |
Read-only properties:
| Element | Property | |
|---|---|---|
<sparkletree-island> |
state |
The island’s state, as openStream gives it: theme, ctaAction, live, legal, variantId and the rest. null before it opens. |
<sparkletree-fragments> |
result |
The last fragments response. null before it arrives. |
<sparkletree-fragments> |
marked |
Whether the copy on screen counts as adapted for this visitor. |
<sparkletree-fragments> |
disclosure |
The server’s label for AI-generated copy, when it sent one. |
<sparkletree-fact> |
result |
As on a fragments group. |
What the elements do not do
Section titled “What the elements do not do”- No typing and no fades. Copy replaces the served text in one step.
- No shadow DOM and no stylesheet. Your CSS styles the text. The only
inline property written on your markup is
visibility, while a text holds, and it is removed after. The live slot may getaspect-ratiobefore the first paint. - No theme. The campaign’s colours are data on
island.state.theme. Apply them yourself withapplyTheme(element, state.theme)from@sparkletree/coreif you want them. - No AI label. As everywhere in the SDK. Provenance is in
data-content-source,markedanddisclosure. - No legal lines on the page. They are data on
island.state.legal. Render them in a space your page reserves for them. - No layouts, background image or video. The words are the only part of
the campaign the elements write.
state.backgroundImageandstate.videoUrlare data. - No server reads. There is no
initialCopyorserverRendered: the served text is the fallback, and live copy comes from the stream in the browser. - No session continuity (
sessionId) and no audio.
For any of these, use @sparkletree/core directly,
or the React components.