Skip to content

Commit

Permalink
dev: Provide a JS Switch controller
Browse files Browse the repository at this point in the history
  • Loading branch information
marien-probesys committed Nov 7, 2023
1 parent da36f4d commit 3bea2cd
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
2 changes: 2 additions & 0 deletions assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import NotificationsController from '@/controllers/notifications_controller.js';
import PasswordController from '@/controllers/password_controller.js';
import PopupController from '@/controllers/popup_controller.js';
import ScrollToController from '@/controllers/scroll_to_controller.js';
import SwitchController from '@/controllers/switch_controller.js';
import TabsController from '@/controllers/tabs_controller.js';
import TinymceController from '@/controllers/tinymce_controller.js';

Expand All @@ -39,6 +40,7 @@ application.register('notifications', NotificationsController);
application.register('password', PasswordController);
application.register('popup', PopupController);
application.register('scroll-to', ScrollToController);
application.register('switch', SwitchController);
application.register('tabs', TabsController);
application.register('tinymce', TinymceController);

Expand Down
9 changes: 1 addition & 8 deletions assets/javascripts/controllers/modal_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@

import { Controller } from '@hotwired/stimulus';

const FOCUSABLE_ELEMENTS = [
'a[href]:not([tabindex="-1"])',
'button:not([disabled]):not([tabindex="-1"])',
'input:not([disabled]):not([type="hidden"]):not([tabindex="-1"])',
'select:not([disabled]):not([tabindex="-1"])',
'textarea:not([disabled]):not([tabindex="-1"])',
'[tabindex]:not([tabindex="-1"])',
];
import { FOCUSABLE_ELEMENTS } from '@/query_selectors.js';

// Credits to:
// - JoliCode: https://jolicode.com/blog/une-fenetre-modale-accessible
Expand Down
26 changes: 26 additions & 0 deletions assets/javascripts/controllers/switch_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// This file is part of Bileto.
// Copyright 2022-2023 Probesys
// SPDX-License-Identifier: AGPL-3.0-or-later

import { Controller } from '@hotwired/stimulus';

import { FOCUSABLE_ELEMENTS } from '@/query_selectors.js';

export default class extends Controller {
static get targets () {
return ['panel'];
}

change (event) {
this.panelTargets.forEach((panel) => {
panel.hidden = panel.id !== event.params.for;

if (!panel.hidden) {
const focusableElements = Array.from(panel.querySelectorAll(FOCUSABLE_ELEMENTS));
if (focusableElements.length > 0) {
focusableElements[0].focus();
}
}
});
}
}
12 changes: 12 additions & 0 deletions assets/javascripts/query_selectors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// This file is part of Bileto.
// Copyright 2022-2023 Probesys
// SPDX-License-Identifier: AGPL-3.0-or-later

export const FOCUSABLE_ELEMENTS = [
'a[href]:not([tabindex="-1"])',
'button:not([disabled]):not([tabindex="-1"])',
'input:not([disabled]):not([type="hidden"]):not([tabindex="-1"])',
'select:not([disabled]):not([tabindex="-1"])',
'textarea:not([disabled]):not([tabindex="-1"])',
'[tabindex]:not([tabindex="-1"])',
];

0 comments on commit 3bea2cd

Please sign in to comment.