[TASK] apply sticky to first two summary headlines
All checks were successful
CI/CD Pipeline / deploy-main (push) Successful in 1m2s

This commit is contained in:
Marie Birner 2024-02-19 21:33:45 +01:00
parent 5cdf0927d5
commit 133b83d7da
3 changed files with 66 additions and 24 deletions

View File

@ -57,7 +57,7 @@ fn print_header(header: Heading, level: usize) -> String {
ret.push_str(&format!( ret.push_str(&format!(
"<details>\ "<details>\
<summary><h{0}>{1}</h{0}></summary>\n\ <summary class='sticky'><h{0}>{1}</h{0}></summary>\n\
<div>", <div>",
level + 2, level + 2,
header_title header_title

View File

@ -1,6 +1,7 @@
document.addEventListener('DOMContentLoaded', function() { document.addEventListener('DOMContentLoaded', function() {
changeTheme(); changeTheme();
initcolorTheme(); initcolorTheme();
apply_stickies()
}); });
function changeTheme() { function changeTheme() {
@ -69,3 +70,24 @@ function setTheme(theme, setLocalStorage = true) {
document.documentElement.dataset.theme = 'dark'; document.documentElement.dataset.theme = 'dark';
} }
} }
window.addEventListener('scroll', function() {
apply_stickies()
})
function apply_stickies() {
var _$stickies = [].slice.call(document.querySelectorAll('.sticky'))
_$stickies.forEach(function(_$sticky) {
if (CSS.supports && CSS.supports('position', 'sticky')) {
apply_sticky_class(_$sticky)
}
})
}
function apply_sticky_class(_$sticky) {
var currentOffset = _$sticky.getBoundingClientRect().top
var stickyOffset = parseInt(getComputedStyle(_$sticky).top.replace('px', ''))
var isStuck = currentOffset <= stickyOffset
_$sticky.classList.toggle('js-is-sticky', isStuck)
}

View File

@ -50,32 +50,56 @@ main {
details summary { details summary {
line-height: unset; line-height: unset;
display: flex;
width: 100%;
align-items: center;
}
details summary::after {
flex-shrink: 0;
} }
details > summary { details > summary {
position: sticky; position: sticky;
top: 0px;
z-index: 99;
background-color: var(--pico-background-color); background-color: var(--pico-background-color);
} top: 0;
details details > summary { background: yellow;
top: 1.3rem; /* Height of h2*/
z-index: 98;
}
details details details > summary {
top: calc(1.3rem + 1.2rem);
z-index: 97;
}
details details details details > summary {
top: calc(1.3rem + 1.2rem + 1.1rem);
z-index: 96;
}
details details details details details > summary {
/* make sure to properly style enough levels of sticky headers :^) */
height: 1000px;
background-color:red;
} }
details details > summary {
background: red;
}
details details details > summary {
position: unset !important;
background: blue;
}
details details details details > summary {
background: pink;
}
details details details details details > summary {
background-color:green;
}
summary h2,
summary h3,
summary h4,
summary h5 {
flex-grow: 1;
}
summary.js-is-sticky h2,
summary.js-is-sticky h3,
summary.js-is-sticky h4,
summary.js-is-sticky h5 {
max-width: 100%;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
margin-bottom: 0;
}
.par { .par {
margin-bottom: calc(var(--pico-spacing) * 2); margin-bottom: calc(var(--pico-spacing) * 2);
@ -118,10 +142,6 @@ h4 {
--pico-font-weight: 200; --pico-font-weight: 200;
} }
details summary {
line-height: unset;
}
.par > ul { .par > ul {
margin: var(--pico-spacing) 0; margin: var(--pico-spacing) 0;
padding: 0; padding: 0;