Skip to content

Commit 0634533

Browse files
feat: add dismiss button to announcement banner (#2287)
Addresses #1384 Using essentially the same approach as was used for the version warning banner in #1763 Some questions for the announcement banner compared to the versions: - Do we also want to only remember it for 14 days, or just forever for a certain announcement? - Ideally the stored dismiss state takes into account the content of the announcement, so that when you add a new announcement, all users see it? - I am assuming that for a general announcement banner, we do _not_ want this to be version specific (i.e. remember it separately per version) Note: I only tested it with a remote announcement that gets injected, not yet with a local snippet.
1 parent 3a35f55 commit 0634533

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,28 @@ function debounce(callback, wait) {
922922
*/
923923
async function setupAnnouncementBanner() {
924924
const banner = document.querySelector(".bd-header-announcement");
925+
926+
// check if banner has been dismissed recently
927+
const dismiss_date_str = JSON.parse(
928+
localStorage.getItem("pst_announcement_banner_pref") || "{}",
929+
)["closed"];
930+
if (dismiss_date_str != null) {
931+
const dismiss_date = new Date(dismiss_date_str);
932+
const now = new Date();
933+
const milliseconds_in_a_day = 24 * 60 * 60 * 1000;
934+
const days_passed = (now - dismiss_date) / milliseconds_in_a_day;
935+
const timeout_in_days = 14;
936+
if (days_passed < timeout_in_days) {
937+
console.info(
938+
`[PST] Suppressing announcement banner; was dismissed ${Math.floor(
939+
days_passed,
940+
)} day(s) ago`,
941+
);
942+
banner.remove();
943+
return;
944+
}
945+
}
946+
925947
const { pstAnnouncementUrl } = banner ? banner.dataset : null;
926948

927949
if (!pstAnnouncementUrl) {
@@ -947,6 +969,31 @@ async function setupAnnouncementBanner() {
947969
console.log(`[PST]: Failed to load announcement at: ${pstAnnouncementUrl}`);
948970
console.error(_error);
949971
}
972+
973+
// Add close button
974+
console.debug("[PST] Adding close button to announcement banner");
975+
const close_btn = document.createElement("a");
976+
close_btn.classList = "ms-3 my-1 align-baseline";
977+
const close_x = document.createElement("i");
978+
close_btn.append(close_x);
979+
close_x.classList = "fa-solid fa-xmark";
980+
close_btn.onclick = DismissAnnouncementBannerAndStorePref;
981+
banner.append(close_btn);
982+
}
983+
984+
async function DismissAnnouncementBannerAndStorePref(event) {
985+
const banner = document.querySelector(".bd-header-announcement");
986+
banner.remove();
987+
const now = new Date();
988+
const banner_pref = JSON.parse(
989+
localStorage.getItem("pst_announcement_banner_pref") || "{}",
990+
);
991+
console.debug(`[PST] Dismissing the announcement banner starting ${now}.`);
992+
banner_pref["closed"] = now.toISOString();
993+
localStorage.setItem(
994+
"pst_announcement_banner_pref",
995+
JSON.stringify(banner_pref),
996+
);
950997
}
951998

952999
/*******************************************************************************

src/pydata_sphinx_theme/assets/styles/sections/_announcement.scss

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@
3737
// Ensure there is enough contrast against the background
3838
a {
3939
color: var(--pst-color-link-higher-contrast);
40+
41+
&:hover {
42+
// applying the same style as the SD "buttons"
43+
@include link-style-hover;
44+
45+
cursor: pointer;
46+
}
4047
}
4148

4249
// The "Switch to stable version" link (styled like a button)

0 commit comments

Comments
 (0)