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

📌 Feature Request: Add setLocalStorage Extension to Store hx-data in Local Storage #141

Open
xehrad opened this issue Feb 4, 2025 · 1 comment

Comments

@xehrad
Copy link
Contributor

xehrad commented Feb 4, 2025

Issue Description:

HTMX currently does not have a built-in way to persist hx-data values in localStorage. A new extension, setLocalStorage, would allow developers to automatically store hx-data attributes when an element is clicked. This would enable client-side data persistence and enhance state management in HTMX applications.


✨ Proposed Feature:

The setLocalStorage extension will:

  1. Detect elements with hx-data when they are processed by HTMX.
  2. Add a click event listener to store hx-data values in localStorage.
  3. Allow developers to retrieve stored values for future requests or UI updates.

💡 Example Usage:

<button hx-ext="setLocalStorage" hx-data='{"theme": "dark", "username": "Alice"}'>Save to Local Storage</button>
  • Clicking this button will store:
    • localStorage.setItem("theme", "dark")
    • localStorage.setItem("username", "Alice")

🛠 Suggested Implementation:

htmx.defineExtension('setLocalStorage', {
    onEvent: function (name, evt) {
        if (name === 'htmx:afterProcessNode') {
            let element = evt.detail.elt;

            if (element.hasAttribute('hx-data')) {
                element.addEventListener('click', function () {
                    try {
                        const hxData = JSON.parse(element.getAttribute('hx-data'));

                        for (const [key, value] of Object.entries(hxData)) {
                            localStorage.setItem(key, JSON.stringify(value));
                        }
                    } catch (error) {
                        console.error("Invalid hx-data JSON:", error);
                    }
                });
            }
        }
    }
});

🎯 Why This Feature Matters:

Enhances Client-Side State Management – Allows developers to persist important UI data (e.g., theme, form inputs, filters).
Improves UX – Users can reload a page and still retain previously selected values.
Lightweight & Simple – Works within HTMX’s declarative approach without requiring extra JavaScript.

Would love to see this feature added to HTMX! Let me know if you need any refinements or contributions. 🙌🔥

@xehrad xehrad changed the title 📌 **Feature Request: Add setLocalStorage Extension to Store hx-data in Local Storage** 📌 Feature Request: Add setLocalStorage Extension to Store hx-data in Local Storage Feb 4, 2025
@Telroshan
Copy link
Collaborator

Hey, just to let you know that we don't accept new extensions in the htmx repo itself, but rather welcome them as community extensions, see our extensions contribution guidelines for more details!
Long-story short : feel free to create a repository of your own to host that extension, and add it to our list of community extensions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants