-
Notifications
You must be signed in to change notification settings - Fork 1
/
kicss.js
247 lines (220 loc) · 6.64 KB
/
kicss.js
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import { performInterpolation, purgeRangeCache } from './lib/interpolation.js'
import { getCurrentScript, getScriptParameters } from './lib/helpers.js'
import validations from './lib/validations.js'
const setCSSProperty = (key, value, element = window.document.documentElement) => {
element.style.setProperty(key, value)
}
const reportResponsiveVariable = (name, valueFn, scope) => {
window.addEventListener('resize', () => setCSSProperty(name, valueFn(), scope))
window.addEventListener('orientationchange', () => setCSSProperty(name, valueFn(), scope))
setCSSProperty(name, valueFn(), scope)
}
const reportPageCursor = (event) => {
let x, y
if (event.touches) {
x = event.touches.item(0).clientX
y = event.touches.item(0).clientY
} else {
x = event.x
y = event.y
}
setCSSProperty('--cursor-x', `${x}px`)
setCSSProperty('--cursor-y',`${y}px`)
const { innerWidth, innerHeight } = window
setCSSProperty('--cursor-x-1', x / innerWidth)
setCSSProperty('--cursor-y-1', y / innerHeight)
}
const reportPageScroll = ({ direction, interpolations }) => () => {
validations.reportPageScroll({ direction, interpolations })
const { scrollTop, scrollLeft } = document.documentElement
setCSSProperty('--scroll-x', scrollLeft)
setCSSProperty('--scroll-y', scrollTop)
const { scrollWidth, scrollHeight } = document.documentElement
const { innerWidth, innerHeight } = window
setCSSProperty('--scroll-x-1', scrollLeft / (scrollWidth - innerWidth))
setCSSProperty('--scroll-y-1', scrollTop / (scrollHeight - innerHeight))
if (interpolations) {
interpolations.forEach((interpolation, index) => {
const { interpolationName, interpolated, scope } = performInterpolation({
interpolation,
id: index,
value: direction === 'horizontal' ? scrollLeft : scrollTop
})
setCSSProperty(interpolationName, interpolated, scope)
})
}
}
const reportScroll = (...args) => (event) => {
validations.reportScroll(...args)
let name
let direction = 'vertical'
let interpolations
if (typeof args[0] === 'string') {
name = args[0]
if (typeof args[1] === 'string') {
direction = args[1]
} else if (typeof args[1] === 'object') {
direction = args[1].direction || direction
interpolations = args[1].interpolations
}
} else if (typeof args[0] === 'object') {
name = args[0].name
direction = args[0].direction
interpolations = args[0].interpolations
}
const { target } = event
let absoluteScroll
let targetScrollSize
let targetSize
if (direction === 'horizontal') {
absoluteScroll = target.scrollLeft
targetScrollSize = target.scrollWidth
targetSize = target.clientWidth
} else if (direction === 'vertical') {
absoluteScroll = target.scrollTop
targetScrollSize = target.scrollHeight
targetSize = target.clientHeight
}
setCSSProperty(name, absoluteScroll)
setCSSProperty(`${name}-1`, absoluteScroll / (targetScrollSize - targetSize))
if (interpolations) {
interpolations.forEach((interpolation, index) => {
const { interpolationName, interpolated, scope } = performInterpolation({
interpolation,
id: index,
value: absoluteScroll
})
setCSSProperty(interpolationName, interpolated, scope)
})
}
}
const reportVariable = (...args) => {
validations.reportVariable(...args)
let name
let value
let scope
if (typeof args[0] === 'string') {
name = args[0]
if (typeof args[1] === 'function') {
return reportResponsiveVariable(name, args[1])
}
if (typeof args[1] === 'object') {
scope = args[1].scope
value = args[1].value
if (typeof args[1].value === 'function') {
return reportResponsiveVariable(name, args[1].value, scope)
}
} else {
value = args[1]
}
setCSSProperty(name, value, scope)
} else if (typeof args[0] === 'object') {
name = args[0].name
value = args[0].value
scope = args[0].scope
if (typeof value === 'function') {
return reportResponsiveVariable(name, value, scope)
}
setCSSProperty(name, value, scope)
}
}
const reportIndex = (selector, {
indexVariableName = '--index',
rowIndexVariableName = '--row-index',
rowIndexBy
} = {
indexVariableName: '--index',
rowIndexVariableName: '--row-index'
}) => {
const elements = Array.from(document.querySelectorAll(selector))
elements.forEach((element, index) => {
setCSSProperty(indexVariableName, index, element)
if (typeof rowIndexBy === 'number') {
const rowIndex = Math.floor(index / rowIndexBy)
setCSSProperty(rowIndexVariableName, rowIndex, element)
}
})
}
const cursor = () => {
window.addEventListener('mousemove', reportPageCursor)
window.addEventListener('touchmove', reportPageCursor)
reportPageCursor({ x: 0, y: 0 })
}
const time = () => {
const reportSeconds = () => {
const seconds = (Date.now() - start) / 1000
reportVariable('--seconds', seconds)
}
const reportMilliseconds = () => {
const milliseconds = (Date.now() - start)
reportVariable('--milliseconds', milliseconds)
millisecondsLoop = requestAnimationFrame(reportMilliseconds)
}
let start = Date.now()
let secondsLoop = window.setInterval(reportSeconds, 1000)
let millisecondsLoop = requestAnimationFrame(reportMilliseconds)
return {
clear() {
window.clearInterval(secondsLoop)
window.cancelAnimationFrame(millisecondsLoop)
}
}
}
const reportGlobals = ({ scroll, cursor } = { scroll: true, cursor: true }) => {
if (cursor) {
window.addEventListener('mousemove', reportPageCursor)
window.addEventListener('touchmove', reportPageCursor)
reportPageCursor({ x: 0, y: 0 })
}
if (scroll) {
let interpolations = scroll.interpolations
let direction = scroll.direction
window.addEventListener('scroll', reportPageScroll({
direction,
interpolations
}))
window.addEventListener('resize', (e) => {
purgeRangeCache()
reportPageScroll({
direction,
interpolations
})(e)
})
reportPageScroll({
direction,
interpolations
})()
}
}
const currentScript = getCurrentScript('kicss.js')
if (currentScript) {
const queryParameters = getScriptParameters(currentScript)
if (queryParameters && queryParameters.report) {
const globalsToReport = queryParameters.report
reportGlobals(globalsToReport)
}
window.kicss = {
reportScroll,
reportVariable,
reportIndex,
reportGlobals,
cursor,
time
}
}
export {
reportScroll,
reportVariable,
reportIndex,
reportGlobals,
cursor,
time
}
export default {
reportScroll,
reportVariable,
reportIndex,
reportGlobals,
cursor,
time
}