@bluecadet/embla-carousel-a11y

中文文档
0.1.2发布于 2025/04/18MITTypeScriptESM + CJS零依赖近 30 天下载 2937

A11y Plugin for Embla Carousel

原始使用文档
文档目录14 节
  1. Features
  2. Installation
  3. Basic Usage
  4. API Reference
  5. Options
  6. Plugin Methods
  7. Accessibility Features
  8. Proper ARIA Roles and Attributes
  9. Live Region for Announcements
  10. Respecting User Attributes
  11. Development Warnings
  12. Best Practices for Accessible Carousels
  13. Why Accessibility Matters
  14. Contributing

Embla Carousel A11y Plugin

An accessibility plugin for Embla Carousel that follows WAI-ARIA Authoring Practices for carousels.

Features

  • Adds proper ARIA roles and attributes to carousel elements
  • Creates an accessible live region to announce slide changes
  • Respects user-provided ARIA attributes
  • Shows helpful development warnings for accessibility improvements

Installation

# npm
npm install embla-carousel @bluecadet/embla-carousel-a11y

# yarn
yarn add embla-carousel @bluecadet/embla-carousel-a11y

# pnpm
pnpm add embla-carousel @bluecadet/embla-carousel-a11y

Basic Usage

import A11y from "@bluecadet/embla-carousel-a11y";
import EmblaCarousel from "embla-carousel";

// Initialize Embla with the A11y plugin
const emblaNode = document.querySelector(".embla");
const embla = EmblaCarousel(emblaNode, { loop: true }, [A11y()]);

// The plugin is accessible through embla.plugins().a11y
const a11yPlugin = embla.plugins().a11y;

API Reference

Options

The A11y plugin accepts the following options:

OptionTypeDefaultDescription
carouselLabelstring | undefinedundefinedA custom label for the carousel. If not provided, uses an existing aria-label or sets a default.
slideSelectorstring | undefinedundefinedCustom selector for slide elements. If not provided, uses Embla's default slide nodes.
announceSlideChangesbooleantrueWhether to announce slide changes to screen readers.
slideAnnouncementTemplatestring"Slide {{currentSlide}} of {{totalSlides}}"Template for slide change announcements. Use {{currentSlide}} and {{totalSlides}} as variables.
showDeveloperWarningsbooleantrueWhether to show developer console warnings in non-production environments.

Plugin Methods

The A11y plugin exposes the following methods:

MethodDescription
update()Updates ARIA attributes based on the current carousel state. Called automatically when the selected slide changes.

Accessibility Features

This plugin implements the following accessibility features:

Proper ARIA Roles and Attributes

  • Adds role="region" to the carousel container
  • Adds aria-roledescription="carousel" to identify it as a carousel
  • Adds proper aria-label (or respects existing labels)
  • Adds role="group" and aria-roledescription="slide" to slides
  • Applies aria-hidden="true" to non-visible slides
  • Sets aria-setsize and aria-posinset for proper slide position information

Live Region for Announcements

  • Creates a visually hidden live region for screen reader announcements
  • Announces the current slide position when navigation occurs
  • Uses customizable announcement templates

Respecting User Attributes

  • Detects existing ARIA attributes provided by the user before initialization
  • Preserves these attributes instead of overriding them
  • Only updates dynamic attributes like aria-hidden as needed

Development Warnings

In development mode, the plugin provides helpful console warnings for accessibility improvements:

  • Warns when no accessible name (label) is provided for the carousel
  • Suggests adding descriptive labels to slides
  • Can be disabled in production or via options

Best Practices for Accessible Carousels

For maximum accessibility, we recommend:

  1. Provide meaningful labels: Use the carouselLabel option or add aria-label attributes to your carousel and slides with descriptive content.
<div class="embla" aria-label="Featured products">
  <div class="embla__container">
    <div class="embla__slide" aria-label="Red summer dress, $49.99">...</div>
    <div class="embla__slide" aria-label="Blue winter coat, $89.99">...</div>
  </div>
</div>
  1. Add accessible navigation controls: Include Previous/Next buttons with proper labels.
<button class="embla__prev" aria-label="Previous slide">Prev</button>
<div class="embla">...</div>
<button class="embla__next" aria-label="Next slide">Next</button>
  1. Pause autoplay on interaction: If using autoplay, pause it when users interact with the carousel.
const options = {
  loop: true,
  autoplay: true,
};

const embla = EmblaCarousel(emblaNode, options, [A11y()]);

// Pause autoplay on focus within the carousel
emblaNode.addEventListener("focusin", () => {
  embla.plugins().autoplay.stop();
});

// Resume when focus leaves
emblaNode.addEventListener("focusout", (event) => {
  if (!emblaNode.contains(event.relatedTarget)) {
    embla.plugins().autoplay.play();
  }
});

Why Accessibility Matters

Making your carousel accessible ensures that:

  • Users with screen readers can understand and navigate your content
  • Keyboard-only users can access all carousel features
  • Your site complies with accessibility standards like WCAG
  • You reach a wider audience, including people with disabilities

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

版本与发布记录

@bluecadet/embla-carousel-a11y 共发布 3 个版本,首次发布于 2025/04/02;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.1.2latest2025/04/181.6 年前
0.1.12025/04/181.6 年前
0.1.02025/04/021.6 年前

关于 @bluecadet/embla-carousel-a11y 的常见问题

@bluecadet/embla-carousel-a11y 是什么?

A11y Plugin for Embla Carousel

如何安装 @bluecadet/embla-carousel-a11y?

在项目目录运行 npm install @bluecadet/embla-carousel-a11y,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:embla-carousel(^8.5.2)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@bluecadet/embla-carousel-a11y 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@bluecadet/embla-carousel-a11y 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@bluecadet/embla-carousel-a11y 可以商用吗?

@bluecadet/embla-carousel-a11y 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@bluecadet/embla-carousel-a11y 的下载量有多少?

根据 npm 官方下载统计,@bluecadet/embla-carousel-a11y 在 2026/10/09 – 2026/11/07 这 30 天内被下载 2,937 次,日均约 98 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bluecadet/embla-carousel-a11y 还在维护吗?

最新版本 0.1.2 发布于 2025/04/18(1.6 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2025/04/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。