Skip to content

Commit

Permalink
add leadingActionId and aria-hidden to LeadingAction subcomponent
Browse files Browse the repository at this point in the history
  • Loading branch information
ayy-bc committed May 9, 2024
1 parent 770e321 commit f9b880d
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions packages/react/src/TreeView/TreeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const ItemContext = React.createContext<{
setIsExpanded: (isExpanded: boolean) => void
leadingVisualId: string
trailingVisualId: string
leadingActionId: string
}>({
itemId: '',
level: 1,
Expand All @@ -54,6 +55,7 @@ const ItemContext = React.createContext<{
setIsExpanded: () => {},
leadingVisualId: '',
trailingVisualId: '',
leadingActionId: '',
})

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -374,6 +376,7 @@ const Item = React.forwardRef<HTMLElement, TreeViewItemProps>(
const labelId = useId()
const leadingVisualId = useId()
const trailingVisualId = useId()
const leadingActionId = useId()
const [isExpanded, setIsExpanded] = useControllableState({
name: itemId,
// If the item was previously mounted, it's expanded state might be cached.
Expand Down Expand Up @@ -449,6 +452,7 @@ const Item = React.forwardRef<HTMLElement, TreeViewItemProps>(
setIsExpanded: setIsExpandedWithCache,
leadingVisualId,
trailingVisualId,
leadingActionId,
}}
>
{/* @ts-ignore Box doesn't have type support for `ref` used in combination with `as` */}
Expand All @@ -459,7 +463,7 @@ const Item = React.forwardRef<HTMLElement, TreeViewItemProps>(
id={itemId}
role="treeitem"
aria-labelledby={labelId}
aria-describedby={`${leadingVisualId} ${trailingVisualId}`}
aria-describedby={`${leadingActionId} ${leadingVisualId} ${trailingVisualId}`}
aria-level={level}
aria-expanded={isSubTreeEmpty ? undefined : isExpanded}
aria-current={isCurrentItem ? 'true' : undefined}
Expand Down Expand Up @@ -850,14 +854,16 @@ TrailingVisual.displayName = 'TreeView.TrailingVisual'
// TreeView.LeadingAction

const LeadingAction: React.FC<TreeViewVisualProps> = props => {
const {isExpanded, leadingVisualId} = React.useContext(ItemContext)
const {isExpanded, leadingActionId} = React.useContext(ItemContext)
const children = typeof props.children === 'function' ? props.children({isExpanded}) : props.children
return (
<>
<div className="PRIVATE_VisuallyHidden" aria-hidden={true} id={leadingVisualId}>
<div className="PRIVATE_VisuallyHidden" aria-hidden={true} id={leadingActionId}>
{props.label}
</div>
<div className="PRIVATE_TreeView-item-leading-action">{children}</div>
<div className="PRIVATE_TreeView-item-leading-action" aria-hidden={true}>
{children}
</div>
</>
)
}
Expand Down

0 comments on commit f9b880d

Please sign in to comment.