-
-
Notifications
You must be signed in to change notification settings - Fork 78.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix carousel sliding direction in RTL pages #33076
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
392aed7
fixed movement direction to reflect off of the ltr version.
aqeelat 1afd445
1- Added unit tests
aqeelat aa54a72
Merge branch 'main' of https://github.com/twbs/bootstrap into rtl-car…
aqeelat 48980ec
Merge branch 'main' into rtl-carousel
aqeelat 098708b
deleted commented code and fixed typo
aqeelat 4c6cb1d
Merge remote-tracking branch 'origin/rtl-carousel' into rtl-carousel
aqeelat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -120,6 +120,7 @@ class Carousel extends BaseComponent { | |
this._touchSupported = 'ontouchstart' in document.documentElement || navigator.maxTouchPoints > 0 | ||
this._pointerEvent = Boolean(window.PointerEvent) | ||
|
||
this.isRTL = this._element.dir === 'rtl' || isRTL | ||
this._addEventListeners() | ||
} | ||
|
||
|
@@ -241,19 +242,19 @@ class Carousel extends BaseComponent { | |
} | ||
|
||
_handleSwipe() { | ||
const absDeltax = Math.abs(this.touchDeltaX) | ||
const absDeltaX = Math.abs(this.touchDeltaX) | ||
|
||
if (absDeltax <= SWIPE_THRESHOLD) { | ||
if (absDeltaX <= SWIPE_THRESHOLD) { | ||
return | ||
} | ||
|
||
const direction = absDeltax / this.touchDeltaX | ||
const direction = absDeltaX / this.touchDeltaX | ||
|
||
this.touchDeltaX = 0 | ||
|
||
// swipe left | ||
if (direction > 0) { | ||
if (isRTL) { | ||
if (this.isRTL) { | ||
this.next() | ||
} else { | ||
this.prev() | ||
|
@@ -262,7 +263,7 @@ class Carousel extends BaseComponent { | |
|
||
// swipe right | ||
if (direction < 0) { | ||
if (isRTL) { | ||
if (this.isRTL) { | ||
this.prev() | ||
} else { | ||
this.next() | ||
|
@@ -350,14 +351,14 @@ class Carousel extends BaseComponent { | |
|
||
if (event.key === ARROW_LEFT_KEY) { | ||
event.preventDefault() | ||
if (isRTL) { | ||
if (this.isRTL) { | ||
this.next() | ||
} else { | ||
this.prev() | ||
} | ||
} else if (event.key === ARROW_RIGHT_KEY) { | ||
event.preventDefault() | ||
if (isRTL) { | ||
if (this.isRTL) { | ||
this.prev() | ||
} else { | ||
this.next() | ||
|
@@ -449,9 +450,20 @@ class Carousel extends BaseComponent { | |
const nextElementIndex = this._getItemIndex(nextElement) | ||
const isCycling = Boolean(this._interval) | ||
|
||
const directionalClassName = direction === DIRECTION_NEXT ? CLASS_NAME_START : CLASS_NAME_END | ||
const orderClassName = direction === DIRECTION_NEXT ? CLASS_NAME_NEXT : CLASS_NAME_PREV | ||
const eventDirectionName = direction === DIRECTION_NEXT ? DIRECTION_LEFT : DIRECTION_RIGHT | ||
// the logic behind the next three constants is a shorthand for: | ||
// if (this.isRTL) return direction === DIRECTION_NEXT ? option 2 : option 1 | ||
// else return direction === DIRECTION_NEXT ? option 1 : option 2 | ||
// +-------+------------------------------+----------+ | ||
// | isRTL | direction === DIRECTION_NEXT | | | ||
// +-------+------------------------------+----------+ | ||
// | T | T | Option 2 | | ||
// | T | F | Option 1 | | ||
// | F | T | Option 1 | | ||
// | F | F | Option 2 | | ||
// +-------+------------------------------+----------+ | ||
const directionalClassName = this.isRTL === (direction === DIRECTION_NEXT) ? CLASS_NAME_END : CLASS_NAME_START | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure I like these, I feel like it's less readable... |
||
const orderClassName = this.isRTL === (direction === DIRECTION_NEXT) ? CLASS_NAME_PREV : CLASS_NAME_NEXT | ||
const eventDirectionName = this.isRTL === (direction === DIRECTION_NEXT) ? DIRECTION_RIGHT : DIRECTION_LEFT | ||
|
||
if (nextElement && nextElement.classList.contains(CLASS_NAME_ACTIVE)) { | ||
this._isSliding = false | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the new check needed? Or why doesn't the previous one suffice?
BTW, I have a draft PR around to switch
isRTL
to a function so that it's tree-shaken, but I doubt it's related.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to add it so that I can produce the unit tests I was asked for. Then I realized it might be helpful in some cases.
isRTL only checks if the tag has dir=rtl attribute. This means that you can't have an RTL carousel otherwise. One use case: in the documentation for bootstrap itself on how to make a page rtl. The page itself could be in English, but the carousel could be in Arabic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we support such a case in v5 @ffoodd ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that case is not supported yet.And this check is then redundant 🙂There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aqeelat: I merged #32446. Your use case makes sense, but I thought it wasn't supported.
That being said, it seems it's sort of supported https://getbootstrap.com/docs/5.0/getting-started/rtl/#ltr-and-rtl-at-the-same-time, so after #32913 or in #32913, we could refactor the isRTL function to check for an element's direction and or
document.documentElement.dir
.We just need to make sure we have JS tests to cover these changes