Class SplitText

SplitText splits text within a container into individual lines, words, and letters.

Features:

  • Supports resizing, HTML content, and special symbols like emojis.
  • Handles multi-line breaks and non-breaking spaces.
  • Saves initial nodes for easy restoration.
  • Allows splitting into lines, words, or letters as needed.

Note: Apply fontKerning: none to prevent layout shifts.

Documentation

Hierarchy (View Summary)

Constructors

  • Initializes the SplitText instance and saves the initial state.

    Parameters

    • Optionalprops: ISplitTextStaticProps & ISplitTextMutableProps & Partial<
          {
              onBeforeSplit: (data: undefined, ctx: SplitText) => void;
              onDestroy: (data: undefined, ctx: SplitText) => void;
              onProps: (data: Partial, ctx: SplitText) => void;
              onSplit: (data: undefined, ctx: SplitText) => void;
          },
      >
    • OptionalonCallbacks: Partial<
          {
              onBeforeSplit: (data: undefined, ctx: SplitText) => void;
              onDestroy: (data: undefined, ctx: SplitText) => void;
              onProps: (data: Partial, ctx: SplitText) => void;
              onSplit: (data: undefined, ctx: SplitText) => void;
          },
      >

    Returns SplitText

Accessors

  • get isDestroyed(): boolean

    Checks if the module has been destroyed.

    Returns boolean

  • get name(): string

    The name of the module, derived from the class name

    Returns string

Methods

  • Adds a class name on an element, and keeps track of it for removal when the module is destroyed.

    Parameters

    • element: Element

      The target DOM element.

    • className: string

      The class name to toggle.

    Returns void

  • Helper function to generate classnames with the module's prefix.

    Parameters

    • ...classNames: string[]

      The class names to generate.

    Returns string

    A string of class names with the module's prefix applied.

  • Get default static properties.

    Returns {
        __staticProp: true;
        ariaLabel: string | boolean;
        container: HTMLElement;
        ignore: string | HTMLElement[] | (element: HTMLElement) => boolean;
        letterClass: string;
        letters: boolean;
        letterTag: keyof HTMLElementTagNameMap;
        lineClass: string;
        lines: boolean;
        linesWrapper: boolean;
        lineTag: keyof HTMLElementTagNameMap;
        lineWrapperClass: string;
        prepareText: (text: string) => string;
        resizeDebounce: number;
        wordClass: string;
        wordDelimiter: string;
        wordDelimiterOutput: string;
        wordTag: keyof HTMLElementTagNameMap;
    }

    • __staticProp: true
    • ariaLabel: string | boolean

      Allow aria-label. false prevents adding aria-label attributes to container. String value overrides the default aria-label.

      true
      
    • container: HTMLElement

      The text container where the text will be split.

    • ignore: string | HTMLElement[] | (element: HTMLElement) => boolean

      Do not split certain elements. Supports string selectors, array of elements, or function.

      null
      
    • letterClass: string

      Letter class name.

      v-split-text__letter

    • letters: boolean

      Specifies whether the text should be split into individual letters.

      false
      
    • letterTag: keyof HTMLElementTagNameMap

      HTML tag to wrap each letter.

      span

    • lineClass: string

      Line class name.

      v-split-text__line

    • lines: boolean

      Specifies whether the text should be split into lines.

      false
      
    • linesWrapper: boolean

      Specifies whether to wrap each line in an additional container.

      false
      
    • lineTag: keyof HTMLElementTagNameMap

      HTML tag to wrap each line.

      span

    • lineWrapperClass: string

      Line wrapper class name.

      v-split-text__line-wrapper

    • prepareText: (text: string) => string

      Optional callback to preprocess text before it is split into words. This function receives the original text and should return the modified text. It is useful for languages like Chinese where standard word splitting may not work correctly.

      const segmenter = new Intl.Segmenter('zh', { granularity: 'word' });

      const instance = new SplitText({
      container,
      prepareText: (source) => [...segmenter.segment(source)].map((s) => s.segment).join(' '),
      });
    • resizeDebounce: number

      The debounce delay for the resize event in milliseconds.

      0
      
    • wordClass: string

      Word class name.

      v-split-text__word

    • wordDelimiter: string

      Specifies a custom delimiter used to split text into words. By default, splitting occurs on regular whitespace.

      " "
      
    • wordDelimiterOutput: string

      Provides an alternative delimiter to use when outputting the split words. Useful when a custom input delimiter is used but the output should differ.

      null
      
    • wordTag: keyof HTMLElementTagNameMap

      HTML tag to wrap each word.

      span

  • Adds a callback on the module's destruction.

    Parameters

    • action: () => void

      The function to execute during destruction.

    Returns void