Skip to content

Commit

Permalink
Splitting.lines!
Browse files Browse the repository at this point in the history
  • Loading branch information
shshaw committed Feb 8, 2018
1 parent 837a9ae commit 5fe304a
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,20 @@ _Output:_

---

# Splitting.lines(el)

Splits an element by words, if not already done so, and adds an extra `--line-index` variable to each `.word`. The parent element also gets a `--line-total` var.

_Does not update automatically on resize._ You'll need to attach your own event listeners _with debouncing_ for when the element's line width may have changed. Simply call `Splitting.lines` again on the element and the indexes will be updated.

### Example

```js
Splitting.lines("p[data-splitting]");
```

---

# Splitting.fromString(str, opts)

Split a string and receive back the HTML as a string for splitting before hitting the DOM. Useful for frameworks like Vue where you may be splitting a dynamic value.
Expand Down
29 changes: 29 additions & 0 deletions splitting.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,35 @@ Splitting.chars = function(els) {
});
};

/**
* # Splitting.lines
* Index each word by line. Does not automatically update on resize, so retrigger `Splitting.lines` again _with debouncing_ when the element's line width may have changed.
* @param {*} els
*/
Splitting.lines = function(els) {
return Splitting.words(els).map(function(s) {
var lines = [],
lineIndex = -1,
top,
lastTop = 0;

s.words.map(function(w) {
top = w.offsetTop;
if (top > lastTop) {
lineIndex++;
lastTop = top;
}
lines[lineIndex] = lines[lineIndex] || [];
lines[lineIndex].push(w);
w.style.setProperty("--line-index", lineIndex);
});

s.lines = lines;
s.el.style.setProperty("--line-total", lines.length);
return s;
});
};

/**
* # Splitting.fromString
* Splits a string and returns the processed HTML with elements and CSS Vars.
Expand Down
2 changes: 1 addition & 1 deletion splitting.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions src/splitting.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,35 @@ Splitting.chars = function(els) {
});
};

/**
* # Splitting.lines
* Index each word by line. Does not automatically update on resize, so retrigger `Splitting.lines` again _with debouncing_ when the element's line width may have changed.
* @param {*} els
*/
Splitting.lines = function(els) {
return Splitting.words(els).map(function(s) {
var lines = [],
lineIndex = -1,
top,
lastTop = 0;

s.words.map(function(w) {
top = w.offsetTop;
if (top > lastTop) {
lineIndex++;
lastTop = top;
}
lines[lineIndex] = lines[lineIndex] || [];
lines[lineIndex].push(w);
w.style.setProperty("--line-index", lineIndex);
});

s.lines = lines;
s.el.style.setProperty("--line-total", lines.length);
return s;
});
};

/**
* # Splitting.fromString
* Splits a string and returns the processed HTML with elements and CSS Vars.
Expand Down

0 comments on commit 5fe304a

Please sign in to comment.