@lumx/react 3.9.4-alpha.3 → 3.9.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.
@@ -1,10 +1,9 @@
1
- import { SetStateAction, useCallback, useEffect, useState } from 'react';
2
- import { clamp } from '@lumx/react';
1
+ import { useState, useCallback, useEffect } from 'react';
2
+
3
3
  import { useInterval } from '@lumx/react/hooks/useInterval';
4
+ import { AUTOPLAY_DEFAULT_INTERVAL } from '@lumx/react/components/slideshow/constants';
4
5
  import { useId } from '@lumx/react/hooks/useId';
5
6
 
6
- import { AUTOPLAY_DEFAULT_INTERVAL, NEXT_SLIDE_EVENT, PREV_SLIDE_EVENT } from './constants';
7
-
8
7
  export interface UseSlideshowControlsOptions {
9
8
  /** default active index to be displayed */
10
9
  defaultActiveIndex?: number;
@@ -44,9 +43,9 @@ export interface UseSlideshowControls {
44
43
  /** id to be used for the wrapper that contains the slides */
45
44
  slideshowSlidesId: string;
46
45
  /** callback that triggers the previous slide while using the slideshow controls */
47
- onPreviousClick: (loopBack: boolean) => void;
46
+ onPreviousClick: (loopback: boolean) => void;
48
47
  /** callback that triggers the next slide while using the slideshow controls */
49
- onNextClick: (loopBack: boolean) => void;
48
+ onNextClick: (loopback: boolean) => void;
50
49
  /** callback that triggers a specific page while using the slideshow controls */
51
50
  onPaginationClick: (index: number) => void;
52
51
  /** whether the slideshow is autoplaying or not */
@@ -55,18 +54,16 @@ export interface UseSlideshowControls {
55
54
  isForcePaused: boolean;
56
55
  /** callback to change whether the slideshow is autoplaying or not */
57
56
  toggleAutoPlay: () => void;
58
- /** callback to change whether the slideshow should be force paused or not */
57
+ /** calback to change whether the slideshow should be force paused or not */
59
58
  toggleForcePause: () => void;
60
59
  /** current active slide index */
61
60
  activeIndex: number;
62
61
  /** set the current index as the active one */
63
62
  setActiveIndex: (index: number) => void;
64
- /** callback that stops the autoplay */
63
+ /** callback that stops the auto play */
65
64
  stopAutoPlay: () => void;
66
- /** callback that starts the autoplay */
65
+ /** callback that starts the auto play */
67
66
  startAutoPlay: () => void;
68
- /** True if the last slide change is user activated */
69
- isUserActivated?: boolean;
70
67
  }
71
68
 
72
69
  export const DEFAULT_OPTIONS: Partial<UseSlideshowControlsOptions> = {
@@ -93,34 +90,40 @@ export const useSlideshowControls = ({
93
90
  // Number of slides when using groupBy prop.
94
91
  const slidesCount = Math.ceil(itemsCount / Math.min(groupBy as number, itemsCount));
95
92
 
96
- // Set current active index (& if is user activated)
97
- const setActiveIndex = useCallback(
98
- (setStateAction: SetStateAction<number>, isUser?: boolean) => {
99
- // Store on element a boolean value when the slide change was not from a user action.
100
- const elementDataset = element?.dataset as any;
101
- if (elementDataset) {
102
- if (isUser) elementDataset.lumxUserActivated = true;
103
- else delete elementDataset.lumxUserActivated;
104
- }
105
-
106
- setCurrentIndex(setStateAction);
93
+ // Change current index to display next slide.
94
+ const goToNextSlide = useCallback(
95
+ (loopback = true) => {
96
+ setCurrentIndex((index) => {
97
+ if (loopback && index === slidesCount - 1) {
98
+ // Loopback to the start.
99
+ return 0;
100
+ }
101
+ if (index < slidesCount - 1) {
102
+ // Next slide.
103
+ return index + 1;
104
+ }
105
+ return index;
106
+ });
107
107
  },
108
- [element],
108
+ [slidesCount, setCurrentIndex],
109
109
  );
110
110
 
111
- // Change slide given delta (-1/+1) with or without loop back.
112
- const goTo = useCallback(
113
- (delta: -1 | 1 = 1, loopBack = true, isUser?: boolean) => {
114
- setActiveIndex((index) => {
115
- if (loopBack) {
116
- const newIndex = (index + delta) % slidesCount;
117
- if (newIndex < 0) return slidesCount + newIndex;
118
- return newIndex;
111
+ // Change current index to display previous slide.
112
+ const goToPreviousSlide = useCallback(
113
+ (loopback = true) => {
114
+ setCurrentIndex((index) => {
115
+ if (loopback && index === 0) {
116
+ // Loopback to the end.
117
+ return slidesCount - 1;
118
+ }
119
+ if (index > 0) {
120
+ // Previous slide.
121
+ return index - 1;
119
122
  }
120
- return clamp(index + delta, 0, slidesCount - 1);
121
- }, isUser);
123
+ return index;
124
+ });
122
125
  },
123
- [slidesCount, setActiveIndex],
126
+ [slidesCount, setCurrentIndex],
124
127
  );
125
128
 
126
129
  // Auto play
@@ -129,22 +132,22 @@ export const useSlideshowControls = ({
129
132
 
130
133
  const isSlideshowAutoPlaying = isForcePaused ? false : isAutoPlaying;
131
134
  // Start
132
- useInterval(goTo, isSlideshowAutoPlaying && slidesCount > 1 ? (interval as number) : null);
135
+ useInterval(goToNextSlide, isSlideshowAutoPlaying && slidesCount > 1 ? (interval as number) : null);
133
136
 
134
- // Reset current index if it becomes invalid.
137
+ // Reset current index if it become invalid.
135
138
  useEffect(() => {
136
139
  if (currentIndex > slidesCount - 1) {
137
- setActiveIndex(defaultActiveIndex as number);
140
+ setCurrentIndex(defaultActiveIndex as number);
138
141
  }
139
- }, [currentIndex, slidesCount, defaultActiveIndex, setActiveIndex]);
142
+ }, [currentIndex, slidesCount, defaultActiveIndex]);
140
143
 
141
- const startAutoPlay = useCallback(() => {
144
+ const startAutoPlay = () => {
142
145
  setIsAutoPlaying(Boolean(autoPlay));
143
- }, [autoPlay]);
146
+ };
144
147
 
145
- const stopAutoPlay = useCallback(() => {
148
+ const stopAutoPlay = () => {
146
149
  setIsAutoPlaying(false);
147
- }, []);
150
+ };
148
151
 
149
152
  // Handle click on a bullet to go to a specific slide.
150
153
  const onPaginationClick = useCallback(
@@ -153,48 +156,36 @@ export const useSlideshowControls = ({
153
156
  setIsForcePaused(true);
154
157
 
155
158
  if (index >= 0 && index < slidesCount) {
156
- setActiveIndex(index, true);
159
+ setCurrentIndex(index);
157
160
  }
158
161
  },
159
- [stopAutoPlay, slidesCount, setActiveIndex],
162
+ [slidesCount, setCurrentIndex],
160
163
  );
161
164
 
162
165
  // Handle click or keyboard event to go to next slide.
163
166
  const onNextClick = useCallback(
164
- (loopBack = true) => {
167
+ (loopback = true) => {
165
168
  stopAutoPlay();
166
169
  setIsForcePaused(true);
167
- goTo(1, loopBack, true);
170
+ goToNextSlide(loopback);
168
171
  },
169
- [goTo, stopAutoPlay],
172
+ [goToNextSlide],
170
173
  );
171
174
 
172
175
  // Handle click or keyboard event to go to previous slide.
173
176
  const onPreviousClick = useCallback(
174
- (loopBack = true) => {
177
+ (loopback = true) => {
175
178
  stopAutoPlay();
176
179
  setIsForcePaused(true);
177
- goTo(-1, loopBack, true);
180
+ goToPreviousSlide(loopback);
178
181
  },
179
- [goTo, stopAutoPlay],
182
+ [goToPreviousSlide],
180
183
  );
181
184
 
182
- // Listen to custom next/prev slide events
183
- useEffect(() => {
184
- if (!element) return undefined;
185
-
186
- element.addEventListener(NEXT_SLIDE_EVENT, onNextClick);
187
- element.addEventListener(PREV_SLIDE_EVENT, onPreviousClick);
188
- return () => {
189
- element.removeEventListener(NEXT_SLIDE_EVENT, onNextClick);
190
- element.removeEventListener(PREV_SLIDE_EVENT, onPreviousClick);
191
- };
192
- }, [element, onNextClick, onPreviousClick]);
193
-
194
185
  // If the activeIndex props changes, update the current slide
195
186
  useEffect(() => {
196
- setActiveIndex(activeIndex as number);
197
- }, [activeIndex, setActiveIndex]);
187
+ setCurrentIndex(activeIndex as number);
188
+ }, [activeIndex]);
198
189
 
199
190
  // If the slide changes, with autoplay for example, trigger "onChange"
200
191
  useEffect(() => {
@@ -208,15 +199,15 @@ export const useSlideshowControls = ({
208
199
  const generatedSlidesId = useId();
209
200
  const slideshowSlidesId = slidesId || generatedSlidesId;
210
201
 
211
- const toggleAutoPlay = useCallback(() => {
202
+ const toggleAutoPlay = () => {
212
203
  if (isSlideshowAutoPlaying) {
213
204
  stopAutoPlay();
214
205
  } else {
215
206
  startAutoPlay();
216
207
  }
217
- }, [isSlideshowAutoPlaying, startAutoPlay, stopAutoPlay]);
208
+ };
218
209
 
219
- const toggleForcePause = useCallback(() => {
210
+ const toggleForcePause = () => {
220
211
  const shouldBePaused = !isForcePaused;
221
212
 
222
213
  setIsForcePaused(shouldBePaused);
@@ -226,7 +217,7 @@ export const useSlideshowControls = ({
226
217
  } else {
227
218
  stopAutoPlay();
228
219
  }
229
- }, [isForcePaused, startAutoPlay, stopAutoPlay]);
220
+ };
230
221
 
231
222
  // Start index and end index of visible slides.
232
223
  const startIndexVisible = currentIndex * (groupBy as number);
@@ -246,7 +237,7 @@ export const useSlideshowControls = ({
246
237
  toggleAutoPlay,
247
238
  activeIndex: currentIndex,
248
239
  slidesCount,
249
- setActiveIndex,
240
+ setActiveIndex: setCurrentIndex,
250
241
  startAutoPlay,
251
242
  stopAutoPlay,
252
243
  isForcePaused,
@@ -3,6 +3,7 @@
3
3
  */
4
4
  export default { title: 'LumX components/mosaic/Mosaic Demos' };
5
5
 
6
+ export { App as Clickable } from './clickable';
6
7
  export { App as FourImages } from './four-images';
7
8
  export { App as MoreImages } from './more-images';
8
9
  export { App as ThreeImages } from './three-images';