Skip to content

Commit f9e321f

Browse files
committed
feat(Slider): integrate base Swiper Class for enhanced slide navigation and accessibility
- Added Swiper library for improved slider functionality. - Created a new Slider class to manage Swiper instances with accessibility features. - Implemented custom controls for previous and next slide navigation. - Updated package dependencies to include Swiper and WordPress i18n for translations. - Added French translations for slider navigation messages. - Introduced SCSS styles for Swiper controls and integrated them into the main stylesheets.
1 parent 1cfba36 commit f9e321f

11 files changed

Lines changed: 353 additions & 5 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/**
4+
* Swiper Controls
5+
* Swiper add/replace automatically the aria-label, the role, the icon on the buttons
6+
*
7+
* @var array $args
8+
*/
9+
10+
?>
11+
12+
<div class="swiper__controls">
13+
<div class="swiper__buttons">
14+
<div class="swiper-button-prev swiper__button" role="button" aria-label="Diapositive précédente"></div>
15+
<div class="swiper-button-next swiper__button" role="button" aria-label="Diapositive suivante"></div>
16+
</div>
17+
</div>

‎inc/Services/Assets.php‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function register_assets(): void {
6464
$this->assets_tools->register_script(
6565
'scripts',
6666
'dist/' . $file,
67-
array_merge( [ 'jquery' ], $asset_data['dependencies'] ), // ensure jQuery dependency is set even if not declared explicitly in the JS
67+
array_merge( [ 'jquery', 'wp-i18n' ], $asset_data['dependencies'] ), // ensure jQuery dependency is set even if not declared explicitly in the JS
6868
$asset_data['version'],
6969
[ 'strategy' => 'defer' ]
7070
);
@@ -88,6 +88,12 @@ public function register_assets(): void {
8888
public function enqueue_scripts(): void {
8989
// JavaScript
9090
$this->assets_tools->enqueue_script( 'scripts' );
91+
92+
wp_set_script_translations(
93+
'scripts',
94+
'beapi-frontend-framework',
95+
\get_theme_file_path( 'languages' )
96+
);
9197
}
9298

9399
/**
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"translation-revision-date":"2026-05-07 16:41+0200","generator":"WP-CLI\/2.12.0","source":"dist\/app.js","domain":"messages","locale_data":{"messages":{"":{"domain":"messages","lang":"fr","plural-forms":"nplurals=2; plural=(n > 1);"},"Previous slide":["Diapositive pr\u00e9c\u00e9dente"],"Next slide":["Diapositive suivante"],"This is the first slide":["C'est la premi\u00e8re diapositive"],"This is the last slide":["C'est la derni\u00e8re diapositive"],"Go to slide {{index}}":["Aller \u00e0 la diapositive {{index}}"],"{{index}} of {{slidesLength}}":["{{index}} sur {{slidesLength}}"]}}}

‎languages/beapi-frontend-framework.pot‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,3 +258,27 @@ msgstr ""
258258
msgctxt "Space size name"
259259
msgid "lg (64px)"
260260
msgstr ""
261+
262+
#: dist/app.js
263+
msgid "Previous slide"
264+
msgstr ""
265+
266+
#: dist/app.js
267+
msgid "Next slide"
268+
msgstr ""
269+
270+
#: dist/app.js
271+
msgid "This is the first slide"
272+
msgstr ""
273+
274+
#: dist/app.js
275+
msgid "This is the last slide"
276+
msgstr ""
277+
278+
#: dist/app.js
279+
msgid "Go to slide {{index}}"
280+
msgstr ""
281+
282+
#: dist/app.js
283+
msgid "{{index}} of {{slidesLength}}"
284+
msgstr ""

‎languages/fr_FR.po‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,27 @@ msgstr ""
316316

317317
#~ msgid "Toggle menu"
318318
#~ msgstr "Ouvrir/Fermer le menu"
319+
320+
#: dist/app.js
321+
msgid "Previous slide"
322+
msgstr "Diapositive précédente"
323+
324+
#: dist/app.js
325+
msgid "Next slide"
326+
msgstr "Diapositive suivante"
327+
328+
#: dist/app.js
329+
msgid "This is the first slide"
330+
msgstr "C'est la première diapositive"
331+
332+
#: dist/app.js
333+
msgid "This is the last slide"
334+
msgstr "C'est la dernière diapositive"
335+
336+
#: dist/app.js
337+
msgid "Go to slide {{index}}"
338+
msgstr "Aller à la diapositive {{index}}"
339+
340+
#: dist/app.js
341+
msgid "{{index}} of {{slidesLength}}"
342+
msgstr "{{index}} sur {{slidesLength}}"

‎package-lock.json‎

Lines changed: 22 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@
1919
"@fontsource-utils/scss": "^0.2.4",
2020
"@fontsource/poppins": "^5.3.0",
2121
"lazysizes": "^5.3.2",
22-
"oneloop.js": "^5.3.1"
22+
"oneloop.js": "^5.3.1",
23+
"swiper": "^14.3.0"
2324
},
2425
"devDependencies": {
2526
"@wordpress/blocks": "^11.21.0",
2627
"@wordpress/browserslist-config": "^6.53.0",
2728
"@wordpress/dependency-extraction-webpack-plugin": "^6.53.0",
2829
"@wordpress/dom-ready": "^3.58.0",
30+
"@wordpress/i18n": "^4.58.0",
2931
"@wordpress/hooks": "^3.58.0",
3032
"@wordpress/stylelint-config": "^24.2.0",
3133
"clean-webpack-plugin": "^4.0.0",

‎src/js/classes/Slider.js‎

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
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: (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?.(swiper)
137+
},
138+
// Swiper resets tabindex on slide change, so we need to hide buttons again
139+
transitionEnd: (swiper) => {
140+
hideNavigationButtons(swiper)
141+
options.on?.transitionEnd?.(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

Comments
 (0)