forked from mui/material-ui
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core] Host normalize-scroll-left (mui#19638)
- Loading branch information
1 parent
43c8f1a
commit 53558f1
Showing
4 changed files
with
67 additions
and
7 deletions.
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
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,66 @@ | ||
// Source from https://github.com/alitaheri/normalize-scroll-left | ||
let cachedType; | ||
|
||
/** | ||
* Based on the jquery plugin https://github.com/othree/jquery.rtl-scroll-type | ||
* | ||
* Types of scrollLeft, assiming scrollWidth=100 and direction is rtl. | ||
* | ||
* Browser | Type | <- Most Left | Most Right -> | Initial | ||
* -------------- | ------------- | ------------ | ------------- | ------- | ||
* WebKit | default | 0 | 100 | 100 | ||
* Firefox/Opera | negative | -100 | 0 | 0 | ||
* IE/Edge | reverse | 100 | 0 | 0 | ||
*/ | ||
export function detectScrollType() { | ||
if (cachedType) { | ||
return cachedType; | ||
} | ||
|
||
const dummy = document.createElement('div'); | ||
dummy.appendChild(document.createTextNode('ABCD')); | ||
dummy.dir = 'rtl'; | ||
dummy.style.fontSize = '14px'; | ||
dummy.style.width = '4px'; | ||
dummy.style.height = '1px'; | ||
dummy.style.position = 'absolute'; | ||
dummy.style.top = '-1000px'; | ||
dummy.style.overflow = 'scroll'; | ||
|
||
document.body.appendChild(dummy); | ||
|
||
cachedType = 'reverse'; | ||
|
||
if (dummy.scrollLeft > 0) { | ||
cachedType = 'default'; | ||
} else { | ||
dummy.scrollLeft = 1; | ||
if (dummy.scrollLeft === 0) { | ||
cachedType = 'negative'; | ||
} | ||
} | ||
|
||
document.body.removeChild(dummy); | ||
return cachedType; | ||
} | ||
|
||
// Based on https://stackoverflow.com/a/24394376 | ||
export function getNormalizedScrollLeft(element, direction) { | ||
const scrollLeft = element.scrollLeft; | ||
|
||
// Perform the calculations only when direction is rtl to avoid messing up the ltr bahavior | ||
if (direction !== 'rtl') { | ||
return scrollLeft; | ||
} | ||
|
||
const type = detectScrollType(); | ||
|
||
switch (type) { | ||
case 'negative': | ||
return element.scrollWidth - element.clientWidth + scrollLeft; | ||
case 'reverse': | ||
return element.scrollWidth - element.clientWidth - scrollLeft; | ||
default: | ||
return scrollLeft; | ||
} | ||
} |
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