-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
feat(cdk-scrollable): add methods to normalize scrolling in RTL #12607
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
/** Cached result of whether the user's browser supports passive event listeners. */ | ||
let supportsPassiveEvents: boolean; | ||
|
||
/** | ||
* Checks whether the user's browser supports passive event listeners. | ||
* See: https://github.com/WICG/EventListenerOptions/blob/gh-pages/explainer.md | ||
*/ | ||
export function supportsPassiveEventListeners(): boolean { | ||
if (supportsPassiveEvents == null && typeof window !== 'undefined') { | ||
try { | ||
window.addEventListener('test', null!, Object.defineProperty({}, 'passive', { | ||
get: () => supportsPassiveEvents = true | ||
})); | ||
} finally { | ||
supportsPassiveEvents = supportsPassiveEvents || false; | ||
} | ||
} | ||
|
||
return supportsPassiveEvents; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
/** The possible ways the browser may handle the horizontal scroll axis in RTL languages. */ | ||
export enum RtlScrollAxisType { | ||
/** | ||
* scrollLeft is 0 when scrolled all the way left and (scrollWidth - clientWidth) when scrolled | ||
* all the way right. | ||
*/ | ||
NORMAL, | ||
/** | ||
* scrollLeft is -(scrollWidth - clientWidth) when scrolled all the way left and 0 when scrolled | ||
* all the way right. | ||
*/ | ||
NEGATED, | ||
/** | ||
* scrollLeft is (scrollWidth - clientWidth) when scrolled all the way left and 0 when scrolled | ||
* all the way right. | ||
*/ | ||
INVERTED | ||
} | ||
|
||
/** Cached result of the way the browser handles the horizontal scroll axis in RTL mode. */ | ||
let rtlScrollAxisType: RtlScrollAxisType; | ||
|
||
/** Check whether the browser supports scroll behaviors. */ | ||
export function supportsScrollBehavior(): boolean { | ||
return !!(typeof document == 'object' && 'scrollBehavior' in document.documentElement.style); | ||
} | ||
|
||
/** | ||
* Checks the type of RTL scroll axis used by this browser. As of time of writing, Chrome is NORMAL, | ||
* Firefox & Safari are NEGATED, and IE & Edge are INVERTED. | ||
*/ | ||
export function getRtlScrollAxisType(): RtlScrollAxisType { | ||
// We can't check unless we're on the browser. Just assume 'normal' if we're not. | ||
if (typeof document !== 'object' || !document) { | ||
return RtlScrollAxisType.NORMAL; | ||
} | ||
|
||
if (!rtlScrollAxisType) { | ||
// Create a 1px wide scrolling container and a 2px wide content element. | ||
const scrollContainer = document.createElement('div'); | ||
const containerStyle = scrollContainer.style; | ||
scrollContainer.dir = 'rtl'; | ||
containerStyle.height = '1px'; | ||
containerStyle.width = '1px'; | ||
containerStyle.overflow = 'auto'; | ||
containerStyle.visibility = 'hidden'; | ||
containerStyle.pointerEvents = 'none'; | ||
containerStyle.position = 'absolute'; | ||
|
||
const content = document.createElement('div'); | ||
const contentStyle = content.style; | ||
contentStyle.width = '2px'; | ||
contentStyle.height = '1px'; | ||
|
||
scrollContainer.appendChild(content); | ||
document.body.appendChild(scrollContainer); | ||
|
||
rtlScrollAxisType = RtlScrollAxisType.NORMAL; | ||
|
||
// The viewport starts scrolled all the way to the right in RTL mode. If we are in a NORMAL | ||
// browser this would mean that the scrollLeft should be 1. If it's zero instead we know we're | ||
// dealing with one of the other two types of browsers. | ||
if (scrollContainer.scrollLeft === 0) { | ||
// In a NEGATED browser the scrollLeft is always somewhere in [-maxScrollAmount, 0]. For an | ||
// INVERTED browser it is always somewhere in [0, maxScrollAmount]. We can determine which by | ||
// setting to the scrollLeft to 1. This is past the max for a NEGATED browser, so it will | ||
// return 0 when we read it again. | ||
scrollContainer.scrollLeft = 1; | ||
rtlScrollAxisType = | ||
scrollContainer.scrollLeft === 0 ? RtlScrollAxisType.NEGATED : RtlScrollAxisType.INVERTED; | ||
} | ||
|
||
scrollContainer.parentNode!.removeChild(scrollContainer); | ||
} | ||
return rtlScrollAxisType; | ||
} |
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
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
Oops, something went wrong.
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.
Didn't we have something about not using enums @jelbourn?
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.
@jelbourn actually requested this, since its not a value that a user would supply through a template
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 have no memory about not wanting to use enums (for things that aren't used via templates). It is very possible I forgot something, though.
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 have some memory about it messing some internal tooling (Closure maybe?), but I could be mixing things up.