StringTune/Docs

Modules

StringSplit

Rebuilds text into lines, words, or characters and exposes deterministic indices for animation.

Type
Built-in module
Status
Stable
Scope
Element-level
Activation
string="split"

StringSplit

StringSplit rebuilds text into line, word, and character wrapper spans, each with CSS variables you can use for animation. It preserves inline HTML inside the split, handles kerning, and can optionally scale text to fill the container width.

Registration

TypeScript
import StringTune, { StringSplit } from '@fiddle-digital/string-tune';

const stringTune = StringTune.getInstance();
stringTune.use(StringSplit);
stringTune.start(60);

Attributes

AttributeTypeDefaultDescription
string-splitstring""Declares which wrappers and alignment variables to build. Also accepts data-string-split.
string-split-restore-afternumber-like stringnot setRestores the original HTML after the given timeout in milliseconds.

Grammar

The string-split attribute controls what gets generated. Full reference: Split Grammar.

Tokens are separated by |. Each token enables a wrapper level and accepts an optional alignment parameter in []:

HTML
string-split="char[start]"
string-split="word[start]|char-word[start]"
string-split="line[center]|char-line[center]"
string-split="line[start]|fit"

Available tokens:

TokenCreates
line.-s-line wrappers
word.-s-word wrappers
word-line.-s-line + .-s-word
char.-s-char wrappers
char-line.-s-line + .-s-char
char-word.-s-word + .-s-char
fitScales text to fill container width

Available alignment params: start, center, end, random(min,max).

Quick Example

HTML
<h1
  class="hero-title"
  string="split"
  string-split="line[center]|char-line[center]"
>
  Crafting<br />Digital<br />Experiences
</h1>
CSS
.hero-title .-s-line {
  overflow: hidden;
  padding-bottom: 0.1em;
}

.hero-title .-s-char {
  display: inline-block;
  transform: translateY(100%);
  animation: charReveal 0.8s cubic-bezier(0.2, 0, 0, 1) forwards;
  animation-delay: calc(
    var(--line-index, 0) * 0.15s + var(--char-index, 0) * 0.03s
  );
}

@keyframes charReveal {
  to { transform: translateY(0); }
}

CSS Variables

Full reference: DOM Output.

On .-s-line

VariableDescription
--line-index0-based line index
--word-totalNumber of words in this line
--fit-font-sizeComputed fit font size (when line-level fit is active)

Plus alignment variables when requested: --line-start, --line-center, --line-end, --line-random.

On .-s-word

VariableDescription
--word-indexGlobal 0-based word index
--char-totalNumber of chars in this word

Plus alignment variables: --word-start, --wordLine-start, etc.

On .-s-char

VariableDescription
--char-indexGlobal 0-based char index
--kerningKerning adjustment between this char and the next (when needed)

Plus alignment variables: --char-start, --charWord-start, --charLine-center, etc.

On the source element

VariableWhen
--char-global-totalChar wrappers are active
--word-global-totalWord wrappers are active
--line-global-totalLine wrappers are active
--fit-font-sizeGlobal fit without line-level split
--fit-scale-yGlobal fit without line-level split
--fit-aspect-ratioGlobal fit without line-level split

Inline HTML

You can use standard inline elements inside split text:

HTML
<h1 string="split" string-split="word[start]|char-word[start]">
  Crafted for <em>motion</em> systems
</h1>

The module preserves your inline markup in the rebuilt DOM. <em>, <strong>, <a>, <span>, and other inline elements keep their tag and attributes so your CSS continues to work.

Elements like <img>, <svg>, and <table> are kept as-is without splitting.

<br> forces a line break.

Full details: Inline HTML.

split-class

<split-class> is a helper element for applying CSS classes to generated wrappers:

HTML
<h2 string="split" string-split="word[start]|char-word[start]">
  <split-class class="is-accent">Signal</split-class> Layer
</h2>

The <split-class> element itself does not appear in the output. Its classes are applied to the generated word or char wrappers that contain its children.

If every char in a word shares the same split-class, the classes are hoisted to the word wrapper.

Fit

Adding fit to string-split scales text to fill the container width:

HTML
<h1 string="split" string-split="line[start]|fit">
  Big Headline
</h1>
CSS
.headline .-s-line {
  font-size: calc(var(--fit-font-size) * 1px);
}

With line-level split, each line gets its own --fit-font-size. Without line-level split, the source element gets --fit-font-size, --fit-scale-y, and --fit-aspect-ratio.

Full details: Fit And Rebuild.

Lifecycle

When the DOM is rebuilt

  • On initial connection
  • On desktop width resize
  • On reconnect

The module caches the original HTML on first split. On each rebuild, it starts from that cached original to produce a clean result.

Mobile

Mobile rebuild is disabled by default. The split DOM stays as initially built when the viewport height or width changes on mobile.

Restore After

HTML
<h1
  string="split"
  string-split="char[start]"
  string-split-restore-after="2000"
>
  Intro Text
</h1>

After 2000ms, the module restores the original HTML and adds class -restored. Useful for one-shot intro effects.

Accessibility

  • All generated wrappers get aria-hidden="true"
  • The source element gets aria-label with the original text
  • Screen readers read the original content, not individual wrapper spans

Events

StringSplit does not emit module-specific events. Consume its output through CSS selectors and variables.

Deep Dives