Skip to content

Commit 1d4f936

Browse files
Knhashnielsenramon
authored andcommitted
feat: theme toggle as option in config
1 parent 11cd2c2 commit 1d4f936

File tree

6 files changed

+65
-42
lines changed

6 files changed

+65
-42
lines changed

_assets/javascripts/application.js

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,4 @@ $(document).ready(function() {
1616
window.open($(this).attr('href'), 'Share', 'width=200,height=200,noopener');
1717
return false;
1818
});
19-
});
20-
21-
//Theme initialize
22-
if (!localStorage.getItem('theme'))
23-
localStorage.setItem('theme', "light");
24-
themer();
25-
26-
// Theme toggle
27-
function toggle() {
28-
if(localStorage.getItem('theme') == "light")
29-
localStorage.setItem('theme', "dark");
30-
else if(localStorage.getItem('theme') == "dark")
31-
localStorage.setItem('theme', "light");
32-
themer();
33-
}
34-
35-
// Theme set
36-
function themer() {
37-
var tone = localStorage.getItem('theme');
38-
var light = document.getElementById("light");
39-
40-
if(tone == "dark"){
41-
dark.disabled = false;
42-
}
43-
else{
44-
dark.disabled = true;
45-
}
46-
}
19+
});

_assets/javascripts/themetoggle.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//Theme initialize
2+
if (!localStorage.getItem('theme'))
3+
localStorage.setItem('theme', "light");
4+
themer();
5+
6+
// Theme toggle
7+
function toggle() {
8+
if(localStorage.getItem('theme') == "light")
9+
localStorage.setItem('theme', "dark");
10+
else if(localStorage.getItem('theme') == "dark")
11+
localStorage.setItem('theme', "light");
12+
themer();
13+
}
14+
15+
// Theme set
16+
function themer() {
17+
var tone = localStorage.getItem('theme');
18+
var light = document.getElementById("light");
19+
20+
if(tone == "dark"){
21+
dark.disabled = false;
22+
}
23+
else{
24+
dark.disabled = true;
25+
}
26+
}

_config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ url: # add your site url (format: https://example.com)
88

99
# Optional settings
1010

11+
theme_toggle: false # Change to true if you wish to show an icon in the navigation for dynamic theme toggling
1112
about_enabled: false # Change to true if you wish to show an icon in the navigation that redirects to the about page
1213
baseurl: # Set if blog doesn't sit at the root of the domain (format: /blog)
1314
discus_identifier: # Add your Disqus identifier

_includes/head.html

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,32 @@
3535
{% asset fonts.css %}
3636
{% endif %}
3737

38-
{% if site.blog_theme == "light" %}
39-
<link rel="icon" type="image/x-icon" href="{% asset favicon-light.ico @path %}">
40-
<link rel="apple-touch-icon" href="{% asset apple-touch-icon-light.png @path %}">
41-
<link rel="stylesheet" type="text/css" title="light" id="light" href="{% asset light.css @path%}">
42-
<link rel="stylesheet" type="text/css" title="dark" id="dark" href="{% asset dark.css @path%}" disabled="true">
38+
{% if site.theme_toggle == true %}
39+
40+
{% if site.blog_theme == "light" %}
41+
<link rel="icon" type="image/x-icon" href="{% asset favicon-light.ico @path %}">
42+
<link rel="apple-touch-icon" href="{% asset apple-touch-icon-light.png @path %}">
43+
<link rel="stylesheet" type="text/css" title="light" id="light" href="{% asset light.css @path%}">
44+
<link rel="stylesheet" type="text/css" title="dark" id="dark" href="{% asset dark.css @path%}" disabled="true">
45+
{% else %}
46+
<link rel="icon" type="image/x-icon" href="{% asset favicon-dark.ico @path %}">
47+
<link rel="apple-touch-icon" href="{% asset apple-touch-icon-dark.png @path %}">
48+
<link rel="stylesheet" type="text/css" title="light" id="light" href="{% asset light.css @path%}">
49+
<link rel="stylesheet" type="text/css" title="dark" id="dark" href="{% asset dark.css @path%}" disabled="false">
50+
{% endif %}
51+
4352
{% else %}
44-
<link rel="icon" type="image/x-icon" href="{% asset favicon-dark.ico @path %}">
45-
<link rel="apple-touch-icon" href="{% asset apple-touch-icon-dark.png @path %}">
46-
<link rel="stylesheet" type="text/css" title="light" id="light" href="{% asset light.css @path%}">
47-
<link rel="stylesheet" type="text/css" title="dark" id="dark" href="{% asset dark.css @path%}" disabled="false">
53+
54+
{% if site.blog_theme == "light" %}
55+
<link rel="icon" type="image/x-icon" href="{% asset favicon-light.ico @path %}">
56+
<link rel="apple-touch-icon" href="{% asset apple-touch-icon-light.png @path %}">
57+
{% asset light.css %}
58+
{% else %}
59+
<link rel="icon" type="image/x-icon" href="{% asset favicon-dark.ico @path %}">
60+
<link rel="apple-touch-icon" href="{% asset apple-touch-icon-dark.png @path %}">
61+
{% asset dark.css %}
62+
{% endif %}
63+
4864
{% endif %}
65+
4966
</head>

_includes/javascripts.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@
1919
{% endif %}
2020

2121
{% asset application.js %}
22+
23+
{% if site.theme_toggle == true %}
24+
{% asset themetoggle.js %}
25+
{% endif %}

_includes/navigation.html

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,12 @@
169169
</a>
170170
</li>
171171
{% endif %}
172-
<li>
173-
<a onclick="toggle()" title="Toggle Theme">
174-
{% include svg-icon.html icon="theme" %}
175-
</a>
176-
</li>
172+
{% if site.theme_toggle == true %}
173+
<li>
174+
<a onclick="toggle()" title="Toggle Theme">
175+
{% include svg-icon.html icon="theme" %}
176+
</a>
177+
</li>
178+
{% endif %}
177179
</ul>
178180
</nav>

0 commit comments

Comments
 (0)