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