/** * (c) 2026, Micro:bit Educational Foundation and contributors * * SPDX-License-Identifier: MIT */ import { useMediaQuery } from "@microbit/ui"; import { css, cx } from "styled-system/css"; import React, { ReactElement, useCallback, useEffect, useRef } from "react"; import { useLocale } from "react-aria"; import { useIntl } from "react-intl"; import { Swiper as SwiperClass } from "swiper"; import "./swiper.css"; import { A11y } from "swiper/modules"; import { Swiper, SwiperProps, SwiperSlide } from "swiper/react"; import { carouselMessage } from "./messages"; import SwiperCarouselButtons from "./SwiperCarouselButtons"; export interface SwiperCarouselProps extends SwiperProps { carouselItems: ReactElement[]; /** * Accessible name for the carousel, already translated. Supply this or an * `aria-labelledby` (forwarded to the root element by Swiper). */ containerLabel?: string; /** Extra classes for each slide (e.g. sizing — see Carousel). */ slideClassName?: string; /** Extra classes for the root (e.g. a `css(...)` result from the caller). */ className?: string; } const swiperModules = [A11y]; /** * Thin shared wrapper over Swiper: APG carousel semantics, translated ARIA * annotations, focus-follows-slide, and our edge-pinned prev/next buttons * (enable with `navigation`). Layout decisions — breakpoints, slide sizing, * padding — stay with the caller; see `Carousel` for the standard card row. */ const SwiperCarousel = ({ carouselItems, containerLabel, navigation, slideClassName, className, speed, ...props }: SwiperCarouselProps) => { const intl = useIntl(); const { direction } = useLocale(); const reducedMotion = useMediaQuery("(prefers-reduced-motion: reduce)"); const swiperRef = useRef(undefined); const pointerModality = useRef(false); useEffect(() => { const onPointerDown = () => { pointerModality.current = true; }; const onKeyDown = () => { pointerModality.current = false; }; window.addEventListener("pointerdown", onPointerDown, true); window.addEventListener("keydown", onKeyDown, true); return () => { window.removeEventListener("pointerdown", onPointerDown, true); window.removeEventListener("keydown", onKeyDown, true); }; }, []); const handleSlideFocus = useCallback( (e: React.FocusEvent) => { const swiper = swiperRef.current; if (swiper && !swiper.destroyed) { // Undo the browser's native scroll-focused-element-into-view: // Swiper positions by transform, so a real scrollLeft on the // overflow-hidden container desyncs the view. swiper.el.scrollLeft = 0; swiper.slides.forEach((slide, i) => { if ( slide.contains(e.target) && !pointerModality.current && !slide.classList.contains("swiper-slide-fully-visible") ) { swiper.activeIndex = i; swiper.updateSlidesClasses(); swiper.slideTo(i); } }); } }, [], ); const handleSwiper = useCallback((swiper: SwiperClass) => { swiperRef.current = swiper; swiper.update(); }, []); // Swiper reads the direction from the DOM only at init, so on a live // locale switch its translate maths run backwards against the flipped // layout (drag clamps early, buttons move the wrong way). The `dir` prop // below only covers first render; this is the official API for changes // after it, and a no-op when the direction already matches. useEffect(() => { const swiper = swiperRef.current; if (swiper && !swiper.destroyed) { swiper.changeLanguageDirection(direction); } }, [direction]); return (
{navigation && } {carouselItems.map((item) => ( {item} ))}
); }; export default SwiperCarousel;