Skip to content

Commit

Permalink
TreeView: Add aria-label to TreeView subtree (#5174)
Browse files Browse the repository at this point in the history
* Add `aria-label` to `TreeView` subtree

* Add changeset

* Add name to `SubTree` via `aria-label`

* test fix

---------

Co-authored-by: Hussam Ghazzi <hussam-i-am@github.com>
  • Loading branch information
TylerJDev and hussam-i-am authored Jan 28, 2025
1 parent 3da9af9 commit cf80bf2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-mirrors-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': minor
---

TreeView: Adds `aria-label` prop to `TreeView.Subtree`
5 changes: 5 additions & 0 deletions packages/react/src/TreeView/TreeView.docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@
"name": "count",
"type": "number",
"description": "The number of items expected to be in the subtree. When in the loading state, the subtree will render a skeleton loading placeholder with the specified count of items"
},
{
"name": "aria-label",
"type": "string",
"description": "An accessible name for the subtree. It is recommended that you provide a short version of the parent list item's name as the name of the subtree."
}
]
},
Expand Down
6 changes: 3 additions & 3 deletions packages/react/src/TreeView/TreeView.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ describe('Markup', () => {
})

it('should render with containIntrinsicSize', () => {
const {getByLabelText} = renderWithTheme(
const {getByText} = renderWithTheme(
<TreeView aria-label="Test tree">
<TreeView.Item id="parent" containIntrinsicSize="2rem" defaultExpanded>
Parent
Expand All @@ -253,9 +253,9 @@ describe('Markup', () => {

// The test runner removes the contain-intrinsic-size and content-visibility
// properties, so we can only test that the elements are still rendering.
const childItem = getByLabelText(/Child/)
const childItem = getByText(/Child/)
expect(childItem).toBeInTheDocument()
const parentItem = getByLabelText(/Parent/)
const parentItem = getByText(/Parent/)
expect(parentItem).toBeInTheDocument()
})

Expand Down
7 changes: 6 additions & 1 deletion packages/react/src/TreeView/TreeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -647,14 +647,16 @@ export type TreeViewSubTreeProps = {
* Display a skeleton loading state with the specified count of items
*/
count?: number
'aria-label'?: string
}

const SubTree: React.FC<TreeViewSubTreeProps> = ({count, state, children}) => {
const SubTree: React.FC<TreeViewSubTreeProps> = ({count, state, children, 'aria-label': ariaLabel}) => {
const {announceUpdate} = React.useContext(RootContext)
const {itemId, isExpanded, isSubTreeEmpty, setIsSubTreeEmpty} = React.useContext(ItemContext)
const loadingItemRef = React.useRef<HTMLElement>(null)
const ref = React.useRef<HTMLElement>(null)
const [loadingFocused, setLoadingFocused] = React.useState(false)
const [subTreeLabel, setSubTreeLabel] = React.useState('')
const previousState = usePreviousValue(state)
const {safeSetTimeout} = useSafeTimeout()

Expand All @@ -676,6 +678,8 @@ const SubTree: React.FC<TreeViewSubTreeProps> = ({count, state, children}) => {
React.useEffect(() => {
const parentElement = document.getElementById(itemId)
if (!parentElement) return

setSubTreeLabel(getAccessibleName(parentElement))
if (previousState === 'loading' && state === 'done') {
// Announce update to screen readers
const parentName = getAccessibleName(parentElement)
Expand Down Expand Up @@ -753,6 +757,7 @@ const SubTree: React.FC<TreeViewSubTreeProps> = ({count, state, children}) => {
}}
// @ts-ignore Box doesn't have type support for `ref` used in combination with `as`
ref={ref}
aria-label={ariaLabel || subTreeLabel}
>
{state === 'loading' ? <LoadingItem ref={loadingItemRef} count={count} /> : children}
{isSubTreeEmpty && state !== 'loading' ? <EmptyItem /> : null}
Expand Down

0 comments on commit cf80bf2

Please sign in to comment.