idea-react 3.0.0-alpha.1 → 3.0.0-beta.1

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.
Files changed (107) hide show
  1. package/AGENTS.md +11 -2
  2. package/ReadMe.md +86 -46
  3. package/dist/DatePickerPlugin-CpGuQRsi.js +1 -0
  4. package/dist/DatePickerPlugin-DB3nnslN.mjs +1 -0
  5. package/dist/index.d.mts +1069 -245
  6. package/dist/index.d.ts +1069 -245
  7. package/dist/index.js +16 -16
  8. package/dist/index.mjs +15 -15
  9. package/dist/rolldown-runtime-5iZMYShA.js +1 -0
  10. package/dist/style.css +69 -66
  11. package/package.json +4 -2
  12. package/source/Accordion.tsx +137 -0
  13. package/source/Alert.tsx +138 -0
  14. package/source/Avatar.tsx +7 -1
  15. package/source/Badge.tsx +2 -0
  16. package/source/Breadcrumb.tsx +36 -0
  17. package/source/Button.tsx +40 -5
  18. package/source/ButtonGroup.tsx +10 -1
  19. package/source/Card.tsx +73 -4
  20. package/source/Carousel/index.tsx +391 -0
  21. package/source/Carousel/model.ts +113 -0
  22. package/source/Carousel/scroll.ts +79 -0
  23. package/source/Collapse.tsx +178 -0
  24. package/source/Drawer.tsx +127 -30
  25. package/source/{CodeBlock.tsx → Extension/CodeBlock/index.tsx} +23 -5
  26. package/source/Extension/CodeBlock/style.module.css +3 -0
  27. package/source/{Dialog.tsx → Extension/Dialog.tsx} +10 -6
  28. package/source/{EditorHTML.tsx → Extension/EditorHTML.tsx} +1 -1
  29. package/source/{HorizontalMarquee → Extension/HorizontalMarquee}/index.module.less +1 -3
  30. package/source/{Icon.tsx → Extension/Icon.tsx} +1 -7
  31. package/source/{Loading.tsx → Extension/Loading.tsx} +8 -3
  32. package/source/{MonthCalendar.tsx → Extension/MonthCalendar.tsx} +3 -3
  33. package/source/{Nameplate.tsx → Extension/Nameplate.tsx} +1 -1
  34. package/source/Extension/OverlayBox.tsx +280 -0
  35. package/source/{PageNav.tsx → Extension/PageNav.tsx} +27 -9
  36. package/source/{ScrollNav → Extension/ScrollNav}/index.tsx +1 -1
  37. package/source/Extension/Select.tsx +127 -0
  38. package/source/{ShareBox.tsx → Extension/ShareBox.tsx} +1 -1
  39. package/source/{SpinnerButton.tsx → Extension/SpinnerButton.tsx} +2 -2
  40. package/source/{TableSpinner.tsx → Extension/TableSpinner.tsx} +1 -1
  41. package/source/{Timeline → Extension/Timeline}/index.tsx +1 -1
  42. package/source/{UserRank → Extension/UserRank}/index.tsx +4 -4
  43. package/source/Extension/index.ts +27 -0
  44. package/source/Form/Adorn.tsx +59 -0
  45. package/source/Form/Checkbox.tsx +78 -0
  46. package/source/Form/ChipInput.tsx +125 -0
  47. package/source/Form/Chips.tsx +69 -0
  48. package/source/Form/Combobox.tsx +374 -0
  49. package/source/Form/Control.tsx +94 -0
  50. package/source/Form/DatePicker.tsx +280 -0
  51. package/source/Form/DatePickerPlugin.ts +37 -0
  52. package/source/Form/Feedback.tsx +35 -0
  53. package/source/Form/FloatingLabel.tsx +32 -0
  54. package/source/Form/InputGroup.tsx +173 -0
  55. package/source/Form/OTP.tsx +141 -0
  56. package/source/Form/PasswordStrength.tsx +183 -0
  57. package/source/Form/Plugin.tsx +53 -0
  58. package/source/Form/Primitives.tsx +88 -0
  59. package/source/Form/Radio.tsx +43 -0
  60. package/source/Form/Range.tsx +116 -0
  61. package/source/Form/Switch.tsx +58 -0
  62. package/source/Form/index.ts +16 -0
  63. package/source/Grid.tsx +4 -0
  64. package/source/ListGroup.tsx +28 -15
  65. package/source/Menu.tsx +226 -17
  66. package/source/NativeDialog/index.tsx +109 -5
  67. package/source/Nav.tsx +18 -2
  68. package/source/NavOverflow/index.tsx +307 -0
  69. package/source/NavOverflow/model.ts +21 -0
  70. package/source/Navbar.tsx +31 -6
  71. package/source/Overlay.ts +163 -1
  72. package/source/Pagination.tsx +92 -0
  73. package/source/Placeholder.tsx +66 -0
  74. package/source/Popover.tsx +7 -2
  75. package/source/Progress/index.tsx +76 -0
  76. package/source/Progress/style.module.css +15 -0
  77. package/source/ScrollSpy/index.tsx +365 -0
  78. package/source/ScrollSpy/model.ts +17 -0
  79. package/source/Spinner.tsx +10 -0
  80. package/source/Stepper.tsx +85 -0
  81. package/source/Tab.tsx +248 -0
  82. package/source/Toast.tsx +65 -27
  83. package/source/ToggleButton.tsx +21 -7
  84. package/source/Toggler.tsx +108 -0
  85. package/source/Tooltip.tsx +6 -1
  86. package/source/index.ts +16 -27
  87. package/source/OverlayBox.tsx +0 -208
  88. package/source/Select.tsx +0 -95
  89. /package/source/{ClickBoundary.tsx → Extension/ClickBoundary.tsx} +0 -0
  90. /package/source/{Countdown.tsx → Extension/Countdown.tsx} +0 -0
  91. /package/source/{Editor.tsx → Extension/Editor.tsx} +0 -0
  92. /package/source/{HorizontalMarquee → Extension/HorizontalMarquee}/index.tsx +0 -0
  93. /package/source/{LiveTSX.tsx → Extension/LiveTSX.tsx} +0 -0
  94. /package/source/{ScrollNav → Extension/ScrollNav}/Scroller.tsx +0 -0
  95. /package/source/{ScrollNav → Extension/ScrollNav}/index.module.less +0 -0
  96. /package/source/{TextTruncate → Extension/TextTruncate}/Content.tsx +0 -0
  97. /package/source/{TextTruncate → Extension/TextTruncate}/index.tsx +0 -0
  98. /package/source/{Time.tsx → Extension/Time.tsx} +0 -0
  99. /package/source/{TimeDistance.tsx → Extension/TimeDistance.tsx} +0 -0
  100. /package/source/{Timeline → Extension/Timeline}/index.module.less +0 -0
  101. /package/source/{TypeEcho → Extension/TypeEcho}/Content.tsx +0 -0
  102. /package/source/{TypeEcho → Extension/TypeEcho}/index.tsx +0 -0
  103. /package/source/{UserRank → Extension/UserRank}/Address.tsx +0 -0
  104. /package/source/{UserRank → Extension/UserRank}/index.module.less +0 -0
  105. /package/source/{VerticalMarquee → Extension/VerticalMarquee}/index.module.less +0 -0
  106. /package/source/{VerticalMarquee → Extension/VerticalMarquee}/index.tsx +0 -0
  107. /package/source/{ZodiacBar.tsx → Extension/ZodiacBar.tsx} +0 -0
@@ -0,0 +1,391 @@
1
+ 'use client';
2
+
3
+ import classNames from 'classnames';
4
+ import { observer } from 'mobx-react';
5
+ import {
6
+ Children,
7
+ cloneElement,
8
+ Component,
9
+ ComponentPropsWithRef,
10
+ createRef,
11
+ CSSProperties,
12
+ FC,
13
+ isValidElement,
14
+ KeyboardEvent,
15
+ MouseEvent,
16
+ ReactNode
17
+ } from 'react';
18
+
19
+ import { Icon } from '../Extension/Icon';
20
+ import { CarouselEnds, CarouselModel } from './model';
21
+ import { CarouselScroll } from './scroll';
22
+
23
+ export interface CarouselSlideProps extends ComponentPropsWithRef<'div'> {
24
+ interval?: number;
25
+ active?: boolean;
26
+ }
27
+
28
+ export const CarouselSlide: FC<CarouselSlideProps> = ({
29
+ interval,
30
+ active,
31
+ className,
32
+ children,
33
+ ...props
34
+ }) => (
35
+ <div
36
+ {...props}
37
+ className={classNames('carousel-item', { active }, className)}
38
+ data-bs-interval={interval}
39
+ >
40
+ {children}
41
+ </div>
42
+ );
43
+
44
+ CarouselSlide.displayName = 'CarouselSlide';
45
+
46
+ export interface CarouselProps extends Omit<ComponentPropsWithRef<'div'>, 'onChange'> {
47
+ autoplay?: boolean;
48
+ center?: boolean;
49
+ controls?: boolean;
50
+ controlsPlacement?: 'overlay' | 'top' | 'bottom';
51
+ dark?: boolean;
52
+ ends?: CarouselEnds;
53
+ fade?: boolean;
54
+ indicators?: boolean;
55
+ interval?: number;
56
+ items?: number;
57
+ itemsGap?: string | number;
58
+ itemsPeek?: string | number;
59
+ keyboard?: boolean;
60
+ onChange?: (index: number) => unknown;
61
+ pause?: boolean | 'hover';
62
+ playPause?: boolean;
63
+ }
64
+
65
+ @observer
66
+ export class Carousel extends Component<CarouselProps> {
67
+ static displayName = 'Carousel';
68
+
69
+ root = createRef<HTMLDivElement>();
70
+
71
+ track = createRef<HTMLDivElement>();
72
+
73
+ store = new CarouselModel(0, 'loop', () => 5000);
74
+
75
+ scroller = new CarouselScroll();
76
+
77
+ componentDidMount() {
78
+ this.updateStore();
79
+ if (this.props.autoplay) this.store.play();
80
+
81
+ document.addEventListener('visibilitychange', this.handleVisibilityChange);
82
+ this.track.current?.addEventListener('scroll', this.handleScroll, { passive: true });
83
+ }
84
+
85
+ componentDidUpdate() {
86
+ this.updateStore();
87
+ }
88
+
89
+ componentWillUnmount() {
90
+ this.store.dispose();
91
+ this.scroller.dispose();
92
+
93
+ document.removeEventListener('visibilitychange', this.handleVisibilityChange);
94
+ this.track.current?.removeEventListener('scroll', this.handleScroll);
95
+ }
96
+
97
+ private getSlides = () => Children.toArray(this.props.children);
98
+
99
+ private getSlideInterval = (index: number) => {
100
+ const slide = this.getSlides()[index];
101
+ const itemInterval = isValidElement<CarouselSlideProps>(slide)
102
+ ? slide.props.interval
103
+ : undefined;
104
+
105
+ return itemInterval ?? this.props.interval ?? 5000;
106
+ };
107
+
108
+ private updateStore = () => {
109
+ const slides = this.getSlides();
110
+
111
+ this.store.onChange = this.props.onChange;
112
+ this.store.onNavigate = this.scrollToActive;
113
+ this.store.update(slides.length, this.props.ends ?? 'loop', this.getSlideInterval);
114
+ };
115
+
116
+ handleVisibilityChange = () => this.store.setVisible(!document.hidden);
117
+
118
+ handleMouseEnter = () => {
119
+ if (this.props.pause !== false) this.store.setHovering(true);
120
+ };
121
+
122
+ handleMouseLeave = () => this.store.setHovering(false);
123
+
124
+ handleFocusCapture = () => {
125
+ if (this.props.pause !== false) this.store.setHovering(true);
126
+ };
127
+
128
+ handleBlurCapture = ({ currentTarget, relatedTarget }: React.FocusEvent<HTMLDivElement>) => {
129
+ if (relatedTarget instanceof Node && currentTarget.contains(relatedTarget)) return;
130
+
131
+ this.store.setHovering(false);
132
+ };
133
+
134
+ handleScroll = () => {
135
+ const track = this.track.current;
136
+ if (!track || this.scroller.looping) return;
137
+
138
+ const slides = [...track.children] as HTMLElement[];
139
+ const index = slides.reduce(
140
+ (closest, slide, candidate) =>
141
+ Math.abs(slide.offsetLeft - track.scrollLeft) <
142
+ Math.abs(slides[closest]?.offsetLeft - track.scrollLeft)
143
+ ? candidate
144
+ : closest,
145
+ 0
146
+ );
147
+ if (index !== this.store.activeIndex) this.store.setIndex(index, true, undefined, true);
148
+ };
149
+
150
+ handleKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
151
+ this.props.onKeyDown?.(event);
152
+
153
+ if (event.defaultPrevented || this.scroller.looping) return;
154
+ if (this.props.keyboard === false || event.altKey || event.ctrlKey || event.metaKey) return;
155
+ if (
156
+ event.target instanceof HTMLElement &&
157
+ /^(INPUT|TEXTAREA|SELECT)$/.test(event.target.tagName)
158
+ )
159
+ return;
160
+
161
+ if (event.key === 'ArrowLeft') {
162
+ event.preventDefault();
163
+ this.store.previous(true);
164
+ } else if (event.key === 'ArrowRight') {
165
+ event.preventDefault();
166
+ this.store.next(true);
167
+ }
168
+ };
169
+
170
+ handleClick = (event: MouseEvent<HTMLDivElement>) => {
171
+ this.props.onClick?.(event);
172
+ if (event.defaultPrevented || this.scroller.looping) return;
173
+
174
+ const button =
175
+ event.target instanceof Element
176
+ ? event.target.closest<HTMLButtonElement>('[data-carousel-action]')
177
+ : null;
178
+ const { carouselAction, index } = button?.dataset ?? {};
179
+
180
+ switch (carouselAction) {
181
+ case 'previous':
182
+ this.store.previous(true);
183
+ break;
184
+ case 'next':
185
+ this.store.next(true);
186
+ break;
187
+ case 'slide':
188
+ if (index !== undefined) this.store.setIndex(Number(index), true);
189
+ break;
190
+ case 'toggle':
191
+ this.store.toggle();
192
+ }
193
+ };
194
+
195
+ scrollToActive = (index: number, from: number, direction?: -1 | 1) => {
196
+ const track = this.track.current;
197
+ const root = this.root.current;
198
+ if (!track || !root) return;
199
+
200
+ this.scroller.dispose();
201
+ const slides = [...track.children] as HTMLElement[];
202
+ const destination = slides[index];
203
+ if (!destination) return;
204
+
205
+ const reducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
206
+ const styles = getComputedStyle(root);
207
+ const items = Number.parseFloat(styles.getPropertyValue('--bs-carousel-items')) || 1;
208
+ const peek = Number.parseFloat(styles.getPropertyValue('--bs-carousel-items-peek')) || 0;
209
+ const variableWidth = slides.some(slide => slide.style.width || slide.style.flexBasis);
210
+ const crossingEnd =
211
+ (direction === 1 && index === 0 && from === slides.length - 1) ||
212
+ (direction === -1 && from === 0 && index === slides.length - 1);
213
+ const canLoop =
214
+ (this.props.ends ?? 'loop') === 'loop' &&
215
+ !this.props.fade &&
216
+ !reducedMotion &&
217
+ items === 1 &&
218
+ peek === 0 &&
219
+ !this.props.center &&
220
+ !variableWidth;
221
+
222
+ if (crossingEnd && canLoop)
223
+ this.scroller.loop(track, slides[from], destination, direction === 1);
224
+ else this.scroller.navigate(track, destination.offsetLeft, !crossingEnd && !reducedMotion);
225
+ };
226
+
227
+ renderSlide = (slide: ReactNode, index: number) => {
228
+ const active = this.store.activeIndex === index;
229
+
230
+ return isValidElement<CarouselSlideProps>(slide) && typeof slide.type !== 'string' ? (
231
+ cloneElement(slide, { active, 'aria-hidden': !active })
232
+ ) : (
233
+ <CarouselSlide key={index} active={active} aria-hidden={!active}>
234
+ {slide}
235
+ </CarouselSlide>
236
+ );
237
+ };
238
+
239
+ renderControls = () => {
240
+ const { controls = true, ends = 'loop' } = this.props;
241
+ if (!controls) return null;
242
+
243
+ const atStart = this.store.activeIndex === 0;
244
+ const atEnd = this.store.activeIndex === this.getSlides().length - 1;
245
+ const buttonClass =
246
+ this.props.controlsPlacement === 'overlay'
247
+ ? 'carousel-control-prev'
248
+ : 'btn btn-subtle theme-secondary';
249
+
250
+ return (
251
+ <>
252
+ <button
253
+ type="button"
254
+ className={buttonClass}
255
+ data-carousel-action="previous"
256
+ aria-label="Previous"
257
+ disabled={ends === 'stop' && atStart}
258
+ >
259
+ <Icon name="chevron-left" aria-hidden="true" />
260
+ <span className="visually-hidden">Previous</span>
261
+ </button>
262
+ <button
263
+ type="button"
264
+ className={
265
+ this.props.controlsPlacement === 'overlay'
266
+ ? 'carousel-control-next'
267
+ : 'btn btn-subtle theme-secondary'
268
+ }
269
+ data-carousel-action="next"
270
+ aria-label="Next"
271
+ disabled={ends === 'stop' && atEnd}
272
+ >
273
+ <Icon name="chevron-right" aria-hidden="true" />
274
+ <span className="visually-hidden">Next</span>
275
+ </button>
276
+ </>
277
+ );
278
+ };
279
+
280
+ renderIndicators = () =>
281
+ this.props.indicators && this.getSlides().length > 1 ? (
282
+ <div className="carousel-indicators">
283
+ {this.getSlides().map((_, index) => (
284
+ <button
285
+ key={index}
286
+ type="button"
287
+ data-carousel-action="slide"
288
+ data-index={index}
289
+ aria-label={`Slide ${index + 1}`}
290
+ aria-current={this.store.activeIndex === index ? 'true' : undefined}
291
+ className={classNames({ active: this.store.activeIndex === index })}
292
+ />
293
+ ))}
294
+ </div>
295
+ ) : null;
296
+
297
+ renderPlayPause = () =>
298
+ (this.props.playPause || this.props.autoplay) && (
299
+ <button
300
+ type="button"
301
+ className="btn-icon btn-sm carousel-control-play-pause"
302
+ data-carousel-action="toggle"
303
+ aria-label={this.store.playing ? 'Pause' : 'Play'}
304
+ >
305
+ <Icon name={this.store.playing ? 'pause-fill' : 'play-fill'} aria-hidden="true" />
306
+ </button>
307
+ );
308
+
309
+ render() {
310
+ const slides = this.getSlides();
311
+ const {
312
+ autoplay: _autoplay,
313
+ center,
314
+ children: _children,
315
+ className,
316
+ controls: _controls,
317
+ controlsPlacement = 'overlay',
318
+ dark,
319
+ ends: _ends,
320
+ fade: _fade,
321
+ fade,
322
+ indicators: _indicators,
323
+ interval: _interval,
324
+ items,
325
+ itemsGap,
326
+ itemsPeek,
327
+ keyboard: _keyboard,
328
+ onChange: _onChange,
329
+ onClick: _onClick,
330
+ pause: _pause,
331
+ playPause: _playPause,
332
+ style,
333
+ ...props
334
+ } = this.props;
335
+ const placement = controlsPlacement === 'overlay';
336
+ const styleWithItems = {
337
+ ...style,
338
+ ...(items && { '--bs-carousel-items': items }),
339
+ ...(itemsGap !== undefined && { '--bs-carousel-items-gap': itemsGap }),
340
+ ...(itemsPeek !== undefined && { '--bs-carousel-items-peek': itemsPeek })
341
+ } as CSSProperties;
342
+ const outerControls = (
343
+ <div className="d-flex justify-content-between align-items-center gap-2">
344
+ {this.renderIndicators()}
345
+ <div className="d-flex align-items-center gap-2">
346
+ {this.renderPlayPause()}
347
+ {this.renderControls()}
348
+ </div>
349
+ </div>
350
+ );
351
+
352
+ return (
353
+ <div
354
+ {...props}
355
+ ref={this.root}
356
+ className={classNames(
357
+ 'carousel',
358
+ { 'carousel-fade': fade, 'carousel-dark': dark, 'carousel-center': center },
359
+ className
360
+ )}
361
+ style={styleWithItems}
362
+ role="region"
363
+ aria-label={this.props['aria-label'] ?? 'Carousel'}
364
+ aria-roledescription="carousel"
365
+ onClick={this.handleClick}
366
+ onKeyDown={this.handleKeyDown}
367
+ onMouseEnter={this.handleMouseEnter}
368
+ onMouseLeave={this.handleMouseLeave}
369
+ onFocusCapture={this.handleFocusCapture}
370
+ onBlurCapture={this.handleBlurCapture}
371
+ >
372
+ {controlsPlacement === 'top' && outerControls}
373
+ <div
374
+ ref={this.track}
375
+ className="carousel-inner"
376
+ tabIndex={0}
377
+ aria-label={this.props['aria-label'] ?? 'Carousel slides'}
378
+ onScroll={this.handleScroll}
379
+ >
380
+ {slides.map(this.renderSlide)}
381
+ </div>
382
+ {placement && this.renderControls()}
383
+ {controlsPlacement === 'bottom' && outerControls}
384
+ {placement &&
385
+ (this.props.playPause || this.props.autoplay) &&
386
+ this.renderPlayPause()}
387
+ {placement && this.renderIndicators()}
388
+ </div>
389
+ );
390
+ }
391
+ }
@@ -0,0 +1,113 @@
1
+ import { action, observable } from 'mobx';
2
+
3
+ export type CarouselEnds = 'loop' | 'wrap' | 'stop';
4
+
5
+ export class CarouselModel {
6
+ @observable accessor activeIndex = 0;
7
+
8
+ @observable accessor playing = false;
9
+
10
+ private timer?: ReturnType<typeof setTimeout>;
11
+
12
+ private hovering = false;
13
+
14
+ private visible = true;
15
+
16
+ onChange?: (index: number) => unknown;
17
+
18
+ onNavigate?: (index: number, from: number, direction?: -1 | 1) => unknown;
19
+
20
+ constructor(
21
+ private count: number,
22
+ private ends: CarouselEnds,
23
+ private interval: (index: number) => number
24
+ ) {}
25
+
26
+ @action
27
+ setIndex = (index: number, userInitiated = false, direction?: -1 | 1, fromScroll = false) => {
28
+ const nextIndex = Math.max(0, Math.min(index, this.count - 1));
29
+
30
+ if (nextIndex !== this.activeIndex) {
31
+ const from = this.activeIndex;
32
+ this.activeIndex = nextIndex;
33
+ this.onChange?.(nextIndex);
34
+ if (!fromScroll) this.onNavigate?.(nextIndex, from, direction);
35
+ }
36
+ if (userInitiated) this.pause();
37
+ else this.schedule();
38
+ };
39
+
40
+ @action
41
+ next = (userInitiated = false) => {
42
+ if (this.activeIndex === this.count - 1 && this.ends === 'stop') {
43
+ if (!userInitiated) this.pause();
44
+ return;
45
+ }
46
+ this.setIndex(
47
+ this.activeIndex === this.count - 1 ? 0 : this.activeIndex + 1,
48
+ userInitiated,
49
+ 1
50
+ );
51
+ };
52
+
53
+ @action
54
+ previous = (userInitiated = false) => {
55
+ if (this.activeIndex === 0 && this.ends === 'stop') {
56
+ if (!userInitiated) this.pause();
57
+ return;
58
+ }
59
+ this.setIndex(
60
+ this.activeIndex === 0 ? this.count - 1 : this.activeIndex - 1,
61
+ userInitiated,
62
+ -1
63
+ );
64
+ };
65
+
66
+ @action
67
+ play = () => {
68
+ this.playing = true;
69
+ this.schedule();
70
+ };
71
+
72
+ @action
73
+ pause = () => {
74
+ this.playing = false;
75
+ this.clearTimer();
76
+ };
77
+
78
+ @action
79
+ toggle = () => (this.playing ? this.pause() : this.play());
80
+
81
+ setHovering = (hovering: boolean) => {
82
+ this.hovering = hovering;
83
+ this.schedule();
84
+ };
85
+
86
+ setVisible = (visible: boolean) => {
87
+ this.visible = visible;
88
+ this.schedule();
89
+ };
90
+
91
+ update = (count: number, ends: CarouselEnds, interval: (index: number) => number) => {
92
+ this.count = count;
93
+ this.ends = ends;
94
+ this.interval = interval;
95
+
96
+ if (this.activeIndex >= count) this.setIndex(Math.max(0, count - 1));
97
+ else this.schedule();
98
+ };
99
+
100
+ dispose = () => this.clearTimer();
101
+
102
+ private schedule = () => {
103
+ this.clearTimer();
104
+ if (!this.playing || this.hovering || !this.visible || this.count < 2) return;
105
+
106
+ this.timer = setTimeout(this.next, Math.max(0, this.interval(this.activeIndex)));
107
+ };
108
+
109
+ private clearTimer = () => {
110
+ if (this.timer) clearTimeout(this.timer);
111
+ this.timer = undefined;
112
+ };
113
+ }
@@ -0,0 +1,79 @@
1
+ export class CarouselScroll {
2
+ private frame?: number;
3
+
4
+ private clone?: HTMLElement;
5
+
6
+ private track?: HTMLElement;
7
+
8
+ private snap = '';
9
+
10
+ get looping() {
11
+ return Boolean(this.clone) || this.frame !== undefined;
12
+ }
13
+
14
+ navigate = (track: HTMLElement, left: number, smooth: boolean) => {
15
+ this.dispose();
16
+ if (!smooth || track.scrollLeft === left) {
17
+ track.scrollTo({ left, behavior: 'instant' });
18
+ return;
19
+ }
20
+ this.track = track;
21
+ this.snap = track.style.scrollSnapType;
22
+ track.style.scrollSnapType = 'none';
23
+ this.animate(track, track.scrollLeft, left);
24
+ };
25
+
26
+ loop = (track: HTMLElement, from: HTMLElement, destination: HTMLElement, forward: boolean) => {
27
+ this.dispose();
28
+ this.track = track;
29
+ this.snap = track.style.scrollSnapType;
30
+
31
+ const clone = destination.cloneNode(true) as HTMLElement;
32
+ clone.removeAttribute('id');
33
+
34
+ for (const element of clone.querySelectorAll('[id]')) element.removeAttribute('id');
35
+
36
+ clone.setAttribute('aria-hidden', 'true');
37
+ clone.inert = true;
38
+ clone.dataset.carouselClone = 'true';
39
+ clone.classList.remove('active');
40
+ this.clone = clone;
41
+ track.style.scrollSnapType = 'none';
42
+
43
+ if (forward) track.append(clone);
44
+ else track.prepend(clone);
45
+
46
+ const start = from.offsetLeft;
47
+ const end = clone.offsetLeft;
48
+
49
+ track.scrollTo({ left: start, behavior: 'instant' });
50
+ this.animate(track, start, end, () => {
51
+ this.dispose();
52
+ track.scrollTo({ left: destination.offsetLeft, behavior: 'instant' });
53
+ });
54
+ };
55
+
56
+ private animate = (track: HTMLElement, start: number, end: number, complete = this.dispose) => {
57
+ const startedAt = window.performance.now();
58
+
59
+ const animate = (time: number) => {
60
+ const progress = Math.min(1, (time - startedAt) / 400);
61
+ const eased = 1 - (1 - progress) ** 3;
62
+
63
+ track.scrollTo({ left: start + (end - start) * eased, behavior: 'instant' });
64
+
65
+ if (progress < 1) this.frame = window.requestAnimationFrame(animate);
66
+ else complete();
67
+ };
68
+ this.frame = window.requestAnimationFrame(animate);
69
+ };
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
+ }