feat: add RTL (right-to-left) support#109
Conversation
Fixes #86. Browsers report negative scrollLeft in RTL scroll containers, so all scroll math now uses a direction factor and Math.abs() where needed. - Add `_rtl(el)` helper using getComputedStyle as source of truth - `initSlider` stamps `dir="rtl"` on the element so CSS and JS share the same source of truth - `slide()` applies direction factor and flips loop-boundary checks for RTL - `slideTo()` multiplies slide index by direction factor - `handleIndicators()` uses Math.abs(scrollLeft) for RTL-safe percentage - Extensions: `handleMouseDrag()` flips drag direction and boundaries for RTL - Docs: RTL example added to examples page; direction toggle added to configuration page - Docs: fixed `init()` calls scoped to `sliderPreview`; added `window.swiffyslider` bridge for inline scripts in examples page - Version bumped to 2.1.0 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Adds right-to-left (RTL) support across Swiffy Slider’s core navigation, indicators, and mouse-drag extension to address unresponsive controls inside dir="rtl" layouts (Fixes #86).
Changes:
- Add RTL direction detection and apply direction-aware scroll math in core slider navigation/indicator logic.
- Update mouse-drag extension to account for RTL direction.
- Update docs/configuration/examples and bump version to 2.1.0 (including rebuilt dist/docs assets and changelog entry).
Reviewed changes
Copilot reviewed 8 out of 14 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| src/swiffy-slider.js | Adds _rtl() helper and adjusts slide(), slideTo(), and indicator math for RTL. |
| src/swiffy-slider-extensions.js | Updates mouse-drag calculations/bounds for RTL direction. |
| package.json | Bumps package version to 2.1.0. |
| docs/examples/index.html | Adds global window.swiffyslider bridge and a new RTL example section. |
| docs/configuration/index.html | Adds RTL toggle and scopes re-init to the preview container. |
| docs/assets/js/swiffy-slider.js | Rebuilt docs bundle reflecting RTL changes. |
| docs/assets/js/swiffy-slider.js.map | Rebuilt source map for docs bundle. |
| docs/assets/js/swiffy-slider-extensions.js | Rebuilt docs bundle reflecting RTL drag changes. |
| docs/assets/js/swiffy-slider-extensions.js.map | Rebuilt source map for docs extensions bundle. |
| dist/js/swiffy-slider.js | Rebuilt distribution bundle reflecting RTL changes. |
| dist/js/swiffy-slider.js.map | Rebuilt source map for dist bundle. |
| dist/js/swiffy-slider-extensions.js | Rebuilt distribution bundle reflecting RTL drag changes. |
| dist/js/swiffy-slider-extensions.js.map | Rebuilt source map for dist extensions bundle. |
| Changelog.md | Adds v2.1.0 entry documenting RTL support. |
Comments suppressed due to low confidence (1)
src/swiffy-slider.js:182
handleIndicators()usesMath.abs(container.scrollLeft)to compute progress. This produces incorrect percentages in browsers where RTLscrollLeftis non-negative (e.g., starts atscrollableWidthand decreases), causing the active indicator to be wrong. It should use the same normalized RTL scroll position as navigation (logical 0..scrollableWidth) rather thanMath.abs().
const container = this._c(sliderElement);
const scrollableWidth = container.scrollWidth - container.offsetWidth;
if (scrollableWidth === 0) return;
const percentSlide = Math.abs(container.scrollLeft) / scrollableWidth;
for (const indicatorContainer of sliderElement.querySelectorAll(".slider-indicators")) {
const indicators = indicatorContainer.children;
const activeIndex = Math.abs(Math.round((indicators.length - 1) * percentSlide));
for (const el of indicators) el.classList.remove("active");
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
thanks for including this fix. |
Screencast.From.2026-03-22.10-53-03.mp4 |
|
What is next in rtl? If I click left arrow when on slide 2, do I go to slide 1 or 3 |
|
you should see slide 3, if you click left arrow when on slide 2 |
|
Look the examples in previous RTL issue (second sliders in both pages) |
|
@nicped Does it make more sense ? LeftToRight RightToLeft Great resource on RTL styling |
Summary
dir="rtl"pages were unresponsive because browsers report negativescrollLeftin RTL scroll containers±1direction factor and usesMath.abs()where neededinitSliderstampsdir="rtl"on the wrapper element so CSS and JS share the same source of truth viagetComputedStyleChanges
src/swiffy-slider.js— new_rtl()helper; RTL-awareslide(),slideTo(),handleIndicators()src/swiffy-slider-extensions.js— RTL-aware mouse drag direction and boundary checksdocs/examples/index.html— RTL carousel example;window.swiffysliderbridge for inline scriptsdocs/configuration/index.html— RTL direction toggle;init()scoped tosliderPreviewpackage.json/Changelog.md— version bumped to 2.1.0Test plan
dir="rtl"to a slider wrapper and confirm prev/next navigate correctly/examples/— noswiffyslider is not definedconsole errors/configuration/page — slider reinitializes correctly🤖 Generated with Claude Code