Skip to content

Text Flow Examples

The examples below assume that you have imported the component and set up the theme.

Basic

Animate text from one string to the next. The text inside <sv-text-flow> is the first string, and ::part(text) sets its font size.

Each new value from the controls rolls the characters to it.

Reduced Motion

Your system is set to reduce motion, so the animations shown on this page will not play for you.

0
Value
0
Animation
400
Style
1.2
Sets line-height .
0.2lh
Sets --sv-text-flow-fade-sz . The fade shows only while characters roll.
<div class="text-flow-basic">
<sv-text-flow class="flow">0</sv-text-flow>
</div>
<style>
.text-flow-basic {
display: grid;
place-items: center;
.flow::part(text) {
font-size: 2rem;
}
}
</style>

Smart Diff

Keep unchanged characters still when a string changes in the middle. use-diff compares by edit distance instead of by index.

rgb(255, 0, 50) default
rgb(255, 0, 50) use-diff
<div class="text-flow-smart-diff">
<div class="variant">
<sv-text-flow class="flow">rgb(255, 0, 50)</sv-text-flow>
<span>default</span>
</div>
<div class="variant">
<sv-text-flow class="flow" use-diff>rgb(255, 0, 50)</sv-text-flow>
<span>use-diff</span>
</div>
<button type="button" class="btn btn-primary swap-button">Swap</button>
<script type="application/x-typescript" module>
const colors = ["rgb(255, 0, 50)", "rgb(0, 0, 50)"];
const flows = document.querySelectorAll<TextFlow>(".text-flow-smart-diff .flow");
const swapButton = document.querySelector<HTMLButtonElement>(
".text-flow-smart-diff .swap-button"
)!;
swapButton.addEventListener("click", () => {
for (const flow of flows) {
flow.value = colors.find(color => color !== flow.value)!;
}
});
</script>
</div>
<style>
.text-flow-smart-diff {
display: grid;
gap: 1.25em;
justify-items: center;
.variant {
display: grid;
gap: 0.4em;
justify-items: center;
span {
font-size: 0.8em;
color: var(--sv-gray-3);
}
}
.flow::part(text) {
font-family: monospace;
font-size: 1.25rem;
}
}
</style>