Skip to content

Commit cf80bf2

Browse files
TreeView: Add aria-label to TreeView subtree (#5174)
* Add `aria-label` to `TreeView` subtree * Add changeset * Add name to `SubTree` via `aria-label` * test fix --------- Co-authored-by: Hussam Ghazzi <[email protected]>
1 parent 3da9af9 commit cf80bf2

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

.changeset/ten-mirrors-refuse.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@primer/react': minor
3+
---
4+
5+
TreeView: Adds `aria-label` prop to `TreeView.Subtree`

packages/react/src/TreeView/TreeView.docs.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,11 @@
190190
"name": "count",
191191
"type": "number",
192192
"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"
193+
},
194+
{
195+
"name": "aria-label",
196+
"type": "string",
197+
"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."
193198
}
194199
]
195200
},

packages/react/src/TreeView/TreeView.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ describe('Markup', () => {
238238
})
239239

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

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

packages/react/src/TreeView/TreeView.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,14 +647,16 @@ export type TreeViewSubTreeProps = {
647647
* Display a skeleton loading state with the specified count of items
648648
*/
649649
count?: number
650+
'aria-label'?: string
650651
}
651652

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

@@ -676,6 +678,8 @@ const SubTree: React.FC<TreeViewSubTreeProps> = ({count, state, children}) => {
676678
React.useEffect(() => {
677679
const parentElement = document.getElementById(itemId)
678680
if (!parentElement) return
681+
682+
setSubTreeLabel(getAccessibleName(parentElement))
679683
if (previousState === 'loading' && state === 'done') {
680684
// Announce update to screen readers
681685
const parentName = getAccessibleName(parentElement)
@@ -753,6 +757,7 @@ const SubTree: React.FC<TreeViewSubTreeProps> = ({count, state, children}) => {
753757
}}
754758
// @ts-ignore Box doesn't have type support for `ref` used in combination with `as`
755759
ref={ref}
760+
aria-label={ariaLabel || subTreeLabel}
756761
>
757762
{state === 'loading' ? <LoadingItem ref={loadingItemRef} count={count} /> : children}
758763
{isSubTreeEmpty && state !== 'loading' ? <EmptyItem /> : null}

0 commit comments

Comments
 (0)