Skip to content

Commit

Permalink
Allow keyboard navigation to dialog's scroll region (#4000)
Browse files Browse the repository at this point in the history
* move ScrollableRegion to its own internal component

* use scrollable region in dialog body

* changeset

* Update src/internal/components/ScrollableRegion.tsx

Co-authored-by: Josh Black <joshblack@github.com>

* Revert "Update src/internal/components/ScrollableRegion.tsx"

This reverts commit 37d3231.

---------

Co-authored-by: Josh Black <joshblack@github.com>
  • Loading branch information
strackoverflow and joshblack authored Dec 12, 2023
1 parent cf22577 commit a416298
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .changeset/mighty-ghosts-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': patch
---

Fix an issue where the scrollable Dialog body could not be focused with the keyboard
25 changes: 1 addition & 24 deletions src/DataTable/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {Column, CellAlignment} from './column'
import {UniqueRow} from './row'
import {SortDirection} from './sorting'
import {useTableLayout} from './useTable'
import {useOverflow} from '../internal/hooks/useOverflow'
import {ScrollableRegion} from '../internal/components/ScrollableRegion'

// ----------------------------------------------------------------------------
// Table
Expand Down Expand Up @@ -662,29 +662,6 @@ const Button = styled.button`
}
`

type ScrollableRegionProps = React.PropsWithChildren<{
'aria-labelledby'?: string
className?: string
}>

function ScrollableRegion({'aria-labelledby': labelledby, children, ...rest}: ScrollableRegionProps) {
const ref = React.useRef(null)
const hasOverflow = useOverflow(ref)
const regionProps = hasOverflow
? {
'aria-labelledby': labelledby,
role: 'region',
tabIndex: 0,
}
: {}

return (
<Box {...rest} {...regionProps} ref={ref} sx={{overflow: 'auto'}}>
{children}
</Box>
)
}

export {
TableContainer,
TableTitle,
Expand Down
5 changes: 4 additions & 1 deletion src/Dialog/Dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {FocusKeys} from '@primer/behaviors'
import Portal from '../Portal'
import {useRefObjectAsForwardedRef} from '../hooks/useRefObjectAsForwardedRef'
import {useId} from '../hooks/useId'
import {ScrollableRegion} from '../internal/components/ScrollableRegion'

/* Dialog Version 2 */

Expand Down Expand Up @@ -324,7 +325,9 @@ const _Dialog = React.forwardRef<HTMLDivElement, React.PropsWithChildren<DialogP
sx={sx}
>
{header}
{body}
<ScrollableRegion aria-labelledby={dialogLabelId} className="DialogOverflowWrapper">
{body}
</ScrollableRegion>
{footer}
</StyledDialog>
</Backdrop>
Expand Down
26 changes: 26 additions & 0 deletions src/internal/components/ScrollableRegion.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react'
import Box from '../../Box'
import {useOverflow} from '../hooks/useOverflow'

type ScrollableRegionProps = React.PropsWithChildren<{
'aria-labelledby'?: string
className?: string
}>

export function ScrollableRegion({'aria-labelledby': labelledby, children, ...rest}: ScrollableRegionProps) {
const ref = React.useRef(null)
const hasOverflow = useOverflow(ref)
const regionProps = hasOverflow
? {
'aria-labelledby': labelledby,
role: 'region',
tabIndex: 0,
}
: {}

return (
<Box {...rest} {...regionProps} ref={ref} sx={{overflow: 'auto'}}>
{children}
</Box>
)
}

0 comments on commit a416298

Please sign in to comment.