Skip to content

feat: add RTL (right-to-left) support#109

Merged
nicped merged 2 commits into
mainfrom
feat/rtl-support
Mar 22, 2026
Merged

feat: add RTL (right-to-left) support#109
nicped merged 2 commits into
mainfrom
feat/rtl-support

Conversation

@nicped
Copy link
Copy Markdown
Member

@nicped nicped commented Mar 22, 2026

Summary

  • Fixes Button next and previous not working in RTL #86 — sliders inside dir="rtl" pages were unresponsive because browsers report negative scrollLeft in RTL scroll containers
  • All scroll math now applies a ±1 direction factor and uses Math.abs() where needed
  • initSlider stamps dir="rtl" on the wrapper element so CSS and JS share the same source of truth via getComputedStyle

Changes

  • src/swiffy-slider.js — new _rtl() helper; RTL-aware slide(), slideTo(), handleIndicators()
  • src/swiffy-slider-extensions.js — RTL-aware mouse drag direction and boundary checks
  • docs/examples/index.html — RTL carousel example; window.swiffyslider bridge for inline scripts
  • docs/configuration/index.html — RTL direction toggle; init() scoped to sliderPreview
  • package.json / Changelog.md — version bumped to 2.1.0

Test plan

  • Add dir="rtl" to a slider wrapper and confirm prev/next navigate correctly
  • Confirm loop works in both directions in RTL (last→first, first→last)
  • Confirm indicator dots update as slides scroll in RTL
  • Confirm mouse drag scrolls in the correct direction in RTL
  • Navigate to /examples/ — no swiffyslider is not defined console errors
  • Use RTL toggle on /configuration/ page — slider reinitializes correctly

🤖 Generated with Claude Code

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>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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() uses Math.abs(container.scrollLeft) to compute progress. This produces incorrect percentages in browsers where RTL scrollLeft is non-negative (e.g., starts at scrollableWidth and decreases), causing the active indicator to be wrong. It should use the same normalized RTL scroll position as navigation (logical 0..scrollableWidth) rather than Math.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.

Comment thread src/swiffy-slider.js
Comment thread src/swiffy-slider-extensions.js
Comment thread docs/examples/index.html Outdated
Comment thread docs/examples/index.html Outdated
@nicped nicped merged commit 8bee363 into main Mar 22, 2026
2 checks passed
@heyyo-droid
Copy link
Copy Markdown

thanks for including this fix.
I just checked the online configurator.
Direction doesn't work as expected.
When clicking on left arrow, the slider should go to the next slide. now it goes to the last slide.
When clicking on the right arrow, it goes to the next slide, but it should go the last slide.
Also indicators follows this wrong behavior.

@heyyo-droid
Copy link
Copy Markdown

Screencast.From.2026-03-22.10-53-03.mp4

@nicped
Copy link
Copy Markdown
Member Author

nicped commented Mar 22, 2026

What is next in rtl? If I click left arrow when on slide 2, do I go to slide 1 or 3

@heyyoyo
Copy link
Copy Markdown

heyyoyo commented Mar 22, 2026

you should see slide 3, if you click left arrow when on slide 2

@heyyoyo
Copy link
Copy Markdown

heyyoyo commented Mar 22, 2026

@heyyo-droid
Copy link
Copy Markdown

@nicped Does it make more sense ?
In RTL everything is reversed compared to LTR.

LeftToRight
< PREVIOUS 1,2,3,4,5 NEXT>
ooooo

RightToLeft
<NEXT 5,4,3,2,1 PREVIOUS>
ooooo

Great resource on RTL styling
https://rtlstyling.com/posts/rtl-styling/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Button next and previous not working in RTL

4 participants