Skip to content

Commit 1869807

Browse files
Charlie-XIAOdrammock
authored andcommitted
ENH animation for the top banner (pydata#1693)
* ENH animation for the top banner * unset forcefully set styles to let css take over; animation also added for version warning banner * make transition a bit longer * resolve conversations * retrigger CI * Update src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js --------- Co-authored-by: Daniel McCloy <[email protected]>
1 parent 6adda08 commit 1869807

File tree

3 files changed

+55
-19
lines changed

3 files changed

+55
-19
lines changed

src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js

+24-1
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,8 @@ function showVersionWarningBanner(data) {
484484
const bold = document.createElement("strong");
485485
const button = document.createElement("a");
486486
// these classes exist since pydata-sphinx-theme v0.10.0
487-
outer.classList = "bd-header-version-warning container-fluid";
487+
// the init class is used for animation
488+
outer.classList = "bd-header-version-warning container-fluid init";
488489
middle.classList = "bd-header-announcement__content";
489490
inner.classList = "sidebar-message";
490491
button.classList =
@@ -516,6 +517,28 @@ function showVersionWarningBanner(data) {
516517
inner.appendChild(button);
517518
const skipLink = document.getElementById("pst-skip-link");
518519
skipLink.after(outer);
520+
// At least 3rem height
521+
const autoHeight = Math.max(
522+
outer.offsetHeight,
523+
3 * parseFloat(getComputedStyle(document.documentElement).fontSize)
524+
);
525+
// Set height and vertical padding to 0 to prepare the height transition
526+
outer.style.setProperty("height", 0);
527+
outer.style.setProperty("padding-top", 0);
528+
outer.style.setProperty("padding-bottom", 0);
529+
outer.classList.remove("init");
530+
// Set height to the computed height with a small timeout to activate the transition
531+
setTimeout(() => {
532+
outer.style.setProperty("height", `${autoHeight}px`);
533+
// Wait for a bit more than 300ms (the transition duration) then remove the
534+
// forcefully set styles and let CSS take over
535+
setTimeout(() => {
536+
outer.style.removeProperty("padding-top");
537+
outer.style.removeProperty("padding-bottom");
538+
outer.style.removeProperty("height");
539+
outer.style.setProperty("min-height", "3rem");
540+
}, 320);
541+
}, 10);
519542
}
520543

521544
/*******************************************************************************
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,30 @@
11
.bd-header-version-warning,
22
.bd-header-announcement {
3-
min-height: 3rem;
43
width: 100%;
54
display: flex;
65
position: relative;
76
align-items: center;
87
justify-content: center;
98
text-align: center;
9+
transition: height 300ms ease-in-out;
10+
overflow-y: hidden;
1011
padding: 0.5rem 12.5%; // Horizontal padding so the width is 75%
1112
// One breakpoint less than $breakpoint-sidebar-primary. See variables/_layout.scss for more info.
1213
@include media-breakpoint-down(lg) {
1314
// Announcements can take a bit more width on mobile
1415
padding: 0.5rem 2%;
1516
}
1617

18+
&.init {
19+
position: fixed;
20+
visibility: hidden;
21+
}
22+
1723
p {
1824
font-weight: bold;
1925
margin: 0;
2026
}
2127

22-
&:after {
23-
position: absolute;
24-
width: 100%;
25-
height: 100%;
26-
left: 0;
27-
top: 0;
28-
content: "";
29-
z-index: -1; // So it doesn't hover over the content
30-
}
31-
3228
&:empty {
3329
display: none;
3430
}
@@ -41,13 +37,9 @@
4137

4238
// Bg color is now defined in the theme color palette - using our secondary color
4339
.bd-header-announcement {
44-
&:after {
45-
background-color: var(--pst-color-secondary-bg);
46-
}
40+
background-color: var(--pst-color-secondary-bg);
4741
}
4842

4943
.bd-header-version-warning {
50-
&:after {
51-
background-color: var(--pst-color-danger-bg);
52-
}
44+
background-color: var(--pst-color-danger-bg);
5345
}

src/pydata_sphinx_theme/theme/pydata_sphinx_theme/sections/announcement.html

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% set header_classes = ["bd-header-announcement", "container-fluid"] %}
1+
{% set header_classes = ["bd-header-announcement", "container-fluid", "init"] %}
22
{% set is_remote=theme_announcement.startswith("http") %}
33
{# If we are remote, add a script to make an HTTP request for the value on page load #}
44
{%- if is_remote %}
@@ -10,6 +10,27 @@
1010
div = document.querySelector(".bd-header-announcement");
1111
div.classList.add(...{{ header_classes | tojson }});
1212
div.innerHTML = `<div class="bd-header-announcement__content">${data}</div>`;
13+
// At least 3rem height
14+
const autoHeight = Math.min(
15+
div.offsetHeight,
16+
3 * parseFloat(getComputedStyle(document.documentElement).fontSize));
17+
// Set height and vertical padding to 0 to prepare the height transition
18+
div.style.setProperty("height", 0);
19+
div.style.setProperty("padding-top", 0);
20+
div.style.setProperty("padding-bottom", 0);
21+
div.classList.remove("init");
22+
// Set height to the computed height with a small timeout to activate the transition
23+
setTimeout(() => {
24+
div.style.setProperty("height", `${autoHeight}px`);
25+
// Wait for a bit more than 300ms (the transition duration) then remove the
26+
// forcefully set styles and let CSS take over
27+
setTimeout(() => {
28+
div.style.removeProperty("padding-top");
29+
div.style.removeProperty("padding-bottom");
30+
div.style.removeProperty("height");
31+
div.style.setProperty("min-height", "3rem");
32+
}, 320);
33+
}, 10);
1334
})
1435
.catch(error => {
1536
console.log("[PST]: Failed to load announcement at: {{ theme_announcement }}");

0 commit comments

Comments
 (0)