@cleartrip/ct-design-scroll-container 5.24.0-SNAPSHOT-native-main.46 → 5.24.0

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,515 +1,823 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react/jsx-runtime'), require('react'), require('@cleartrip/ct-design-style-manager'), require('eventemitter3'), require('@cleartrip/ct-design-common-utils')) :
3
- typeof define === 'function' && define.amd ? define(['exports', 'react/jsx-runtime', 'react', '@cleartrip/ct-design-style-manager', 'eventemitter3', '@cleartrip/ct-design-common-utils'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.CTDesignSystemScrollContainer = {}, global.jsxRuntime, global.React, global.ctDesignStyleManager, global.EventEmitter, global.ctDesignCommonUtils));
5
- })(this, (function (exports, jsxRuntime, react, ctDesignStyleManager, EventEmitter, ctDesignCommonUtils) { 'use strict';
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react/jsx-runtime'), require('react'), require('@cleartrip/ct-design-style-manager'), require('@cleartrip/ct-design-common-utils')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', 'react/jsx-runtime', 'react', '@cleartrip/ct-design-style-manager', '@cleartrip/ct-design-common-utils'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.CTDesignSystemScrollContainer = {}, global.jsxRuntime, global.React, global.ctDesignStyleManager, global.ctDesignCommonUtils));
5
+ })(this, (function (exports, jsxRuntime, react, ctDesignStyleManager, ctDesignCommonUtils) { 'use strict';
6
6
 
7
- function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
7
+ var eventemitter3Exports = {};
8
+ var eventemitter3 = {
9
+ get exports(){ return eventemitter3Exports; },
10
+ set exports(v){ eventemitter3Exports = v; },
11
+ };
8
12
 
9
- var EventEmitter__default = /*#__PURE__*/_interopDefault(EventEmitter);
13
+ (function (module) {
10
14
 
11
- exports.EVENT_NAMES = void 0;
12
- (function (EVENT_NAMES) {
13
- EVENT_NAMES["SCROLL_VIEW"] = "scroll-view";
14
- EVENT_NAMES["SCROLL_INTO_VIEW"] = "scroll-into-view";
15
- })(exports.EVENT_NAMES || (exports.EVENT_NAMES = {}));
15
+ var has = Object.prototype.hasOwnProperty,
16
+ prefix = '~';
16
17
 
17
- class CustomDocumentEvent {
18
- constructor() {
19
- this.eventMap = new Map();
20
- this.emitter = new EventEmitter__default.default();
21
- }
22
- addEventListener(event, listener) {
23
- if (event === exports.EVENT_NAMES.SCROLL_VIEW) {
24
- const _listener = listener;
25
- const eventHandler = () => {
26
- _listener([{ x: window.scrollX, y: window.scrollY }]);
27
- };
28
- window.addEventListener('scroll', eventHandler);
29
- this.eventMap.set(listener, eventHandler);
30
- }
31
- else {
32
- this.emitter.addListener(event, listener);
33
- this.eventMap.set(listener, listener);
34
- }
35
- }
36
- removeEventListener(event, listener) {
37
- if (event === exports.EVENT_NAMES.SCROLL_VIEW) {
38
- const eventHandler = this.eventMap.get(listener);
39
- if (eventHandler) {
40
- window.removeEventListener('scroll', eventHandler);
41
- this.eventMap.delete(listener);
42
- }
43
- }
44
- else {
45
- this.emitter.removeListener(event, listener);
46
- }
47
- }
48
- dispose() {
49
- this.emitter.removeAllListeners();
50
- }
51
- emit(event, data) {
52
- return this.emitter.emit(event, data);
53
- }
18
+ /**
19
+ * Constructor to create a storage for our `EE` objects.
20
+ * An `Events` instance is a plain object whose properties are event names.
21
+ *
22
+ * @constructor
23
+ * @private
24
+ */
25
+ function Events() {}
26
+
27
+ //
28
+ // We try to not inherit from `Object.prototype`. In some engines creating an
29
+ // instance in this way is faster than calling `Object.create(null)` directly.
30
+ // If `Object.create(null)` is not supported we prefix the event names with a
31
+ // character to make sure that the built-in object properties are not
32
+ // overridden or used as an attack vector.
33
+ //
34
+ if (Object.create) {
35
+ Events.prototype = Object.create(null);
36
+
37
+ //
38
+ // This hack is needed because the `__proto__` property is still inherited in
39
+ // some old browsers like Android 4, iPhone 5.1, Opera 11 and Safari 5.
40
+ //
41
+ if (!new Events().__proto__) prefix = false;
54
42
  }
55
- const scrollEvent = new CustomDocumentEvent();
56
-
57
- const staticScrollStyles = ctDesignStyleManager.makeStyles(() => ({
58
- viewport: {
59
- display: 'flex',
60
- flexDirection: 'column',
61
- minHeight: 0,
62
- minWidth: 0,
63
- width: '100%',
64
- position: 'relative',
65
- WebkitOverflowScrolling: 'touch',
66
- },
67
- viewportVertical: {
68
- flex: 1,
69
- overflowX: 'hidden',
70
- overflowY: 'auto',
71
- overscrollBehavior: 'contain',
72
- },
73
- viewportHorizontal: {
74
- flex: 1,
75
- minWidth: 0,
76
- overflowX: 'auto',
77
- overflowY: 'hidden',
78
- alignItems: 'flex-start',
79
- },
80
- contentVertical: {
81
- display: 'flex',
82
- flexDirection: 'column',
83
- flexShrink: 0,
84
- width: '100%',
85
- },
86
- contentHorizontal: {
87
- display: 'flex',
88
- flexDirection: 'row',
89
- flexWrap: 'nowrap',
90
- flexShrink: 0,
91
- width: 'max-content',
92
- minWidth: '100%',
93
- },
94
- hideScrollbar: {
95
- scrollbarWidth: 'none',
96
- msOverflowStyle: 'none',
97
- '&::-webkit-scrollbar': {
98
- display: 'none',
99
- },
100
- },
101
- pagingSnapVertical: { scrollSnapType: 'y mandatory' },
102
- pagingSnapHorizontal: { scrollSnapType: 'x mandatory' },
103
- viewportVerticalNested: {
104
- flex: 1,
105
- minHeight: 0,
106
- height: '100%',
107
- maxHeight: '100%',
108
- overflowX: 'hidden',
109
- overflowY: 'auto',
110
- overscrollBehavior: 'auto',
111
- },
112
- }));
113
- const getScrollableParent = (node) => {
114
- var _a;
115
- let parent = (_a = node === null || node === void 0 ? void 0 : node.parentElement) !== null && _a !== void 0 ? _a : null;
116
- while (parent) {
117
- const { overflowY } = getComputedStyle(parent);
118
- if (/(auto|scroll|overlay)/.test(overflowY) && parent.scrollHeight > parent.clientHeight + 1) {
119
- return parent;
120
- }
121
- parent = parent.parentElement;
122
- }
123
- return null;
124
- };
125
- const emitScrollEvent = (target) => ({
126
- nativeEvent: {
127
- contentOffset: {
128
- x: target.scrollLeft,
129
- y: target.scrollTop,
130
- },
131
- layoutMeasurement: {
132
- height: target.clientHeight,
133
- width: target.clientWidth,
134
- },
135
- contentSize: {
136
- height: target.scrollHeight,
137
- width: target.scrollWidth,
138
- },
139
- },
140
- });
141
- const ScrollContainer = react.forwardRef(({ children, styleConfig, onScroll, horizontal = false, scrollEventThrottle = 0, showsVerticalScrollIndicator = false, showsHorizontalScrollIndicator = true, width, onIndexChange, currentActiveIndex, pagingEnabled = false, snapToOffsets, totalItemsLength, contentContainerStyle, onLayout, attachScrollEvent = false, nestedScrollEnabled = false, stickyHeaderIndices: _stickyHeaderIndices, removeClippedSubviews: _removeClippedSubviews, isVisible: _isVisible, keyboardShouldPersistTaps: _keyboardShouldPersistTaps, bounces: _bounces, id, onClick, onMouseEnter, onMouseLeave, onTransitionEnd, onTouchStart, onTouchEnd, onTouchCancel, onTouchMove, }, ref) => {
142
- const localRef = react.useRef(null);
143
- const lastScrollEventTimeRef = react.useRef(0);
144
- const handleFocus = react.useCallback(() => {
145
- var _a;
146
- (_a = localRef.current) === null || _a === void 0 ? void 0 : _a.focus();
147
- }, []);
148
- const handleGetBoundingRect = react.useCallback((callback) => {
149
- var _a;
150
- const rect = (_a = localRef.current) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect();
151
- if (rect) {
152
- callback === null || callback === void 0 ? void 0 : callback({
153
- x: rect.x,
154
- y: rect.y,
155
- width: rect.width,
156
- height: rect.height,
157
- });
158
- }
159
- }, []);
160
- const handleAddEventListener = react.useCallback((type, listener, options) => {
161
- var _a;
162
- (_a = localRef.current) === null || _a === void 0 ? void 0 : _a.addEventListener(type, listener, options);
163
- }, []);
164
- const handleRemoveEventListener = react.useCallback((type, listener, options) => {
165
- var _a;
166
- (_a = localRef.current) === null || _a === void 0 ? void 0 : _a.removeEventListener(type, listener, options);
167
- }, []);
168
- react.useImperativeHandle(ref, () => {
169
- var _a;
170
- return ({
171
- focus: handleFocus,
172
- getBoundingRectWithCallback: handleGetBoundingRect,
173
- getElementRef: () => localRef,
174
- measure: (callback) => {
175
- var _a;
176
- const rect = (_a = localRef.current) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect();
177
- if (!rect)
178
- return;
179
- callback(rect.x, rect.y, rect.width, rect.height, rect.x, rect.y);
180
- },
181
- classList: {
182
- add: (className) => {
183
- var _a;
184
- (_a = localRef.current) === null || _a === void 0 ? void 0 : _a.classList.add(className);
185
- },
186
- },
187
- addEventListener: handleAddEventListener,
188
- removeEventListener: handleRemoveEventListener,
189
- style: (((_a = localRef.current) === null || _a === void 0 ? void 0 : _a.style) || {}),
190
- scrollTo: (x, y) => {
191
- const node = localRef.current;
192
- if (!node)
193
- return;
194
- node.scrollTo({ left: x, top: y, behavior: 'auto' });
195
- },
196
- scrollToEnd: () => {
197
- const node = localRef.current;
198
- if (!node)
199
- return;
200
- if (horizontal) {
201
- node.scrollTo({
202
- left: node.scrollWidth - node.clientWidth,
203
- top: 0,
204
- behavior: 'auto',
205
- });
206
- }
207
- else {
208
- node.scrollTo({
209
- left: 0,
210
- top: node.scrollHeight - node.clientHeight,
211
- behavior: 'auto',
212
- });
213
- }
214
- },
215
- scrollById: (id, animated = true) => {
216
- const target = document.getElementById(id);
217
- target === null || target === void 0 ? void 0 : target.scrollIntoView({ behavior: animated ? 'smooth' : 'auto', block: 'nearest', inline: 'nearest' });
218
- },
219
- get scrollLeft() {
220
- var _a, _b;
221
- return (_b = (_a = localRef.current) === null || _a === void 0 ? void 0 : _a.scrollLeft) !== null && _b !== void 0 ? _b : 0;
222
- },
223
- set scrollLeft(value) {
224
- if (localRef.current) {
225
- localRef.current.scrollLeft = value;
226
- }
227
- },
228
- get scrollTop() {
229
- var _a, _b;
230
- return (_b = (_a = localRef.current) === null || _a === void 0 ? void 0 : _a.scrollTop) !== null && _b !== void 0 ? _b : 0;
231
- },
232
- set scrollTop(value) {
233
- if (localRef.current) {
234
- localRef.current.scrollTop = value;
235
- }
236
- },
237
- });
238
- }, [handleFocus, handleGetBoundingRect, handleAddEventListener, handleRemoveEventListener, horizontal]);
239
- const { root: rootStyles = [] } = styleConfig || {};
240
- const contentStyles = contentContainerStyle !== null && contentContainerStyle !== void 0 ? contentContainerStyle : [];
241
- const hideHorizontalScrollbar = horizontal && showsHorizontalScrollIndicator === false;
242
- const hideVerticalScrollbar = !horizontal && showsVerticalScrollIndicator === false;
243
- const viewportClassName = ctDesignStyleManager.useWebMergeStyles([
244
- staticScrollStyles.viewport,
245
- horizontal
246
- ? staticScrollStyles.viewportHorizontal
247
- : nestedScrollEnabled
248
- ? staticScrollStyles.viewportVerticalNested
249
- : staticScrollStyles.viewportVertical,
250
- ...(hideHorizontalScrollbar || hideVerticalScrollbar ? [staticScrollStyles.hideScrollbar] : []),
251
- ...(pagingEnabled
252
- ? [horizontal ? staticScrollStyles.pagingSnapHorizontal : staticScrollStyles.pagingSnapVertical]
253
- : []),
254
- ...rootStyles,
255
- ], [horizontal, nestedScrollEnabled, hideHorizontalScrollbar, hideVerticalScrollbar, pagingEnabled, rootStyles]);
256
- const contentClassName = ctDesignStyleManager.useWebMergeStyles([horizontal ? staticScrollStyles.contentHorizontal : staticScrollStyles.contentVertical, ...contentStyles], [horizontal, contentStyles]);
257
- const fireScroll = react.useCallback((target) => {
258
- const event = emitScrollEvent(target);
259
- if (scrollEventThrottle > 0) {
260
- const now = Date.now();
261
- if (now - lastScrollEventTimeRef.current < scrollEventThrottle) {
262
- return;
263
- }
264
- lastScrollEventTimeRef.current = now;
265
- }
266
- if (attachScrollEvent) {
267
- scrollEvent.emit('scroll-view', [event.nativeEvent.contentOffset]);
268
- }
269
- onScroll === null || onScroll === void 0 ? void 0 : onScroll(event);
270
- if (horizontal && width && totalItemsLength && onIndexChange) {
271
- const offsetX = target.scrollLeft;
272
- const visibleWidth = target.clientWidth;
273
- const tolerance = 2;
274
- let index;
275
- if (offsetX <= tolerance) {
276
- index = 0;
277
- }
278
- else {
279
- const centerX = offsetX + visibleWidth / 2;
280
- index = Math.max(0, Math.min(totalItemsLength - 1, Math.round((centerX - width / 2) / width)));
281
- }
282
- onIndexChange(index);
283
- }
284
- }, [attachScrollEvent, horizontal, onIndexChange, onScroll, scrollEventThrottle, totalItemsLength, width]);
285
- const handleScroll = react.useCallback((e) => {
286
- fireScroll(e.currentTarget);
287
- }, [fireScroll]);
288
- const handleNestedWheel = react.useCallback((e) => {
289
- if (!nestedScrollEnabled || horizontal) {
290
- return;
291
- }
292
- const node = e.currentTarget;
293
- const hasOverflow = node.scrollHeight > node.clientHeight + 1;
294
- const maxScrollTop = node.scrollHeight - node.clientHeight;
295
- const atTop = node.scrollTop <= 0;
296
- const atBottom = node.scrollTop >= maxScrollTop - 1;
297
- const scrollingUp = e.deltaY < 0;
298
- const scrollingDown = e.deltaY > 0;
299
- const shouldForward = !hasOverflow || (scrollingUp && atTop) || (scrollingDown && atBottom);
300
- if (!shouldForward) {
301
- return;
302
- }
303
- const scrollableParent = getScrollableParent(node);
304
- if (!scrollableParent) {
305
- return;
306
- }
307
- e.preventDefault();
308
- scrollableParent.scrollTop += e.deltaY;
309
- }, [horizontal, nestedScrollEnabled]);
310
- react.useLayoutEffect(() => {
311
- const node = localRef.current;
312
- if (!node || !onLayout)
313
- return;
314
- onLayout({
315
- layout: {
316
- height: node.clientHeight,
317
- width: node.clientWidth,
318
- x: node.getBoundingClientRect().x,
319
- y: node.getBoundingClientRect().y,
320
- },
321
- });
322
- }, [onLayout]);
323
- react.useEffect(() => {
324
- const node = localRef.current;
325
- if (!node || typeof currentActiveIndex !== 'number' || currentActiveIndex < 0 || !width) {
326
- return;
327
- }
328
- node.scrollTo({
329
- left: horizontal ? currentActiveIndex * (width + 16) : 0,
330
- top: horizontal ? 0 : currentActiveIndex * (width + 16),
331
- behavior: 'smooth',
332
- });
333
- }, [currentActiveIndex, horizontal, width]);
334
- react.useEffect(() => {
335
- if (!(snapToOffsets === null || snapToOffsets === void 0 ? void 0 : snapToOffsets.length) || !localRef.current)
336
- return;
337
- }, [snapToOffsets]);
338
- return (jsxRuntime.jsx("div", { ref: localRef, id: id, className: viewportClassName, onScroll: handleScroll, onWheel: nestedScrollEnabled && !horizontal ? handleNestedWheel : undefined, onClick: (event) => onClick === null || onClick === void 0 ? void 0 : onClick({
339
- stopPropagation: () => event.stopPropagation(),
340
- preventDefault: () => event.preventDefault(),
341
- nativeEvent: {
342
- locationX: event.clientX,
343
- locationY: event.clientY,
344
- },
345
- }), onMouseEnter: onMouseEnter, onMouseLeave: onMouseLeave, onTransitionEnd: onTransitionEnd, onTouchStart: onTouchStart, onTouchEnd: onTouchEnd, onTouchCancel: onTouchCancel, onTouchMove: onTouchMove, children: jsxRuntime.jsx("div", { className: contentClassName, children: children }) }));
346
- });
347
- ScrollContainer.displayName = 'ScrollContainer';
348
-
349
- const ScrollContext = react.createContext({
350
- isScrolling: false,
351
- refs: {},
352
- observableChildRefs: [],
353
- parentScrollAvailable: false,
354
- horizontal: false,
355
- scrollToBottom: () => {
356
- },
357
- });
358
- const ScrollContextProvider = ({ children, isScrolling, refs, scrollTo, scrollToSkip, isScrollToScrolling, observableChildRefs, horizontal, scrollToBottom, }) => {
359
- return (jsxRuntime.jsx(ScrollContext.Provider, { value: {
360
- observableChildRefs,
361
- isScrolling,
362
- refs,
363
- scrollTo,
364
- scrollToSkip,
365
- isScrollToScrolling,
366
- parentScrollAvailable: true,
367
- horizontal,
368
- scrollToBottom,
369
- }, children: children }));
370
- };
371
- const useGetScrolling = () => {
372
- const context = react.useContext(ScrollContext);
373
- if (!context) {
374
- return { isScrolling: false, isScrollToScrolling: false, scrollParentAvailable: false, horizontal: false };
375
- }
376
- return {
377
- isScrolling: context.isScrolling,
378
- isScrollToScrolling: context.isScrollToScrolling,
379
- scrollParentAvailable: context.parentScrollAvailable,
380
- horizontal: context.horizontal,
381
- isList: context.isList,
382
- };
383
- };
384
- const useGetRefMaps = () => {
385
- const { refs } = react.useContext(ScrollContext);
386
- return refs;
387
- };
388
- const useGetScrollToLast = () => {
389
- const { scrollToBottom } = react.useContext(ScrollContext);
390
- return {
391
- scrollToBottom,
392
- };
393
- };
394
- const useAddObservableChild = (observableChildMethod) => {
395
- const context = react.useContext(ScrollContext);
396
- const insideScrollParent = !!context;
397
- const { observableChildRefs } = context || { observableChildRefs: [] };
398
- react.useEffect(() => {
399
- const observableChild = observableChildMethod();
400
- const index = insideScrollParent ? observableChildRefs.push(observableChild) : 0;
401
- return () => {
402
- if (insideScrollParent) {
403
- observableChildRefs.splice(index - 1, 1);
404
- }
405
- };
406
- }, [observableChildRefs, insideScrollParent]);
407
- };
408
- const useObserver = () => {
409
- const { observableChildRefs } = react.useContext(ScrollContext);
410
- return observableChildRefs;
43
+
44
+ /**
45
+ * Representation of a single event listener.
46
+ *
47
+ * @param {Function} fn The listener function.
48
+ * @param {*} context The context to invoke the listener with.
49
+ * @param {Boolean} [once=false] Specify if the listener is a one-time listener.
50
+ * @constructor
51
+ * @private
52
+ */
53
+ function EE(fn, context, once) {
54
+ this.fn = fn;
55
+ this.context = context;
56
+ this.once = once || false;
57
+ }
58
+
59
+ /**
60
+ * Add a listener for a given event.
61
+ *
62
+ * @param {EventEmitter} emitter Reference to the `EventEmitter` instance.
63
+ * @param {(String|Symbol)} event The event name.
64
+ * @param {Function} fn The listener function.
65
+ * @param {*} context The context to invoke the listener with.
66
+ * @param {Boolean} once Specify if the listener is a one-time listener.
67
+ * @returns {EventEmitter}
68
+ * @private
69
+ */
70
+ function addListener(emitter, event, fn, context, once) {
71
+ if (typeof fn !== 'function') {
72
+ throw new TypeError('The listener must be a function');
73
+ }
74
+ var listener = new EE(fn, context || emitter, once),
75
+ evt = prefix ? prefix + event : event;
76
+ if (!emitter._events[evt]) emitter._events[evt] = listener, emitter._eventsCount++;else if (!emitter._events[evt].fn) emitter._events[evt].push(listener);else emitter._events[evt] = [emitter._events[evt], listener];
77
+ return emitter;
78
+ }
79
+
80
+ /**
81
+ * Clear event by name.
82
+ *
83
+ * @param {EventEmitter} emitter Reference to the `EventEmitter` instance.
84
+ * @param {(String|Symbol)} evt The Event name.
85
+ * @private
86
+ */
87
+ function clearEvent(emitter, evt) {
88
+ if (--emitter._eventsCount === 0) emitter._events = new Events();else delete emitter._events[evt];
89
+ }
90
+
91
+ /**
92
+ * Minimal `EventEmitter` interface that is molded against the Node.js
93
+ * `EventEmitter` interface.
94
+ *
95
+ * @constructor
96
+ * @public
97
+ */
98
+ function EventEmitter() {
99
+ this._events = new Events();
100
+ this._eventsCount = 0;
101
+ }
102
+
103
+ /**
104
+ * Return an array listing the events for which the emitter has registered
105
+ * listeners.
106
+ *
107
+ * @returns {Array}
108
+ * @public
109
+ */
110
+ EventEmitter.prototype.eventNames = function eventNames() {
111
+ var names = [],
112
+ events,
113
+ name;
114
+ if (this._eventsCount === 0) return names;
115
+ for (name in events = this._events) {
116
+ if (has.call(events, name)) names.push(prefix ? name.slice(1) : name);
117
+ }
118
+ if (Object.getOwnPropertySymbols) {
119
+ return names.concat(Object.getOwnPropertySymbols(events));
120
+ }
121
+ return names;
411
122
  };
412
123
 
413
- const useHandleScroll = ({ threshold = 140, animated = false } = {}) => {
414
- const [isAtTop, setIsAtTop] = react.useState(false);
415
- const handleScroll = react.useCallback((_event) => {
416
- const position = window.scrollY > threshold;
417
- setIsAtTop(position);
418
- }, [threshold]);
419
- const handleWindowScroll = react.useCallback((_event) => {
420
- handleScroll({
421
- nativeEvent: {
422
- contentOffset: { x: 0, y: window.scrollY },
423
- layoutMeasurement: { height: 0, width: 0 },
424
- contentSize: { height: 0, width: 0 },
425
- },
426
- });
427
- }, [handleScroll]);
428
- react.useEffect(() => {
429
- if (ctDesignCommonUtils.isServer() || !animated)
430
- return;
431
- window.addEventListener('scroll', handleWindowScroll);
432
- return () => {
433
- window.removeEventListener('scroll', handleWindowScroll);
434
- };
435
- }, [handleWindowScroll, animated]);
436
- const scrollManager = react.useMemo(() => ({ isAtTop, handleScroll, scrollY: window.scrollY }), [isAtTop, handleScroll]);
437
- return scrollManager;
124
+ /**
125
+ * Return the listeners registered for a given event.
126
+ *
127
+ * @param {(String|Symbol)} event The event name.
128
+ * @returns {Array} The registered listeners.
129
+ * @public
130
+ */
131
+ EventEmitter.prototype.listeners = function listeners(event) {
132
+ var evt = prefix ? prefix + event : event,
133
+ handlers = this._events[evt];
134
+ if (!handlers) return [];
135
+ if (handlers.fn) return [handlers.fn];
136
+ for (var i = 0, l = handlers.length, ee = new Array(l); i < l; i++) {
137
+ ee[i] = handlers[i].fn;
138
+ }
139
+ return ee;
438
140
  };
439
141
 
440
- const AnimatedScrollContext = react.createContext({
441
- isAtTop: false,
442
- handleScroll: () => {
443
- },
444
- scrollY: 0,
445
- });
446
- const AnimatedScrollContextProvider = ({ children, threshold = 0, }) => {
447
- const scrollManager = useHandleScroll({ threshold });
448
- return jsxRuntime.jsx(AnimatedScrollContext.Provider, { value: scrollManager, children: children });
142
+ /**
143
+ * Return the number of listeners listening to a given event.
144
+ *
145
+ * @param {(String|Symbol)} event The event name.
146
+ * @returns {Number} The number of listeners.
147
+ * @public
148
+ */
149
+ EventEmitter.prototype.listenerCount = function listenerCount(event) {
150
+ var evt = prefix ? prefix + event : event,
151
+ listeners = this._events[evt];
152
+ if (!listeners) return 0;
153
+ if (listeners.fn) return 1;
154
+ return listeners.length;
449
155
  };
450
- const useAnimatedScroll = () => {
451
- const context = react.useContext(AnimatedScrollContext);
452
- if (!context) {
453
- return {
454
- isAtTop: false,
455
- handleScroll: () => {
456
- },
457
- scrollY: 0,
458
- };
156
+
157
+ /**
158
+ * Calls each of the listeners registered for a given event.
159
+ *
160
+ * @param {(String|Symbol)} event The event name.
161
+ * @returns {Boolean} `true` if the event had listeners, else `false`.
162
+ * @public
163
+ */
164
+ EventEmitter.prototype.emit = function emit(event, a1, a2, a3, a4, a5) {
165
+ var evt = prefix ? prefix + event : event;
166
+ if (!this._events[evt]) return false;
167
+ var listeners = this._events[evt],
168
+ len = arguments.length,
169
+ args,
170
+ i;
171
+ if (listeners.fn) {
172
+ if (listeners.once) this.removeListener(event, listeners.fn, undefined, true);
173
+ switch (len) {
174
+ case 1:
175
+ return listeners.fn.call(listeners.context), true;
176
+ case 2:
177
+ return listeners.fn.call(listeners.context, a1), true;
178
+ case 3:
179
+ return listeners.fn.call(listeners.context, a1, a2), true;
180
+ case 4:
181
+ return listeners.fn.call(listeners.context, a1, a2, a3), true;
182
+ case 5:
183
+ return listeners.fn.call(listeners.context, a1, a2, a3, a4), true;
184
+ case 6:
185
+ return listeners.fn.call(listeners.context, a1, a2, a3, a4, a5), true;
459
186
  }
460
- return context;
461
- };
462
- const useAnimatedScrollValue = () => {
463
- const { scrollY, isAtTop } = useAnimatedScroll();
464
- return { scrollY, isAtTop };
187
+ for (i = 1, args = new Array(len - 1); i < len; i++) {
188
+ args[i - 1] = arguments[i];
189
+ }
190
+ listeners.fn.apply(listeners.context, args);
191
+ } else {
192
+ var length = listeners.length,
193
+ j;
194
+ for (i = 0; i < length; i++) {
195
+ if (listeners[i].once) this.removeListener(event, listeners[i].fn, undefined, true);
196
+ switch (len) {
197
+ case 1:
198
+ listeners[i].fn.call(listeners[i].context);
199
+ break;
200
+ case 2:
201
+ listeners[i].fn.call(listeners[i].context, a1);
202
+ break;
203
+ case 3:
204
+ listeners[i].fn.call(listeners[i].context, a1, a2);
205
+ break;
206
+ case 4:
207
+ listeners[i].fn.call(listeners[i].context, a1, a2, a3);
208
+ break;
209
+ default:
210
+ if (!args) for (j = 1, args = new Array(len - 1); j < len; j++) {
211
+ args[j - 1] = arguments[j];
212
+ }
213
+ listeners[i].fn.apply(listeners[i].context, args);
214
+ }
215
+ }
216
+ }
217
+ return true;
465
218
  };
466
219
 
467
- const StickyContext = react.createContext({ stickyIndex: -1 });
468
- const StickyProvider = ({ children, stickyIndex }) => {
469
- return jsxRuntime.jsx(StickyContext.Provider, { value: { stickyIndex }, children: children });
470
- };
471
- const ParentStickyContext = react.createContext({});
472
- const ParentStickyProvider = ({ children, addStickyIndices, removeStickIndices, }) => {
473
- return (jsxRuntime.jsx(ParentStickyContext.Provider, { value: { addStickyIndices, removeStickIndices }, children: children }));
220
+ /**
221
+ * Add a listener for a given event.
222
+ *
223
+ * @param {(String|Symbol)} event The event name.
224
+ * @param {Function} fn The listener function.
225
+ * @param {*} [context=this] The context to invoke the listener with.
226
+ * @returns {EventEmitter} `this`.
227
+ * @public
228
+ */
229
+ EventEmitter.prototype.on = function on(event, fn, context) {
230
+ return addListener(this, event, fn, context, false);
474
231
  };
475
- const useStickyContext = () => {
476
- const { stickyIndex } = react.useContext(StickyContext);
477
- return stickyIndex;
232
+
233
+ /**
234
+ * Add a one-time listener for a given event.
235
+ *
236
+ * @param {(String|Symbol)} event The event name.
237
+ * @param {Function} fn The listener function.
238
+ * @param {*} [context=this] The context to invoke the listener with.
239
+ * @returns {EventEmitter} `this`.
240
+ * @public
241
+ */
242
+ EventEmitter.prototype.once = function once(event, fn, context) {
243
+ return addListener(this, event, fn, context, true);
478
244
  };
479
- const useParentStickyContext = () => {
480
- const { addStickyIndices, removeStickIndices } = react.useContext(ParentStickyContext);
481
- return [addStickyIndices, removeStickIndices];
245
+
246
+ /**
247
+ * Remove the listeners of a given event.
248
+ *
249
+ * @param {(String|Symbol)} event The event name.
250
+ * @param {Function} fn Only remove the listeners that match this function.
251
+ * @param {*} context Only remove the listeners that have this context.
252
+ * @param {Boolean} once Only remove one-time listeners.
253
+ * @returns {EventEmitter} `this`.
254
+ * @public
255
+ */
256
+ EventEmitter.prototype.removeListener = function removeListener(event, fn, context, once) {
257
+ var evt = prefix ? prefix + event : event;
258
+ if (!this._events[evt]) return this;
259
+ if (!fn) {
260
+ clearEvent(this, evt);
261
+ return this;
262
+ }
263
+ var listeners = this._events[evt];
264
+ if (listeners.fn) {
265
+ if (listeners.fn === fn && (!once || listeners.once) && (!context || listeners.context === context)) {
266
+ clearEvent(this, evt);
267
+ }
268
+ } else {
269
+ for (var i = 0, events = [], length = listeners.length; i < length; i++) {
270
+ if (listeners[i].fn !== fn || once && !listeners[i].once || context && listeners[i].context !== context) {
271
+ events.push(listeners[i]);
272
+ }
273
+ }
274
+
275
+ //
276
+ // Reset the array, or remove it completely if we have no more listeners.
277
+ //
278
+ if (events.length) this._events[evt] = events.length === 1 ? events[0] : events;else clearEvent(this, evt);
279
+ }
280
+ return this;
482
281
  };
483
282
 
484
- const useScrollById = () => {
485
- const { refs, scrollTo: _scrollTo } = react.useContext(ScrollContext);
486
- const scrollTo = react.useCallback((id, { x: _x, y: _y }) => {
487
- var _a, _b, _c;
488
- (_c = (_b = (_a = refs[id]) === null || _a === void 0 ? void 0 : _a.current) === null || _b === void 0 ? void 0 : _b.getBoundingRectWithCallback) === null || _c === void 0 ? void 0 : _c.call(_b, ({ x, y }) => {
489
- _scrollTo === null || _scrollTo === void 0 ? void 0 : _scrollTo(y - _y, x - _x);
490
- });
491
- }, [_scrollTo, refs]);
492
- return { scrollTo };
283
+ /**
284
+ * Remove all listeners, or those of the specified event.
285
+ *
286
+ * @param {(String|Symbol)} [event] The event name.
287
+ * @returns {EventEmitter} `this`.
288
+ * @public
289
+ */
290
+ EventEmitter.prototype.removeAllListeners = function removeAllListeners(event) {
291
+ var evt;
292
+ if (event) {
293
+ evt = prefix ? prefix + event : event;
294
+ if (this._events[evt]) clearEvent(this, evt);
295
+ } else {
296
+ this._events = new Events();
297
+ this._eventsCount = 0;
298
+ }
299
+ return this;
493
300
  };
494
301
 
495
- exports.AnimatedScrollContextProvider = AnimatedScrollContextProvider;
496
- exports.ParentStickyProvider = ParentStickyProvider;
497
- exports.ScrollContainer = ScrollContainer;
498
- exports.ScrollContext = ScrollContext;
499
- exports.ScrollContextProvider = ScrollContextProvider;
500
- exports.StickyProvider = StickyProvider;
501
- exports.scrollEvent = scrollEvent;
502
- exports.useAddObservableChild = useAddObservableChild;
503
- exports.useAnimatedScroll = useAnimatedScroll;
504
- exports.useAnimatedScrollValue = useAnimatedScrollValue;
505
- exports.useGetRefMaps = useGetRefMaps;
506
- exports.useGetScrollToLast = useGetScrollToLast;
507
- exports.useGetScrolling = useGetScrolling;
508
- exports.useHandleScroll = useHandleScroll;
509
- exports.useObserver = useObserver;
510
- exports.useParentStickyContext = useParentStickyContext;
511
- exports.useScrollById = useScrollById;
512
- exports.useStickyContext = useStickyContext;
302
+ //
303
+ // Alias methods names because people roll like that.
304
+ //
305
+ EventEmitter.prototype.off = EventEmitter.prototype.removeListener;
306
+ EventEmitter.prototype.addListener = EventEmitter.prototype.on;
307
+
308
+ //
309
+ // Expose the prefix.
310
+ //
311
+ EventEmitter.prefixed = prefix;
312
+
313
+ //
314
+ // Allow `EventEmitter` to be imported as module namespace.
315
+ //
316
+ EventEmitter.EventEmitter = EventEmitter;
317
+
318
+ //
319
+ // Expose the module.
320
+ //
321
+ {
322
+ module.exports = EventEmitter;
323
+ }
324
+ })(eventemitter3);
325
+
326
+ exports.EVENT_NAMES = void 0;
327
+ (function (EVENT_NAMES) {
328
+ EVENT_NAMES["SCROLL_VIEW"] = "scroll-view";
329
+ EVENT_NAMES["SCROLL_INTO_VIEW"] = "scroll-into-view";
330
+ })(exports.EVENT_NAMES || (exports.EVENT_NAMES = {}));
331
+
332
+ class CustomDocumentEvent {
333
+ constructor() {
334
+ this.eventMap = new Map();
335
+ this.emitter = new eventemitter3Exports();
336
+ }
337
+ addEventListener(event, listener) {
338
+ if (event === exports.EVENT_NAMES.SCROLL_VIEW) {
339
+ const _listener = listener;
340
+ const eventHandler = () => {
341
+ _listener([{ x: window.scrollX, y: window.scrollY }]);
342
+ };
343
+ window.addEventListener('scroll', eventHandler);
344
+ this.eventMap.set(listener, eventHandler);
345
+ }
346
+ else {
347
+ this.emitter.addListener(event, listener);
348
+ this.eventMap.set(listener, listener);
349
+ }
350
+ }
351
+ removeEventListener(event, listener) {
352
+ if (event === exports.EVENT_NAMES.SCROLL_VIEW) {
353
+ const eventHandler = this.eventMap.get(listener);
354
+ if (eventHandler) {
355
+ window.removeEventListener('scroll', eventHandler);
356
+ this.eventMap.delete(listener);
357
+ }
358
+ }
359
+ else {
360
+ this.emitter.removeListener(event, listener);
361
+ }
362
+ }
363
+ dispose() {
364
+ this.emitter.removeAllListeners();
365
+ }
366
+ emit(event, data) {
367
+ return this.emitter.emit(event, data);
368
+ }
369
+ }
370
+ const scrollEvent = new CustomDocumentEvent();
371
+
372
+ const staticScrollStyles = ctDesignStyleManager.makeStyles(() => ({
373
+ viewport: {
374
+ display: 'flex',
375
+ flexDirection: 'column',
376
+ minHeight: 0,
377
+ minWidth: 0,
378
+ width: '100%',
379
+ position: 'relative',
380
+ WebkitOverflowScrolling: 'touch',
381
+ },
382
+ viewportVertical: {
383
+ flex: 1,
384
+ overflowX: 'hidden',
385
+ overflowY: 'auto',
386
+ overscrollBehavior: 'contain',
387
+ },
388
+ viewportHorizontal: {
389
+ flex: 1,
390
+ minWidth: 0,
391
+ overflowX: 'auto',
392
+ overflowY: 'hidden',
393
+ alignItems: 'flex-start',
394
+ },
395
+ contentVertical: {
396
+ display: 'flex',
397
+ flexDirection: 'column',
398
+ flexShrink: 0,
399
+ width: '100%',
400
+ },
401
+ contentHorizontal: {
402
+ display: 'flex',
403
+ flexDirection: 'row',
404
+ flexWrap: 'nowrap',
405
+ flexShrink: 0,
406
+ width: 'max-content',
407
+ minWidth: '100%',
408
+ },
409
+ hideScrollbar: {
410
+ scrollbarWidth: 'none',
411
+ msOverflowStyle: 'none',
412
+ '&::-webkit-scrollbar': {
413
+ display: 'none',
414
+ },
415
+ },
416
+ pagingSnapVertical: { scrollSnapType: 'y mandatory' },
417
+ pagingSnapHorizontal: { scrollSnapType: 'x mandatory' },
418
+ viewportVerticalNested: {
419
+ flex: 1,
420
+ minHeight: 0,
421
+ height: '100%',
422
+ maxHeight: '100%',
423
+ overflowX: 'hidden',
424
+ overflowY: 'auto',
425
+ overscrollBehavior: 'auto',
426
+ },
427
+ }));
428
+ const getScrollableParent = (node) => {
429
+ var _a;
430
+ let parent = (_a = node === null || node === void 0 ? void 0 : node.parentElement) !== null && _a !== void 0 ? _a : null;
431
+ while (parent) {
432
+ const { overflowY } = getComputedStyle(parent);
433
+ if (/(auto|scroll|overlay)/.test(overflowY) && parent.scrollHeight > parent.clientHeight + 1) {
434
+ return parent;
435
+ }
436
+ parent = parent.parentElement;
437
+ }
438
+ return null;
439
+ };
440
+ const emitScrollEvent = (target) => ({
441
+ nativeEvent: {
442
+ contentOffset: {
443
+ x: target.scrollLeft,
444
+ y: target.scrollTop,
445
+ },
446
+ layoutMeasurement: {
447
+ height: target.clientHeight,
448
+ width: target.clientWidth,
449
+ },
450
+ contentSize: {
451
+ height: target.scrollHeight,
452
+ width: target.scrollWidth,
453
+ },
454
+ },
455
+ });
456
+ const ScrollContainer = react.forwardRef(({ children, styleConfig, onScroll, horizontal = false, scrollEventThrottle = 0, showsVerticalScrollIndicator = false, showsHorizontalScrollIndicator = true, width, onIndexChange, currentActiveIndex, pagingEnabled = false, snapToOffsets, totalItemsLength, contentContainerStyle, onLayout, attachScrollEvent = false, nestedScrollEnabled = false, stickyHeaderIndices: _stickyHeaderIndices, removeClippedSubviews: _removeClippedSubviews, isVisible: _isVisible, keyboardShouldPersistTaps: _keyboardShouldPersistTaps, bounces: _bounces, id, onClick, onMouseEnter, onMouseLeave, onTransitionEnd, onTouchStart, onTouchEnd, onTouchCancel, onTouchMove, }, ref) => {
457
+ const localRef = react.useRef(null);
458
+ const lastScrollEventTimeRef = react.useRef(0);
459
+ const handleFocus = react.useCallback(() => {
460
+ var _a;
461
+ (_a = localRef.current) === null || _a === void 0 ? void 0 : _a.focus();
462
+ }, []);
463
+ const handleGetBoundingRect = react.useCallback((callback) => {
464
+ var _a;
465
+ const rect = (_a = localRef.current) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect();
466
+ if (rect) {
467
+ callback === null || callback === void 0 ? void 0 : callback({
468
+ x: rect.x,
469
+ y: rect.y,
470
+ width: rect.width,
471
+ height: rect.height,
472
+ });
473
+ }
474
+ }, []);
475
+ const handleAddEventListener = react.useCallback((type, listener, options) => {
476
+ var _a;
477
+ (_a = localRef.current) === null || _a === void 0 ? void 0 : _a.addEventListener(type, listener, options);
478
+ }, []);
479
+ const handleRemoveEventListener = react.useCallback((type, listener, options) => {
480
+ var _a;
481
+ (_a = localRef.current) === null || _a === void 0 ? void 0 : _a.removeEventListener(type, listener, options);
482
+ }, []);
483
+ react.useImperativeHandle(ref, () => {
484
+ var _a;
485
+ return ({
486
+ focus: handleFocus,
487
+ getBoundingRectWithCallback: handleGetBoundingRect,
488
+ getElementRef: () => localRef,
489
+ measure: (callback) => {
490
+ var _a;
491
+ const rect = (_a = localRef.current) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect();
492
+ if (!rect)
493
+ return;
494
+ callback(rect.x, rect.y, rect.width, rect.height, rect.x, rect.y);
495
+ },
496
+ classList: {
497
+ add: (className) => {
498
+ var _a;
499
+ (_a = localRef.current) === null || _a === void 0 ? void 0 : _a.classList.add(className);
500
+ },
501
+ },
502
+ addEventListener: handleAddEventListener,
503
+ removeEventListener: handleRemoveEventListener,
504
+ style: (((_a = localRef.current) === null || _a === void 0 ? void 0 : _a.style) || {}),
505
+ scrollTo: (x, y) => {
506
+ const node = localRef.current;
507
+ if (!node)
508
+ return;
509
+ node.scrollTo({ left: x, top: y, behavior: 'auto' });
510
+ },
511
+ scrollToEnd: () => {
512
+ const node = localRef.current;
513
+ if (!node)
514
+ return;
515
+ if (horizontal) {
516
+ node.scrollTo({
517
+ left: node.scrollWidth - node.clientWidth,
518
+ top: 0,
519
+ behavior: 'auto',
520
+ });
521
+ }
522
+ else {
523
+ node.scrollTo({
524
+ left: 0,
525
+ top: node.scrollHeight - node.clientHeight,
526
+ behavior: 'auto',
527
+ });
528
+ }
529
+ },
530
+ scrollById: (id, animated = true) => {
531
+ const target = document.getElementById(id);
532
+ target === null || target === void 0 ? void 0 : target.scrollIntoView({ behavior: animated ? 'smooth' : 'auto', block: 'nearest', inline: 'nearest' });
533
+ },
534
+ get scrollLeft() {
535
+ var _a, _b;
536
+ return (_b = (_a = localRef.current) === null || _a === void 0 ? void 0 : _a.scrollLeft) !== null && _b !== void 0 ? _b : 0;
537
+ },
538
+ set scrollLeft(value) {
539
+ if (localRef.current) {
540
+ localRef.current.scrollLeft = value;
541
+ }
542
+ },
543
+ get scrollTop() {
544
+ var _a, _b;
545
+ return (_b = (_a = localRef.current) === null || _a === void 0 ? void 0 : _a.scrollTop) !== null && _b !== void 0 ? _b : 0;
546
+ },
547
+ set scrollTop(value) {
548
+ if (localRef.current) {
549
+ localRef.current.scrollTop = value;
550
+ }
551
+ },
552
+ });
553
+ }, [handleFocus, handleGetBoundingRect, handleAddEventListener, handleRemoveEventListener, horizontal]);
554
+ const { root: rootStyles = [] } = styleConfig || {};
555
+ const contentStyles = contentContainerStyle !== null && contentContainerStyle !== void 0 ? contentContainerStyle : [];
556
+ const hideHorizontalScrollbar = horizontal && showsHorizontalScrollIndicator === false;
557
+ const hideVerticalScrollbar = !horizontal && showsVerticalScrollIndicator === false;
558
+ const viewportClassName = ctDesignStyleManager.useWebMergeStyles([
559
+ staticScrollStyles.viewport,
560
+ horizontal
561
+ ? staticScrollStyles.viewportHorizontal
562
+ : nestedScrollEnabled
563
+ ? staticScrollStyles.viewportVerticalNested
564
+ : staticScrollStyles.viewportVertical,
565
+ ...(hideHorizontalScrollbar || hideVerticalScrollbar ? [staticScrollStyles.hideScrollbar] : []),
566
+ ...(pagingEnabled
567
+ ? [horizontal ? staticScrollStyles.pagingSnapHorizontal : staticScrollStyles.pagingSnapVertical]
568
+ : []),
569
+ ...rootStyles,
570
+ ], [horizontal, nestedScrollEnabled, hideHorizontalScrollbar, hideVerticalScrollbar, pagingEnabled, rootStyles]);
571
+ const contentClassName = ctDesignStyleManager.useWebMergeStyles([horizontal ? staticScrollStyles.contentHorizontal : staticScrollStyles.contentVertical, ...contentStyles], [horizontal, contentStyles]);
572
+ const fireScroll = react.useCallback((target) => {
573
+ const event = emitScrollEvent(target);
574
+ if (scrollEventThrottle > 0) {
575
+ const now = Date.now();
576
+ if (now - lastScrollEventTimeRef.current < scrollEventThrottle) {
577
+ return;
578
+ }
579
+ lastScrollEventTimeRef.current = now;
580
+ }
581
+ if (attachScrollEvent) {
582
+ scrollEvent.emit('scroll-view', [event.nativeEvent.contentOffset]);
583
+ }
584
+ onScroll === null || onScroll === void 0 ? void 0 : onScroll(event);
585
+ if (horizontal && width && totalItemsLength && onIndexChange) {
586
+ const offsetX = target.scrollLeft;
587
+ const visibleWidth = target.clientWidth;
588
+ const tolerance = 2;
589
+ let index;
590
+ if (offsetX <= tolerance) {
591
+ index = 0;
592
+ }
593
+ else {
594
+ const centerX = offsetX + visibleWidth / 2;
595
+ index = Math.max(0, Math.min(totalItemsLength - 1, Math.round((centerX - width / 2) / width)));
596
+ }
597
+ onIndexChange(index);
598
+ }
599
+ }, [attachScrollEvent, horizontal, onIndexChange, onScroll, scrollEventThrottle, totalItemsLength, width]);
600
+ const handleScroll = react.useCallback((e) => {
601
+ fireScroll(e.currentTarget);
602
+ }, [fireScroll]);
603
+ const handleNestedWheel = react.useCallback((e) => {
604
+ if (!nestedScrollEnabled || horizontal) {
605
+ return;
606
+ }
607
+ const node = e.currentTarget;
608
+ const hasOverflow = node.scrollHeight > node.clientHeight + 1;
609
+ const maxScrollTop = node.scrollHeight - node.clientHeight;
610
+ const atTop = node.scrollTop <= 0;
611
+ const atBottom = node.scrollTop >= maxScrollTop - 1;
612
+ const scrollingUp = e.deltaY < 0;
613
+ const scrollingDown = e.deltaY > 0;
614
+ const shouldForward = !hasOverflow || (scrollingUp && atTop) || (scrollingDown && atBottom);
615
+ if (!shouldForward) {
616
+ return;
617
+ }
618
+ const scrollableParent = getScrollableParent(node);
619
+ if (!scrollableParent) {
620
+ return;
621
+ }
622
+ e.preventDefault();
623
+ scrollableParent.scrollTop += e.deltaY;
624
+ }, [horizontal, nestedScrollEnabled]);
625
+ react.useLayoutEffect(() => {
626
+ const node = localRef.current;
627
+ if (!node || !onLayout)
628
+ return;
629
+ onLayout({
630
+ layout: {
631
+ height: node.clientHeight,
632
+ width: node.clientWidth,
633
+ x: node.getBoundingClientRect().x,
634
+ y: node.getBoundingClientRect().y,
635
+ },
636
+ });
637
+ }, [onLayout]);
638
+ react.useEffect(() => {
639
+ const node = localRef.current;
640
+ if (!node || typeof currentActiveIndex !== 'number' || currentActiveIndex < 0 || !width) {
641
+ return;
642
+ }
643
+ node.scrollTo({
644
+ left: horizontal ? currentActiveIndex * (width + 16) : 0,
645
+ top: horizontal ? 0 : currentActiveIndex * (width + 16),
646
+ behavior: 'smooth',
647
+ });
648
+ }, [currentActiveIndex, horizontal, width]);
649
+ react.useEffect(() => {
650
+ if (!(snapToOffsets === null || snapToOffsets === void 0 ? void 0 : snapToOffsets.length) || !localRef.current)
651
+ return;
652
+ }, [snapToOffsets]);
653
+ return (jsxRuntime.jsx("div", { ref: localRef, id: id, className: viewportClassName, onScroll: handleScroll, onWheel: nestedScrollEnabled && !horizontal ? handleNestedWheel : undefined, onClick: onClick, onMouseEnter: onMouseEnter, onMouseLeave: onMouseLeave, onTransitionEnd: onTransitionEnd, onTouchStart: onTouchStart, onTouchEnd: onTouchEnd, onTouchCancel: onTouchCancel, onTouchMove: onTouchMove, children: jsxRuntime.jsx("div", { className: contentClassName, children: children }) }));
654
+ });
655
+ ScrollContainer.displayName = 'ScrollContainer';
656
+
657
+ const ScrollContext = react.createContext({
658
+ isScrolling: false,
659
+ refs: {},
660
+ observableChildRefs: [],
661
+ parentScrollAvailable: false,
662
+ horizontal: false,
663
+ scrollToBottom: () => {
664
+ },
665
+ });
666
+ const ScrollContextProvider = ({ children, isScrolling, refs, scrollTo, scrollToSkip, isScrollToScrolling, observableChildRefs, horizontal, scrollToBottom, }) => {
667
+ return (jsxRuntime.jsx(ScrollContext.Provider, { value: {
668
+ observableChildRefs,
669
+ isScrolling,
670
+ refs,
671
+ scrollTo,
672
+ scrollToSkip,
673
+ isScrollToScrolling,
674
+ parentScrollAvailable: true,
675
+ horizontal,
676
+ scrollToBottom,
677
+ }, children: children }));
678
+ };
679
+ const useGetScrolling = () => {
680
+ const context = react.useContext(ScrollContext);
681
+ if (!context) {
682
+ return { isScrolling: false, isScrollToScrolling: false, scrollParentAvailable: false, horizontal: false };
683
+ }
684
+ return {
685
+ isScrolling: context.isScrolling,
686
+ isScrollToScrolling: context.isScrollToScrolling,
687
+ scrollParentAvailable: context.parentScrollAvailable,
688
+ horizontal: context.horizontal,
689
+ isList: context.isList,
690
+ };
691
+ };
692
+ const useGetRefMaps = () => {
693
+ const { refs } = react.useContext(ScrollContext);
694
+ return refs;
695
+ };
696
+ const useGetScrollToLast = () => {
697
+ const { scrollToBottom } = react.useContext(ScrollContext);
698
+ return {
699
+ scrollToBottom,
700
+ };
701
+ };
702
+ const useAddObservableChild = (observableChildMethod) => {
703
+ const context = react.useContext(ScrollContext);
704
+ const insideScrollParent = !!context;
705
+ const { observableChildRefs } = context || { observableChildRefs: [] };
706
+ react.useEffect(() => {
707
+ const observableChild = observableChildMethod();
708
+ const index = insideScrollParent ? observableChildRefs.push(observableChild) : 0;
709
+ return () => {
710
+ if (insideScrollParent) {
711
+ observableChildRefs.splice(index - 1, 1);
712
+ }
713
+ };
714
+ }, [observableChildRefs, insideScrollParent]);
715
+ };
716
+ const useObserver = () => {
717
+ const { observableChildRefs } = react.useContext(ScrollContext);
718
+ return observableChildRefs;
719
+ };
720
+
721
+ const useHandleScroll = ({ threshold = 140, animated = false } = {}) => {
722
+ const [isAtTop, setIsAtTop] = react.useState(false);
723
+ const handleScroll = react.useCallback((_event) => {
724
+ const position = window.scrollY > threshold;
725
+ setIsAtTop(position);
726
+ }, [threshold]);
727
+ const handleWindowScroll = react.useCallback((_event) => {
728
+ handleScroll({
729
+ nativeEvent: {
730
+ contentOffset: { x: 0, y: window.scrollY },
731
+ layoutMeasurement: { height: 0, width: 0 },
732
+ contentSize: { height: 0, width: 0 },
733
+ },
734
+ });
735
+ }, [handleScroll]);
736
+ react.useEffect(() => {
737
+ if (ctDesignCommonUtils.isServer() || !animated)
738
+ return;
739
+ window.addEventListener('scroll', handleWindowScroll);
740
+ return () => {
741
+ window.removeEventListener('scroll', handleWindowScroll);
742
+ };
743
+ }, [handleWindowScroll, animated]);
744
+ const scrollManager = react.useMemo(() => ({ isAtTop, handleScroll, scrollY: window.scrollY }), [isAtTop, handleScroll]);
745
+ return scrollManager;
746
+ };
747
+
748
+ const AnimatedScrollContext = react.createContext({
749
+ isAtTop: false,
750
+ handleScroll: () => {
751
+ },
752
+ scrollY: 0,
753
+ });
754
+ const AnimatedScrollContextProvider = ({ children, threshold = 0, }) => {
755
+ const scrollManager = useHandleScroll({ threshold });
756
+ return jsxRuntime.jsx(AnimatedScrollContext.Provider, { value: scrollManager, children: children });
757
+ };
758
+ const useAnimatedScroll = () => {
759
+ const context = react.useContext(AnimatedScrollContext);
760
+ if (!context) {
761
+ return {
762
+ isAtTop: false,
763
+ handleScroll: () => {
764
+ },
765
+ scrollY: 0,
766
+ };
767
+ }
768
+ return context;
769
+ };
770
+ const useAnimatedScrollValue = () => {
771
+ const { scrollY, isAtTop } = useAnimatedScroll();
772
+ return { scrollY, isAtTop };
773
+ };
774
+
775
+ const StickyContext = react.createContext({ stickyIndex: -1 });
776
+ const StickyProvider = ({ children, stickyIndex }) => {
777
+ return jsxRuntime.jsx(StickyContext.Provider, { value: { stickyIndex }, children: children });
778
+ };
779
+ const ParentStickyContext = react.createContext({});
780
+ const ParentStickyProvider = ({ children, addStickyIndices, removeStickIndices, }) => {
781
+ return (jsxRuntime.jsx(ParentStickyContext.Provider, { value: { addStickyIndices, removeStickIndices }, children: children }));
782
+ };
783
+ const useStickyContext = () => {
784
+ const { stickyIndex } = react.useContext(StickyContext);
785
+ return stickyIndex;
786
+ };
787
+ const useParentStickyContext = () => {
788
+ const { addStickyIndices, removeStickIndices } = react.useContext(ParentStickyContext);
789
+ return [addStickyIndices, removeStickIndices];
790
+ };
791
+
792
+ const useScrollById = () => {
793
+ const { refs, scrollTo: _scrollTo } = react.useContext(ScrollContext);
794
+ const scrollTo = react.useCallback((id, { x: _x, y: _y }) => {
795
+ var _a, _b, _c;
796
+ (_c = (_b = (_a = refs[id]) === null || _a === void 0 ? void 0 : _a.current) === null || _b === void 0 ? void 0 : _b.getBoundingRectWithCallback) === null || _c === void 0 ? void 0 : _c.call(_b, ({ x, y }) => {
797
+ _scrollTo === null || _scrollTo === void 0 ? void 0 : _scrollTo(y - _y, x - _x);
798
+ });
799
+ }, [_scrollTo, refs]);
800
+ return { scrollTo };
801
+ };
802
+
803
+ exports.AnimatedScrollContextProvider = AnimatedScrollContextProvider;
804
+ exports.ParentStickyProvider = ParentStickyProvider;
805
+ exports.ScrollContainer = ScrollContainer;
806
+ exports.ScrollContext = ScrollContext;
807
+ exports.ScrollContextProvider = ScrollContextProvider;
808
+ exports.StickyProvider = StickyProvider;
809
+ exports.scrollEvent = scrollEvent;
810
+ exports.useAddObservableChild = useAddObservableChild;
811
+ exports.useAnimatedScroll = useAnimatedScroll;
812
+ exports.useAnimatedScrollValue = useAnimatedScrollValue;
813
+ exports.useGetRefMaps = useGetRefMaps;
814
+ exports.useGetScrollToLast = useGetScrollToLast;
815
+ exports.useGetScrolling = useGetScrolling;
816
+ exports.useHandleScroll = useHandleScroll;
817
+ exports.useObserver = useObserver;
818
+ exports.useParentStickyContext = useParentStickyContext;
819
+ exports.useScrollById = useScrollById;
820
+ exports.useStickyContext = useStickyContext;
513
821
 
514
822
  }));
515
823
  //# sourceMappingURL=ct-design-scroll-container.umd.js.map