@rethink-js/rt-slider 1.1.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -14,7 +14,7 @@
14
14
  - Support for **multiple instances**
15
15
  - A clean global API under `window.rtSlider`
16
16
  - Defensive fallbacks to native scrolling on mobile
17
- - Clear console logs for debugging and verification
17
+ - Built-in **slide state tracking**, **scroll progress tracking**, DOM attributes, and custom events for advanced UI sync
18
18
 
19
19
  **Primary dependency (GitHub):** <https://github.com/darkroomengineering/lenis>
20
20
 
@@ -63,6 +63,8 @@ Add the script to your page. To create a slider, add the `data-rt-slider` attrib
63
63
  - Auto-initialize on DOM ready
64
64
  - Load Lenis dynamically for desktop inertia
65
65
  - Apply native touch scrolling styles for mobile
66
+ - Compute active slide state and overall scroll progress automatically
67
+ - Expose slider state through attributes, events, and the global API
66
68
 
67
69
  Example:
68
70
 
@@ -84,10 +86,11 @@ Example:
84
86
 
85
87
  ## 3. Activation Rules
86
88
 
87
- The library activates when **any** of the following are true:
89
+ The library activates when **all** of the following are true:
88
90
 
89
91
  - An element with the attribute `data-rt-slider` is found in the DOM.
90
- - The required `data-rt-list` and `data-rt-item` selectors resolve to valid elements within that container.
92
+ - The required `data-rt-list` and `data-rt-item` selectors are present on that container.
93
+ - The `data-rt-list` selector resolves to a valid element within that container.
91
94
 
92
95
  If these conditions are met, the library initializes the instance automatically.
93
96
 
@@ -119,17 +122,20 @@ If these conditions are met, the library initializes the instance automatically.
119
122
 
120
123
  ### Core Attributes (Selectors)
121
124
 
122
- | Attribute | Description | Required |
123
- | ---------------------- | ----------------------------------------------- | -------- |
124
- | `data-rt-slider` | Activates the slider instance | **Yes** |
125
- | `data-rt-slider-id` | Optional identifier (auto-generated if missing) | No |
126
- | `data-rt-list` | Selector for the scrollable wrapper | **Yes** |
127
- | `data-rt-item` | Selector for individual slides | **Yes** |
128
- | `data-rt-spacer` | Selector/Class for edge spacers (padding) | No |
129
- | `data-rt-btn-prev` | Selector for "Previous" button | No |
130
- | `data-rt-btn-next` | Selector for "Next" button | No |
131
- | `data-rt-scroll-track` | Selector for custom scrollbar track | No |
132
- | `data-rt-scroll-bar` | Selector for custom scrollbar thumb | No |
125
+ | Attribute | Description | Required |
126
+ | ----------------------- | ---------------------------------------------------------- | -------- |
127
+ | `data-rt-slider` | Activates the slider instance | **Yes** |
128
+ | `data-rt-slider-id` | Optional identifier (auto-generated if missing) | No |
129
+ | `data-rt-list` | Selector for the scrollable wrapper | **Yes** |
130
+ | `data-rt-item` | Selector for individual slides | **Yes** |
131
+ | `data-rt-spacer` | Selector/Class for edge spacers (padding) | No |
132
+ | `data-rt-btn-prev` | Selector for "Previous" button | No |
133
+ | `data-rt-btn-next` | Selector for "Next" button | No |
134
+ | `data-rt-scroll-track` | Selector for custom scrollbar track | No |
135
+ | `data-rt-scroll-bar` | Selector for custom scrollbar thumb | No |
136
+ | `data-rt-margin-ref` | Selector used to align slide anchors to a layout reference | No |
137
+ | `data-rt-overlay-start` | Selector for the leading edge overlay | No |
138
+ | `data-rt-overlay-end` | Selector for the trailing edge overlay | No |
133
139
 
134
140
  ---
135
141
 
@@ -145,15 +151,18 @@ These attributes control the Lenis instance used on desktop.
145
151
  ></div>
146
152
  ```
147
153
 
148
- | Attribute | Description | Default |
149
- | ----------------------------- | -------------------------------------- | ------------- |
150
- | `data-rt-slider-lerp` | Inertia interpolation (lower = slower) | `0.1` |
151
- | `data-rt-slider-duration` | Scroll duration (alt to lerp) | `1.2` |
152
- | `data-rt-slider-easing` | Easing function (e.g., `easeOutExpo`) | `easeOutQuad` |
153
- | `data-rt-slider-orientation` | Scroll orientation | `horizontal` |
154
- | `data-rt-slider-smooth-wheel` | Enable mouse wheel smoothing | `true` |
155
- | `data-rt-slider-infinite` | Infinite scrolling | `false` |
156
- | `data-rt-slider-auto-resize` | Recalculate on window resize | `true` |
154
+ | Attribute | Description | Default |
155
+ | ------------------------------------ | -------------------------------------- | ------------- |
156
+ | `data-rt-slider-lerp` | Inertia interpolation (lower = slower) | Lenis default |
157
+ | `data-rt-slider-duration` | Scroll duration (alt to lerp) | Lenis default |
158
+ | `data-rt-slider-easing` | Easing function (e.g., `easeOutExpo`) | Lenis default |
159
+ | `data-rt-slider-orientation` | Scroll orientation | `horizontal` |
160
+ | `data-rt-slider-gesture-orientation` | Gesture orientation | `horizontal` |
161
+ | `data-rt-slider-smooth-wheel` | Enable mouse wheel smoothing | `true` |
162
+ | `data-rt-slider-wheel-multiplier` | Multiplier for wheel-based scrolling | Lenis default |
163
+ | `data-rt-slider-touch-multiplier` | Multiplier for touch-based scrolling | Lenis default |
164
+ | `data-rt-slider-infinite` | Infinite scrolling | `false` |
165
+ | `data-rt-slider-auto-resize` | Recalculate on window resize | `true` |
157
166
 
158
167
  ---
159
168
 
@@ -170,6 +179,81 @@ Used to pass complex configuration objects directly to the underlying Lenis inst
170
179
 
171
180
  ---
172
181
 
182
+ ### Slide State Output
183
+
184
+ As the slider moves, `rt-slider` writes state back to the DOM automatically.
185
+
186
+ #### Root attributes
187
+
188
+ | Attribute | Description |
189
+ | ----------------------------------------- | ------------------------------------------ |
190
+ | `data-rt-slider-active-index` | Current nearest active slide index |
191
+ | `data-rt-slider-from-index` | Current segment start slide index |
192
+ | `data-rt-slider-to-index` | Current segment end slide index |
193
+ | `data-rt-slider-segment-progress` | Current segment progress from `0` to `1` |
194
+ | `data-rt-slider-segment-progress-percent` | Current segment progress from `0` to `100` |
195
+ | `data-rt-slider-scroll-current` | Current clamped horizontal scroll position |
196
+ | `data-rt-slider-scroll-max` | Maximum horizontal scroll position |
197
+ | `data-rt-slider-scroll-progress` | Overall slider progress from `0` to `1` |
198
+ | `data-rt-slider-scroll-progress-percent` | Overall slider progress from `0` to `100` |
199
+ | `data-rt-slider-scroll-direction` | `forward`, `backward`, or `none` |
200
+ | `data-rt-slider-scroll-at-start` | `true` when the slider is at the start |
201
+ | `data-rt-slider-scroll-at-end` | `true` when the slider is at the end |
202
+
203
+ #### Per-item attributes
204
+
205
+ | Attribute | Description |
206
+ | --------------------------------------------- | -------------------------------------------------------------------------- |
207
+ | `data-rt-slider-item-index` | Slide index |
208
+ | `data-rt-slider-item-active` | `true` when this item is the active slide |
209
+ | `data-rt-slider-item-from` | `true` when this item is the current segment start |
210
+ | `data-rt-slider-item-to` | `true` when this item is the current segment end |
211
+ | `data-rt-slider-item-previous` | `true` when this item is immediately before the active slide |
212
+ | `data-rt-slider-item-next` | `true` when this item is immediately after the active slide |
213
+ | `data-rt-slider-item-before-active` | `true` when this item is before the active slide |
214
+ | `data-rt-slider-item-after-active` | `true` when this item is after the active slide |
215
+ | `data-rt-slider-item-anchor-progress` | This slide's anchor position from `0` to `1` |
216
+ | `data-rt-slider-item-anchor-progress-percent` | This slide's anchor position from `0` to `100` |
217
+ | `data-rt-slider-item-distance` | Distance in pixels from the current scroll position to this slide's anchor |
218
+
219
+ These attributes are useful for CSS-driven animations, slide-aware UI, progress indicators, edge-aware controls, and syncing other interface elements to the slider.
220
+
221
+ ---
222
+
223
+ ### Custom Events
224
+
225
+ Each slider instance dispatches events from the root element.
226
+
227
+ | Event | Description |
228
+ | ------------------- | ------------------------------------------- |
229
+ | `rtSlider:progress` | Fires when computed scroll progress changes |
230
+ | `rtSlider:slide` | Fires when the computed slide state changes |
231
+ | `rtSlider:active` | Fires when the active slide index changes |
232
+
233
+ Example:
234
+
235
+ ```js
236
+ const slider = document.querySelector("[data-rt-slider]");
237
+
238
+ slider.addEventListener("rtSlider:progress", function (event) {
239
+ console.log("Progress:", event.detail.progress);
240
+ });
241
+
242
+ slider.addEventListener("rtSlider:slide", function (event) {
243
+ console.log(event.detail);
244
+ });
245
+
246
+ slider.addEventListener("rtSlider:active", function (event) {
247
+ console.log("Active slide:", event.detail.active.index);
248
+ });
249
+ ```
250
+
251
+ `rtSlider:progress` includes a cloned scroll-state object in `event.detail`.
252
+
253
+ `rtSlider:slide` and `rtSlider:active` include a full cloned slide-state object in `event.detail`.
254
+
255
+ ---
256
+
173
257
  ### Dependency Loader Overrides
174
258
 
175
259
  The library automatically loads Lenis from a CDN if not present. You can rely on the default or load your own version before `rt-slider`.
@@ -186,9 +270,11 @@ The library automatically loads Lenis from a CDN if not present. You can rely on
186
270
 
187
271
  Each instance:
188
272
 
189
- - Has its own independent scroll physics.
190
- - Calculates its own progress bars and button states.
191
- - Is registered internally with a unique ID.
273
+ - Has its own independent scroll physics
274
+ - Calculates its own progress bars and button states
275
+ - Tracks its own active slide, segment state, and overall scroll progress
276
+ - Dispatches its own custom events
277
+ - Is registered internally with a unique ID
192
278
 
193
279
  ---
194
280
 
@@ -202,12 +288,15 @@ window.rtSlider;
202
288
 
203
289
  ### Common Methods
204
290
 
205
- | Method | Description |
206
- | -------------- | ------------------------------------------------ |
207
- | `ids()` | Returns an array of active slider IDs |
208
- | `get(id)` | Returns the slider instance object |
209
- | `refresh()` | Forces a recalculation of layout (all instances) |
210
- | `destroy(id?)` | Destroys specific instance or all if no ID given |
291
+ | Method | Description |
292
+ | -------------------- | ---------------------------------------------------- |
293
+ | `ids()` | Returns an array of active slider IDs |
294
+ | `get(id)` | Returns the slider instance object |
295
+ | `getSlideState(id)` | Returns a cloned slide-state object for an instance |
296
+ | `getScrollState(id)` | Returns a cloned scroll-state object for an instance |
297
+ | `getProgress(id)` | Returns overall slider progress from `0` to `1` |
298
+ | `refresh()` | Forces a recalculation of layout (all instances) |
299
+ | `destroy(id?)` | Destroys specific instance or all if no ID given |
211
300
 
212
301
  Example usage:
213
302
 
@@ -215,19 +304,35 @@ Example usage:
215
304
  // Refresh layout after an AJAX load
216
305
  window.rtSlider.refresh();
217
306
 
307
+ // Get computed state
308
+ const ids = window.rtSlider.ids();
309
+ const firstSliderSlideState = window.rtSlider.getSlideState(ids[0]);
310
+ const firstSliderScrollState = window.rtSlider.getScrollState(ids[0]);
311
+ const firstSliderProgress = window.rtSlider.getProgress(ids[0]);
312
+
218
313
  // Destroy a specific slider
219
314
  window.rtSlider.destroy("my-slider-id");
220
315
  ```
221
316
 
317
+ ### Instance Helpers
318
+
319
+ When using `window.rtSlider.get(id)`, each instance also exposes helper methods:
320
+
321
+ | Method | Description |
322
+ | -------------------- | ------------------------------------------------------ |
323
+ | `getSlideState()` | Returns a cloned slide-state object for that instance |
324
+ | `getScrollState()` | Returns a cloned scroll-state object for that instance |
325
+ | `getProgress()` | Returns overall slider progress from `0` to `1` |
326
+ | `getActiveIndex()` | Returns the current active slide index |
327
+ | `getActiveElement()` | Returns the current active slide element |
328
+
222
329
  ---
223
330
 
224
331
  ## 7. Console Logging
225
332
 
226
- `rt-slider` operates silently by default but provides warnings if:
333
+ `rt-slider` operates silently by default.
227
334
 
228
- - Required selectors (`data-rt-list`, `data-rt-item`) are missing.
229
- - JSON configuration is invalid.
230
- - Dependency loading fails.
335
+ It does not rely on console output during normal use. If initialization fails, the most common causes are invalid selectors, missing required attributes, or invalid external configuration.
231
336
 
232
337
  ---
233
338
 
@@ -237,16 +342,34 @@ window.rtSlider.destroy("my-slider-id");
237
342
 
238
343
  - Ensure `data-rt-slider` is present on the parent.
239
344
  - **Crucial:** Ensure `data-rt-list` and `data-rt-item` attributes match valid elements inside the container.
345
+ - Ensure the `data-rt-list` selector resolves successfully inside the slider root.
240
346
 
241
347
  ### Buttons not working
242
348
 
243
- - Ensure the selectors in `data-rt-btn-prev` match your button elements.
349
+ - Ensure the selectors in `data-rt-btn-prev` and `data-rt-btn-next` match your button elements.
244
350
  - If buttons are outside the slider container, give them the attribute `data-rt-slider-for="slider-id"`.
245
351
 
246
352
  ### Scrollbar not moving
247
353
 
248
354
  - Ensure `data-rt-scroll-track` and `data-rt-scroll-bar` are set correctly.
249
- - The "bar" must be a child of the "track" in the DOM for standard styling, though the logic handles positioning.
355
+ - The "bar" should be inside the "track" for the cleanest result, though the logic handles positioning independently.
356
+
357
+ ### Slide state attributes not updating
358
+
359
+ - Ensure the slider is actually scrollable. If content does not overflow horizontally, active state will still resolve, but progress and movement-driven changes will be limited.
360
+ - Ensure `data-rt-item` matches the actual slide elements and not a wrapper around them.
361
+
362
+ ### Progress not reaching `1` at the end
363
+
364
+ - `rt-slider` uses edge-aware scroll detection and clamps progress to `1` when the visible viewport reaches the end of the slider.
365
+ - If you are building custom progress indicators, use `window.rtSlider.getProgress(id)`, `window.rtSlider.getScrollState(id)`, or the `rtSlider:progress` event instead of deriving progress manually from raw DOM measurements.
366
+
367
+ ### Custom events not firing as expected
368
+
369
+ - `rtSlider:progress` fires when computed scroll progress changes.
370
+ - `rtSlider:slide` fires when computed slide state changes.
371
+ - `rtSlider:active` fires when the active slide changes.
372
+ - All events are dispatched from the element with `data-rt-slider`, not from the list or item elements.
250
373
 
251
374
  ---
252
375
 
@@ -255,7 +378,7 @@ window.rtSlider.destroy("my-slider-id");
255
378
  MIT License
256
379
 
257
380
  Package: `@rethink-js/rt-slider` <br>
258
- GitHub: [https://github.com/Rethink-JS/rt-slider](https://github.com/Rethink-JS/rt-slider)
381
+ GitHub: [https://github.com/Rethink-JS/rt-slider](https://github.com/Rethink-JS)
259
382
 
260
383
  ---
261
384