Skip to content

Commit 2e45372

Browse files
feat: migrate theme switcher to Stimulus controller
1 parent fa97197 commit 2e45372

3 files changed

Lines changed: 6 additions & 7 deletions

File tree

src/BootstrapAdminUi/assets/scripts/theme-switcher.js renamed to src/BootstrapAdminUi/assets/controllers/theme-switcher-controller.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
const toggleButton = document.querySelector('[data-theme-toggle]');
1+
import { Controller } from '@hotwired/stimulus';
22

3-
if (toggleButton) {
4-
toggleButton.addEventListener('click', () => {
3+
export default class extends Controller {
4+
toggle() {
55
const current = document.documentElement.getAttribute('data-bs-theme') || 'light';
66
const next = current === 'dark' ? 'light' : 'dark';
77
localStorage.setItem('sylius-theme', next);
88
document.documentElement.setAttribute('data-bs-theme', next);
9-
});
9+
}
1010
}

src/BootstrapAdminUi/assets/entrypoint.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,3 @@ import './scripts/check-all';
1414
import './scripts/menu-search';
1515

1616
import './scripts/bootstrap';
17-
import './scripts/theme-switcher';

src/BootstrapAdminUi/templates/shared/crud/common/navbar/items/theme_switcher.html.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
<div class="nav-item">
2-
<button class="btn btn-icon btn-ghost-secondary" data-theme-toggle aria-label="Toggle theme">
1+
<div class="nav-item" {{ stimulus_controller('theme-switcher') }}>
2+
<button class="btn btn-icon btn-ghost-secondary" {{ stimulus_action('theme-switcher', 'toggle') }} aria-label="Toggle theme">
33
{{ ux_icon('tabler:sun', {'class': 'icon', 'data-theme-switch': 'light'}) }}
44
{{ ux_icon('tabler:moon', {'class': 'icon', 'data-theme-switch': 'dark'}) }}
55
</button>

0 commit comments

Comments
 (0)