idea-react 3.0.0-rc.3 → 3.0.0-rc.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +93 -5
- package/dist/index.d.mts +98 -33
- package/dist/index.d.ts +98 -33
- package/dist/index.js +13 -13
- package/dist/index.mjs +12 -12
- package/package.json +10 -10
- package/source/Base/Carousel/index.tsx +98 -78
- package/source/Base/Carousel/model.ts +245 -65
- package/source/Base/Carousel/scroll.ts +174 -28
- package/source/Extension/PageNav.tsx +8 -7
|
@@ -1,14 +1,84 @@
|
|
|
1
|
-
import { action, computed, observable } from 'mobx';
|
|
1
|
+
import { action, computed, observable, reaction } from 'mobx';
|
|
2
2
|
|
|
3
3
|
export type CarouselEnds = 'loop' | 'wrap' | 'stop';
|
|
4
4
|
|
|
5
|
-
export
|
|
5
|
+
export interface CarouselModelOptions {
|
|
6
|
+
intervals: number[];
|
|
7
|
+
ends: CarouselEnds;
|
|
8
|
+
onChange?: (index: number) => unknown;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface CarouselMotion {
|
|
12
|
+
index: number;
|
|
13
|
+
from: number;
|
|
14
|
+
left: number;
|
|
15
|
+
smooth: boolean;
|
|
16
|
+
loop?: -1 | 1;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface CarouselLayout {
|
|
20
|
+
positions: number[];
|
|
21
|
+
widths: number[];
|
|
22
|
+
viewport: number;
|
|
23
|
+
maximum: number;
|
|
24
|
+
rtl: boolean;
|
|
25
|
+
items: number;
|
|
26
|
+
peek: number;
|
|
27
|
+
center: boolean;
|
|
28
|
+
variableWidth: boolean;
|
|
29
|
+
reducedMotion: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export abstract class CarouselModel {
|
|
33
|
+
abstract readonly fade: boolean;
|
|
34
|
+
|
|
35
|
+
get navigating() {
|
|
36
|
+
return Boolean(this.motion?.smooth);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
@observable accessor motion: CarouselMotion | undefined;
|
|
40
|
+
|
|
6
41
|
@observable accessor activeIndex = 0;
|
|
7
42
|
|
|
8
43
|
@observable accessor playing = false;
|
|
9
44
|
|
|
45
|
+
@observable accessor indexes: number[] = [];
|
|
46
|
+
|
|
47
|
+
@computed
|
|
48
|
+
get atStart() {
|
|
49
|
+
return this.activeIndex === this.indexes[0];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
@computed
|
|
53
|
+
get atEnd() {
|
|
54
|
+
return this.activeIndex === this.indexes.at(-1);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
@computed
|
|
58
|
+
get canPrevious() {
|
|
59
|
+
return this.options.ends !== 'stop' || !this.atStart;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
@computed
|
|
63
|
+
get canNext() {
|
|
64
|
+
return this.options.ends !== 'stop' || !this.atEnd;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
@computed
|
|
68
|
+
get interval() {
|
|
69
|
+
return this.options.intervals[this.activeIndex] ?? 5000;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
@observable protected accessor options: CarouselModelOptions;
|
|
73
|
+
|
|
74
|
+
protected layout?: CarouselLayout;
|
|
75
|
+
|
|
76
|
+
protected positions: number[] = [];
|
|
77
|
+
|
|
10
78
|
private timer?: ReturnType<typeof setTimeout>;
|
|
11
79
|
|
|
80
|
+
private stopPlayback?: () => void;
|
|
81
|
+
|
|
12
82
|
@observable private accessor hovering = false;
|
|
13
83
|
@observable private accessor visible = true;
|
|
14
84
|
|
|
@@ -17,101 +87,211 @@ export class CarouselModel {
|
|
|
17
87
|
return this.playing && !this.hovering && this.visible;
|
|
18
88
|
}
|
|
19
89
|
|
|
20
|
-
|
|
90
|
+
constructor(options: CarouselModelOptions) {
|
|
91
|
+
this.options = options;
|
|
92
|
+
this.indexes = Array.from({ length: options.intervals.length }, (_, index) => index);
|
|
93
|
+
}
|
|
21
94
|
|
|
22
|
-
|
|
95
|
+
mount() {
|
|
96
|
+
if (this.stopPlayback) return;
|
|
23
97
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
98
|
+
this.stopPlayback = reaction(
|
|
99
|
+
() => [this.cycling && this.indexes.length > 1, this.activeIndex, this.interval],
|
|
100
|
+
this.schedule,
|
|
101
|
+
{
|
|
102
|
+
fireImmediately: true,
|
|
103
|
+
equals: (next, previous) => next.every((value, index) => value === previous[index])
|
|
104
|
+
}
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
@action
|
|
109
|
+
restore(previous: CarouselModel) {
|
|
110
|
+
this.activeIndex = previous.activeIndex;
|
|
111
|
+
this.playing = previous.playing;
|
|
112
|
+
this.hovering = previous.hovering;
|
|
113
|
+
this.visible = previous.visible;
|
|
114
|
+
}
|
|
29
115
|
|
|
30
116
|
@action
|
|
31
117
|
setIndex = (index: number, userInitiated = false, direction?: -1 | 1, fromScroll = false) => {
|
|
32
|
-
|
|
118
|
+
if (!this.navigating || fromScroll) {
|
|
119
|
+
const bounded = Math.max(0, Math.min(index, this.options.intervals.length - 1));
|
|
120
|
+
const nextIndex = this.indexes.findLast(candidate => candidate <= bounded) ?? 0;
|
|
121
|
+
|
|
122
|
+
if (nextIndex !== this.activeIndex) {
|
|
123
|
+
const from = this.activeIndex;
|
|
124
|
+
this.activeIndex = nextIndex;
|
|
125
|
+
this.options.onChange?.(nextIndex);
|
|
33
126
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
this.activeIndex = nextIndex;
|
|
37
|
-
this.onChange?.(nextIndex);
|
|
38
|
-
if (!fromScroll) this.onNavigate?.(nextIndex, from, direction);
|
|
127
|
+
if (!fromScroll) this.motion = this.createMotion?.(nextIndex, from, direction);
|
|
128
|
+
}
|
|
39
129
|
}
|
|
40
130
|
if (userInitiated) this.pause();
|
|
41
|
-
else this.schedule();
|
|
42
131
|
};
|
|
43
132
|
|
|
44
|
-
|
|
45
|
-
next = (userInitiated = false) => {
|
|
46
|
-
if (this.activeIndex === this.count - 1 && this.ends === 'stop') {
|
|
47
|
-
if (!userInitiated) this.pause();
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
this.setIndex(
|
|
51
|
-
this.activeIndex === this.count - 1 ? 0 : this.activeIndex + 1,
|
|
52
|
-
userInitiated,
|
|
53
|
-
1
|
|
54
|
-
);
|
|
55
|
-
};
|
|
133
|
+
next = (userInitiated = false) => this.move(1, userInitiated);
|
|
56
134
|
|
|
57
|
-
|
|
58
|
-
previous = (userInitiated = false) => {
|
|
59
|
-
if (this.activeIndex === 0 && this.ends === 'stop') {
|
|
60
|
-
if (!userInitiated) this.pause();
|
|
61
|
-
return;
|
|
62
|
-
}
|
|
63
|
-
this.setIndex(
|
|
64
|
-
this.activeIndex === 0 ? this.count - 1 : this.activeIndex - 1,
|
|
65
|
-
userInitiated,
|
|
66
|
-
-1
|
|
67
|
-
);
|
|
68
|
-
};
|
|
135
|
+
previous = (userInitiated = false) => this.move(-1, userInitiated);
|
|
69
136
|
|
|
70
137
|
@action
|
|
71
|
-
play = () =>
|
|
72
|
-
this.playing = true;
|
|
73
|
-
this.schedule();
|
|
74
|
-
};
|
|
138
|
+
play = () => (this.playing = true);
|
|
75
139
|
|
|
76
140
|
@action
|
|
77
|
-
pause = () =>
|
|
78
|
-
this.playing = false;
|
|
79
|
-
this.clearTimer();
|
|
80
|
-
};
|
|
141
|
+
pause = () => (this.playing = false);
|
|
81
142
|
|
|
82
143
|
@action
|
|
83
144
|
toggle = () => (this.playing ? this.pause() : this.play());
|
|
84
145
|
|
|
85
|
-
setHovering = action((hovering: boolean) =>
|
|
86
|
-
|
|
87
|
-
|
|
146
|
+
setHovering = action((hovering: boolean) => (this.hovering = hovering));
|
|
147
|
+
|
|
148
|
+
setVisible = action((visible: boolean) => (this.visible = visible));
|
|
149
|
+
|
|
150
|
+
@action
|
|
151
|
+
update(options: CarouselModelOptions) {
|
|
152
|
+
if (options.intervals.length !== this.options.intervals.length) this.interrupt();
|
|
153
|
+
|
|
154
|
+
this.options = options;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
refresh = action((layout?: CarouselLayout, align?: boolean) => {
|
|
158
|
+
const previous = this.positions;
|
|
159
|
+
this.layout = layout;
|
|
160
|
+
this.positions =
|
|
161
|
+
layout?.positions.map(left =>
|
|
162
|
+
layout.viewport
|
|
163
|
+
? Math.max(
|
|
164
|
+
layout.rtl ? -layout.maximum : 0,
|
|
165
|
+
Math.min(left, layout.rtl ? 0 : layout.maximum)
|
|
166
|
+
)
|
|
167
|
+
: left
|
|
168
|
+
) ?? [];
|
|
169
|
+
const indexes = this.readIndexes(layout?.viewport ? this.positions : undefined);
|
|
170
|
+
if (
|
|
171
|
+
indexes.length !== this.indexes.length ||
|
|
172
|
+
indexes.some((index, i) => index !== this.indexes[i])
|
|
173
|
+
)
|
|
174
|
+
this.indexes = indexes;
|
|
175
|
+
|
|
176
|
+
this.setIndex(this.activeIndex, false, undefined, true);
|
|
177
|
+
|
|
178
|
+
const changed =
|
|
179
|
+
previous.length !== this.positions.length ||
|
|
180
|
+
this.positions.some((left, index) => Math.abs(left - previous[index]) > 1);
|
|
181
|
+
|
|
182
|
+
if ((align ?? changed) && layout && this.positions[this.activeIndex] !== undefined)
|
|
183
|
+
this.motion = this.createMotion?.(this.activeIndex, this.activeIndex);
|
|
88
184
|
});
|
|
89
185
|
|
|
90
|
-
|
|
91
|
-
this.
|
|
92
|
-
|
|
186
|
+
syncScroll = action((left: number) => {
|
|
187
|
+
if (this.navigating) return;
|
|
188
|
+
|
|
189
|
+
const index = this.indexes.reduce<number | undefined>(
|
|
190
|
+
(closest, index) =>
|
|
191
|
+
closest === undefined ||
|
|
192
|
+
Math.abs(this.positions[index] - left) < Math.abs(this.positions[closest] - left)
|
|
193
|
+
? index
|
|
194
|
+
: closest,
|
|
195
|
+
undefined
|
|
196
|
+
);
|
|
197
|
+
if (index !== undefined && index !== this.activeIndex)
|
|
198
|
+
this.setIndex(index, true, undefined, true);
|
|
93
199
|
});
|
|
94
200
|
|
|
95
|
-
|
|
96
|
-
this.
|
|
97
|
-
|
|
98
|
-
this.interval = interval;
|
|
201
|
+
complete = action((motion: CarouselMotion) => {
|
|
202
|
+
if (this.motion === motion) this.motion = undefined;
|
|
203
|
+
});
|
|
99
204
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
205
|
+
interrupt = action(() => (this.motion = undefined));
|
|
206
|
+
|
|
207
|
+
dispose() {
|
|
208
|
+
this.stopPlayback?.();
|
|
209
|
+
this.stopPlayback = undefined;
|
|
210
|
+
this.clearTimer();
|
|
211
|
+
this.interrupt();
|
|
212
|
+
}
|
|
103
213
|
|
|
104
|
-
|
|
214
|
+
protected readIndexes(positions = this.options.intervals) {
|
|
215
|
+
return [...positions.keys()];
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
protected createMotion?: (
|
|
219
|
+
index: number,
|
|
220
|
+
from: number,
|
|
221
|
+
direction?: -1 | 1
|
|
222
|
+
) => CarouselMotion | undefined;
|
|
223
|
+
|
|
224
|
+
private advance = () => {
|
|
225
|
+
const index = this.activeIndex;
|
|
226
|
+
this.next();
|
|
227
|
+
if (index === this.activeIndex) this.schedule();
|
|
228
|
+
};
|
|
105
229
|
|
|
106
230
|
private schedule = () => {
|
|
107
231
|
this.clearTimer();
|
|
108
|
-
if (!this.cycling || this.count < 2) return;
|
|
109
232
|
|
|
110
|
-
|
|
233
|
+
if (this.cycling && this.indexes.length > 1)
|
|
234
|
+
this.timer = setTimeout(this.advance, Math.max(0, this.interval));
|
|
111
235
|
};
|
|
112
236
|
|
|
237
|
+
@action
|
|
238
|
+
private move(direction: -1 | 1, userInitiated: boolean) {
|
|
239
|
+
const { indexes } = this;
|
|
240
|
+
if (indexes.length < 2) return;
|
|
241
|
+
|
|
242
|
+
if (!(direction === 1 ? this.canNext : this.canPrevious)) {
|
|
243
|
+
if (!userInitiated) this.pause();
|
|
244
|
+
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
const position =
|
|
248
|
+
(indexes.indexOf(this.activeIndex) + direction + indexes.length) % indexes.length;
|
|
249
|
+
|
|
250
|
+
this.setIndex(indexes[position], userInitiated, direction);
|
|
251
|
+
}
|
|
252
|
+
|
|
113
253
|
private clearTimer = () => {
|
|
114
|
-
if (this.timer) clearTimeout(this.timer);
|
|
254
|
+
if (this.timer !== undefined) clearTimeout(this.timer);
|
|
115
255
|
this.timer = undefined;
|
|
116
256
|
};
|
|
117
257
|
}
|
|
258
|
+
|
|
259
|
+
export class ScrollCarouselModel extends CarouselModel {
|
|
260
|
+
readonly fade = false;
|
|
261
|
+
|
|
262
|
+
protected readIndexes(positions?: number[]) {
|
|
263
|
+
return positions
|
|
264
|
+
? positions.flatMap((left, index) =>
|
|
265
|
+
!index || Math.abs(left - positions[index - 1]) > 1 ? [index] : []
|
|
266
|
+
)
|
|
267
|
+
: super.readIndexes();
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
protected createMotion = (index: number, from: number, direction?: -1 | 1) => {
|
|
271
|
+
const { layout, positions } = this;
|
|
272
|
+
if (!layout) return undefined;
|
|
273
|
+
|
|
274
|
+
const crossingEnd = (direction === 1 && this.atStart) || (direction === -1 && this.atEnd);
|
|
275
|
+
const canLoop =
|
|
276
|
+
this.options.ends === 'loop' &&
|
|
277
|
+
!layout.reducedMotion &&
|
|
278
|
+
layout.items === 1 &&
|
|
279
|
+
layout.peek === 0 &&
|
|
280
|
+
!layout.center &&
|
|
281
|
+
!layout.variableWidth &&
|
|
282
|
+
(!layout.viewport ||
|
|
283
|
+
layout.widths.every(width => Math.abs(width - layout.viewport) <= 1));
|
|
284
|
+
|
|
285
|
+
return {
|
|
286
|
+
index,
|
|
287
|
+
from,
|
|
288
|
+
left: positions[index],
|
|
289
|
+
smooth: index !== from && !layout.reducedMotion,
|
|
290
|
+
loop: crossingEnd && canLoop ? direction : undefined
|
|
291
|
+
};
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
export class FadeCarouselModel extends CarouselModel {
|
|
296
|
+
readonly fade = true;
|
|
297
|
+
}
|
|
@@ -1,31 +1,184 @@
|
|
|
1
|
+
import { CarouselLayout, CarouselMotion } from './model';
|
|
2
|
+
|
|
3
|
+
export interface CarouselScrollEvents {
|
|
4
|
+
onLayout: (layout: CarouselLayout, align?: boolean) => unknown;
|
|
5
|
+
onScroll: (left: number) => unknown;
|
|
6
|
+
onComplete: (motion: CarouselMotion) => unknown;
|
|
7
|
+
onResize: () => unknown;
|
|
8
|
+
}
|
|
9
|
+
|
|
1
10
|
export class CarouselScroll {
|
|
11
|
+
private root?: HTMLElement;
|
|
12
|
+
|
|
13
|
+
private track?: HTMLElement;
|
|
14
|
+
|
|
15
|
+
private center = false;
|
|
16
|
+
|
|
2
17
|
private frame?: number;
|
|
3
18
|
|
|
4
19
|
private clone?: HTMLElement;
|
|
5
20
|
|
|
6
|
-
private
|
|
21
|
+
private snap?: string;
|
|
22
|
+
|
|
23
|
+
private events?: CarouselScrollEvents;
|
|
24
|
+
|
|
25
|
+
private media?: MediaQueryList;
|
|
7
26
|
|
|
8
|
-
private
|
|
27
|
+
private resizeObserver?: ResizeObserver;
|
|
9
28
|
|
|
10
|
-
|
|
29
|
+
private observedSlides = new Set<Element>();
|
|
30
|
+
|
|
31
|
+
get navigating() {
|
|
11
32
|
return Boolean(this.clone) || this.frame !== undefined;
|
|
12
33
|
}
|
|
13
34
|
|
|
14
|
-
|
|
15
|
-
this.
|
|
35
|
+
mount(root: HTMLElement, track: HTMLElement, events: CarouselScrollEvents, center = false) {
|
|
36
|
+
this.center = center;
|
|
37
|
+
|
|
38
|
+
if (this.root !== root || this.track !== track) {
|
|
39
|
+
this.dispose();
|
|
40
|
+
this.root = root;
|
|
41
|
+
this.track = track;
|
|
42
|
+
track.addEventListener('scroll', this.handleScroll, { passive: true });
|
|
43
|
+
this.resizeObserver = new window.ResizeObserver(this.handleResize);
|
|
44
|
+
this.media = window.matchMedia('(prefers-reduced-motion: reduce)');
|
|
45
|
+
this.media.addEventListener('change', this.handleResize);
|
|
46
|
+
}
|
|
47
|
+
this.events = events;
|
|
48
|
+
this.refresh();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
refresh = (align?: boolean) => {
|
|
52
|
+
this.observeLayout();
|
|
53
|
+
|
|
54
|
+
const { root, track } = this;
|
|
55
|
+
if (!root || !track || this.navigating) return;
|
|
56
|
+
|
|
57
|
+
const slides = [...track.children] as HTMLElement[];
|
|
58
|
+
const style = getComputedStyle(root);
|
|
59
|
+
|
|
60
|
+
this.events?.onLayout(
|
|
61
|
+
{
|
|
62
|
+
positions: slides.map(slide => this.position(track, slide)),
|
|
63
|
+
widths: slides.map(slide => slide.getBoundingClientRect().width),
|
|
64
|
+
viewport: track.clientWidth,
|
|
65
|
+
maximum: Math.max(0, track.scrollWidth - track.clientWidth),
|
|
66
|
+
rtl: getComputedStyle(track).direction === 'rtl',
|
|
67
|
+
items: Number.parseFloat(style.getPropertyValue('--bs-carousel-items')) || 1,
|
|
68
|
+
peek: Number.parseFloat(style.getPropertyValue('--bs-carousel-items-peek')) || 0,
|
|
69
|
+
center: this.center,
|
|
70
|
+
variableWidth: slides.some(slide =>
|
|
71
|
+
Boolean(slide.style.width || slide.style.flexBasis)
|
|
72
|
+
),
|
|
73
|
+
reducedMotion: window.matchMedia('(prefers-reduced-motion: reduce)').matches
|
|
74
|
+
},
|
|
75
|
+
align
|
|
76
|
+
);
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
dispose() {
|
|
80
|
+
this.interrupt();
|
|
81
|
+
this.track?.removeEventListener('scroll', this.handleScroll);
|
|
82
|
+
this.resizeObserver?.disconnect();
|
|
83
|
+
this.media?.removeEventListener('change', this.handleResize);
|
|
84
|
+
this.resizeObserver = undefined;
|
|
85
|
+
this.media = undefined;
|
|
86
|
+
this.observedSlides.clear();
|
|
87
|
+
this.root = this.track = undefined;
|
|
88
|
+
this.events = undefined;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
apply = (motion?: CarouselMotion) => {
|
|
92
|
+
this.interrupt();
|
|
93
|
+
const { track } = this;
|
|
94
|
+
|
|
95
|
+
if (!motion || !track) return this.refresh();
|
|
96
|
+
|
|
97
|
+
const slides = [...track.children] as HTMLElement[];
|
|
98
|
+
const complete = () => this.events?.onComplete(motion);
|
|
99
|
+
|
|
100
|
+
if (motion.loop)
|
|
101
|
+
this.loop(track, slides[motion.from], slides[motion.index], motion, complete);
|
|
102
|
+
else this.navigate(track, motion.left, motion.smooth, complete);
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
interrupt = () => {
|
|
106
|
+
if (this.frame !== undefined) window.cancelAnimationFrame(this.frame);
|
|
107
|
+
this.frame = undefined;
|
|
108
|
+
this.clone?.remove();
|
|
109
|
+
this.clone = undefined;
|
|
110
|
+
if (this.track && this.snap !== undefined) this.track.style.scrollSnapType = this.snap;
|
|
111
|
+
this.snap = undefined;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
private observeLayout = () => {
|
|
115
|
+
const { track, resizeObserver } = this;
|
|
116
|
+
if (!track || !resizeObserver) return;
|
|
117
|
+
|
|
118
|
+
resizeObserver.observe(track);
|
|
119
|
+
|
|
120
|
+
const slides = new Set(
|
|
121
|
+
[...track.children].filter(slide => !slide.hasAttribute('data-carousel-clone'))
|
|
122
|
+
);
|
|
123
|
+
for (const slide of this.observedSlides)
|
|
124
|
+
if (!slides.has(slide)) resizeObserver.unobserve(slide);
|
|
125
|
+
for (const slide of slides)
|
|
126
|
+
if (!this.observedSlides.has(slide)) resizeObserver.observe(slide);
|
|
127
|
+
this.observedSlides = slides;
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
private handleResize = () => this.events?.onResize();
|
|
131
|
+
|
|
132
|
+
private handleScroll = () => {
|
|
133
|
+
const { track } = this;
|
|
134
|
+
if (!track || this.navigating) return;
|
|
135
|
+
|
|
136
|
+
this.refresh(false);
|
|
137
|
+
this.events?.onScroll(track.scrollLeft);
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
private position = (track: HTMLElement, slide: HTMLElement) => {
|
|
141
|
+
if (!track.clientWidth) return slide.offsetLeft;
|
|
142
|
+
|
|
143
|
+
const style = getComputedStyle(track);
|
|
144
|
+
const rtl = style.direction === 'rtl';
|
|
145
|
+
const bounds = track.getBoundingClientRect();
|
|
146
|
+
const padding =
|
|
147
|
+
Number.parseFloat(rtl ? style.scrollPaddingRight : style.scrollPaddingLeft) || 0;
|
|
148
|
+
|
|
149
|
+
const rect = slide.getBoundingClientRect();
|
|
150
|
+
const alignment = this.center
|
|
151
|
+
? (track.clientWidth - rect.width) / 2
|
|
152
|
+
: rtl
|
|
153
|
+
? track.clientWidth - padding - rect.width
|
|
154
|
+
: padding;
|
|
155
|
+
|
|
156
|
+
return track.scrollLeft + rect.left - bounds.left - track.clientLeft - alignment;
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
private navigate = (
|
|
160
|
+
track: HTMLElement,
|
|
161
|
+
left: number,
|
|
162
|
+
smooth: boolean,
|
|
163
|
+
complete: () => unknown
|
|
164
|
+
) => {
|
|
16
165
|
if (!smooth || track.scrollLeft === left) {
|
|
17
166
|
track.scrollTo({ left, behavior: 'instant' });
|
|
18
|
-
|
|
167
|
+
|
|
168
|
+
return complete();
|
|
19
169
|
}
|
|
20
|
-
this.track = track;
|
|
21
170
|
this.snap = track.style.scrollSnapType;
|
|
22
171
|
track.style.scrollSnapType = 'none';
|
|
23
|
-
this.animate(track, track.scrollLeft, left);
|
|
172
|
+
this.animate(track, track.scrollLeft, left, complete);
|
|
24
173
|
};
|
|
25
174
|
|
|
26
|
-
loop = (
|
|
27
|
-
|
|
28
|
-
|
|
175
|
+
private loop = (
|
|
176
|
+
track: HTMLElement,
|
|
177
|
+
from: HTMLElement,
|
|
178
|
+
destination: HTMLElement,
|
|
179
|
+
motion: CarouselMotion,
|
|
180
|
+
complete: () => unknown
|
|
181
|
+
) => {
|
|
29
182
|
this.snap = track.style.scrollSnapType;
|
|
30
183
|
|
|
31
184
|
const clone = destination.cloneNode(true) as HTMLElement;
|
|
@@ -40,24 +193,26 @@ export class CarouselScroll {
|
|
|
40
193
|
this.clone = clone;
|
|
41
194
|
track.style.scrollSnapType = 'none';
|
|
42
195
|
|
|
43
|
-
if (
|
|
196
|
+
if (motion.loop === 1) track.append(clone);
|
|
44
197
|
else track.prepend(clone);
|
|
45
198
|
|
|
46
|
-
const start = from
|
|
47
|
-
const end = clone
|
|
199
|
+
const start = this.position(track, from);
|
|
200
|
+
const end = this.position(track, clone);
|
|
48
201
|
|
|
49
202
|
track.scrollTo({ left: start, behavior: 'instant' });
|
|
50
203
|
this.animate(track, start, end, () => {
|
|
51
|
-
this.
|
|
52
|
-
track.scrollTo({ left:
|
|
204
|
+
this.interrupt();
|
|
205
|
+
track.scrollTo({ left: motion.left, behavior: 'instant' });
|
|
206
|
+
complete();
|
|
53
207
|
});
|
|
54
208
|
};
|
|
55
209
|
|
|
56
|
-
private animate = (track: HTMLElement, start: number, end: number, complete
|
|
57
|
-
|
|
210
|
+
private animate = (track: HTMLElement, start: number, end: number, complete: () => unknown) => {
|
|
211
|
+
let startedAt: number | undefined;
|
|
58
212
|
|
|
59
213
|
const animate = (time: number) => {
|
|
60
|
-
|
|
214
|
+
startedAt ??= time;
|
|
215
|
+
const progress = Math.max(0, Math.min(1, (time - startedAt) / 400));
|
|
61
216
|
const eased = 1 - (1 - progress) ** 3;
|
|
62
217
|
|
|
63
218
|
track.scrollTo({ left: start + (end - start) * eased, behavior: 'instant' });
|
|
@@ -67,13 +222,4 @@ export class CarouselScroll {
|
|
|
67
222
|
};
|
|
68
223
|
this.frame = window.requestAnimationFrame(animate);
|
|
69
224
|
};
|
|
70
|
-
|
|
71
|
-
dispose = () => {
|
|
72
|
-
if (this.frame !== undefined) window.cancelAnimationFrame(this.frame);
|
|
73
|
-
this.frame = undefined;
|
|
74
|
-
this.clone?.remove();
|
|
75
|
-
this.clone = undefined;
|
|
76
|
-
if (this.track) this.track.style.scrollSnapType = this.snap;
|
|
77
|
-
this.track = undefined;
|
|
78
|
-
};
|
|
79
225
|
}
|
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
import classNames from 'classnames';
|
|
4
4
|
import { action, computed, observable } from 'mobx';
|
|
5
5
|
import { observer } from 'mobx-react';
|
|
6
|
-
import { Component, ElementType } from 'react';
|
|
7
|
-
import {
|
|
6
|
+
import { Component, ElementType, MouseEvent } from 'react';
|
|
7
|
+
import { uniqueID } from 'web-utility';
|
|
8
8
|
|
|
9
9
|
import { Nav, NavLink, NavProps } from '../Base/Nav';
|
|
10
10
|
import { AsProps, PolymorphicClassProps } from '../Helper/Polymorphic';
|
|
@@ -104,13 +104,14 @@ export class PageNav<T extends ElementType | undefined = 'nav'> extends Componen
|
|
|
104
104
|
window.removeEventListener('resize', this.updateScrollY);
|
|
105
105
|
}
|
|
106
106
|
|
|
107
|
-
|
|
108
|
-
|
|
107
|
+
handleItemClick = ({ currentTarget }: MouseEvent<HTMLAnchorElement>) => {
|
|
108
|
+
const id = currentTarget.hash.slice(1);
|
|
109
|
+
const meta = this.list.find(item => item.id === id);
|
|
109
110
|
|
|
110
|
-
this.componentProps.onItemClick?.(meta);
|
|
111
|
+
if (meta) this.componentProps.onItemClick?.(meta);
|
|
111
112
|
};
|
|
112
113
|
|
|
113
|
-
renderLink = ({ id, level, text
|
|
114
|
+
renderLink = ({ id, level, text }: HeadingMeta) => {
|
|
114
115
|
const { currentActiveId } = this,
|
|
115
116
|
{ depth = Infinity, minDepth = 1 } = this.componentProps;
|
|
116
117
|
|
|
@@ -126,7 +127,7 @@ export class PageNav<T extends ElementType | undefined = 'nav'> extends Componen
|
|
|
126
127
|
style={{ paddingInlineStart: `${level - minDepth + 0.75}rem` }}
|
|
127
128
|
href={`#${id}`}
|
|
128
129
|
aria-current={currentActiveId === id ? 'location' : undefined}
|
|
129
|
-
onClick={this.
|
|
130
|
+
onClick={this.handleItemClick}
|
|
130
131
|
>
|
|
131
132
|
{text}
|
|
132
133
|
</NavLink>
|