felixhahnweilheim / humhub-dark-mode

Dark Mode for HumHub
https://marketplace.humhub.com/module/dark-mode/description
GNU Affero General Public License v3.0
7 stars 4 forks source link

Toggle with JavaScript #33

Open ArchBlood opened 11 months ago

ArchBlood commented 11 months ago

Have you thought of handling the toggle through JS?

Here's an example

humhub.module('dark-mode').on('humhub:ready', function(event, el) {
    const toggleSwitch = document.createElement('input');
    toggleSwitch.setAttribute('type', 'checkbox');
    toggleSwitch.setAttribute('id', 'modeToggle');

    const label = document.createElement('label');
    label.setAttribute('for', 'modeToggle');
    label.textContent = 'Switch mode:';

    const contentContainer = document.querySelector('.dark-mode-container-selector');
    contentContainer.appendChild(label);
    contentContainer.appendChild(toggleSwitch);

    // Function to set the theme based on the toggle state
    function switchTheme(e) {
        if (e.target.checked) {
            // Apply night mode theme or trigger HumHub's theme change method
            // Example: humhub.theme.apply('night-mode');
        } else {
            // Apply day mode theme or trigger HumHub's theme change method
            // Example: humhub.theme.apply('day-mode');
        }
    }

    // Event listener for the toggle switch
    toggleSwitch.addEventListener('change', switchTheme);

    // Logic to check user's preference from HumHub settings or storage

    // Save user's preference to HumHub settings or storage
    toggleSwitch.addEventListener('change', function(e) {
        if (e.target.checked) {
            // Save night mode preference
            // Example: humhub.settings.set('theme', 'night');
        } else {
            // Save day mode preference
            // Example: humhub.settings.set('theme', 'day');
        }
    });
});
felixhahnweilheim commented 11 months ago

I like to keep things simple.

But if you want to remove the full page reload after the modal is saved, you can create a PR.

I think the asset should be registered in the modal view, so the initial page load does not increase.

ArchBlood commented 11 months ago

I like to keep things simple.

But if you want to remove the full page reload after the modal is saved, you can create a PR.

I think the asset should be registered in the modal view, so the initial page load does not increase.

I'll do some tests on my end of things.