You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

readme.md 4.0 KiB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # Normalize Scroll Left for Right-to-Left
  2. This library normalizes the `Element.scrollLeft` property when direction is `rtl`.
  3. All the hardwork are based on [this juqery plugin](https://github.com/othree/jquery.rtl-scroll-type)
  4. and [this stackoverflow answer](https://stackoverflow.com/a/24394376).
  5. Since `Element.scrollLeft`'s behavior with `dir="rtl"` is not defined in any spec we use
  6. a feature detection logic to determine the behavior of the current browser.
  7. Types of `scrollLeft` (`scrollWidth` = 100) (Copied from
  8. [here](https://github.com/othree/jquery.rtl-scroll-type#3-types-of-scrollleft-scrollwidth--100))
  9. Browser | Type | Most Left | Most Right | Initial
  10. -------------- | ------------- | --------- | ---------- | -------
  11. WebKit | default | 0 | 100 | 100
  12. Firefox/Opera | negative | -100 | 0 | 0
  13. IE/Edge | reverse | 100 | 0 | 0
  14. ## Installation
  15. You can install this package with the following command:
  16. ```sh
  17. npm install normalize-scroll-left
  18. ```
  19. ## API
  20. This library exposes these methods:
  21. ### `detectScrollType`
  22. ```ts
  23. type ScrollType = 'indeterminate' | 'default' | 'negative' | 'reverse';
  24. function detectScrollType(): ScrollType;
  25. ```
  26. This function returns the scroll type detected, Keep in mind, this function
  27. caches the result as it should render a dummy on the DOM (which is expensive).
  28. Make sure the first invocation of this function happens **after** the body is loaded.
  29. **note**: To support server-side-rendering, it will output `indeterminate` if
  30. it detects a non-browser environment.
  31. ```javascript
  32. import { detectScrollType } from 'normalize-scroll-left';
  33. const type = detectScrollType();
  34. ```
  35. The output is not based on the browser, but feature detection:
  36. Browser | Type
  37. -------------- | -------------
  38. WebKit | `default`
  39. Firefox/Opera | `negative`
  40. IE/Edge | `reverse`
  41. Other/Server | `indeterminate`
  42. ### `getNormalizedScrollLeft`
  43. ```ts
  44. function getNormalizedScrollLeft(element: HTMLElement, direction: 'rtl' | 'ltr'): number;
  45. ```
  46. You can use this method to get the normalized `scrollLeft` property of an element.
  47. You should explicitly pass the direction for the following reasons:
  48. 1. Querying the `getComputedStyle` is expensive and might cause a reflow.
  49. 2. The behavior shouldn't be changed when direction is `ltr`.
  50. The output is `NaN` on the server. Otherwise, it will mimic the behavior of
  51. `WebKit` as it's the esiest to work with.
  52. ```ts
  53. import { getNormalizedScrollLeft } from 'normalize-scroll-left';
  54. const element = document.getElementById('my-scrollable-container');
  55. // element.scrollWidth = 100;
  56. const scrollLeft = getNormalizedScrollLeft(element, 'rtl');
  57. // scrollLeft will always be from 0 (Most Left) to 100 (Most Right).
  58. // It will initially be 100, That means the most right.
  59. ```
  60. ### `setNormalizedScrollLeft`
  61. ```ts
  62. function setNormalizedScrollLeft(
  63. element: HTMLElement,
  64. scrollLeft: number,
  65. direction: 'rtl' | 'ltr',
  66. ): void;
  67. ```
  68. You can use this method to set the `scrollLeft` property of an element as normalized.
  69. You should explicitly pass the direction for the same reasons as `getNormalizedScrollLeft`:
  70. For `scrollWidth = 100` the argument `scrollLeft` must be between `0` and `100`. This
  71. function will automatically convert it into something the current browser understands.
  72. ```ts
  73. import { setNormalizedScrollLeft } from 'normalize-scroll-left';
  74. const element = document.getElementById('my-scrollable-container');
  75. // element.scrollWidth = 100, element.clientWidth = 20;
  76. setNormalizedScrollLeft(element, 20, 'rtl');
  77. // Will set element.scrollLeft to ...
  78. // 20 in WebKit (chrome)
  79. // -60 in Firefox/Opera
  80. // 60 in IE/Edge
  81. // Does nothing on the server
  82. ```
  83. ## Typings
  84. The typescript type definitions are also available and are installed via npm.
  85. ## License
  86. This project is licensed under the
  87. [MIT license](https://github.com/alitaheri/normalize-scroll-left/blob/master/LICENSE).