Problem/Motivation
The Bootstrap gallery grid (bs-slider--bootstrap-gallery-grid.html.twig) renders each thumbnail inside a plain <div class="bs-slider__item">, and gallery.js opens the lightbox only on a mouse click. Keyboard users cannot reach a thumbnail with Tab or open it with Enter or Space, and screen readers do not announce thumbnails as controls. This fails WCAG 2.1 success criteria 2.1.1 Keyboard (level A) and 4.1.2 Name, Role, Value (level A).
The lightbox has the same problem. The close control is <a role="button"> without href, so it cannot get focus, and its CSS sets outline: 0. When the lightbox opens, focus stays on the page behind it, Escape does nothing, and after closing, focus is not returned to the thumbnail. Inside the lightbox, the previous and next controls show no focus, because Bootstrap removes their outline, and the arrow keys do not change the image unless the carousel itself has focus. The carousel template also prints "Previous" and "Next" without translation.
Steps to reproduce
- Display media with a BS Slider configuration of the
bootstrap_gallery_gridtype. - Press Tab on the page. The thumbnails are skipped.
- Click a thumbnail to open the lightbox, then press Tab or Escape. Focus stays on the page behind the lightbox, and Escape does not close it.
Proposed resolution
- Wrap each thumbnail in
<button type="button" class="bs-slider__thumbnail">inside the existing.bs-slider__item. The browser then provides focus, Enter and Space, and the button role. The existing click handler on.bs-slider__itemkeeps working, because the button's click event bubbles to it. - The button takes its accessible name from the image alt text. When the image has no alt text,
gallery.jssetsaria-labelto the translatableDrupal.t('Enlarge image @n of @count'). - Reset the button styles in
gallery.scssso the grid looks the same as before. - Change the close control to
<button type="button">with anaria-label, and remove itsoutline: 0so keyboard focus is visible. - Render the lightbox as a native
<dialog>with a translatablearia-label, opened withshowModal(). The browser then moves focus into the lightbox, makes the rest of the page inert so Tab cannot leave it, closes it on Escape, which also fixes #3097956: Full screen gallery should close on ESC key, and returns focus to the thumbnail that opened it.gallery.jsdoes its clean-up in the dialog'scloseevent, which fires for both the close button and Escape. - When the lightbox opens, focus goes to the next control, because browsing is the main task in a gallery. Without controls, focus goes to the close button, which has
autofocus. - The left and right arrow keys show the previous and next image wherever focus is in the lightbox. When focus is inside the carousel, Bootstrap handles the key itself, and
gallery.jsskips events whose default action was prevented, so the image moves only once. - Show keyboard focus on the previous and next controls in the lightbox, with an outline around the visible arrow box.
- Translate "Previous" and "Next" in
bs-slider--bootstrap-carousel.html.twigwith|t. - Reset the browser styles of
<dialog>ingallery.scssso the lightbox fills the viewport as before.
Note for themes: a thumbnail view mode that renders a link would put a link inside the new button, which is invalid HTML. Thumbnail view modes should render the image only.
Remaining tasks
- Review and test with a screen reader. Keyboard use was tested in Chromium, Firefox and WebKit: Tab, Enter and Space on a thumbnail, focus starting on the next control and staying inside the open lightbox, the arrow keys, Escape and the close button, and focus returning to the thumbnail.
- Close #3097956: Full screen gallery should close on ESC key as fixed by this issue once it is merged.
- Follow-up: check the Swiper thumbs gallery (
bs-slider--swiper-thumbs-gallery.html.twig). Its thumbnails are handled by Swiper's thumbs module, which is a different mechanism. - bs_slider has no automated tests, so this change has none.
User interface changes
Thumbnails, the close button and the previous and next controls can be reached and used with the keyboard, and show a focus outline. The arrow keys change the image in the open lightbox. The layout of the grid and the lightbox does not change.
API changes
Template change in bs-slider--bootstrap-gallery-grid.html.twig: a new <button class="bs-slider__thumbnail"> inside .bs-slider__item, the close control changes from <a> to <button> with the same bs-slider__close class, and .bs-slider__gallery changes from <div> to <dialog>. Themes that override this template need the same changes, because gallery.js now calls showModal() and close() on .bs-slider__gallery.
Data model changes
None.
Issue fork bs_slider-3628427
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #4
pivica commentedDone.