Adding a sun/moon dark mode toggle to Ghost Casper
user: can we implement a light/dark mode toggle on our Ghost Casper Theme we have running on myconversationswith.ai
Using Sun/Moon icon that floats on the top right corner
assistant: Yes, you can implement a **light/dark mode toggle with a sun/moon icon floating in the top-right corner** of the Ghost **Casper theme** on your site `myconversationswith.ai`. Here's how to do it cleanly without disrupting Ghost updates too much:
---
## ✅ Step-by-step: Add Dark Mode Toggle to Casper
### 1. **Add theme CSS for dark mode**
Edit `/var/www/myconversationswith.ai/ghost/content/themes/casper/assets/css/screen.css` (or use `screen.scss` if building from source) and append:
```css
/* Dark mode toggle styles */
.theme-toggle {
position: fixed;
top: 1rem;
right: 1rem;
z-index: 9999;
cursor: pointer;
background: none;
border: none;
font-size: 1.5rem;
}
html[data-theme='dark'] {
filter: invert(1) hue-rotate(180deg);
background-color: #111;
}
html[data-theme='dark'] img,
html[data-theme='dark'] video {
filter: invert(1) hue-rotate(180deg); /* Revert images */
}
```
You can style more elements later with dedicated dark mode rules instead of invert/hue.
---
### 2. **Add toggle button to the top-right**
Open `/var/www/myconversationswith.ai/ghost/content/themes/casper/default.hbs` and just before the closing `</body>` tag, add:
```hbs
<button class="theme-toggle" id="themeToggle" aria-label="Toggle dark mode">
<span id="themeIcon">🌙</span>
</button>
```
---
### 3. **Add toggle JavaScript**
Still in `default.hbs`, right after the toggle button, add this script:
```html
<script>
const toggle = document.getElementById('themeToggle');
const icon = document.getElementById('themeIcon');
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
const current = localStorage.getItem('theme');
if (current === 'dark' || (!current && prefersDark)) {
document.documentElement.setAttribute('data-theme', 'dark');
icon.textContent = '☀️';
}
toggle.addEventListener('click', () => {
const isDark = document.documentElement.getAttribute('data-theme') === 'dark';
document.documentElement.setAttr…
user: I will let you know later how it works out and what I think of the colors, then we will perfect it later.
I'm just curious if you can write a script in node.js to take care of the steps you said above automatically
assistant: Absolutely — here's a **Node.js script** that automates adding the dark mode toggle to your **Ghost Casper theme**, including:
- Injecting the toggle button and script into `default.hbs`
- Appending the required CSS into `screen.css`
- Creating backups first ✅
---
### 🛠️ Script: `add-dark-mode-toggle.js`
```js
const fs = require('fs');
const path = require('path');
// CONFIGURATION
const THEME_PATH = '/var/www/myconversationswith.ai/ghost/content/themes/casper';
const HBS_FILE = path.join(THEME_PATH, 'default.hbs');
const CSS_FILE = path.join(THEME_PATH, 'assets/css/screen.css');
// 1. CSS to inject
const DARK_MODE_CSS = `
/* === Dark Mode Toggle === */
.theme-toggle {
position: fixed;
top: 1rem;
right: 1rem;
z-index: 9999;
cursor: pointer;
background: none;
border: none;
font-size: 1.5rem;
}
html[data-theme='dark'] {
filter: invert(1) hue-rotate(180deg);
background-color: #111;
}
html[data-theme='dark'] img,
html[data-theme='dark'] video {
filter: invert(1) hue-rotate(180deg);
}
/* === End Dark Mode === */
`;
// 2. Button & Script to inject
const TOGGLE_SNIPPET = `
<!-- 🌗 Theme Toggle Button -->
<button class="theme-toggle" id="themeToggle" aria-label="Toggle dark mode">
<span id="themeIcon">🌙</span>
</button>
<script>
const toggle = document.getElementById('themeToggle');
const icon = document.getElementById('themeIcon');
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
const current = localStorage.getItem('theme');
if (current === 'dark' || (!current && prefersDark)) {
document.documentElement.setAttribute('data-theme', 'dark');
icon.textContent = '☀️';
}
toggle.addEventListener('click', () => {
const isDark = document.documentElement.getAttribute('data-theme') === 'dark';
document.documentElement.setAttribute('data-theme', isDark ? 'light' : 'dark');
localStorage.setItem('theme', isDark ? 'light' : 'dark');
icon.textContent = isDark ? '🌙' : '☀️';
});
</script…
assistant: Thanks for the screenshots — the site looks awesome already! Based on what you showed and the logs, here are the top 3 reasons why you **might not see the dark mode toggle** yet:
---
### ✅ Quick Checklist to Fix It
#### 1. **Ensure `default.hbs` is being used**
Sometimes Ghost themes use a different `.hbs` layout for the homepage or other pages (like `index.hbs`, `home.hbs`, or `post.hbs`).
🔧 **Fix:** Also inject the toggle + script snippet into:
```bash
/var/www/myconversationswith.ai/ghost/content/themes/casper/index.hbs
/var/www/myconversationswith.ai/ghost/content/themes/casper/page.hbs
/var/www/myconversationswith.ai/ghost/content/themes/casper/post.hbs
```
Just like we did with `default.hbs`, add the toggle before `</body>` in each.
---
#### 2. **Restart Ghost properly**
It looks like you may have run `sudo sd`, which might not be the correct Ghost restart command if Ghost is installed globally.
🔧 **Proper command:**
```bash
cd /var/www/myconversationswith.ai/ghost && ghost restart
```
---
#### 3. **Clear Ghost theme cache**
Ghost caches themes aggressively. To refresh it:
```bash
cd /var/www/myconversationswith.ai/ghost
ghost stop
ghost start
```
Or you can zip the theme and re-upload it through the Ghost Admin under **Settings → Design → Change Theme → Upload a .zip** (as a workaround).
---
### 🧪 Bonus: Test for Button
After restart, inspect the page source (Right-click → View Source) and search for:
```html
<button class="theme-toggle"
```
If it’s there but invisible, we’ll tweak the CSS.
---
Would you like me to give you a script that injects into all `*.hbs` layouts at once?
assistant: Perfect — your screenshot confirms that the **toggle button HTML and JS were injected successfully**, but it’s not *visible* on the page. That means the issue is **100% CSS-related**, not JS or layout.
Let’s fix it now.
---
## ✅ Likely Cause
The button is probably being hidden by:
- `z-index` conflicts
- Transparent background over it
- Lack of color contrast (invisible on white)
- Casper's top bar overlapping it
---
## 🔧 Fix the Visibility & Styling
Edit your Casper CSS file:
```bash
nano /var/www/myconversationswith.ai/ghost/content/themes/casper/assets/css/screen.css
```
Search (`Ctrl+W`) for this section we appended:
```css
.theme-toggle {
```
And replace it with this stronger version:
```css
/* === Dark Mode Toggle === */
.theme-toggle {
position: fixed;
top: 1.25rem;
right: 1.25rem;
z-index: 99999;
background: rgba(255,255,255,0.9);
color: #000;
padding: 0.4rem 0.6rem;
border-radius: 100px;
border: 1px solid #ccc;
font-size: 1.2rem;
box-shadow: 0 0 8px rgba(0,0,0,0.2);
transition: all 0.2s ease;
}
html[data-theme='dark'] .theme-toggle {
background: rgba(30,30,30,0.9);
color: #fff;
border-color: #444;
}
.theme-toggle:hover {
transform: scale(1.1);
box-shadow: 0 0 10px rgba(0,0,0,0.4);
}
/* Dark mode filter */
html[data-theme='dark'] {
filter: invert(1) hue-rotate(180deg);
background-color: #111;
}
html[data-theme='dark'] img,
html[data-theme='dark'] video {
filter: invert(1) hue-rotate(180deg);
}
```
---
### 🔁 Then restart Ghost again
```bash
cd /var/www/myconversationswith.ai/ghost && ghost restart
```
Then do a **hard refresh in your browser** (`Ctrl+Shift+R` or `Cmd+Shift+R`) to flush the cache.
---
Once you're back, the sun/moon toggle should appear at the top right like a champ. Want to try a Solarized theme override next?
Showing first 6 of 8 messages. View the full conversation at https://mcwai.org/ai-conversation/adding-a-sun-moon-dark-mode-toggle-to-ghost-casper