Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add index CSS variables to lines, words and chars #58

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export default {
types: ['lines', 'words', 'chars'],
absolute: false,
tagName: 'div',
addIndex: true,
}
1 change: 1 addition & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ declare module 'split-type' {
splitClass: string
types: TypesListString | TypesValue[]
split: TypesListString | TypesValue[]
addIndex: boolean
}

type TargetElement =
Expand Down
5 changes: 3 additions & 2 deletions lib/repositionAfterSplit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 5 additions & 3 deletions lib/splitWordsAndChars.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand Down