Skip to content

Commit fe9a051

Browse files
authored
fix(nearest): target that matches the scrolling frame height should scroll (#141)
Relates to scroll-into-view/scroll-into-view-if-needed#432
1 parent 9ec0842 commit fe9a051

File tree

5 files changed

+1160
-34
lines changed

5 files changed

+1160
-34
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`the "nearest" option works with target and frame having equal dimensions block: "nearest" 1`] = `
4+
Array [
5+
Object {
6+
"el": "div.container",
7+
"left": 0,
8+
"top": 100,
9+
},
10+
Object {
11+
"el": "html",
12+
"left": 0,
13+
"top": 0,
14+
},
15+
]
16+
`;

integration/target_same_height.html

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<!DOCTYPE html>
2+
<meta charset="utf-8">
3+
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
4+
<script src="../umd/compute-scroll-into-view.js"></script>
5+
<script src="./utils.js"></script>
6+
<style>
7+
html, body {
8+
display: flex;
9+
align-items: center;
10+
justify-content: center;
11+
margin: 0;
12+
padding: 0;
13+
height: 100vh;
14+
width: 100vw;
15+
}
16+
.container {
17+
background: whitesmoke;
18+
height: 100px;
19+
width: 100px;
20+
overflow: auto;
21+
}
22+
.pane {
23+
height: 100px;
24+
width: 100px;
25+
}
26+
.target {
27+
background: crimson;
28+
}
29+
</style>
30+
<div class="container">
31+
<div class="pane"></div>
32+
<div class="pane target"></div>
33+
<div class="pane"></div>
34+
</div>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
beforeAll(async () => {
2+
await page.goto('http://localhost:3000/integration/target_same_height')
3+
})
4+
5+
describe('the "nearest" option works with target and frame having equal dimensions', () => {
6+
test('block: "nearest"', async () => {
7+
expect.assertions(3)
8+
const actual = await page.evaluate(() => {
9+
return window
10+
.computeScrollIntoView(document.querySelector('.target'), {
11+
block: 'nearest',
12+
})
13+
.map(window.mapActions)
14+
})
15+
expect(actual).toHaveLength(2)
16+
expect(actual[0]).toMatchObject({ el: 'div.container', left: 0, top: 100 })
17+
expect(actual).toMatchSnapshot()
18+
})
19+
})

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ function alignNearest(
161161
* ┗ ━ ━ ━ ━ ┛ ┗ ━ ━ ━ ━ ┛
162162
*/
163163
if (
164-
(elementEdgeStart < scrollingEdgeStart && elementSize < scrollingSize) ||
165-
(elementEdgeEnd > scrollingEdgeEnd && elementSize > scrollingSize)
164+
(elementEdgeStart <= scrollingEdgeStart && elementSize <= scrollingSize) ||
165+
(elementEdgeEnd >= scrollingEdgeEnd && elementSize >= scrollingSize)
166166
) {
167167
return elementEdgeStart - scrollingEdgeStart - scrollingBorderStart
168168
}

0 commit comments

Comments
 (0)