1
0
mirror of https://github.com/cotes2020/jekyll-theme-chirpy.git synced 2026-06-21 23:38:39 +00:00

feat(theme): persist user theme preferences (#2756)

- Migrate theme persistence from `sessionStorage` to `localStorage`
- Rename theme HTML attribute to `data-bs-theme` for Bootstrap compatibility
- Trim and compile CSS according to the chosen theme mode
This commit is contained in:
Cotes Chung
2026-06-17 23:20:12 +08:00
committed by GitHub
parent ceb2a41463
commit 7496dd41fa
51 changed files with 541 additions and 153 deletions
+2 -2
View File
@@ -19,7 +19,7 @@
{%- comment -%} Auto switch theme {%- endcomment -%}
function reloadDisqus(event) {
if (event.source === window && event.data && event.data.id === Theme.ID) {
if (event.source === window && event.data && event.data.id === Theme.eventId) {
{%- comment -%} Disqus hasn't been loaded {%- endcomment -%}
if (typeof DISQUS === 'undefined') {
return;
@@ -33,7 +33,7 @@
addDisqus();
if (Theme.switchable) {
if (Theme.isToggleable) {
addEventListener('message', reloadDisqus);
}
+5 -4
View File
@@ -1,8 +1,8 @@
<!-- https://giscus.app/ -->
<script>
(function () {
const themeMapper = Theme.getThemeMapper('light', 'dark_dimmed');
const initTheme = themeMapper[Theme.visualState];
const themeMap = Theme.newThemeMap('light', 'dark_dimmed');
const initTheme = themeMap[Theme.resolvedTheme];
let lang = '{{ site.comments.giscus.lang | default: lang }}';
{%- comment -%} https://github.com/giscus/giscus/tree/main/locales {%- endcomment -%}
@@ -37,8 +37,9 @@
$footer.insertAdjacentElement("beforebegin", giscusNode);
addEventListener('message', (event) => {
if (event.source === window && event.data && event.data.id === Theme.ID) {
const newTheme = themeMapper[Theme.visualState];
if (event.source === window && event.data && event.data.id === Theme.eventId) {
const newTheme = themeMap[Theme.resolvedTheme];
const message = {
setConfig: {
theme: newTheme
+4 -4
View File
@@ -2,8 +2,8 @@
<script>
(function () {
const origin = 'https://utteranc.es';
const themeMapper = Theme.getThemeMapper('github-light', 'github-dark');
const initTheme = themeMapper[Theme.visualState];
const themeMap = Theme.newThemeMap('github-light', 'github-dark');
const initTheme = themeMap[Theme.resolvedTheme];
let script = document.createElement('script');
script.src = 'https://utteranc.es/client.js';
@@ -22,8 +22,8 @@
{%- comment -%}
Credit to <https://github.com/utterance/utterances/issues/170#issuecomment-594036347>
{%- endcomment -%}
if (event.source === window && event.data && event.data.id === Theme.ID) {
newTheme = themeMapper[Theme.visualState];
if (event.source === window && event.data && event.data.id === Theme.eventId) {
newTheme = themeMap[Theme.resolvedTheme];
const message = {
type: 'set-theme',
+1 -1
View File
@@ -81,7 +81,7 @@
<!-- Bootstrap -->
{% unless jekyll.environment == 'production' %}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="{{ site.data.origin.cors.bootstrap.css }}">
{% endunless %}
<!-- Theme style -->
+47 -5
View File
@@ -40,11 +40,53 @@
</ul>
</nav>
<div class="sidebar-bottom d-flex flex-wrap align-items-center w-100">
{% unless site.theme_mode %}
<button type="button" class="btn btn-link nav-link" aria-label="Switch Mode" id="mode-toggle">
<i class="fas fa-adjust"></i>
</button>
<div class="sidebar-bottom d-flex flex-wrap align-items-center w-100">
{% unless site.theme_mode == 'light' or site.theme_mode == 'dark' %}
{%- capture icon_system -%}
<i class="fa-solid fa-display" data-theme-mode="system"></i>
{%- endcapture -%}
{%- capture icon_light -%}
<i class="fa-regular fa-sun" data-theme-mode="light"></i>
{%- endcapture -%}
{%- capture icon_dark -%}
<i class="fa-regular fa-moon" data-theme-mode="dark"></i>
{%- endcapture -%}
<div class="btn-group dropup">
<button
type="button"
class="btn btn-link nav-link"
aria-label="Switch Mode"
id="mode-toggle"
data-bs-toggle="dropdown"
>
{{- icon_light -}}
{{- icon_dark -}}
{{- icon_system -}}
</button>
<ul class="dropdown-menu rounded-3 mb-1 p-1">
<li>
<button class="dropdown-item d-flex align-items-center" type="button" data-theme-mode="light">
{{- icon_light -}}
{{- site.data.locales[lang].theme.light -}}
</button>
</li>
<li>
<button class="dropdown-item d-flex align-items-center" type="button" data-theme-mode="dark">
{{- icon_dark -}}
{{- site.data.locales[lang].theme.dark -}}
</button>
</li>
<li>
<button class="dropdown-item d-flex align-items-center" type="button" data-theme-mode="system">
{{- icon_system -}}
{{- site.data.locales[lang].theme.system -}}
</button>
</li>
</ul>
</div>
{% if site.data.contact.size > 0 %}
<span class="icon-border"></span>