Skip to content

Commit 8a599b7

Browse files
authored
fix: Set font size from cookie before DOM is ready, to avoid flickering (ietf-tools#4882)
* fix: Set font size from cookie before DOM is ready, to avoid flickering * fix: Use localStorage instead of cookies where possible
1 parent 38748cb commit 8a599b7

1 file changed

Lines changed: 26 additions & 15 deletions

File tree

ietf/static/js/document_html.js

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,23 @@ import { populate_nav } from "./nav.js";
1111

1212
const cookies = Cookies.withAttributes({ sameSite: "strict" });
1313

14-
document.addEventListener("DOMContentLoaded", function (event) {
15-
// handle point size slider
16-
const cookie = "doc-ptsize-max";
14+
// set initial point size from cookie before DOM is ready, to avoid flickering
15+
const ptsize_var = "doc-ptsize-max";
1716

18-
function change_ptsize(ptsize) {
19-
document.documentElement.style.setProperty(`--${cookie}`,
20-
`${ptsize}pt`);
21-
cookies.set(cookie, ptsize);
22-
}
17+
function change_ptsize(ptsize) {
18+
document.documentElement.style.setProperty(`--${ptsize_var}`,
19+
`${ptsize}pt`);
20+
localStorage.setItem(ptsize_var, ptsize);
21+
}
2322

23+
const ptsize = localStorage.getItem(ptsize_var);
24+
change_ptsize(ptsize ? Math.min(Math.max(7, ptsize), 16) : 12);
25+
26+
document.addEventListener("DOMContentLoaded", function (event) {
27+
// handle point size slider
2428
document.getElementById("ptsize")
2529
.oninput = function () { change_ptsize(this.value) };
2630

27-
const ptsize = cookies.get(cookie);
28-
change_ptsize(ptsize ? Math.min(Math.max(7, ptsize), 16) : 12);
29-
3031
// Use the Bootstrap tooltip plugin for all elements with a title attribute
3132
const tt_triggers = document.querySelectorAll(
3233
"[title]:not([title=''])");
@@ -45,15 +46,25 @@ document.addEventListener("DOMContentLoaded", function (event) {
4546
#content .h1, #content .h2, #content .h3, #content .h4, #content .h5, #content .h6`,
4647
["py-0"]);
4748

48-
// activate pref buttons selected by pref cookies
49+
// activate pref buttons selected by pref cookies or localStorage
50+
const in_localStorage = ["deftab"];
4951
document.querySelectorAll(".btn-check")
5052
.forEach(btn => {
5153
const id = btn.id.replace("-radio", "");
52-
if (cookies.get(btn.name) == id) {
54+
55+
const val = in_localStorage.includes(btn.name) ?
56+
localStorage.getItem(btn.name) : cookies.get(btn.name);
57+
if (val == id) {
5358
btn.checked = true;
5459
}
60+
5561
btn.addEventListener("click", el => {
56-
cookies.set(btn.name, id);
62+
// only use cookies for things used in HTML templates
63+
if (in_localStorage.includes(btn.name)) {
64+
localStorage.setItem(btn.name, id)
65+
} else {
66+
cookies.set(btn.name, id);
67+
}
5768
window.location.reload();
5869
});
5970
});
@@ -62,7 +73,7 @@ document.addEventListener("DOMContentLoaded", function (event) {
6273
let defpane;
6374
try {
6475
defpane = Tab.getOrCreateInstance(
65-
`#${cookies.get("deftab")}-tab`);
76+
`#${localStorage.getItem("deftab")}-tab`);
6677
} catch (err) {
6778
defpane = Tab.getOrCreateInstance("#docinfo-tab");
6879
};

0 commit comments

Comments
 (0)