tangled
alpha
login
or
join now
leaflet.pub
/
leaflet
289
fork
atom
a tool for shared writing and social publishing
289
fork
atom
overview
issues
28
pulls
pipelines
fixed bottom scroll margin issues on doc
cozylittle.house
3 months ago
06f33489
cb5781ee
+65
-15
2 changed files
expand all
collapse all
unified
split
src
utils
focusBlock.ts
scrollIntoViewIfNeeded.ts
+2
src/utils/focusBlock.ts
···
24
24
scrollIntoViewIfNeeded(
25
25
document.getElementById(elementId.block(block.value).container),
26
26
false,
27
27
+
undefined,
28
28
+
-80,
27
29
);
28
30
if (block.type === "math" || block.type === "code") {
29
31
let el = document.getElementById(
+63
-15
src/utils/scrollIntoViewIfNeeded.ts
···
2
2
el: Element | null,
3
3
centerIfNeeded: boolean = true,
4
4
behavior?: ScrollBehavior,
5
5
+
rootBottomMargin?: number,
5
6
) {
7
7
+
console.log("this should scroll up now!");
8
8
+
6
9
if (!el) {
7
10
return;
8
11
}
9
9
-
let observer = new IntersectionObserver(function ([entry]) {
10
10
-
const ratio = entry.intersectionRatio;
11
12
12
12
-
if (ratio < 1) {
13
13
-
let place =
14
14
-
ratio <= 0 && centerIfNeeded
15
15
-
? ("center" as const)
16
16
-
: ("nearest" as const);
17
17
-
el.scrollIntoView({
18
18
-
block: place,
19
19
-
inline: place,
20
20
-
behavior: behavior ? behavior : "auto",
21
21
-
});
22
22
-
}
23
23
-
observer.disconnect();
24
24
-
});
13
13
+
let observer = new IntersectionObserver(
14
14
+
function ([entry]) {
15
15
+
const ratio = entry.intersectionRatio;
16
16
+
if (ratio < 1) {
17
17
+
let place =
18
18
+
ratio <= 0 && centerIfNeeded
19
19
+
? ("center" as const)
20
20
+
: ("nearest" as const);
21
21
+
el.scrollIntoView({
22
22
+
block: place,
23
23
+
inline: place,
24
24
+
behavior: behavior ? behavior : "auto",
25
25
+
});
26
26
+
27
27
+
// If rootBottomMargin is defined, adjust scroll position to keep element away from bottom
28
28
+
if (rootBottomMargin) {
29
29
+
requestAnimationFrame(() => {
30
30
+
let rect = el.getBoundingClientRect();
31
31
+
let scrollContainer = getScrollParent(el);
32
32
+
let scrollContainerHeight = scrollContainer.clientHeight;
33
33
+
let distanceFromBottom = scrollContainerHeight - rect.bottom;
34
34
+
35
35
+
console.log("containerHeight: " + scrollContainerHeight);
36
36
+
console.log("el bottom: " + rect.bottom);
37
37
+
38
38
+
scrollContainer.scrollBy({
39
39
+
top: Math.abs(rootBottomMargin + distanceFromBottom),
40
40
+
behavior: behavior ? behavior : "auto",
41
41
+
});
42
42
+
});
43
43
+
}
44
44
+
}
45
45
+
observer.disconnect();
46
46
+
},
47
47
+
{
48
48
+
rootMargin: rootBottomMargin
49
49
+
? `0px 0px ${rootBottomMargin}px 0px`
50
50
+
: "0px",
51
51
+
},
52
52
+
);
25
53
observer.observe(el);
26
54
}
55
55
+
56
56
+
function getScrollParent(el: Element) {
57
57
+
let parent = el.parentElement;
58
58
+
59
59
+
while (parent) {
60
60
+
const { overflow, overflowY } = window.getComputedStyle(parent);
61
61
+
62
62
+
if (
63
63
+
overflow === "auto" ||
64
64
+
overflow === "scroll" ||
65
65
+
overflowY === "auto" ||
66
66
+
overflowY === "scroll"
67
67
+
) {
68
68
+
return parent;
69
69
+
}
70
70
+
71
71
+
parent = parent.parentElement;
72
72
+
}
73
73
+
return document.documentElement;
74
74
+
}