idea-react 3.0.0-rc.2 → 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/dist/style.css CHANGED
@@ -1,7 +1,7 @@
1
1
  .HR-isq_paused .carousel-indicators .active:after {
2
2
  animation-play-state: paused;
3
3
  }
4
- .rDVV3q_hiddenBackdrop {
4
+ .h491eq_hiddenBackdrop {
5
5
  --bs-dialog-backdrop-bg: transparent;
6
6
  --bs-dialog-backdrop-blur: 0;
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "idea-react",
3
- "version": "3.0.0-rc.2",
3
+ "version": "3.0.0-rc.4",
4
4
  "license": "LGPL-3.0-or-later",
5
5
  "author": "shiy2008@gmail.com",
6
6
  "description": "A React advanced components library based on TypeScript & Bootstrap, built by idea2app remote developers team.",
@@ -10,7 +10,7 @@
10
10
  "typescript",
11
11
  "bootstrap"
12
12
  ],
13
- "homepage": "https://idea2app.github.io/Idea-React/",
13
+ "homepage": "https://idea-react.pages.dev/",
14
14
  "repository": {
15
15
  "type": "git",
16
16
  "url": "git+https://github.com/idea2app/Idea-React.git"
@@ -36,12 +36,12 @@
36
36
  "html2canvas": "^1.4.1",
37
37
  "iterable-observer": "^1.1.1",
38
38
  "lodash": "^4.18.1",
39
- "marked": "^18.2.0",
39
+ "marked": "^18.2.1",
40
40
  "mobx-react-helper": "^0.5.4",
41
41
  "react-live": "^5.0.1",
42
42
  "shiki": "^4.6.0",
43
43
  "vanilla-calendar-pro": "3.4.0",
44
- "web-utility": "^4.8.1"
44
+ "web-utility": "^4.8.2"
45
45
  },
46
46
  "peerDependencies": {
47
47
  "mobx": ">=6",
@@ -66,19 +66,19 @@
66
66
  "@types/react": "^19.3.0",
67
67
  "@types/react-dom": "^19.3.0",
68
68
  "buffer": "^6.0.3",
69
- "eslint": "^10.14.0",
69
+ "eslint": "^10.15.0",
70
70
  "eslint-idea2app-next-js": "^0.1.1",
71
71
  "happy-dom": "^21.0.4",
72
72
  "husky": "^9.1.7",
73
73
  "jiti": "^2.7.0",
74
74
  "koapache": "^3.0.0",
75
75
  "less": "^4.9.1",
76
- "lint-staged": "^17.6.0",
77
- "mobx": "^7.1.6",
78
- "mobx-react": "^10.0.3",
76
+ "lint-staged": "^17.6.1",
77
+ "mobx": "^7.1.7",
78
+ "mobx-react": "^10.0.4",
79
79
  "parcel": "~2.16.4",
80
- "postcss": "^8.5.32",
81
- "prettier": "^3.10.0",
80
+ "postcss": "^8.5.33",
81
+ "prettier": "^3.10.1",
82
82
  "prettier-plugin-css-order": "^2.2.0",
83
83
  "process": "^0.11.10",
84
84
  "prop-types": "15.8.1",
@@ -1,6 +1,7 @@
1
1
  'use client';
2
2
 
3
3
  import classNames from 'classnames';
4
+ import { action, observable, reaction } from 'mobx';
4
5
  import { observer } from 'mobx-react';
5
6
  import {
6
7
  Children,
@@ -13,13 +14,21 @@ import {
13
14
  isValidElement,
14
15
  KeyboardEvent,
15
16
  MouseEvent,
16
- ReactNode
17
+ PointerEvent,
18
+ ReactNode,
19
+ WheelEvent
17
20
  } from 'react';
18
21
 
19
22
  import { Icon } from '../../Extension/Icon';
20
23
  import { polymorphic, PolymorphicClassProps } from '../../Helper/Polymorphic';
21
24
  import { Button } from '../Button';
22
- import { CarouselEnds, CarouselModel } from './model';
25
+ import {
26
+ CarouselEnds,
27
+ CarouselModel,
28
+ CarouselModelOptions,
29
+ FadeCarouselModel,
30
+ ScrollCarouselModel
31
+ } from './model';
23
32
  import { CarouselScroll } from './scroll';
24
33
  import styles from './style.module.css';
25
34
 
@@ -78,15 +87,20 @@ export class Carousel<T extends ElementType | undefined = 'div'> extends Compone
78
87
 
79
88
  track = createRef<HTMLDivElement>();
80
89
 
81
- store = new CarouselModel(0, 'loop', () => 5000);
82
-
83
90
  scroller = new CarouselScroll();
84
91
 
92
+ @observable accessor store: CarouselModel = this.createStore();
93
+
94
+ private stopMotion?: () => void;
95
+
85
96
  componentDidMount() {
97
+ this.stopMotion = reaction(() => this.store.motion, this.scroller.apply);
98
+
99
+ document.addEventListener('visibilitychange', this.handleVisibilityChange);
100
+ this.handleVisibilityChange();
101
+
86
102
  this.updateStore();
87
103
  if (this.props.autoplay) this.store.play();
88
- document.addEventListener('visibilitychange', this.handleVisibilityChange);
89
- this.track.current?.addEventListener('scroll', this.handleScroll, { passive: true });
90
104
  }
91
105
 
92
106
  componentDidUpdate() {
@@ -106,32 +120,80 @@ export class Carousel<T extends ElementType | undefined = 'div'> extends Compone
106
120
  }
107
121
 
108
122
  componentWillUnmount() {
109
- this.store.dispose();
110
- this.scroller.dispose();
111
-
123
+ this.stopMotion?.();
112
124
  document.removeEventListener('visibilitychange', this.handleVisibilityChange);
113
- this.track.current?.removeEventListener('scroll', this.handleScroll);
125
+ this.scroller.dispose();
126
+ this.store.dispose();
114
127
  }
115
128
 
116
129
  private getSlides = () => Children.toArray(this.props.children);
117
130
 
118
- private getSlideInterval = (index: number) => {
119
- const slide = this.getSlides()[index];
120
- const itemInterval = isValidElement<CarouselSlideProps>(slide)
121
- ? slide.props.interval
122
- : undefined;
131
+ private get modelOptions(): CarouselModelOptions {
132
+ const { ends = 'loop', interval = 5000, children, onChange } = this.props;
133
+
134
+ const intervals = Children.toArray(children).map(slide =>
135
+ isValidElement<CarouselSlideProps>(slide)
136
+ ? (slide.props.interval ?? interval)
137
+ : interval
138
+ );
139
+
140
+ return { intervals, ends, onChange };
141
+ }
142
+
143
+ private createStore() {
144
+ return this.props.fade
145
+ ? new FadeCarouselModel(this.modelOptions)
146
+ : new ScrollCarouselModel(this.modelOptions);
147
+ }
148
+
149
+ private updateStore = action(() => {
150
+ if (Boolean(this.props.fade) !== this.store.fade) {
151
+ const previous = this.store;
152
+ const next = this.createStore();
123
153
 
124
- return itemInterval ?? this.props.interval ?? 5000;
154
+ next.restore(previous);
155
+ previous.dispose();
156
+ this.scroller.dispose();
157
+ this.store = next;
158
+ } else this.store.update(this.modelOptions);
159
+
160
+ const root = this.root.current;
161
+ const track = this.track.current;
162
+
163
+ if (this.store.fade) this.store.refresh();
164
+ else if (root && track)
165
+ this.scroller.mount(
166
+ root,
167
+ track,
168
+ {
169
+ onLayout: this.store.refresh,
170
+ onScroll: this.store.syncScroll,
171
+ onComplete: this.store.complete,
172
+ onResize: this.handleResize
173
+ },
174
+ this.props.center
175
+ );
176
+ this.store.mount();
177
+ });
178
+
179
+ private handleVisibilityChange = () => this.store.setVisible(!document.hidden);
180
+
181
+ private handleResize = () => {
182
+ this.store.interrupt();
183
+ this.scroller.refresh(true);
125
184
  };
126
185
 
127
- private updateStore = () => {
128
- const slides = this.getSlides();
129
- this.store.onChange = this.props.onChange;
130
- this.store.onNavigate = this.scrollToActive;
131
- this.store.update(slides.length, this.props.ends ?? 'loop', this.getSlideInterval);
186
+ handlePointerDown = (event: PointerEvent<HTMLDivElement>) => {
187
+ this.props.onPointerDown?.(event);
188
+
189
+ if (!event.defaultPrevented) this.store.interrupt();
132
190
  };
133
191
 
134
- handleVisibilityChange = () => this.store.setVisible(!document.hidden);
192
+ handleWheel = (event: WheelEvent<HTMLDivElement>) => {
193
+ this.props.onWheel?.(event);
194
+
195
+ if (!event.defaultPrevented) this.store.interrupt();
196
+ };
135
197
 
136
198
  handleMouseEnter = () => {
137
199
  if (this.props.pause !== false) this.store.setHovering(true);
@@ -149,25 +211,11 @@ export class Carousel<T extends ElementType | undefined = 'div'> extends Compone
149
211
  this.store.setHovering(false);
150
212
  };
151
213
 
152
- handleScroll = () => {
153
- const track = this.track.current;
154
- if (!track || this.scroller.looping) return;
155
-
156
- const slides = [...track.children] as HTMLElement[];
157
- const index = slides.reduce(
158
- (closest, slide, candidate) =>
159
- Math.abs(slide.offsetLeft - track.scrollLeft) <
160
- Math.abs(slides[closest]?.offsetLeft - track.scrollLeft)
161
- ? candidate
162
- : closest,
163
- 0
164
- );
165
- if (index !== this.store.activeIndex) this.store.setIndex(index, true, undefined, true);
166
- };
167
-
168
214
  handleKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
169
215
  this.props.onKeyDown?.(event);
170
- if (event.defaultPrevented || this.scroller.looping) return;
216
+
217
+ if (event.defaultPrevented) return;
218
+
171
219
  if (this.props.keyboard === false || event.altKey || event.ctrlKey || event.metaKey) return;
172
220
  if (
173
221
  event.target instanceof HTMLElement &&
@@ -186,7 +234,7 @@ export class Carousel<T extends ElementType | undefined = 'div'> extends Compone
186
234
 
187
235
  handleClick = (event: MouseEvent<HTMLDivElement>) => {
188
236
  this.props.onClick?.(event);
189
- if (event.defaultPrevented || this.scroller.looping) return;
237
+ if (event.defaultPrevented) return;
190
238
 
191
239
  const button =
192
240
  event.target instanceof Element
@@ -209,38 +257,6 @@ export class Carousel<T extends ElementType | undefined = 'div'> extends Compone
209
257
  }
210
258
  };
211
259
 
212
- scrollToActive = (index: number, from: number, direction?: -1 | 1) => {
213
- const track = this.track.current;
214
- const root = this.root.current;
215
- if (!track || !root) return;
216
-
217
- this.scroller.dispose();
218
- const slides = [...track.children] as HTMLElement[];
219
- const destination = slides[index];
220
- if (!destination) return;
221
-
222
- const reducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
223
- const styles = getComputedStyle(root);
224
- const items = Number.parseFloat(styles.getPropertyValue('--bs-carousel-items')) || 1;
225
- const peek = Number.parseFloat(styles.getPropertyValue('--bs-carousel-items-peek')) || 0;
226
- const variableWidth = slides.some(slide => slide.style.width || slide.style.flexBasis);
227
- const crossingEnd =
228
- (direction === 1 && index === 0 && from === slides.length - 1) ||
229
- (direction === -1 && from === 0 && index === slides.length - 1);
230
- const canLoop =
231
- (this.props.ends ?? 'loop') === 'loop' &&
232
- !this.props.fade &&
233
- !reducedMotion &&
234
- items === 1 &&
235
- peek === 0 &&
236
- !this.props.center &&
237
- !variableWidth;
238
-
239
- if (crossingEnd && canLoop)
240
- this.scroller.loop(track, slides[from], destination, direction === 1);
241
- else this.scroller.navigate(track, destination.offsetLeft, !crossingEnd && !reducedMotion);
242
- };
243
-
244
260
  renderSlide = (slide: ReactNode, index: number) => {
245
261
  const active = this.store.activeIndex === index;
246
262
 
@@ -254,11 +270,11 @@ export class Carousel<T extends ElementType | undefined = 'div'> extends Compone
254
270
  };
255
271
 
256
272
  renderControl = (direction: 'previous' | 'next') => {
257
- const { controls = true, ends = 'loop' } = this.props;
273
+ const { controls = true } = this.props;
258
274
  if (!controls) return null;
259
275
 
260
276
  const previous = direction === 'previous';
261
- const atEnd = this.store.activeIndex === (previous ? 0 : this.getSlides().length - 1);
277
+ const disabled = previous ? !this.store.canPrevious : !this.store.canNext;
262
278
  const overlay = (this.props.controlsPlacement ?? 'overlay') === 'overlay';
263
279
  const label = previous ? 'Previous' : 'Next';
264
280
 
@@ -269,7 +285,7 @@ export class Carousel<T extends ElementType | undefined = 'div'> extends Compone
269
285
  size="sm"
270
286
  data-carousel-action={direction}
271
287
  aria-label={label}
272
- disabled={ends === 'stop' && atEnd}
288
+ disabled={disabled}
273
289
  icon
274
290
  >
275
291
  <Icon name={previous ? 'chevron-left' : 'chevron-right'} size={1} />
@@ -281,7 +297,7 @@ export class Carousel<T extends ElementType | undefined = 'div'> extends Compone
281
297
  renderIndicators = () =>
282
298
  this.props.indicators && this.getSlides().length > 1 ? (
283
299
  <div className="carousel-indicators">
284
- {this.getSlides().map((_, index) => (
300
+ {this.store.indexes.map(index => (
285
301
  <button
286
302
  key={index}
287
303
  type="button"
@@ -381,6 +397,8 @@ export class Carousel<T extends ElementType | undefined = 'div'> extends Compone
381
397
  keyboard: _keyboard,
382
398
  onChange: _onChange,
383
399
  onClick: _onClick,
400
+ onPointerDown: _onPointerDown,
401
+ onWheel: _onWheel,
384
402
  pause: _pause,
385
403
  playPause: _playPause,
386
404
  style,
@@ -393,7 +411,7 @@ export class Carousel<T extends ElementType | undefined = 'div'> extends Compone
393
411
  ...(itemsGap !== undefined && { '--bs-carousel-items-gap': itemsGap }),
394
412
  ...(itemsPeek !== undefined && { '--bs-carousel-items-peek': itemsPeek }),
395
413
  ...(this.store.playing && {
396
- '--bs-carousel-interval': `${this.getSlideInterval(this.store.activeIndex)}ms`
414
+ '--bs-carousel-interval': `${this.store.interval}ms`
397
415
  })
398
416
  } as CSSProperties;
399
417
 
@@ -425,14 +443,16 @@ export class Carousel<T extends ElementType | undefined = 'div'> extends Compone
425
443
  onMouseLeave={this.handleMouseLeave}
426
444
  onFocusCapture={this.handleFocusCapture}
427
445
  onBlurCapture={this.handleBlurCapture}
446
+ onPointerDown={this.handlePointerDown}
447
+ onWheel={this.handleWheel}
428
448
  >
429
449
  {controlsPlacement === 'top' && this.renderControls()}
430
450
  <div
431
451
  ref={this.track}
432
452
  className={classNames('carousel-inner', innerClassName)}
453
+ // eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex
433
454
  tabIndex={0}
434
455
  aria-label={this.props['aria-label'] ?? 'Carousel slides'}
435
- onScroll={this.handleScroll}
436
456
  >
437
457
  {slides.map(this.renderSlide)}
438
458
  </div>