Skip to content

Commit

Permalink
Use own Stimulus based theme changer
Browse files Browse the repository at this point in the history
  • Loading branch information
davidalejandroaguilar committed Sep 22, 2024
1 parent 669fbec commit 0425f98
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
17 changes: 17 additions & 0 deletions app/javascript/controllers/theme_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Controller } from "@hotwired/stimulus";

export default class extends Controller {
connect() {
const theme = localStorage.getItem("theme") || "light";
document.documentElement.dataset.theme = theme;
}

change(event) {
const theme = event.target.dataset.theme;
document.documentElement.dataset.theme = theme;

if (localStorage.getItem("theme") !== theme) {
localStorage.setItem("theme", theme);
}
}
}
7 changes: 0 additions & 7 deletions app/javascript/entrypoints/application.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,2 @@
import "@hotwired/turbo-rails";
import "../controllers";

import { themeChange } from "theme-change";
themeChange();

document.addEventListener("turbo:load", () => {
themeChange();
});
4 changes: 2 additions & 2 deletions app/views/components/shared/nav.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def view_template
private

def render_theme_changer
Dropdown :end do |dropdown|
Dropdown :end, data: {controller: :theme} do |dropdown|
dropdown.button :ghost, class: "mb-2" do
"Theme"
end
Expand All @@ -86,7 +86,7 @@ def render_theme_changer
div class: "grid grid-cols-1 gap-3 p-3" do
THEMES.each do |theme|
menu.item do
a data: {set_theme: theme} do
a data: {theme:, action: "theme#change"} do
theme.capitalize
end
end
Expand Down
Binary file modified bun.lockb
Binary file not shown.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"prettier": "^3.3.3",
"stimulus-vite-helpers": "^3.1.0",
"tailwindcss": "^3.4.7",
"theme-change": "^2.5.0",
"vite": "^5.0.0",
"vite-plugin-ruby": "^5.0.0",
"vite-plugin-stimulus-hmr": "^3.0.0"
Expand Down

0 comments on commit 0425f98

Please sign in to comment.