Skip to content

Commit 93b6fac

Browse files
authored
nimdoc: Initialize theme switch and pragma dots on DOMContentLoaded (#16247)
The default HTML template for nimdoc currently initializes the dark mode switch and pragma dots when the onload event is fired. But since the onload event does not fire until all external resources (images, stylesheets, fonts, etc.) have been loaded, the light theme is shown for a brief moment before the document is fully loaded, and it switches to the dark theme. This is quite jarring, especially on slower internet connections. So let's instead initialize these things on the DOMContentLoaded event, which is fired right after the document has been parsed and the initial DOM structure is ready. This means that we now display the dark mode immediately, without having to wait for external resources to load first. For reference, see: - https://developer.mozilla.org/en-US/docs/Web/API/Window/load_event - https://developer.mozilla.org/en-US/docs/Web/API/Window/DOMContentLoaded_event I have updated the snapshot tests in `nimdoc/`, and done some manual testing of both the theme switch and the pragma dots, to confirm that this does not break anything. Please let me know if I've missed anything.
1 parent 1e320bc commit 93b6fac

File tree

7 files changed

+21
-7
lines changed

7 files changed

+21
-7
lines changed

config/nimdoc.cfg

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,12 @@ function main() {
268268
}
269269
}
270270
}
271+
272+
window.addEventListener('DOMContentLoaded', main);
271273
</script>
272274

273275
</head>
274-
<body onload="main()">
276+
<body>
275277
<div class="document" id="documentId">
276278
<div class="container">
277279
<h1 class="title">$title</h1>

nimdoc/rst2html/expected/rst_examples.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@
5656
}
5757
}
5858
}
59+
60+
window.addEventListener('DOMContentLoaded', main);
5961
</script>
6062

6163
</head>
62-
<body onload="main()">
64+
<body>
6365
<div class="document" id="documentId">
6466
<div class="container">
6567
<h1 class="title">Not a Nim Manual</h1>

nimdoc/test_out_index_dot_html/expected/index.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@
5656
}
5757
}
5858
}
59+
60+
window.addEventListener('DOMContentLoaded', main);
5961
</script>
6062

6163
</head>
62-
<body onload="main()">
64+
<body>
6365
<div class="document" id="documentId">
6466
<div class="container">
6567
<h1 class="title">foo</h1>

nimdoc/test_out_index_dot_html/expected/theindex.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@
5656
}
5757
}
5858
}
59+
60+
window.addEventListener('DOMContentLoaded', main);
5961
</script>
6062

6163
</head>
62-
<body onload="main()">
64+
<body>
6365
<div class="document" id="documentId">
6466
<div class="container">
6567
<h1 class="title">Index</h1>

nimdoc/testproject/expected/subdir/subdir_b/utils.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@
5656
}
5757
}
5858
}
59+
60+
window.addEventListener('DOMContentLoaded', main);
5961
</script>
6062

6163
</head>
62-
<body onload="main()">
64+
<body>
6365
<div class="document" id="documentId">
6466
<div class="container">
6567
<h1 class="title">subdir/subdir_b/utils</h1>

nimdoc/testproject/expected/testproject.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@
5656
}
5757
}
5858
}
59+
60+
window.addEventListener('DOMContentLoaded', main);
5961
</script>
6062

6163
</head>
62-
<body onload="main()">
64+
<body>
6365
<div class="document" id="documentId">
6466
<div class="container">
6567
<h1 class="title">testproject</h1>

nimdoc/testproject/expected/theindex.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@
5656
}
5757
}
5858
}
59+
60+
window.addEventListener('DOMContentLoaded', main);
5961
</script>
6062

6163
</head>
62-
<body onload="main()">
64+
<body>
6365
<div class="document" id="documentId">
6466
<div class="container">
6567
<h1 class="title">Index</h1>

0 commit comments

Comments
 (0)