idea-react 3.0.0-beta.0 → 3.0.0-beta.2

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