Skip to content

Commit 02d16e2

Browse files
committed
Switch to spans and outer spaces
1 parent 9d42da9 commit 02d16e2

File tree

5 files changed

+152
-7
lines changed

5 files changed

+152
-7
lines changed

README.md

Lines changed: 140 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,143 @@
22

33
_CSS Vars for split words and chars!_
44

5-
Splitting is a JavaScript microlibrary (less than 1kb) to split a DOM element's words and characters into elements. The word and character elements are populated with CSS vars to assist with CSS transitions and animations that were previously not feasible with CSS.
5+
Splitting is a JavaScript microlibrary (1.2kb min, 0.6kb gzipped) to split a DOM element's words and characters into elements. The word and character elements are populated with CSS vars to assist with transitions and animations that were previously not feasible with CSS.
6+
7+
## Installation
8+
9+
Add Splitting to an existing project with [npm](https://npmjs.org) using `npm install -s splitting` or use [unpkg](https://unpkg.com) with `<script src="https://unpkg.com/splitting/index.js"></script>` for easy embedding on platforms like [Codepen](https://codepen.io).
10+
11+
## Methods
12+
13+
All methods can accept a selector, element, or a NodeList/Array of elements. The parent/targetted element will receive a `splitting` class.
14+
15+
### Splitting.words
16+
17+
Divide an element's `innerText` into words.
18+
19+
Parent element receives a `--total-words` CSS var containing the total number of words. Each word is wrapped in an `<span>` element with a `--word-index` containing the word's position, and a `data-word` attribute containing the actual word.
20+
21+
#### Example
22+
23+
```js
24+
Splitting.wordss("[data-splitting-words]");
25+
```
26+
27+
_Input:_
28+
29+
```html
30+
<h1 data-splitting-words>Splitting words!</h1>
31+
```
32+
33+
_Output:_
34+
35+
```html
36+
<h1 class="splitting" data-splitting-chars style="--total-words:1;">
37+
<span class="word" data-word="Splitting" style="--word-index:0;">Splitting</i>
38+
<span class="word" data-word="words!" style="--word-index:1;">words!</i>
39+
</h1>
40+
```
41+
42+
### Splitting.chars
43+
44+
Divide an element's `innerText` into words and characters. `Splitting.words` is called on the element first to prevent characters from wrapping to the next line unnecessarily, then each word is divided into characters.
45+
46+
Parent element receives a `--total-char` CSS var containing the total number of characters. Each character is wrapped in an `<span>` element with a `--char-index` containing the characters's position, and a `data-char` attribute containing the actual character.
47+
48+
#### Example
49+
50+
```js
51+
Splitting.chars("[data-splitting-chars]");
52+
```
53+
54+
_Input:_
55+
56+
```html
57+
<h1 data-splitting-chars>SPLITTING!</h1>
58+
```
59+
60+
_Output:_
61+
62+
```html
63+
<h1 class="splitting" data-splitting-chars style="--total-words:1; --total-chars:9;">
64+
<span class="word" data-word="SPLITTING!" style="--word-index:0;">
65+
<span class="char" data-char="S" style="--char-index:0;">S</i>
66+
<span class="char" data-char="P" style="--char-index:1;">P</i>
67+
<span class="char" data-char="L" style="--char-index:2;">L</i>
68+
<span class="char" data-char="I" style="--char-index:3;">I</i>
69+
<span class="char" data-char="T" style="--char-index:4;">T</i>
70+
<span class="char" data-char="T" style="--char-index:5;">T</i>
71+
<span class="char" data-char="I" style="--char-index:6;">I</i>
72+
<span class="char" data-char="N" style="--char-index:7;">N</i>
73+
<span class="char" data-char="G" style="--char-index:8;">G</i>
74+
<span class="char" data-char="G" style="--char-index:8;">!</i>
75+
</i>
76+
</h1>
77+
```
78+
79+
### Splitting.children
80+
81+
Apply CSS var indexes to an element's children.
82+
83+
#### Example
84+
85+
```js
86+
Splitting.children(".list", ".list-item", "item");
87+
```
88+
89+
_Input:_
90+
91+
```html
92+
<ul class="list">
93+
<li class="list-item">1</li>
94+
<li class="list-item">2</li>
95+
<li class="list-item">3</li>
96+
</ul>
97+
```
98+
99+
_Output:_
100+
101+
```html
102+
<ul class="list" style="--total-items:3;">
103+
<li class="list-item" style="--item-index:0;">1</li>
104+
<li class="list-item" style="--item-index:1;">2</li>
105+
<li class="list-item" style="--item-index:2;">3</li>
106+
</ul>
107+
```
108+
109+
## Styles
110+
111+
### Recommended Styles
112+
113+
Many CSS properties, like `transform`, will not work on `display: inline` elements, so applying `display: inline-block` will keep your words and characters looking the same, while giving you the most flexibility in transitions and animations.
114+
115+
```css
116+
.splitting .word,
117+
.splitting .char {
118+
display: inline-block;
119+
}
120+
```
121+
122+
### Pseudo Elements
123+
124+
You may have noticed that `Splitting.words` and `Splitting.chars` apply `data-word` and `data-char` attributes, respectively.
125+
126+
This allows for great flexibility in your CSS by using Psuedo elements with `content: attr(data-char)`.
127+
128+
```css
129+
.splitting .char {
130+
position: relative;
131+
}
132+
133+
/* Populate the psuedo elements with the character to allow for expanded effects */
134+
.splitting .char:before,
135+
.splitting .char:after {
136+
content: attr(data-char);
137+
position: absolute;
138+
top: 0;
139+
left: 0;
140+
display: none;
141+
transition: inherit;
142+
user-select: none;
143+
}
144+
```

index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,14 @@ function split(el, key, splitBy, space) {
7272
el.innerHTML = "";
7373

7474
return text.split(splitBy).map(function(split, i) {
75-
var splitEl = document.createElement("i");
75+
var splitEl = document.createElement("span");
7676
splitEl.className = key;
7777
splitEl.setAttribute("data-" + key, split);
78-
splitEl.innerText = (space && i > 0 ? " " : "") + split;
78+
splitEl.innerText = split;
7979
el.appendChild(splitEl);
80+
if (space) {
81+
splitEl.insertAdjacentText("beforebegin", " ");
82+
}
8083
return splitEl;
8184
});
8285
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "splitting",
3-
"version": "0.9.2",
3+
"version": "0.9.3",
44
"description":
55
"Microlibrary to split a DOM element's words & chars into elements populated with CSS vars.",
66
"main": "index.js",

splitting.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,14 @@ function split(el, key, splitBy, space) {
6666
el.innerHTML = "";
6767

6868
return text.split(splitBy).map(function(split, i) {
69-
var splitEl = document.createElement("i");
69+
var splitEl = document.createElement("span");
7070
splitEl.className = key;
7171
splitEl.setAttribute("data-" + key, split);
72-
splitEl.innerText = (space && i > 0 ? " " : "") + split;
72+
splitEl.innerText = split;
7373
el.appendChild(splitEl);
74+
if (space) {
75+
splitEl.insertAdjacentText("beforebegin", " ");
76+
}
7477
return splitEl;
7578
});
7679
}

splitting.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)