Module: Daisy::ThemeHelper

Defined in:
app/helpers/daisy/theme_helper.rb

Instance Method Summary collapse

Instance Method Details

#theme_preload_scriptString

Returns an inline script that preloads the saved theme from localStorage to prevent a flash of content when the page loads.

This script runs synchronously in the head section before the page renders, setting the ‘data-theme` attribute on the html element if a theme has been saved in localStorage.

Examples:

In a layout file

%head
  = theme_preload_script
  = stylesheet_link_tag "application"

Returns:

  • (String)

    The inline script as a string



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/helpers/daisy/theme_helper.rb', line 23

def theme_preload_script
  <<~SCRIPT.html_safe
    <script>
      (function() {
        try {
          var savedTheme = localStorage.getItem('savedTheme');
          if (savedTheme) {
            document.documentElement.setAttribute('data-theme', savedTheme);
          }
        } catch (e) {
          // localStorage not available (e.g., private browsing mode)
        }
      })();
    </script>
  SCRIPT
end