-
Notifications
You must be signed in to change notification settings - Fork 661
/
Copy pathuseCollapsable.ts
194 lines (164 loc) · 5.56 KB
/
useCollapsable.ts
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
import SwipeHandler from '../components/swipeHandler';
import IS_TOUCH_SUPPORTED from '../environment/touchSupport';
import {animateSingle, cancelAnimationByKey} from '../helpers/animation';
import {CancellablePromise} from '../helpers/cancellablePromise';
import cancelEvent from '../helpers/dom/cancelEvent';
import findUpClassName from '../helpers/dom/findUpClassName';
import liteMode from '../helpers/liteMode';
import clamp from '../helpers/number/clamp';
import debounce from '../helpers/schedulers/debounce';
import {subscribeOn} from '../helpers/solid/subscribeOn';
import {createSignal, createMemo, onCleanup, createEffect} from 'solid-js';
const STATE_FOLDED = 1;
const STATE_UNFOLDED = 0;
export function useCollapsable(props: {
scrollable: () => HTMLElement,
listenWheelOn: HTMLElement,
container: () => HTMLElement,
shouldIgnore?: () => boolean,
skipAnimationClassName?: string,
disableHoverWhenFolded?: boolean
}) {
const [progress, _setProgress] = createSignal(STATE_FOLDED);
const [isTransition, setIsTransition] = createSignal(false);
const folded = createMemo(() => progress() === STATE_FOLDED);
const setProgress = (progress: number, skipAnimation?: boolean) => {
if(liteMode.isAvailable('animations') && !skipAnimation) setIsTransition(true);
_setProgress(progress);
};
const scrollTo = (wasProgress: number, open?: boolean) => {
const startTime = Date.now();
const _animation = animation = animateSingle(() => {
const value = clamp((Date.now() - startTime) / 125, 0, 1);
let progress = wasProgress;
if((wasProgress > 0.5 || open === false) && open !== true) {
progress += (1 - wasProgress) * value;
animationOpening = false;
} else {
animationOpening = true;
progress -= wasProgress * value;
}
setProgress(progress);
return value < 1;
}, props.container()).finally(() => {
if(_animation === animation) {
animation = undefined;
}
});
};
const clearAnimation = () => {
cancelAnimationByKey(props.container());
};
let animation: CancellablePromise<void>, animationOpening: boolean;
const onScrolled = () => {
return;
const wasProgress = progress();
if(wasProgress >= 1 || wasProgress <= 0) {
return;
}
scrollTo(wasProgress);
};
const debounced = debounce(onScrolled, 75, false, true);
const onMove = (delta: number, e?: WheelEvent | TouchEvent) => {
const scrollTop = props.scrollable().scrollTop;
const isWheel = e instanceof WheelEvent;
if(isWheel || true) {
const newState = delta < 0 ? STATE_UNFOLDED : STATE_FOLDED;
if((scrollTop && progress() !== STATE_UNFOLDED) || debounced.isDebounced()) {
debounced();
return;
}
if(progress() === newState) {
return;
}
e && cancelEvent(e);
setProgress(newState);
return;
}
const wasProgress = progress();
props.container().classList.add(props.skipAnimationClassName);
// if user starts to scroll down when it's being opened
if(delta > 0 && animation && animationOpening) {
debounced.clearTimeout();
scrollTo(wasProgress, false);
return;
}
if(
animation ||
(wasProgress >= STATE_FOLDED && delta > 0) ||
(wasProgress <= STATE_UNFOLDED && delta <= 0)/* ||
(scrollTop && progress() !== STATE_UNFOLDED) */
) {
return;
}
// if(animation) {
// cancelEvent(e);
// return;
// }
let value = delta / 600;
value = clamp(wasProgress + value, 0, 1);
setProgress(value);
if(value >= 1 || value <= 0) {
debounced.clearTimeout();
onScrolled();
} else {
e && cancelEvent(e);
debounced();
}
};
const onWheel = (e: WheelEvent) => {
if(props.shouldIgnore?.()) {
return;
}
const wheelDeltaY = (e as any).wheelDeltaY as number;
const delta: number = -wheelDeltaY;
onMove(delta, e);
};
subscribeOn(props.listenWheelOn)('wheel', onWheel, {passive: false});
if(IS_TOUCH_SUPPORTED) {
const swipeHandler = new SwipeHandler({
element: props.listenWheelOn,
onSwipe: (xDiff, yDiff, e) => {
const delta = -yDiff;
onMove(delta, e as any as TouchEvent);
},
cancelEvent: false,
cursor: '',
verifyTouchTarget: (e) => {
return e instanceof TouchEvent && !props.shouldIgnore?.() && !findUpClassName(e.target, 'folders-tabs-scrollable');
}
});
onCleanup(() => {
swipeHandler.removeListeners();
});
}
const unfold = (e?: MouseEvent) => {
const wasProgress = progress();
if(wasProgress !== STATE_UNFOLDED) {
// scrollTo(wasProgress, true);
clearAnimation();
setProgress(STATE_UNFOLDED);
e && cancelEvent(e);
}
};
const fold = () => {
scrollTo(progress(), false);
};
createEffect(() => {
const container = props.container();
if(!container) {
return;
}
container.classList.toggle('disable-hover', (props.disableHoverWhenFolded ? folded() : false) || isTransition());
props.skipAnimationClassName && container.classList.toggle(props.skipAnimationClassName, folded() && !isTransition());
});
createEffect(() => {
const container = props.container();
if(!container) {
return;
}
subscribeOn(container)('transitionstart', (e) => e.target === container && setIsTransition(true));
subscribeOn(container)('transitionend', (e) => e.target === container && setIsTransition(false));
});
return {folded, unfold, fold, progress, clearAnimation, isTransition, STATE_FOLDED, STATE_UNFOLDED};
}