|
2 | 2 |
|
3 | 3 | _CSS Vars for split words and chars!_ |
4 | 4 |
|
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 | +``` |
0 commit comments