Skip to content

Commit e92a6ab

Browse files
authored
A few quick fixes before repo split (#199)
* A few quick fixes before repo split * Sync with Prettier
1 parent 55e4e12 commit e92a6ab

File tree

5 files changed

+87
-37
lines changed

5 files changed

+87
-37
lines changed

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
# This is the work-in-progress, the current stable version is [here](https://github.com/stipsan/scroll-into-view-if-needed/tree/v1)
22

3-
[![CircleCI Status](https://img.shields.io/circleci/project/github/stipsan/scroll-into-view-if-needed.svg?style=flat-square)](https://circleci.com/gh/stipsan/scroll-into-view-if-needed) [![npm stat](https://img.shields.io/npm/dm/scroll-into-view-if-needed.svg?style=flat-square)](https://npm-stat.com/charts.html?package=scroll-into-view-if-needed) [![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) [![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)
4-
![scroll-into-view-if-needed](https://user-images.githubusercontent.com/81981/36357426-2f0f3152-14fe-11e8-892a-915d06488171.png)
3+
[![CircleCI Status](https://img.shields.io/circleci/project/github/stipsan/scroll-into-view-if-needed.svg?style=flat-square)](https://circleci.com/gh/stipsan/scroll-into-view-if-needed)
4+
[![npm stat](https://img.shields.io/npm/dm/scroll-into-view-if-needed.svg?style=flat-square)](https://npm-stat.com/charts.html?package=scroll-into-view-if-needed)
5+
[![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)
6+
[![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)
7+
![scroll-into-view-if-needed](https://user-images.githubusercontent.com/81981/39338772-34165fb2-49c5-11e8-9d29-38acb6842db8.png)
58

6-
This used to be just a [ponyfill](https://ponyfill.com) for `Element.scrollIntoViewIfNeeded` but is currently being rewritten to cover `Element.scrollIntoView(ScrollIntoViewOptions)` including the new `scrollMode: "if-needed"` option.
7-
This readme will be updated when it's ready for stable release.
9+
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.
813

914
## [Demo](https://scroll-into-view-if-needed.netlify.com)

packages/scroll-into-view-if-needed/README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
[![CircleCI Status](https://img.shields.io/circleci/project/github/stipsan/scroll-into-view-if-needed.svg?style=flat-square)](https://circleci.com/gh/stipsan/scroll-into-view-if-needed) [![npm stat](https://img.shields.io/npm/dm/scroll-into-view-if-needed.svg?style=flat-square)](https://npm-stat.com/charts.html?package=scroll-into-view-if-needed) [![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) [![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)
2-
![scroll-into-view-if-needed](https://user-images.githubusercontent.com/81981/36357426-2f0f3152-14fe-11e8-892a-915d06488171.png)
1+
[![CircleCI Status](https://img.shields.io/circleci/project/github/stipsan/scroll-into-view-if-needed.svg?style=flat-square)](https://circleci.com/gh/stipsan/scroll-into-view-if-needed)
2+
[![npm stat](https://img.shields.io/npm/dm/scroll-into-view-if-needed.svg?style=flat-square)](https://npm-stat.com/charts.html?package=scroll-into-view-if-needed)
3+
[![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)
4+
[![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)
5+
![scroll-into-view-if-needed](https://user-images.githubusercontent.com/81981/39338772-34165fb2-49c5-11e8-9d29-38acb6842db8.png)
36

47
This is a [ponyfill](https://ponyfill.com) with the added ability of animating
58
the scroll itself.
@@ -45,7 +48,8 @@ scrollIntoView(node, { behavior: 'smooth', scrollMode: 'if-needed' })
4548

4649
### Ponyfill smooth scrolling
4750

48-
If you're ok with a larger bundlesize, you can use the ponyfill version so all your users experience smooth scrolling.
51+
If you're ok with a larger bundlesize, you can use the ponyfill version so all
52+
your users experience smooth scrolling.
4953

5054
```js
5155
import scrollIntoView from 'scroll-into-view-if-needed/ponyfill'
@@ -74,7 +78,8 @@ const sequence = async () => {
7478

7579
### Custom scrolling transition
7680

77-
If the default smooth scrolling ponyfill isn't the duration or easing you want, you can provide your own scrolling logic by giving `behavior` a callback.
81+
If the default smooth scrolling ponyfill isn't the duration or easing you want,
82+
you can provide your own scrolling logic by giving `behavior` a callback.
7883

7984
```js
8085
import scrollIntoView from 'scroll-into-view-if-needed'

packages/scroll-into-view-if-needed/src/compute.ts

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,20 @@ function isScrollable(el) {
4545
const alignNearestBlock = (
4646
targetStart: number,
4747
targetSize: number,
48-
frame: Element
48+
frame: Element,
49+
frameRect: ClientRect | DOMRect
4950
) => {
5051
// targetSize is either targetRect.height or targetRect.width depending on if it's `block` or `inline`
5152
const targetEnd = targetStart + targetSize
5253

5354
const elementEdgeA = frame.scrollTop + targetStart
5455
const elementEdgeB = frame.scrollTop + targetEnd
5556
const scrollingEdgeA = frame.scrollTop
56-
const scrollingEdgeB = frame.scrollTop + frame.clientHeight
57+
const height =
58+
frame === document.documentElement ? frame.clientHeight : frameRect.bottom
59+
const scrollingEdgeB = document.documentElement
60+
? frame.scrollTop + height
61+
: frameRect.bottom
5762

5863
//console.log('new coordinates', {elementEdgeA, elementEdgeB, scrollingEdgeA, scrollingEdgeB, targetStart, targetSize, 'frame.scrollTop': frame.scrollTop, 'frame.clientHeight': frame.clientHeight, 'frameRect.top': frameRect.top})
5964

@@ -86,7 +91,7 @@ const alignNearestBlock = (
8691
* ┗━ ━━ ━┛ ┗━ ━━ ━┛
8792
*
8893
*/
89-
if (elementEdgeA < scrollingEdgeA && targetSize < frame.clientHeight) {
94+
if (elementEdgeA < scrollingEdgeA && targetSize < height) {
9095
return targetStart
9196
}
9297

@@ -102,7 +107,7 @@ const alignNearestBlock = (
102107
* │ │
103108
* └──┘
104109
*/
105-
if (elementEdgeB > scrollingEdgeB && targetSize > frame.clientHeight) {
110+
if (elementEdgeB > scrollingEdgeB && targetSize > height) {
106111
return targetStart
107112
}
108113

@@ -117,7 +122,7 @@ const alignNearestBlock = (
117122
* └──┘
118123
*/
119124
///*
120-
if (elementEdgeB > scrollingEdgeB && targetSize < frame.clientHeight) {
125+
if (elementEdgeB > scrollingEdgeB && targetSize < height) {
121126
return elementEdgeB - scrollingEdgeB
122127
}
123128
//*/
@@ -134,7 +139,7 @@ const alignNearestBlock = (
134139
* │ │
135140
* ┗━ ━━ ━┛ ┗━└━━┘━┛
136141
*/
137-
if (elementEdgeA < scrollingEdgeA && targetSize > frame.clientHeight) {
142+
if (elementEdgeA < scrollingEdgeA && targetSize > height) {
138143
return elementEdgeB - scrollingEdgeB
139144
}
140145

@@ -150,6 +155,12 @@ const alignNearestInline = (
150155
// targetSize is either targetRect.height or targetRect.width depending on if it's `block` or `inline`
151156
const targetEnd = targetStart + targetSize
152157

158+
const elementEdgeC = frame.scrollLeft + targetStart
159+
const elementEdgeD = frame.scrollLeft + targetEnd
160+
const scrollingEdgeC = frame.scrollLeft
161+
const scrollingEdgeD = frame.scrollLeft + frame.clientWidth
162+
console.log('test')
163+
153164
/**
154165
* If element edge C and element edge D are both outside scrolling box edge C and scrolling box edge D
155166
*
@@ -181,9 +192,10 @@ const alignNearestInline = (
181192
* ┗ ━ ━ ━ ━ ┛ ┗ ━ ━ ━ ━ ┛
182193
*/
183194
if (
184-
frameRect.left < 0 &&
185-
targetEnd > frameRect.left &&
186-
targetSize < frame.clientWidth
195+
(elementEdgeC < scrollingEdgeC && targetSize < frame.clientWidth) ||
196+
(frameRect.left < 0 &&
197+
targetEnd > frameRect.left &&
198+
targetSize < frame.clientWidth)
187199
) {
188200
return targetStart
189201
}
@@ -208,9 +220,10 @@ const alignNearestInline = (
208220
* ┗ ━ ━ ━ ━ ┛ ┗ ━ ━ ━ ━ ┛
209221
*/
210222
if (
211-
frameRect.right > 0 &&
212-
targetEnd > frameRect.right &&
213-
targetSize < frame.clientWidth
223+
(elementEdgeD < scrollingEdgeD && targetSize < frame.clientWidth) ||
224+
(frameRect.right > 0 &&
225+
targetEnd > frameRect.right &&
226+
targetSize < frame.clientWidth)
214227
) {
215228
return targetEnd - frameRect.left - frame.clientWidth
216229
}
@@ -235,15 +248,6 @@ export const compute = (
235248

236249
let targetRect = target.getBoundingClientRect()
237250

238-
// If the element is already visible we can end it here
239-
if (
240-
scrollMode === 'if-needed' &&
241-
targetRect.top >= 0 &&
242-
targetRect.bottom <= window.innerHeight
243-
) {
244-
return []
245-
}
246-
247251
// Collect all the scrolling boxes, as defined in the spec: https://drafts.csswg.org/cssom-view/#scrolling-box
248252
const frames: Element[] = []
249253
let parent
@@ -256,6 +260,32 @@ export const compute = (
256260
target = parent
257261
}
258262

263+
// If the element is already visible we can end it here
264+
if (scrollMode === 'if-needed') {
265+
// @TODO optimize, as getBoundingClientRect is also called from computations loop
266+
const isVisible = frames.every(frame => {
267+
const frameRect = frame.getBoundingClientRect()
268+
269+
if (targetRect.top < frameRect.top) {
270+
return false
271+
}
272+
if (targetRect.bottom > frameRect.bottom) {
273+
return false
274+
}
275+
276+
if (
277+
frame === document.documentElement &&
278+
targetRect.bottom > frame.clientHeight
279+
) {
280+
return false
281+
}
282+
return true
283+
})
284+
if (isVisible) {
285+
return []
286+
}
287+
}
288+
259289
// These values mutate as we loop through and generate scroll coordinates
260290
let targetBlock
261291
let targetInline
@@ -336,7 +366,12 @@ export const compute = (
336366
targetBlock = targetRect.top
337367
}
338368

339-
const offset = alignNearestBlock(targetBlock, targetRect.height, frame)
369+
const offset = alignNearestBlock(
370+
targetBlock,
371+
targetRect.height,
372+
frame,
373+
frameRect
374+
)
340375
blockScroll = frame.scrollTop + offset
341376

342377
if (document.documentElement !== frame) {

packages/smooth-scroll-into-view-if-needed/README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
[![CircleCI Status](https://img.shields.io/circleci/project/github/stipsan/scroll-into-view-if-needed.svg?style=flat-square)](https://circleci.com/gh/stipsan/scroll-into-view-if-needed) [![npm stat](https://img.shields.io/npm/dm/scroll-into-view-if-needed.svg?style=flat-square)](https://npm-stat.com/charts.html?package=scroll-into-view-if-needed) [![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) [![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)
2-
![scroll-into-view-if-needed](https://user-images.githubusercontent.com/81981/39098948-61a1e60a-4672-11e8-80a3-16f0e57e4b9d.png)
1+
[![CircleCI Status](https://img.shields.io/circleci/project/github/stipsan/scroll-into-view-if-needed.svg?style=flat-square)](https://circleci.com/gh/stipsan/scroll-into-view-if-needed)
2+
[![npm stat](https://img.shields.io/npm/dm/scroll-into-view-if-needed.svg?style=flat-square)](https://npm-stat.com/charts.html?package=scroll-into-view-if-needed)
3+
[![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)
4+
[![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)
5+
![scroll-into-view-if-needed](https://user-images.githubusercontent.com/81981/39338604-0bff23f2-49c4-11e8-9929-2f2b74a67b3c.png)
36

4-
This is a [ponyfill](https://ponyfill.com) for smooth scrolling `scroll-into-view-if-needed`.
7+
This is a [ponyfill](https://ponyfill.com) for smooth scrolling
8+
`scroll-into-view-if-needed`.
59

610
## [Demo](https://scroll-into-view-if-needed.netlify.com/)
711

@@ -40,7 +44,8 @@ const sequence = async () => {
4044

4145
### Custom scrolling transition
4246

43-
If the default smooth scrolling ponyfill isn't the duration or easing you want, you can provide your own scrolling logic by giving `behavior` a callback.
47+
If the default smooth scrolling ponyfill isn't the duration or easing you want,
48+
you can provide your own scrolling logic by giving `behavior` a callback.
4449

4550
```js
4651
import scrollIntoView from 'smooth-scroll-into-view-if-needed'

website/components/Examples/IfNeeded.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ class IfNeeded extends PureComponent {
8080
<div className="buttons is-centered">
8181
<span className="label">Scroll to:&nbsp;</span>
8282
<a
83-
key="B"
83+
key="C"
8484
className="button is-small"
85-
onClick={() => this.doScroll(this.items.B)}
85+
onClick={() => this.doScroll(this.items.C)}
8686
>
87-
B
87+
C
8888
</a>
8989
<a
9090
key="D"

0 commit comments

Comments
 (0)