1- /**
2- * # Splitting
3- * CSS vars for split words & chars!
4- * `Splitting` fn handles array-ifying the
5- * @param {* } els
6- * @param {* } callback
7- */
1+ var Splitting = ( function ( ) {
2+ /**
3+ * # Splitting.$
4+ * Convert selector or NodeList to array for easier manipulation.
5+ *
6+ * @param {* } els - Elements or selector
7+ * @param {* } parent
8+ */
9+ function $ ( els , parent ) {
10+ return Array . prototype . slice . call (
11+ els . nodeName
12+ ? [ els ]
13+ : els [ 0 ] . nodeName ? els : ( parent || document ) . querySelectorAll ( els ) ,
14+ 0
15+ ) ;
16+ }
817
9- function Splitting ( els ) {
10- els = els . nodeName ?
11- [ els ] :
12- els [ 0 ] . nodeName ? els : document . querySelectorAll ( els ) ;
13- return Array . prototype . map . call ( els , function ( el , i ) {
14- if ( ! el . _splitting ) {
15- el . _splitting = {
16- el : el
17- } ;
18- el . className += " splitting" ;
19- }
20- return el . _splitting ;
21- } ) ;
22- }
18+ /**
19+ * # Splitting
20+ * CSS vars for split words & chars!
21+ * `Splitting` fn handles array-ifying the
22+ * @param {* } els
23+ */
24+ function Splitting ( els ) {
25+ return $ ( els ) . map ( function ( el , i ) {
26+ if ( ! el . _splitting ) {
27+ el . _splitting = {
28+ el : el
29+ } ;
30+ el . className += " splitting" ;
31+ }
32+ return el . _splitting ;
33+ } ) ;
34+ }
35+ Splitting . $ = $ ;
2336
24- Splitting . split = function ( el , key , splitBy , space ) {
25- var text = el . innerText ;
26- el . innerHTML = "" ;
37+ /**
38+ * # Splitting.index
39+ * Index split elements and add them to a Splitting instance.
40+ *
41+ * @param {* } s
42+ * @param {* } key
43+ * @param {* } splits
44+ */
45+ function index ( s , key , splits ) {
46+ if ( splits ) {
47+ s [ key + "s" ] = splits ;
48+ s . el . style . setProperty ( "--total-" + key + "s" , splits . length ) ;
49+ splits . map ( function ( el , i ) {
50+ el . style . setProperty ( "--" + key + "-index" , i ) ;
51+ } ) ;
52+ }
53+ return s ;
54+ }
55+ Splitting . index = index ;
2756
28- return text . split ( splitBy ) . map ( function ( split , i ) {
29- var splitEl = document . createElement ( "i" ) ;
30- splitEl . className = key ;
31- splitEl . setAttribute ( "data-" + key , split ) ;
32- splitEl . innerText = ( space && i > 0 ? " " : "" ) + split ;
33- el . appendChild ( splitEl ) ;
34- return splitEl ;
35- } ) ;
36- } ;
57+ /**
58+ * # Splitting.split
59+ * Split an element's innerText into individual elements
60+ * @param {Node } el - Element to split
61+ * @param {String } key -
62+ * @param {String|RegEx } splitBy - string or regex to split the innerText by
63+ * @param {Boolean } space - Add a space to each split if index is greater than 0. Mainly for `Splitting.words`
64+ */
65+ function split ( el , key , splitBy , space ) {
66+ var text = el . innerText ;
67+ el . innerHTML = "" ;
3768
38- Splitting . index = function ( s , key , splits ) {
39- if ( splits ) {
40- s [ key + "s" ] = splits ;
41- s . el . style . setProperty ( "--total-" + key + "s" , splits . length ) ;
42- splits . map ( function ( el , i ) {
43- el . style . setProperty ( "--" + key + "-index" , i ) ;
69+ return text . split ( splitBy ) . map ( function ( split , i ) {
70+ var splitEl = document . createElement ( "i" ) ;
71+ splitEl . className = key ;
72+ splitEl . setAttribute ( "data-" + key , split ) ;
73+ splitEl . innerText = ( space && i > 0 ? " " : "" ) + split ;
74+ el . appendChild ( splitEl ) ;
75+ return splitEl ;
4476 } ) ;
4577 }
46- return s ;
47- } ;
78+ Splitting . split = split ;
4879
49- Splitting . words = function ( els ) {
50- return Splitting ( els ) . map ( function ( s , i ) {
51- return s . words ?
52- s :
53- Splitting . index ( s , "word" , Splitting . split ( s . el , "word" , / \s + / , true ) ) ;
54- } ) ;
55- } ;
80+ /**
81+ * # Splitting.children
82+ * Add CSS Var indexes to a DOM element's children. Useful for lists.
83+ * @param {String|NodeList } parent - Parent element(s) or selector
84+ * @param {String|NodeList } children - Child element(s) or selector
85+ * @param {String } key -
86+ * @example `Splitting.children('ul','li','item'); // Index every unordered list's items with the --item CSS var.`
87+ */
88+ Splitting . children = function ( parent , children , key ) {
89+ return Splitting ( parent ) . map ( function ( s ) {
90+ return index ( s , key , $ ( chidlren , s . el ) ) ;
91+ } ) ;
92+ } ;
93+
94+ Splitting . words = function ( els ) {
95+ return Splitting ( els ) . map ( function ( s , i ) {
96+ return s . words ? s : index ( s , "word" , split ( s . el , "word" , / \s + / , true ) ) ;
97+ } ) ;
98+ } ;
99+
100+ /**
101+ * # Splitting.chars
102+ * Split an element into words and those words into chars
103+ */
104+ Splitting . chars = function ( els ) {
105+ return Splitting . words ( els ) . map ( function ( s ) {
106+ return s . chars
107+ ? s
108+ : index (
109+ s ,
110+ "char" ,
111+ s . words . reduce ( function ( val , word , i ) {
112+ return val . concat ( split ( word , "char" , "" ) ) ;
113+ } , [ ] )
114+ ) ;
115+ } ) ;
116+ } ;
56117
57- Splitting . chars = function ( els ) {
58- return Splitting . words ( els ) . map ( function ( s ) {
59- return s . chars ?
60- s :
61- Splitting . index (
62- s ,
63- "char" ,
64- s . words . reduce ( function ( val , word , i ) {
65- return val . concat ( Splitting . split ( word , "char" , "" ) ) ;
66- } , [ ] )
67- ) ;
68- } ) ;
69- } ;
118+ return Splitting ;
119+ } ) ( ) ;
0 commit comments