Skip to content

Commit

Permalink
fix: update accordion item heading tag to be customizable (nextui-org…
Browse files Browse the repository at this point in the history
…#2265)

* fix: update accordion item heading tag to be customizable

* Update .changeset/heavy-hairs-join.md

Co-authored-by: Junior Garcia <jrgarciadev@gmail.com>

* Update .changeset/heavy-hairs-join.md

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* chore(accordion): lint

* chore(changeset): add issue number

* feat(docs): add HeadingComponent prop

---------

Co-authored-by: Shawn Dong <shawn.dong@flybuys.com.au>
Co-authored-by: Junior Garcia <jrgarciadev@gmail.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: աɨռɢӄաօռɢ <wingkwong.code@gmail.com>
  • Loading branch information
5 people authored and Gaic4o committed May 22, 2024
1 parent 006d405 commit 6de3c72
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/heavy-hairs-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nextui-org/accordion": patch
---

Make the accordion item heading tag customizable to satisfy a11y needs. Headings on web pages need to be consistent and semantic; this will help all users better find the content they are looking for. (#2950)
31 changes: 16 additions & 15 deletions apps/docs/content/docs/components/accordion.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -219,21 +219,22 @@ Here's an example of how to customize the accordion styles:

### Accordion Item Props

| Attribute | Type | Description | Default |
| ------------------------- | ------------------------------------------------- | ----------------------------------------------------------------------------------------------------- | ------- |
| children | `ReactNode` \| `string` | The content of the component. | |
| title | `ReactNode` \| `string` | The accordion item title. | |
| subtitle | `ReactNode` \| `string` | The accordion item subtitle. | |
| indicator | [IndicatorProps](#accordion-item-indicator-props) | The accordion item `expanded` indicator, usually an arrow icon. | |
| startContent | `ReactNode` | The accordion item start content, usually an icon or avatar. | |
| motionProps | [MotionProps](#motion-props) | The props to modify the framer motion animation. Use the `variants` API to create your own animation. | |
| isCompact | `boolean` | Whether the AccordionItem is compact. | `false` |
| isDisabled | `boolean` | The current disabled status. | `false` |
| keepContentMounted | `boolean` | Whether the AccordionItem content is kept mounted when closed. | `false` |
| hideIndicator | `boolean` | Whether the AccordionItem indicator is hidden. | `false` |
| disableAnimation | `boolean` | Whether the AccordionItem animation is disabled. | `false` |
| disableIndicatorAnimation | `boolean` | Whether the AccordionItem indicator animation is disabled. | `false` |
| classNames | [Classnames](#accordion-item-classnames) | Allows to set custom class names for the accordion item slots. | - |
| Attribute | Type | Description | Default |
|---------------------------|---------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------|---------|
| children | `ReactNode` \| `string` | The content of the component. | |
| title | `ReactNode` \| `string` | The accordion item title. | |
| subtitle | `ReactNode` \| `string` | The accordion item subtitle. | |
| indicator | [IndicatorProps](#accordion-item-indicator-props) | The accordion item `expanded` indicator, usually an arrow icon. | |
| startContent | `ReactNode` | The accordion item start content, usually an icon or avatar. | |
| motionProps | [MotionProps](#motion-props) | The props to modify the framer motion animation. Use the `variants` API to create your own animation. | |
| isCompact | `boolean` | Whether the AccordionItem is compact. | `false` |
| isDisabled | `boolean` | The current disabled status. | `false` |
| keepContentMounted | `boolean` | Whether the AccordionItem content is kept mounted when closed. | `false` |
| hideIndicator | `boolean` | Whether the AccordionItem indicator is hidden. | `false` |
| disableAnimation | `boolean` | Whether the AccordionItem animation is disabled. | `false` |
| disableIndicatorAnimation | `boolean` | Whether the AccordionItem indicator animation is disabled. | `false` |
| HeadingComponent | `React.ElementType` | Customizable heading tag for Web accessibility. Use headings to describe content and use them consistently and semantically. | `h2` |
| classNames | [Classnames](#accordion-item-classnames) | Allows to set custom class names for the accordion item slots. | - |

### Accordion Item Events

Expand Down
5 changes: 3 additions & 2 deletions packages/components/accordion/src/accordion-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface AccordionItemProps extends UseAccordionItemProps {}
const AccordionItem = forwardRef<"button", AccordionItemProps>((props, ref) => {
const {
Component,
HeadingComponent,
classNames,
slots,
indicator,
Expand Down Expand Up @@ -89,7 +90,7 @@ const AccordionItem = forwardRef<"button", AccordionItemProps>((props, ref) => {

return (
<Component {...getBaseProps()}>
<h2 {...getHeadingProps()}>
<HeadingComponent {...getHeadingProps()}>
<button {...getButtonProps()}>
{startContent && (
<div className={slots.startContent({class: classNames?.startContent})}>
Expand All @@ -104,7 +105,7 @@ const AccordionItem = forwardRef<"button", AccordionItemProps>((props, ref) => {
<span {...getIndicatorProps()}>{indicatorComponent}</span>
)}
</button>
</h2>
</HeadingComponent>
{content}
</Component>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
SlotsToClasses,
} from "@nextui-org/theme";

import {As} from "@nextui-org/system";
import {ItemProps, BaseItem} from "@nextui-org/aria-utils";
import {FocusableProps, PressEvents} from "@react-types/shared";
import {ReactNode, MouseEventHandler} from "react";
Expand Down Expand Up @@ -85,6 +86,12 @@ export interface Props<T extends object = {}>
* ```
*/
classNames?: SlotsToClasses<AccordionItemSlots>;
/**
* Customizable heading tag for Web accessibility:
* use headings to describe content and use them consistently and semantically.
* This will help all users to better find the content they are looking for.
*/
HeadingComponent?: As;
}

export type AccordionItemBaseProps<T extends object = {}> = Props<T> & AccordionItemVariantProps;
Expand Down
2 changes: 2 additions & 0 deletions packages/components/accordion/src/use-accordion-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export function useAccordionItem<T extends object = {}>(props: UseAccordionItemP
disableAnimation = false,
keepContentMounted = false,
disableIndicatorAnimation = false,
HeadingComponent = as || "h2",
onPress,
onPressStart,
onPressEnd,
Expand Down Expand Up @@ -237,6 +238,7 @@ export function useAccordionItem<T extends object = {}>(props: UseAccordionItemP

return {
Component,
HeadingComponent,
item,
slots,
classNames,
Expand Down

0 comments on commit 6de3c72

Please sign in to comment.