UNPKG

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