Theme Toggle
Toggle between dark/light theme.
Usage
With icon
ThemeToggle do |toggle| toggle.light_mode do Button(variant: :ghost, icon: true) do svg( xmlns: "http://www.w3.org/2000/svg", viewbox: "0 0 24 24", fill: "currentColor", class: "w-4 h-4" ) do |s| s.path( d: "M12 2.25a.75.75 0 01.75.75v2.25a.75.75 0 01-1.5 0V3a.75.75 0 01.75-.75zM7.5 12a4.5 4.5 0 119 0 4.5 4.5 0 01-9 0zM18.894 6.166a.75.75 0 00-1.06-1.06l-1.591 1.59a.75.75 0 101.06 1.061l1.591-1.59zM21.75 12a.75.75 0 01-.75.75h-2.25a.75.75 0 010-1.5H21a.75.75 0 01.75.75zM17.834 18.894a.75.75 0 001.06-1.06l-1.59-1.591a.75.75 0 10-1.061 1.06l1.59 1.591zM12 18a.75.75 0 01.75.75V21a.75.75 0 01-1.5 0v-2.25A.75.75 0 0112 18zM7.758 17.303a.75.75 0 00-1.061-1.06l-1.591 1.59a.75.75 0 001.06 1.061l1.591-1.59zM6 12a.75.75 0 01-.75.75H3a.75.75 0 010-1.5h2.25A.75.75 0 016 12zM6.697 7.757a.75.75 0 001.06-1.06l-1.59-1.591a.75.75 0 00-1.061 1.06l1.59 1.591z" ) end end end toggle.dark_mode do Button(variant: :ghost, icon: true) do svg( xmlns: "http://www.w3.org/2000/svg", viewbox: "0 0 24 24", fill: "currentColor", class: "w-4 h-4" ) do |s| s.path( fill_rule: "evenodd", d: "M9.528 1.718a.75.75 0 01.162.819A8.97 8.97 0 009 6a9 9 0 009 9 8.97 8.97 0 003.463-.69.75.75 0 01.981.98 10.503 10.503 0 01-9.694 6.46c-5.799 0-10.5-4.701-10.5-10.5 0-4.368 2.667-8.112 6.46-9.694a.75.75 0 01.818.162z", clip_rule: "evenodd" ) end end end end
Copied!
Copy failed!
With text
ThemeToggle do |toggle| toggle.light_mode do Button(variant: :primary) { "Light" } end toggle.dark_mode do Button(variant: :primary) { "Dark" } end end
Copied!
Copy failed!
Installation
Using RubyUI CLI
Run the install command
rails g ruby_ui:component ThemeToggle
Copied!
Copy failed!
Manual installation
1
Add RubyUI::ThemeToggle
to app/components/ruby_ui/theme_toggle.rb
# frozen_string_literal: true module RubyUI class ThemeToggle < Base def view_template(&) div(**attrs, &) end def light_mode(**user_attrs, &) light_attrs = mix(default_light_attrs, user_attrs) div(**light_attrs, &) end def dark_mode(**user_attrs, &) dark_attrs = mix(default_dark_attrs, user_attrs) div(**dark_attrs, &) end private def default_attrs { data: {controller: "ruby-ui--theme-toggle"} } end def default_light_attrs { class: "dark:hidden", data: {action: "click->ruby-ui--theme-toggle#setDarkTheme"} } end def default_dark_attrs { class: "hidden dark:inline-block", data: {action: "click->ruby-ui--theme-toggle#setLightTheme"} } end end end
Copied!
Copy failed!
2
Add theme_toggle_controller.js
to app/javascript/controllers/ruby_ui/theme_toggle_controller.js
import { Controller } from "@hotwired/stimulus" export default class extends Controller { initialize() { this.setTheme() } setTheme() { // On page load or when changing themes, best to add inline in `head` to avoid FOUC if (localStorage.theme === 'dark' || (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) { document.documentElement.classList.add('dark') document.documentElement.classList.remove('light') } else { document.documentElement.classList.remove('dark') document.documentElement.classList.add('light') } } setLightTheme() { // Whenever the user explicitly chooses light mode localStorage.theme = 'light' this.setTheme() } setDarkTheme() { // Whenever the user explicitly chooses dark mode localStorage.theme = 'dark' this.setTheme() } }
Copied!
Copy failed!
3
Update the Stimulus controllers manifest file
Importmap!
You don't need to run this command if you are using Importmap
rake stimulus:manifest:update
Copied!
Copy failed!
Components
Component | Built using | Source |
---|---|---|
ThemeToggle | Phlex | |
ThemeToggleController | Stimulus JS |