Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | import * as dom from '../dom/dom';
export interface textSplitConfig {
element: HTMLElement;
/**
* The criteria of how to split up the text found in the element.
* For example:
* '' would break it by every character.
* ' ' would break it up by spaces (or every word)
*/
split: string;
}
/**
* Splits the textContent of a given htmlElement and provides css variables
* that can be used to add various text effects.
*
*
* Here is a basic example.
* ```ts
*
* <div id="myElement">New York City</div>
*
* let splitter = new TextSplit({
* element: document.getElementById('myElement'),
* split: ' ' // Split by spaces. Use '' to split by character.
* });
*
* // Break up the text and wrap spans around it.
* splitter.split();
* ```
*
* This results in:
* ```ts
* <div id="myElement" class="text-split-set" style="--item-total-count:3">
* <span style="--item-index:0">New </span>
* <span style="--item-index:1">York </span>
* <span style="--item-index:2">City</span>
* </div>
*
* ```
*
* Notice that on the root element, you get the total number of items,
* and also on each element, you get an index.
*
* Now you can use css variables to add various effect.
*
* ```
* @keyframes textIn
* from
* opacity: 0;
* to
* opacity: 1;
*
* // Help fight FOUC
* #myElement
* visiblity: hidden
* #myElement.text-split-set
* visibility: visible
*
* #myElement span,
* position: relative
* font-size: 20px
* opacity: 0
* animation: textIn 1.3s ease-in-out 0s 1 forwards
*
* // Create a 0.2s stagger between each item.
* animation-delay: calc(var(--item-index) * 0.2s
*
* // More advanced. You may not know how many words or character there are
* // but you want to the animation to finish in Xs seconds.
* // Using item-total-count, you can break the subdivisions.
* // Here no matter how many words/characters there are, set it so that the
* // stagger animation always completes in 1s.
* animation-delay: calc(var(--item-index) * (1s / var(--item-total-count)))
* ```
*
*/
export class TextSplit {
public originalText: string;
public splits: Array<string>;
public convertSpacesToNbsp: boolean;
private sups: Array<HTMLElement> | null;
constructor(private config: textSplitConfig) {
this.convertSpacesToNbsp = this.config.split === ' ';
// Patch to get around <sup> at the end of sentances for now.
// This is rather hacky and assumes all <sup> in the text
// are at the end of sentances.
this.sups = Array.from(this.config.element.querySelectorAll('sup'));
// Remove sups.
this.sups.forEach((sup) => {
dom.removeElement(sup);
});
// this.originalText = this.config.element.textContent!;
this.originalText = this.config.element.textContent!;
// Convert any   to space.
if (this.convertSpacesToNbsp) {
const re = new RegExp(String.fromCharCode(160), 'g');
this.originalText = this.originalText.replace(re, ' ');
}
this.splits = this.originalText.trim().split(this.config.split);
}
/**
* Splits the text and wraps <span> around each sub item.
*/
split() {
this.config.element.innerHTML = '';
this.splits.forEach((word, i) => {
// Add spaces to the end of each word if it's not the last one.
if (this.convertSpacesToNbsp && i !== this.splits.length - 1) {
word += ' ';
}
const element = dom.createElementFromString(`<span>${word}</span>`);
dom.setCssVariable(element, '--item-index', i + '');
this.config.element.appendChild(element);
});
// Futher append any sups.
this.sups &&
this.sups.forEach((sup, i) => {
const span = dom.createElementFromString('<span></span>');
dom.setCssVariable(span, '--item-index', this.splits.length + i + '');
span.appendChild(sup);
this.config.element.appendChild(span);
});
// Add total count to root element.
dom.setCssVariable(
this.config.element,
'--item-total-count',
this.splits.length + ''
);
this.config.element.classList.add('text-split-set');
}
}
|