Skip to content

feat: accessibility improvements #363

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

jon-cullison
Copy link

@jon-cullison jon-cullison commented Apr 10, 2025

Implements 2/3 of the proposed changes here:
ant-design/ant-design#53203

I did not implement the panel role change because there is already some logic around setting the panel role to "tabpanel" when in accordion mode, and the panel role = "region" is optional for a11y purposes.

  • Adds optional headingLevel prop which will wrap the collapse header in a header element
  • Adds a default id generated by uuid to the collapse which will be used to create ids for the child components in order to link the header and panel.
  • Adds optional id prop which will override the default id mentioned above

Summary by CodeRabbit

  • Style

    • 优化折叠组件头部样式,确保标准与禁用状态下显示统一。
  • 新功能

    • 引入自动生成唯一标识符功能,支持自定义 ID,提升组件稳定性与无障碍体验。
    • 增加了设置标题层级的选项,使显示层次更灵活友好。
  • Tests

    • 扩展了测试覆盖,验证 ID 与标题层级功能的正确性,从而提升了整体可靠性。

Copy link

vercel bot commented Apr 10, 2025

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

Name Status Preview Comments Updated (UTC)
collapse ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 7, 2025 1:15pm

Copy link

coderabbitai bot commented Apr 10, 2025

Walkthrough

此次变更主要包含对 Collapse 组件及其子组件(Panel、PanelContent)的增强改造,同时对样式文件和测试用例进行了相应更新。CSS 中扩展了 header 的选择器以支持包裹结构;组件中新增了通过 useId 自动生成 id 的逻辑,并引入了 headingLevel 属性用于辅助设置无障碍相关属性;接口定义中添加了新的 HeadingLevelType 类型;测试用例也更新以覆盖新增 id 与 headingLevel 的功能。

Changes

文件(s) 变更摘要
assets/index.less 扩展 header 选择器,新增 @{prefixCls}-header-wrapper > .@{prefixCls}-header 样式,统一应用常规及禁用状态下的样式。
src/Collapse.tsx
src/Panel.tsx
src/PanelContent.tsx
src/hooks/…/useItems.tsx
在组件中新增 id 自动生成(基于 useId)和 headingLevel 属性,更新属性传递及无障碍相关的 aria 属性设置。
src/interface.ts 新增 HeadingLevelType 类型(1~6),并在 CollapseProps 与 CollapsePanelProps 中添加 headingLevel 和(CollapseProps 的)id 属性。
tests/index.spec.tsx 更新测试用例,新增针对 id 与 headingLevel 的测试,并重构部分测试逻辑以提升可读性。

Sequence Diagram(s)

sequenceDiagram
    participant U as 用户
    participant C as Collapse组件
    participant H as useItems钩子
    participant P as Panel组件
    participant W as HeaderWrapper

    U->>C: 请求渲染 Collapse
    C->>C: 检查是否提供 id(无则使用 useId 生成)
    C->>H: 传递 id 与 headingLevel
    H->>P: 将 id(可能附加索引)和 headingLevel 传给各 Panel
    P->>W: 根据 headingLevel 包装 header
    W-->>P: 返回处理后的 header
Loading

Poem

我是一只快乐的小兔子,
在代码森林里欢快奔跑,
ID 与标题层级轻轻织就,
无障碍世界更加美妙,
修改微小如花开一瞬,
让每行代码闪耀着光芒!
🥕 跟我一同庆祝这新希望!

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.

src/Collapse.tsx

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the config "prettier" to extend from. Please check that the name of the config is correct.

The config "prettier" was referenced from the config file in "/.eslintrc.js".

If you still have problems, please stop by https://eslint.org/chat/help to chat with the team.


📜 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 089346c and af2984e.

📒 Files selected for processing (1)
  • src/Collapse.tsx (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/Collapse.tsx
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @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.

src/Collapse.tsx Outdated
@@ -6,6 +6,7 @@ import useItems from './hooks/useItems';
import type { CollapseProps } from './interface';
import CollapsePanel from './Panel';
import pickAttrs from '@rc-component/util/lib/pickAttrs';
import { v4 as uuid4 } from 'uuid';
Copy link
Member

Choose a reason for hiding this comment

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

Can we try not to rely on additional third-party packages?

Copy link
Author

Choose a reason for hiding this comment

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

We do not need to add this - the purpose was to create a default ID that is unique across multiple react applications that are rendered at the same time. However, if a consumer needs that, they can pass in an ID so I will go ahead and remove this package.

@jon-cullison
Copy link
Author

@yoyo837 I removed the uuid package, ready for re-review.

@@ -71,6 +80,8 @@ const convertItemsToNodes = (items: ItemType[], props: Props) => {
collapsible={mergeCollapsible}
onItemClick={handleItemClick}
destroyInactivePanel={mergeDestroyInactivePanel}
headingLevel={headingLevel}
id={id ? `${id}__item-${index}` : undefined}
Copy link
Member

Choose a reason for hiding this comment

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

You can use rc-util useId hooks in Panel. It support to use user pass id and fallback with ReactID if not have.

</div>
{showArrow && iconNode}
<span
className={classNames(`${prefixCls}-title`, customizeClassNames?.title)}
Copy link
Member

@zombieJ zombieJ Apr 23, 2025

Choose a reason for hiding this comment

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

replace this with HeadingElement instead of span directly. It's no need to have additional HeaderWrapper.

Copy link
Author

@jon-cullison jon-cullison Apr 29, 2025

Choose a reason for hiding this comment

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

I can change HeaderWrapper, but replacing the span would not satisfy the a11y standards for collapse / accordion components. The heading element should wrap the header button, which is where HeaderWrapper is in this PR. Would you prefer it to be a div that is always present around the button, instead of conditionally rendered, but it only would have the heading role and aria-level when the prop is defined?

Copy link
Member

Choose a reason for hiding this comment

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

OK. So you can add on the rc-collapse-header directly.

@afc163
Copy link
Member

afc163 commented Apr 23, 2025

Trace #362

@@ -77,6 +81,8 @@ const Collapse = React.forwardRef<HTMLDivElement, CollapseProps>((props, ref) =>
activeKey,
classNames: customizeClassNames,
styles,
headingLevel,
id: collapseId,
Copy link
Member

Choose a reason for hiding this comment

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

this is not id but parentId

@@ -85,20 +88,38 @@ const CollapsePanel = React.forwardRef<HTMLDivElement, CollapsePanelProps>((prop
...(['header', 'icon'].includes(collapsible) ? {} : collapsibleProps),
};

const HeaderWrapper = ({ children: headerWrapperChildren }: PropsWithChildren) => {
Copy link
Member

@zombieJ zombieJ May 16, 2025

Choose a reason for hiding this comment

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

HeaderWrapper not satisfied with our semantic rule. You can just assign the heading role on the rc-collapse-title element instead of create new element.

https://github.com/react-component/collapse/pull/363/files#r2092418393

@@ -142,6 +155,8 @@ const getNewChild = (
onItemClick: handleItemClick,
expandIcon,
collapsible: mergeCollapsible,
headingLevel,
id: id ? `${id}__item-${index}` : undefined,
Copy link
Member

Choose a reason for hiding this comment

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

use key instead of index

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.

4 participants