diff --git a/lib/defaults.js b/lib/defaults.js index edd7d52..f279a7f 100644 --- a/lib/defaults.js +++ b/lib/defaults.js @@ -6,4 +6,5 @@ export default { types: ['lines', 'words', 'chars'], absolute: false, tagName: 'div', + addIndex: true, } diff --git a/lib/index.d.ts b/lib/index.d.ts index b0aa48e..9848cd5 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -14,6 +14,7 @@ declare module 'split-type' { splitClass: string types: TypesListString | TypesValue[] split: TypesListString | TypesValue[] + addIndex: boolean } type TargetElement = diff --git a/lib/repositionAfterSplit.js b/lib/repositionAfterSplit.js index 8c4db29..ef31935 100644 --- a/lib/repositionAfterSplit.js +++ b/lib/repositionAfterSplit.js @@ -119,11 +119,12 @@ export default function repositionAfterSplit(element, settings, scrollPos) { // Iterate over lines of text (see 11 b) // Let `line` be the array of words in the current line. // Return an array of the wrapped line elements (lineElements) - lines = wordsInEachLine.map((wordsInThisLine) => { + lines = wordsInEachLine.map((wordsInThisLine, idx) => { // Create an element to wrap the current line. + const lineIndex = settings.addIndex ? `--index-line: ${idx};` : '' const lineElement = createElement(TAG_NAME, { class: `${settings.splitClass} ${settings.lineClass}`, - style: `display: block; text-align: ${align}; width: 100%;`, + style: `${lineIndex} display: block; text-align: ${align}; width: 100%;`, }) data.set(lineElement, 'isLine', true) diff --git a/lib/splitWordsAndChars.js b/lib/splitWordsAndChars.js index dfb8721..eff3ed4 100644 --- a/lib/splitWordsAndChars.js +++ b/lib/splitWordsAndChars.js @@ -45,10 +45,11 @@ export default function splitWordsAndChars(textNode, settings) { // -> If splitting text into characters... if (types.chars) { // Iterate through the characters in the current word - characterElementsForCurrentWord = toChars(WORD).map((CHAR) => { + characterElementsForCurrentWord = toChars(WORD).map((CHAR, idxChar) => { + const charIndex = settings.addIndex ? `--index-char: ${idxChar};` : '' const characterElement = createElement(TAG_NAME, { class: `${settings.splitClass} ${settings.charClass}`, - style: 'display: inline-block;', + style: `${charIndex} display: inline-block;`, children: CHAR, }) data.set(characterElement, 'isChar', true) @@ -63,9 +64,10 @@ export default function splitWordsAndChars(textNode, settings) { // splitting text into characters, the word element will contain the // wrapped character nodes for this word. If not, it will contain the // plain text content (WORD) + const wordIndex = settings.addIndex ? `--index-word: ${idx};` : '' wordElement = createElement(TAG_NAME, { class: `${settings.wordClass} ${settings.splitClass}`, - style: `display: inline-block; ${ + style: `${wordIndex} display: inline-block; ${ types.words && settings.absolute ? `position: relative;` : '' }`, children: types.chars ? characterElementsForCurrentWord : WORD,