Showing a label for the Theme Switch

Want a label for the theme switch icon as in:

To start with, this is the code for the widget:

<div class="nav-item" is="dmx-bs5-theme-switch" id="bs5themeswitch" icons="fa" manager="bs5theme"></div>

Notice the class of .nav-item? This is to ensure that the button does not appear as a button.

The text 'Toggle theme' is already present in the widget, however, this is hidden from view.

To un-hide the text, use this JavaScript:

setInterval(() => {
  let elem = document.getElementById("bs5themeswitch-theme-text");
  if (elem) {
      elem.classList.remove("visually-hidden");
      elem.style.display = "inline !important";
      elem.style.visibility = "visible !important";
      elem.style.opacity = "1 !important";
  }
}, 500); // Runs every 500ms to ensure visibility 

The script is run continuously because Bootstrap scripts are reapplying the hidden class dynamically.

This will show the text. The problem is that there is no space between the icon and the text. To fix this, use this JavaScript:

setInterval(() => {
  let icon = document.querySelector("#bs5themeswitch-theme .theme-icon-active");
  if (icon) {
      icon.classList.add("fa-fw", "me-2"); // Ensures spacing styles persist
  }
}, 500); // Runs every 500ms  

And you are done.

Edit: I posted this mainly for my own purpose - so that I can remember how it's done. :smiley:

2 Likes

Hi Ben, here's an updated version of the component, which allows you to set custom text, via menu-text="" attribute:

dmxBootstrap5Theme.js.zip (5.0 KB)

Use it like this:

 <div class="nav-item" is="dmx-bs5-theme-switch" id="bs5themeswitch1" icons="fa" text-dark="Dark Theme" text-light="Light Theme" text-auto="Auto" manager="bs5theme1" menu-text="Change Theme"></div>

The option will be also available in the UI in the next update.

4 Likes

Thank you @Teodor (and the rest of the Team). Works like a charm. :clap:

2 Likes

This is now available in the latest extensions updates in Wappler 7 beta 31.

2 Likes