Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hx-trigger from:html with data-bs-theme #2737

Open
sderuiter opened this issue Jul 17, 2024 · 1 comment
Open

hx-trigger from:html with data-bs-theme #2737

sderuiter opened this issue Jul 17, 2024 · 1 comment
Labels
question Informational question about using htmx

Comments

@sderuiter
Copy link

I'm trying to introduce a theme switcher using Bootstrap 5.3, which is implemented like:

<script>
        document.addEventListener('DOMContentLoaded', (event) => {
        const htmlElement = document.documentElement;
        const switchElement = document.getElementById('darkModeSwitch');
        
        // Set the default theme to dark if no setting is found in local storage
        const currentTheme = localStorage.getItem('bsTheme') || 'dark';
        htmlElement.setAttribute('data-bs-theme', currentTheme);
        switchElement.checked = currentTheme === 'dark';
        
        switchElement.addEventListener('change', function () {
            if (this.checked) {
                htmlElement.setAttribute('data-bs-theme', 'dark');
                localStorage.setItem('bsTheme', 'dark');
            } else {
                htmlElement.setAttribute('data-bs-theme', 'light');
                localStorage.setItem('bsTheme', 'light');
            }
        });
    });
    </script>

Ie, on the html element data-bs-theme="light" of data-bs-theme="dark" is added.

On the page, I'm using HTMX to load content, example:

<div class="plotly" hx-trigger="load" hx-get="/ajax_statistics_plotly_py"></div>

which works well and loads a plotly.py graph into a div.

As Plotly uses templates, my goal it to have HTMX trigger a reload when the theme switches.
I thought adding from:html would work, see below, but this does not trigger the hx-get.

<div class="plotly" hx-trigger="load,from:html" hx-get="/ajax_statistics_plotly_py"></div>

Is this even possible?

@Telroshan
Copy link
Collaborator

Telroshan commented Jul 18, 2024

Hey, you're using the load event here, which according to its doc, only fires once when the full page loads

The load event is fired when the whole page has loaded

So when doing partial refreshes like here with htmx (or any other client side lib/framework), no load event will be fired again.

What you could do is fire a custom event when switching theme, and using that to trigger a refresh of your element.
So for ex

document.body.dispatchEvent(new CustomEvent("switched-theme"))

And then in your element, use

<div class="plotly" hx-trigger="switched-theme from:body" hx-get="/ajax_statistics_plotly_py"></div>

Besides, when you want to listen for an event "everywhere", you should use from:body and not from:html as per the doc, also please note the comma splits hx-triggers, you should just use a space to separate the event name and its modifiers (cf doc)

Hope this helps!

@Telroshan Telroshan added the question Informational question about using htmx label Jul 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Informational question about using htmx
Projects
None yet
Development

No branches or pull requests

2 participants