|
| 1 | +import AbstractDomElement from './AbstractDomElement' |
| 2 | +import Swiper from 'swiper' |
| 3 | +import { A11y, Navigation } from 'swiper/modules' |
| 4 | +import { __ } from '@wordpress/i18n' |
| 5 | +import noop from '../utils/noop' |
| 6 | + |
| 7 | +/** |
| 8 | + * Thin wrapper around Swiper with project defaults (A11y + Navigation). |
| 9 | + * |
| 10 | + * Default modules are always kept; extra modules in `options.modules` are appended |
| 11 | + * without duplicates. |
| 12 | + * |
| 13 | + * - A11y messages are translated via `@wordpress/i18n`; override per instance via `options.a11y` (`prevSlideMessage`, `nextSlideMessage`, etc.). |
| 14 | + * - If slides are `<li>`, `slideRole` is cleared so Swiper does not set `role="group"` and break list semantics for screen readers (overridable via `options.a11y`). |
| 15 | + * - When `hideNavigationFromA11y` is true, prev/next are removed from the accessibility tree and `.swiper-notification` is removed (nav announcements are unused). |
| 16 | + * - When `hideNavigationFromA11y` is false, `.swiper-notification` is kept so Swiper can announce first/prev/next/last messages on keyboard activation of the nav controls. |
| 17 | + * - `wrapperLiveRegion` is left to Swiper’s default (`true`): `aria-live="polite"` without autoplay, `aria-live="off"` with autoplay (overridable via `options.a11y`). |
| 18 | + * |
| 19 | + * Markup (minimal): |
| 20 | + * ```html |
| 21 | + * <div class="swiper"> |
| 22 | + * <div class="swiper-wrapper"> |
| 23 | + * <div class="swiper-slide">…</div> |
| 24 | + * </div> |
| 25 | + * <!-- get_template_part('components/parts/common/swiper', 'controls') --> |
| 26 | + * </div> |
| 27 | + * ``` |
| 28 | + * |
| 29 | + * @example |
| 30 | + * import Slider from './classes/Slider' |
| 31 | + * import { Pagination } from 'swiper/modules' |
| 32 | + * |
| 33 | + * Slider.init('.my-slider', { |
| 34 | + * hideNavigationFromA11y: true, |
| 35 | + * fixLastSlideActiveOnEnd: true, |
| 36 | + * onChange() {}, |
| 37 | + * options: { |
| 38 | + * slidesPerView: 'auto', |
| 39 | + * modules: [Pagination], |
| 40 | + * navigation: { |
| 41 | + * prevEl: '.swiper-button-prev', |
| 42 | + * nextEl: '.swiper-button-next', |
| 43 | + * }, |
| 44 | + * a11y: { |
| 45 | + * // Optional: override translated defaults per slider |
| 46 | + * nextSlideMessage: 'Next news item', |
| 47 | + * }, |
| 48 | + * }, |
| 49 | + * }) |
| 50 | + * |
| 51 | + * const slider = new Slider(el, { … }) |
| 52 | + * slider.getInstance() // Swiper |
| 53 | + * |
| 54 | + * @extends AbstractDomElement |
| 55 | + */ |
| 56 | +export default class Slider extends AbstractDomElement { |
| 57 | + /** |
| 58 | + * @param {HTMLElement} element - Root `.swiper` element |
| 59 | + * @param {SliderOptions} [options] |
| 60 | + */ |
| 61 | + constructor(element, options) { |
| 62 | + const instance = super(element, options) |
| 63 | + |
| 64 | + // avoid double init : |
| 65 | + if (!instance.isNewInstance()) { |
| 66 | + return instance |
| 67 | + } |
| 68 | + |
| 69 | + this.init() |
| 70 | + } |
| 71 | + |
| 72 | + init() { |
| 73 | + const el = this._element |
| 74 | + const { options, onChange, hideNavigationFromA11y, fixLastSlideActiveOnEnd } = this._settings |
| 75 | + |
| 76 | + // Merge default modules with user modules, avoiding duplicates. |
| 77 | + // AbstractDomElement.extend() replaces arrays instead of concatenating: |
| 78 | + // - If user passes no modules: extend copies defaults → old merge duplicated them |
| 79 | + // - If user passes modules: extend replaces defaults → we lose A11y/Navigation |
| 80 | + // This merge ensures defaults are always present + user modules added without duplicates. |
| 81 | + const defaultModules = Slider.defaults.options.modules |
| 82 | + const userModules = options.modules || [] |
| 83 | + const allModules = [...defaultModules, ...userModules.filter((mod) => !defaultModules.includes(mod))] |
| 84 | + |
| 85 | + // role="group" on <li> breaks list semantics for screen readers. |
| 86 | + const slidesAreListItems = el.querySelector('.swiper-slide')?.tagName === 'LI' |
| 87 | + |
| 88 | + // Temesis feedback: hide navigation buttons from accessibility tree |
| 89 | + // since slides are already keyboard accessible (e.g. cards with focusable content). |
| 90 | + const hideNavigationButtons = (swiper) => { |
| 91 | + if (!hideNavigationFromA11y) { |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + swiper.navigation.prevEl?.setAttribute('tabindex', '-1') |
| 96 | + swiper.navigation.prevEl?.setAttribute('aria-hidden', 'true') |
| 97 | + swiper.navigation.nextEl?.setAttribute('tabindex', '-1') |
| 98 | + swiper.navigation.nextEl?.setAttribute('aria-hidden', 'true') |
| 99 | + } |
| 100 | + |
| 101 | + // Swiper usually sets .swiper-slide-active correctly for integer or half |
| 102 | + // slidesPerView (e.g. 2, 2.5). With 'auto' or other fractions (e.g. 2.75), |
| 103 | + // the last slide may not get that class when reaching the end. |
| 104 | + // Keeping activeIndex in sync also keeps keyboard focus consistent: focusing |
| 105 | + // content in the last slide scrolls it fully into view instead of leaving it cropped. |
| 106 | + const applyLastSlideActiveFix = (swiper) => { |
| 107 | + // When all slides fit, Swiper keeps isBeginning and isEnd true while activeIndex stays 0. |
| 108 | + if (!swiper.isEnd || swiper.isBeginning) { |
| 109 | + return |
| 110 | + } |
| 111 | + |
| 112 | + // Force the index on the last slide |
| 113 | + swiper.activeIndex = swiper.slides.length - 1 |
| 114 | + // Ask Swiper to update the classes (.swiper-slide-active) |
| 115 | + swiper.updateSlidesClasses() |
| 116 | + } |
| 117 | + |
| 118 | + const mergedOptions = { |
| 119 | + ...options, |
| 120 | + modules: allModules, |
| 121 | + a11y: { |
| 122 | + ...(slidesAreListItems ? { slideRole: '' } : {}), |
| 123 | + ...options.a11y, |
| 124 | + }, |
| 125 | + on: { |
| 126 | + ...options.on, |
| 127 | + afterInit: function (swiper) { |
| 128 | + hideNavigationButtons(swiper) |
| 129 | + |
| 130 | + // Live region announces nav messages on keyboard prev/next; keep it when |
| 131 | + // navigation is exposed to AT, remove it when nav is intentionally hidden. |
| 132 | + if (hideNavigationFromA11y) { |
| 133 | + swiper.el.querySelector('.swiper-notification')?.remove() |
| 134 | + } |
| 135 | + |
| 136 | + options.on?.afterInit?.call(this, swiper) |
| 137 | + }, |
| 138 | + // Swiper resets tabindex on slide change, so we need to hide buttons again |
| 139 | + transitionEnd: function (swiper) { |
| 140 | + hideNavigationButtons(swiper) |
| 141 | + options.on?.transitionEnd?.call(this, swiper) |
| 142 | + }, |
| 143 | + progress: function (swiper, progress) { |
| 144 | + if (fixLastSlideActiveOnEnd) { |
| 145 | + applyLastSlideActiveFix(swiper) |
| 146 | + } |
| 147 | + |
| 148 | + options.on?.progress?.call(this, swiper, progress) |
| 149 | + }, |
| 150 | + slideChange: function (swiper) { |
| 151 | + if (fixLastSlideActiveOnEnd) { |
| 152 | + applyLastSlideActiveFix(swiper) |
| 153 | + } |
| 154 | + |
| 155 | + options.on?.slideChange?.call(this, swiper) |
| 156 | + }, |
| 157 | + }, |
| 158 | + } |
| 159 | + |
| 160 | + this.swiper = new Swiper(el, mergedOptions) |
| 161 | + |
| 162 | + this.swiper.on('slideChange', onChange.bind(this)) |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * @returns {import('swiper').Swiper} |
| 167 | + */ |
| 168 | + getInstance() { |
| 169 | + return this.swiper |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +/** |
| 174 | + * @typedef {Object} SliderOptions |
| 175 | + * @property {Function} [onChange] - Called on slideChange (`this` = Slider instance) |
| 176 | + * @property {boolean} [hideNavigationFromA11y=false] - Hide prev/next from AT when slides are already focusable; also removes `.swiper-notification` |
| 177 | + * @property {boolean} [fixLastSlideActiveOnEnd=false] - Fix `.swiper-slide-active` at the end with `slidesPerView: 'auto'` or fractional values other than halves (e.g. 2.75) |
| 178 | + * @property {import('swiper').SwiperOptions} [options] - Native Swiper options (merged with defaults) |
| 179 | + */ |
| 180 | + |
| 181 | +Slider.defaults = { |
| 182 | + onChange: noop, |
| 183 | + hideNavigationFromA11y: false, |
| 184 | + fixLastSlideActiveOnEnd: false, |
| 185 | + options: { |
| 186 | + modules: [A11y, Navigation], |
| 187 | + a11y: { |
| 188 | + prevSlideMessage: __('Previous slide', 'beapi-frontend-framework'), |
| 189 | + nextSlideMessage: __('Next slide', 'beapi-frontend-framework'), |
| 190 | + firstSlideMessage: __('This is the first slide', 'beapi-frontend-framework'), |
| 191 | + lastSlideMessage: __('This is the last slide', 'beapi-frontend-framework'), |
| 192 | + paginationBulletMessage: __('Go to slide {{index}}', 'beapi-frontend-framework'), |
| 193 | + slideLabelMessage: __('{{index}} of {{slidesLength}}', 'beapi-frontend-framework'), |
| 194 | + }, |
| 195 | + }, |
| 196 | +} |
0 commit comments