You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This used to be just a [ponyfill](https://ponyfill.com) for
10
-
`Element.scrollIntoViewIfNeeded` but is currently being rewritten to cover
11
-
`Element.scrollIntoView(ScrollIntoViewOptions)` including the new `scrollMode: "if-needed"` option. This readme will be updated when it's ready for stable
12
-
release.
10
+
This used to be a [ponyfill](https://ponyfill.com) for
11
+
`Element.scrollIntoViewIfNeeded`. Since then the CSS working group have decided to implement its features in `Element.scrollIntoView` as the option `scrollMode: "if-needed"`. Thus this library got rewritten to implement that spec instead of the soon to be deprecated one.
If you're ok with a larger bundlesize and want the smooth scrolling behavior to be ponyfilled you can use the [`smooth-scroll-into-view-if-needed`](https://github.com/stipsan/smooth-scroll-into-view-if-needed) addon.
53
+
What does ponyfilling smooth scrolling mean, and why is it implemented in [`smooth-scroll-into-view-if-needed`](https://github.com/stipsan/smooth-scroll-into-view-if-needed) instead?
54
+
The answer is bundlesize. If this package adds smooth scrolling to browsers that's missing it then the overall bundlesize increases regardless of wether you use this feature or not.
51
55
52
-
### Custom scrolling transition
56
+
Put it this way:
53
57
54
-
If the default smooth scrolling ponyfill isn't the duration or easing you want,
55
-
you can provide your own scrolling logic by giving `behavior` a callback.
Because of this you need to choose a strategy that matches your priorities: load time, consistency or quality.
68
+
69
+
##### Load time
70
+
71
+
In many scenarios smooth scrolling can be used as a progressive enhancement. If the user is on a browser that don't implement smooth scrolling it'll simply scroll instantly and your bundlesize is only as large as it has to be.
If a consistent smooth scrolling experience is a priority and you really don't want any surprises between different browsers and enviroments. In other words don't want to be affected by how a vendor might implement native smooth scrolling, then [`smooth-scroll-into-view-if-needed`](https://github.com/stipsan/smooth-scroll-into-view-if-needed) is your best option. It ensures the same smooth scrolling experience for every browser.
The auto option unlocks a few interesting opportunities.
124
+
The browser will decide based on user preferences wether it should smooth scroll or not.
125
+
On top of that you can control/override scrolling behavior through the [`scroll-behavior`](https://developer.mozilla.org/en-US/docs/Web/CSS/scroll-behavior) CSS property.
126
+
127
+
Some people get [motion sick from animations](https://css-tricks.com/smooth-scrolling-accessibility/#article-header-id-5). You can use CSS to turn off smooth scrolling in those cases to avoid making them dizzy:
128
+
129
+
```css
130
+
html,
131
+
.scroll-container {
132
+
overflow: scroll;
133
+
}
134
+
135
+
html,
136
+
.scroll-container {
137
+
scroll-behavior: smooth;
138
+
}
139
+
@media (prefers-reduced-motion) {
140
+
html,
141
+
.scroll-container {
142
+
scroll-behavior: auto;
143
+
}
144
+
}
145
+
```
146
+
147
+
Quick note, in the CSS property the `auto` keyword equals `behavior: 'instant'`, not `behavior: 'auto'` on `scrollIntoView`. **Yes**, this is confusing.
148
+
149
+
##### `'smooth'`
150
+
151
+
Using `behavior: 'smooth'` is the easiest way to smooth scroll an element as it does not require any CSS, just a browser that implements it. [More information.](#ponyfill-smooth-scrolling)
152
+
153
+
##### `'instant'`
154
+
155
+
This is useful for scenarios where it's certain that smooth scrolling would make an interaction feel sluggish. Like keyboard navigation and other user experiences where the end user expect things to move _instantly_.
156
+
157
+
##### `Function`
158
+
159
+
When given a function then this library will only calculate what should be scrolled and leave it up to you to perform the actual scrolling.
160
+
161
+
The callback is given an array over actions. Each action contain a reference to an element that should be scrolled, with its top and left scrolling coordinates.
162
+
What you return is passed through, allowing you to implement a Promise interface if you want to (check [`smooth-scroll-into-view-if-needed`](https://github.com/stipsan/smooth-scroll-into-view-if-needed) to see an example of that).
Control the logical scroll position on the y-axis. The spec states that the `block` direction is related to the [writing-mode](https://developer.mozilla.org/en-US/docs/Web/CSS/writing-mode), but this is not implemented yet in this library.
194
+
This means that `block: 'start'` aligns to the top edge and `block: 'end'` to the bottom.
Like `block` this is affected by the [writing-mode](https://developer.mozilla.org/en-US/docs/Web/CSS/writing-mode). In left-to-right pages `inline: 'start'` will align to the left edge. In right-to-left it should be flipped. This will be supported in a future release.
This is a proposed addition to the spec that you can track here: https://github.com/w3c/csswg-drafts/pull/1805
211
+
212
+
This library will be updated to reflect any changes to the spec and will provide a migration path.
213
+
To be backwards compatible with `Element.scrollIntoViewIfNeeded` if something is not 100% visible it will count as "needs scrolling". If you need a different visibility ratio your best option would be to implement an [Intersection Observer](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API).
214
+
215
+
#### boundary
216
+
217
+
Type: `Element | Function`
218
+
219
+
> `Function` introduced in `v2.1.0`, `Element` introduced in `v1.1.0`
220
+
221
+
By default there is no boundary. All the parent elements of your target is checked until it reaches the viewport (`document.documentElement`) when calculating layout and what to scroll.
222
+
You can use this option to do things like:
223
+
224
+
* Prevent the browser window from scrolling.
225
+
* Scroll things into view below the fold without scrolling to it.
226
+
* Scroll elements into view in a list, without scrolling container elements.
227
+
* Prematurely optimizing performance instead of code-splitting your app.
228
+
229
+
You can also pass a function to do more dynamic checks to override the scroll scoping:
230
+
231
+
```js
232
+
scrollIntoView(target, {
233
+
boundary:parent=> {
234
+
// By default `overflow: hidden` elements are allowed, only `overflow: visible | clip` is skipped as
235
+
// this is required by the CSSOM spec
236
+
if (getComputedStyle(parent)['overflow'] ==='hidden') {
237
+
returnfalse
238
+
}
239
+
240
+
returntrue
241
+
},
242
+
})
243
+
```
244
+
245
+
# Breaking API changes from v1
246
+
247
+
Since v1 ponyfilled Element.scrollIntoViewIfNeeded, while v2 ponyfills Element.scrollIntoView, there are breaking changes from the differences in their APIs.
248
+
249
+
The biggest difference is that the new behavior follows the spec, so the "if-needed" behavior is **not enabled by default:**
The old `Element.scrollIntoView` api only had two settings, align to top or bottom. [`Element.scrollIntoViewIfNeeded`](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoViewIfNeeded) had two more, align to the center or nearest edge.
272
+
The `Element.scrollIntoView` spec now supports these two modes as `block: 'center'` and `block: 'nearest'`.
273
+
Breaking changes sucks, but on the plus side your code is now more portable and will make this library easier to delete from your codebase on the glorious day browser support is good enough.
This feature is removed, but you can achieve the same thing by implementing [`behavior: Function`](#function).
322
+
323
+
#### handleScroll
324
+
325
+
This is replaced with [`behavior: Function`](#function) with one key difference. Instead of firing once per element that should be scrolled, the new API only fire once and instead give you an array so you can much easier batch and scroll multiple elements at the same time. Or sync scrolling with another element if that's the kind of stuff you're into, I don't judge.
326
+
327
+
```diff
328
+
-import scrollIntoViewIfNeeded from 'scroll-into-view-if-needed'
329
+
+import scrollIntoView from 'scroll-into-view-if-needed'
*[react-scroll-into-view-if-needed](https://www.npmjs.com/package/react-scroll-into-view-if-needed) – A thin wrapper to scroll your component into view.
354
+
*[Don't be shy, add yours!](https://github.com/stipsan/scroll-into-view-if-needed/edit/master/README.md)
355
+
356
+
# Who's using this
357
+
358
+
*[zeit.co/docs](https://github.com/zeit/docs) – Documentation of ZEIT Now and other services.
359
+
*[Selenium IDE](https://github.com/SeleniumHQ/selenium-ide) – An integrated development environment for Selenium scripts.
360
+
*[Box UI Elements](https://github.com/box/box-ui-elements) – Box UI Elements are pre-built UI components that allow developers to add elements of the main Box web application into their own applications.
0 commit comments