Skip to content

Commit 8f48307

Browse files
authored
fix: support node ESM (#1120)
BREAKING CHANGE: drops umd builds, and ships more modern syntax with ESM as baseline
1 parent 40ccca2 commit 8f48307

File tree

14 files changed

+1417
-3024
lines changed

14 files changed

+1417
-3024
lines changed

.babelrc.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

.gitignore

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,10 @@ node_modules
1010
selenium-debug.log
1111

1212
# Typescript stuff
13-
typings
1413
src/*.js
1514

1615
lerna-debug.log
1716
.next
18-
dist
19-
es
20-
umd
21-
/index.js
22-
/types.js
2317
tests/web-platform/local.log
18+
.vscode
19+
dist

.prettierignore

Lines changed: 0 additions & 3 deletions
This file was deleted.

.vscode/settings.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

README.md

Lines changed: 81 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
[![npm version](https://img.shields.io/npm/v/scroll-into-view-if-needed.svg?style=flat-square)](https://www.npmjs.com/package/scroll-into-view-if-needed)
33
[![gzip size][gzip-badge]][unpkg-dist]
44
[![size][size-badge]][unpkg-dist]
5-
[![module formats: umd, cjs, and es][module-formats-badge]][unpkg-dist]
5+
[![module formats: cjs, and es][module-formats-badge]][unpkg-dist]
66
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=flat-square)](https://github.com/semantic-release/semantic-release)
77
[![BrowserStack Status](https://www.browserstack.com/automate/badge.svg?style=flat-square&badge_key=ejZ6OUtTaS9rZFFOYzlkeHlwTzMwSWxpR0FzWFcwOW5TS3ROTmlSdXMrVT0tLVhrVk9La2lCb1o4Y05mcmNXVnAvdkE9PQ==--d17668b8aba5091e4ef3a58927b8209e50b0a788)](https://www.browserstack.com/automate/public-build/ejZ6OUtTaS9rZFFOYzlkeHlwTzMwSWxpR0FzWFcwOW5TS3ROTmlSdXMrVT0tLVhrVk9La2lCb1o4Y05mcmNXVnAvdkE9PQ==--d17668b8aba5091e4ef3a58927b8209e50b0a788)
88

@@ -11,29 +11,63 @@
1111
This used to be a [ponyfill](https://ponyfill.com) for
1212
`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.
1313

14-
## [Demo](https://scroll-into-view.dev)
15-
16-
## Install
14+
- [Demo](#demo)
15+
- [Install](#install)
16+
- [Usage](#usage)
17+
- [Ponyfill smooth scrolling](#ponyfill-smooth-scrolling)
18+
- [Load time](#load-time)
19+
- [Consistency](#consistency)
20+
- [Quality](#quality)
21+
- [API](#api)
22+
- [scrollIntoView(target, \[options\])](#scrollintoviewtarget-options)
23+
- [options](#options)
24+
- [behavior](#behavior)
25+
- [`'auto'`](#auto)
26+
- [`'smooth'`](#smooth)
27+
- [`Function`](#function)
28+
- [block](#block)
29+
- [inline](#inline)
30+
- [scrollMode](#scrollmode)
31+
- [boundary](#boundary)
32+
- [skipOverflowHiddenElements](#skipoverflowhiddenelements)
33+
- [TypeScript support](#typescript-support)
34+
- [Breaking API changes from v1](#breaking-api-changes-from-v1)
35+
- [v1](#v1)
36+
- [v2](#v2)
37+
- [centerIfNeeded](#centerifneeded)
38+
- [v1](#v1-1)
39+
- [v2](#v2-1)
40+
- [duration](#duration)
41+
- [v1](#v1-2)
42+
- [v2](#v2-2)
43+
- [easing](#easing)
44+
- [handleScroll](#handlescroll)
45+
- [offset](#offset)
46+
- [scrollIntoViewIfNeeded(target, \[centerIfNeeded\], \[animateOptions\], \[finalElement\], \[offsetOptions\])](#scrollintoviewifneededtarget-centerifneeded-animateoptions-finalelement-offsetoptions)
47+
- [Related packages](#related-packages)
48+
- [Who's using this](#whos-using-this)
49+
- [Sponsors](#sponsors)
50+
51+
# [Demo](https://scroll-into-view.dev)
52+
53+
# Install
1754

1855
```bash
1956
npm i scroll-into-view-if-needed
2057
```
2158

22-
The UMD build is also available on [unpkg](https://unpkg.com/scroll-into-view-if-needed/umd/):
59+
You can also use it from a CDN:
2360

24-
```html
25-
<script src="https://unpkg.com/scroll-into-view-if-needed/umd/scroll-into-view-if-needed.min.js"></script>
61+
```js
62+
const { default: scrollIntoView } = await import(
63+
'https://esm.sh/scroll-into-view-if-needed'
64+
)
2665
```
2766

28-
You can find the library on `window.scrollIntoView`.
29-
30-
## Usage
67+
# Usage
3168

3269
```js
33-
// es6 import
3470
import scrollIntoView from 'scroll-into-view-if-needed'
35-
// or es5
36-
const scrollIntoView = require('scroll-into-view-if-needed')
3771

3872
const node = document.getElementById('hero')
3973

@@ -57,7 +91,7 @@ scrollIntoView(node, { block: 'center', inline: 'center' })
5791
scrollIntoView(node, { behavior: 'smooth', scrollMode: 'if-needed' })
5892
```
5993

60-
### Ponyfill smooth scrolling
94+
## Ponyfill smooth scrolling
6195

6296
What does ponyfilling smooth scrolling mean, and why is it implemented in [`smooth-scroll-into-view-if-needed`](https://github.com/scroll-into-view/smooth-scroll-into-view-if-needed) instead?
6397
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.
@@ -75,7 +109,7 @@ scrollIntoView(node, { behavior: 'smooth', scrollMode: 'if-needed' })
75109

76110
That's why only native smooth scrolling is supported out of the box. There are two common ways you can smooth scroll browsers that don't support it natively. Below is all three, which one is best for you depends on what is the most important to your use case:: load time, consistency or quality.
77111

78-
##### Load time
112+
### Load time
79113

80114
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.
81115

@@ -85,7 +119,7 @@ import scrollIntoView from 'scroll-into-view-if-needed'
85119
scrollIntoView(node, { behavior: 'smooth' })
86120
```
87121

88-
##### Consistency
122+
### Consistency
89123

90124
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/scroll-into-view/smooth-scroll-into-view-if-needed) is your best option. It ensures the same smooth scrolling experience for every browser.
91125

@@ -95,7 +129,7 @@ import smoothScrollIntoView from 'smooth-scroll-into-view-if-needed'
95129
smoothScrollIntoView(node, { behavior: 'smooth' })
96130
```
97131

98-
##### Quality
132+
### Quality
99133

100134
If you want to use native smooth scrolling when it's available, and fallback to the smooth scrolling ponyfill:
101135

@@ -111,23 +145,23 @@ const scrollIntoViewSmoothly =
111145
scrollIntoViewSmoothly(node, { behavior: 'smooth' })
112146
```
113147

114-
## API
148+
# API
115149

116-
### scrollIntoView(target, [options])
150+
## scrollIntoView(target, [options])
117151

118152
> New API introduced in `v1.3.0`
119153
120-
### options
154+
## options
121155

122156
Type: `Object`
123157

124-
#### behavior
158+
### behavior
125159

126160
Type: `'auto' | 'smooth' | Function`<br> Default: `'auto'`
127161

128162
> Introduced in `v2.1.0`
129163
130-
##### `'auto'`
164+
#### `'auto'`
131165

132166
The auto option unlocks a few interesting opportunities.
133167
The browser will decide based on user preferences wether it should smooth scroll or not.
@@ -153,11 +187,11 @@ html,
153187
}
154188
```
155189

156-
##### `'smooth'`
190+
#### `'smooth'`
157191

158192
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)
159193

160-
##### `Function`
194+
#### `Function`
161195

162196
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.
163197

@@ -189,39 +223,39 @@ Check the demo to see an [example with popmotion and a spring transition](https:
189223

190224
> If you only need the custom behavior you might be better off by using the compute library directly: https://github.com/scroll-into-view/compute-scroll-into-view
191225
192-
#### [block](https://scroll-into-view.dev/#scroll-alignment)
226+
### [block](https://scroll-into-view.dev/#scroll-alignment)
193227

194228
Type: `'start' | 'center' | 'end' | 'nearest'`<br> Default: `'center'`
195229

196230
> Introduced in `v2.1.0`
197231
198232
[More info.](https://github.com/scroll-into-view/compute-scroll-into-view#block)
199233

200-
#### [inline](https://scroll-into-view.dev/#scroll-alignment)
234+
### [inline](https://scroll-into-view.dev/#scroll-alignment)
201235

202236
Type: `'start' | 'center' | 'end' | 'nearest'`<br> Default: `'nearest'`
203237

204238
> Introduced in `v2.1.0`
205239
206240
[More info.](https://github.com/scroll-into-view/compute-scroll-into-view#inline)
207241

208-
#### [scrollMode](https://scroll-into-view.dev/#scrolling-if-needed)
242+
### [scrollMode](https://scroll-into-view.dev/#scrolling-if-needed)
209243

210244
Type: `'always' | 'if-needed'`<br> Default: `'always'`
211245

212246
> Introduced in `v2.1.0`
213247
214248
[More info.](https://github.com/scroll-into-view/compute-scroll-into-view#scrollmode)
215249

216-
#### [boundary](https://scroll-into-view.dev/#limit-propagation)
250+
### [boundary](https://scroll-into-view.dev/#limit-propagation)
217251

218252
Type: `Element | Function`
219253

220254
> `Function` introduced in `v2.1.0`, `Element` introduced in `v1.1.0`
221255
222256
[More info.](https://github.com/scroll-into-view/compute-scroll-into-view#boundary)
223257

224-
#### skipOverflowHiddenElements
258+
### skipOverflowHiddenElements
225259

226260
Type: `Boolean`<br> Default: `false`
227261

@@ -266,7 +300,7 @@ const scrolling = scrollIntoView<Promise<any>>(document.body, {
266300
The options are available for you if you are wrapping this libary in another abstraction (like a React component):
267301

268302
```typescript
269-
import scrollIntoView, { Options } from 'scroll-into-view-if-needed'
303+
import scrollIntoView, { type Options } from 'scroll-into-view-if-needed'
270304

271305
interface CustomOptions extends Options {
272306
useBoundary?: boolean
@@ -284,7 +318,7 @@ Since v1 ponyfilled Element.scrollIntoViewIfNeeded, while v2 ponyfills Element.s
284318

285319
The biggest difference is that the new behavior follows the spec, so the "if-needed" behavior is **not enabled by default:**
286320

287-
##### v1
321+
### v1
288322

289323
```js
290324
import scrollIntoViewIfNeeded from 'scroll-into-view-if-needed'
@@ -293,7 +327,7 @@ import scrollIntoViewIfNeeded from 'scroll-into-view-if-needed'
293327
scrollIntoViewIfNeeded(target)
294328
```
295329

296-
##### v2
330+
### v2
297331

298332
```js
299333
import scrollIntoView from 'scroll-into-view-if-needed'
@@ -302,13 +336,13 @@ import scrollIntoView from 'scroll-into-view-if-needed'
302336
scrollIntoView(target, { block: 'nearest', scrollMode: 'if-needed' })
303337
```
304338

305-
#### centerIfNeeded
339+
## centerIfNeeded
306340

307341
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.
308342
The `Element.scrollIntoView` spec now supports these two modes as `block: 'center'` and `block: 'nearest'`.
309343
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.
310344

311-
##### v1
345+
### v1
312346

313347
```js
314348
import scrollIntoViewIfNeeded from 'scroll-into-view-if-needed'
@@ -321,7 +355,7 @@ scrollIntoViewIfNeeded(target, true)
321355
scrollIntoViewIfNeeded(target, false)
322356
```
323357

324-
##### v2
358+
### v2
325359

326360
```js
327361
import scrollIntoView from 'scroll-into-view-if-needed'
@@ -330,19 +364,19 @@ scrollIntoView(target, { block: 'center' })
330364
scrollIntoView(target, { block: 'nearest' })
331365
```
332366

333-
#### duration
367+
## duration
334368

335369
[More information.](#ponyfill-smooth-scrolling)
336370

337-
##### v1
371+
### v1
338372

339373
```js
340374
import scrollIntoViewIfNeeded from 'scroll-into-view-if-needed'
341375

342376
scrollIntoViewIfNeeded(target, { duration: 300 })
343377
```
344378

345-
##### v2
379+
### v2
346380

347381
```js
348382
import scrollIntoView from 'scroll-into-view-if-needed'
@@ -352,11 +386,11 @@ import scrollIntoView from 'smooth-scroll-into-view-if-needed'
352386
scrollIntoView(target, { behavior: 'smooth' })
353387
```
354388

355-
#### easing
389+
## easing
356390

357391
This feature is removed, but you can achieve the same thing by implementing [`behavior: Function`](#function).
358392

359-
#### handleScroll
393+
## handleScroll
360394

361395
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.
362396

@@ -374,12 +408,12 @@ This is replaced with [`behavior: Function`](#function) with one key difference.
374408
+})})
375409
```
376410

377-
#### offset
411+
## offset
378412

379413
This was always a buggy feature and warned against using in v1 as it might get dropped.
380414
It's much safer to use CSS wrapper elements for this kind of thing.
381415

382-
### scrollIntoViewIfNeeded(target, [centerIfNeeded], [animateOptions], [finalElement], [offsetOptions])
416+
## scrollIntoViewIfNeeded(target, [centerIfNeeded], [animateOptions], [finalElement], [offsetOptions])
383417

384418
This API signature were warned to be dropped in `v2.0.0`, and it was.
385419

@@ -403,12 +437,12 @@ This API signature were warned to be dropped in `v2.0.0`, and it was.
403437
- [docs.expo.io](https://github.com/expo/expo-docs) – Documentation for Expo, its SDK, client and services.
404438
- [Add yourself to the list 😉](https://github.com/scroll-into-view/scroll-into-view-if-needed/edit/main/README.md)
405439

406-
[gzip-badge]: http://img.badgesize.io/https://unpkg.com/scroll-into-view-if-needed/umd/scroll-into-view-if-needed.min.js?compression=gzip&label=gzip%20size&style=flat-square
407-
[size-badge]: http://img.badgesize.io/https://unpkg.com/scroll-into-view-if-needed/umd/scroll-into-view-if-needed.min.js?label=size&style=flat-square
408-
[unpkg-dist]: https://unpkg.com/scroll-into-view-if-needed/umd/
409-
[module-formats-badge]: https://img.shields.io/badge/module%20formats-umd%2C%20cjs%2C%20es-green.svg?style=flat-square
440+
[gzip-badge]: http://img.badgesize.io/https://unpkg.com/scroll-into-view-if-needed/dist/index.js?compression=gzip&label=gzip%20size&style=flat-square
441+
[size-badge]: http://img.badgesize.io/https://unpkg.com/scroll-into-view-if-needed/dist/index.js?label=size&style=flat-square
442+
[unpkg-dist]: https://unpkg.com/scroll-into-view-if-needed/dist/
443+
[module-formats-badge]: https://img.shields.io/badge/module%20formats-cjs%2C%20esm-green.svg?style=flat-square
410444

411-
## Sponsors
445+
# Sponsors
412446

413447
Thanks to [BrowserStack](https://www.browserstack.com) for sponsoring cross browser and device testing 😄
414448

0 commit comments

Comments
 (0)