Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(a11y): add screen reader support for Tooltip #490

Merged
merged 17 commits into from
Jan 7, 2025

Conversation

aojunhao123
Copy link
Contributor

@aojunhao123 aojunhao123 commented Jan 2, 2025

Summary by CodeRabbit

  • 依赖项更新

    • package.json 中添加了 rc-util 依赖项(版本 ^5.44.3)
  • 新功能

    • 改进了 Tooltip 组件的无障碍性
    • 为 Tooltip 添加了唯一 ID 生成和管理功能
  • 测试

    • 新增测试用例,验证 Tooltip 组件的子元素处理
    • 检查 aria-describedby 属性的正确性
    • 确保保留原始子元素属性

Copy link

vercel bot commented Jan 2, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
tooltip ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 6, 2025 10:11am

Copy link

coderabbitai bot commented Jan 2, 2025

Warning

There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure.

🔧 eslint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

tests/index.test.tsx

Oops! Something went wrong! :(

ESLint: 8.57.1

Error: Cannot read config file: /.eslintrc.js
Error: Cannot find module '@umijs/fabric/dist/eslint'
Require stack:

  • /.eslintrc.js
  • /node_modules/.pnpm/@eslint[email protected]/node_modules/@eslint/eslintrc/dist/eslintrc.cjs
  • /node_modules/.pnpm/[email protected]/node_modules/eslint/lib/cli-engine/cli-engine.js
  • /node_modules/.pnpm/[email protected]/node_modules/eslint/lib/eslint/eslint.js
  • /node_modules/.pnpm/[email protected]/node_modules/eslint/lib/eslint/index.js
  • /node_modules/.pnpm/[email protected]/node_modules/eslint/lib/cli.js
  • /node_modules/.pnpm/[email protected]/node_modules/eslint/bin/eslint.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1248:15)
    at Module._load (node:internal/modules/cjs/loader:1074:27)
    at TracingChannel.traceSync (node:diagnostics_channel:315:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:217:24)
    at Module.require (node:internal/modules/cjs/loader:1339:12)
    at require (node:internal/modules/helpers:135:16)
    at Object. (/.eslintrc.js:1:14)
    at Module._compile (node:internal/modules/cjs/loader:1546:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1691:10)
    at Module.load (node:internal/modules/cjs/loader:1317:32)

概述

演练

此次更改主要关注改进 Tooltip 组件的无障碍性能。通过添加 rc-util 依赖,引入 useId 钩子来生成唯一 ID,并修改 Tooltip 组件以自动为子元素添加 aria-describedby 属性。测试套件也相应地扩展,以验证新的无障碍特性。

变更

文件 变更摘要
package.json 新增 rc-util: ^5.44.3 依赖
src/Tooltip.tsx 引入 useId 钩子,添加 getChildren 函数处理无障碍属性
tests/index.test.tsx 新增无障碍性和子元素处理的测试用例

对链接问题的评估

目标 已解决 说明
屏幕阅读器可以宣布工具提示内容 [#49891]
键盘焦点触发工具提示 [#49891] 需要进一步验证焦点触发机制
添加 aria-describedby 属性 [#49891]

诗歌

🐰 小兔子跳跃,代码无障碍
工具提示闪亮,可访问性来袭
ID 生成,属性加持
键盘用户笑开怀
无障碍,代码有爱!


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a378f1a and ae4519f.

📒 Files selected for processing (1)
  • tests/index.test.tsx (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/index.test.tsx

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ea549a3 and 241527e.

📒 Files selected for processing (6)
  • jest.config.js (1 hunks)
  • package.json (2 hunks)
  • src/Popup.tsx (1 hunks)
  • src/Tooltip.tsx (5 hunks)
  • tests/popup.test.tsx (1 hunks)
  • tests/setup.js (1 hunks)
✅ Files skipped from review due to trivial changes (2)
  • jest.config.js
  • tests/setup.js
🔇 Additional comments (21)
src/Popup.tsx (3)

38-38: 使用辅助函数统一提取文本内容的思路良好。

tooltipText 会根据 children 中的多种类型提取并拼接文本,为屏幕阅读器提供完整的辅助文本,逻辑清晰。


41-48: 分层结构可读性高,便于定制。

这里将包裹内容独立于外层 <div> 与内部 <div>,为后续样式或行为调整提供了较好的扩展空间。


50-59: 通过视觉隐藏方式呈现文本,兼顾辅助技术需求。

采用 role="tooltip" 并使用绝对定位且 opacity: 0 来隐藏元素,符合常见的可访问性模式,使屏幕阅读器能够识别并朗读对应文本。

tests/popup.test.tsx (7)

1-3: 测试环境配置完整,引入的库满足组件测试需求。

导入 @testing-library/react 和自定义的 Popup 模块后,即可方便地对组件进行渲染和断言操作。


10-12: 对 Tooltip 导出的基础测试用例简洁易读。

该用例验证组件是否可成功导出,测试覆盖面基础但必要。


30-47: 测试隐藏样式的断言准确,确保可访问性样式不被破坏。

通过 toHaveStyle 断言检查隐形容器的样式配置是否符合预期,能够及早发现样式回归问题。


49-58: 空内容场景测试覆盖周全。

验证 nullundefined 情况时,不渲染 tooltip 内容,能避免空引用导致的潜在错误。


60-75: 嵌套数组文本拼接的逻辑测试全面。

测试通过多层数组结构以验证文本是否正确拼接,可防止意外的空格或缺失。


77-88: 函数返回数组场景测试,增强鲁棒性。

确保函数节点同样能返回数组并正确生成文本,加深对多类型子元素的支持。


90-99: 对函数返回 undefined 情况的测试可避免意外报错。

若函数式子元素不产生实际内容,可使 tooltip 安静地退出以免出现浏览器渲染问题。

src/Tooltip.tsx (8)

4-4: 引入 classNames 便于动态切换样式。

通过 classnames 库可简化条件渲染类名的逻辑,增加可读性与维护性。


6-6: 使用解构方式导入 React 钩子,让组件结构更清晰。

这种写法可快速辨别用到哪些原生 Hooks,便于后续维护。


9-9: 通过 rc-util/lib/hooks/useId 生成唯一 ID,提升组件可访问性。

它能在 id 未指定时自动提供唯一标识,避免多个 Tooltip 重复 ID 引发的可访问性冲突。


64-64: 空接口扩展保留,方便后续增加新属性。

TooltipRef 中保持扩展空间,可随时补充其他方法或字段。


67-67: useId 提供默认值的方式可简化配置流程。

若用户不显式传入 id,也可保证生成可访问性所需的关联属性。


90-90: 在解构参数时优先保留默认值,有助于避免空值带来的逻辑错误。

id = defaultId 让组件在使用时更具语义化和安全性。


117-126: getChildren 函数封装增强重复逻辑复用性。

通过克隆子元素并追加 aria-describedby 属性,可减少在其他地方重复编写相同行为的可能性。


152-152: 通过调用 getChildren() 注入可访问性属性,API 设计干净简洁。

清晰地在源码中表明 children 的二次处理逻辑,易于维护和扩展。

package.json (3)

46-47: 新增依赖优化现有功能及提升兼容性。

"classnames" 已广泛使用;"rc-util" 提供必要的工具函数,为项目整合更多通用能力。


51-53: 新增测试库依赖丰富测试场景,增强可测试性。

引入 @testing-library/jest-dom@testing-library/user-event,可实现更全面的交互式测试,对可访问性及动态交互的测试覆盖度更高。


75-75: 配置文件结构完整,更新后仍确保兼容性。

保留原有字段与脚本的同时,新增依赖无缝对接。

src/Popup.tsx Outdated Show resolved Hide resolved
jest.config.js Outdated Show resolved Hide resolved
jest.config.js Outdated Show resolved Hide resolved
src/Popup.tsx Outdated Show resolved Hide resolved
src/Popup.tsx Outdated Show resolved Hide resolved
src/Tooltip.tsx Outdated Show resolved Hide resolved
Copy link

codecov bot commented Jan 2, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (ea549a3) to head (ae4519f).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##            master      #490   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            3         3           
  Lines           26        33    +7     
  Branches        10        12    +2     
=========================================
+ Hits            26        33    +7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

src/Popup.tsx Outdated Show resolved Hide resolved
src/Tooltip.tsx Outdated Show resolved Hide resolved
src/Tooltip.tsx Outdated Show resolved Hide resolved
tests/setup.js Outdated Show resolved Hide resolved
@zombieJ zombieJ merged commit 759180e into react-component:master Jan 7, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tooltip component accessibility is lacking
3 participants