-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
204 lines (163 loc) · 5.46 KB
/
index.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
export default (root) => {
const frame = root.querySelector('[data-flexscroll-frame]');
const viewport = root.querySelector('[data-flexscroll-viewport]');
const prev = root.querySelector('[data-flexscroll-prev]');
const next = root.querySelector('[data-flexscroll-next]');
let active = [];
let items, gotos;
const getLoop = () => {
return getComputedStyle(viewport).getPropertyValue('--flexscroll-loop') === 'true';
};
const getMove = () => {
return getComputedStyle(viewport).getPropertyValue('--flexscroll-move');
};
const getDirection = () => {
return getComputedStyle(viewport).getPropertyValue('flex-direction');
};
const getAlign = (index) => {
return getComputedStyle(items[index]).getPropertyValue('scroll-snap-align');
};
const getProgress = () => {
const direction = getDirection();
let current, max;
if (direction === 'row') {
current = Math.abs(viewport.scrollLeft);
max = viewport.scrollWidth - viewport.clientWidth;
}
if (direction === 'column') {
current = viewport.scrollTop;
max = viewport.scrollHeight - viewport.clientHeight;
}
return max === 0 ? -1 : Math.round(current / max * 100) / 100;
};
const getIndex = (type) => {
const loop = getLoop();
const progress = getProgress();
const min = 0;
const max = items.length - 1;
if (type === 'prev') {
const target = active[0] - 1;
return Number.isInteger(target) ? (target >= min ? target : (loop && progress === 0 ? max : min)) : false;
}
if (type === 'next') {
const target = active[active.length - 1] + 1;
return Number.isInteger(target) ? (target <= max ? target : (loop && progress === 1 ? min : max)) : false;
}
};
const getPositions = (index) => {
const direction = getDirection();
const item = items[index];
const styles = getComputedStyle(viewport);
const rtl = direction === 'row' && styles.getPropertyValue('direction') === 'rtl';
let target, offset, start, end;
if (direction === 'row') {
target = item.offsetLeft;
start = parseInt(styles.getPropertyValue('scroll-padding-inline-start'), 10) || 0;
end = parseInt(styles.getPropertyValue('scroll-padding-inline-end'), 10) || 0;
offset = viewport.offsetWidth - item.offsetWidth + (rtl ? end - start : start - end);
}
if (direction === 'column') {
target = item.offsetTop;
start = parseInt(styles.getPropertyValue('scroll-padding-block-start'), 10) || 0;
end = parseInt(styles.getPropertyValue('scroll-padding-block-end'), 10) || 0;
offset = viewport.offsetHeight - item.offsetHeight + start - end;
}
return {
start: rtl ? target + end - offset : target - start,
end: rtl ? target - start : target + end - offset,
center: target - offset / 2,
}
};
const setScroll = (index, type = null) => {
const move = getMove();
const direction = getDirection();
const positions = getPositions(index);
let align = getAlign(index);
if (move && type === 'prev') {
if (move === 'one') {
align = 'start';
}
if (move === 'all') {
align = 'end';
}
}
if (move && type === 'next') {
if (move === 'one') {
align = 'end';
}
if (move === 'all') {
align = 'start';
}
}
const x = direction === 'row' ? positions[align] : 0;
const y = direction === 'column' ? positions[align] : 0;
viewport.scroll(x, y);
};
const setDisabled = () => {
const loop = getLoop();
const progress = getProgress();
const start = progress === 0 || progress === -1;
const end = progress === 1 || progress === -1;
prev?.toggleAttribute('disabled', !loop && start);
next?.toggleAttribute('disabled', !loop && end);
};
const init = () => {
items = viewport.querySelectorAll(':scope > *');
gotos = root.querySelectorAll('[data-flexscroll-goto]');
for (const item of items) {
observer.observe(item);
}
};
const observer = new IntersectionObserver((entries) => {
for (const entry of entries) {
const { target, isIntersecting } = entry;
const index = [...items].indexOf(target);
if (isIntersecting) {
active.push(index);
target.removeAttribute('inert');
gotos[index]?.setAttribute('data-flexscroll-goto', '');
} else {
active = active.filter((i) => i !== index);
target.setAttribute('inert', '');
gotos[index]?.setAttribute('data-flexscroll-goto', 'inert');
}
active = [...new Set(active)].sort((a, b) => a - b);
root.dispatchEvent(new CustomEvent('change', {
detail: {
items,
active,
},
}));
setDisabled();
}
}, {
root: frame || viewport,
rootMargin: '1px',
threshold: 1,
});
if (prev) {
prev.addEventListener('click', () => {
const index = getIndex('prev');
Number.isInteger(index) && setScroll(index, 'prev');
});
}
if (next) {
next.addEventListener('click', () => {
const index = getIndex('next');
Number.isInteger(index) && setScroll(index, 'next');
});
}
if (prev || next) {
viewport.addEventListener('scroll', setDisabled);
setDisabled();
}
root.addEventListener('click', (e) => {
const { target } = e;
if (target.hasAttribute('data-flexscroll-goto')) {
const index = [...gotos].indexOf(target);
setScroll(index);
}
});
root.addEventListener('update', init);
init();
};