-
Notifications
You must be signed in to change notification settings - Fork 535
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react): add ScrollableRegion and useOverflow (#4719)
* feat(react): add ScrollableRegion and useOverflow * chore: address ts error in Table * test: update snapshots * chore: add changeset --------- Co-authored-by: Josh Black <joshblack@users.noreply.github.com>
- Loading branch information
Showing
13 changed files
with
202 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@primer/react': minor | ||
--- | ||
|
||
Add experimental ScrollableRegion component and useOverflow hook |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
packages/react/src/ScrollableRegion/ScrollableRegion.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import React from 'react' | ||
import type {Meta, StoryObj} from '@storybook/react' | ||
import {ScrollableRegion} from '../ScrollableRegion' | ||
|
||
const meta = { | ||
title: 'Drafts/Components/ScrollableRegion', | ||
component: ScrollableRegion, | ||
} satisfies Meta<typeof ScrollableRegion> | ||
|
||
export default meta | ||
|
||
export const Default = () => { | ||
return ( | ||
<ScrollableRegion aria-label="Example scrollable region"> | ||
<p>Example content that triggers overflow.</p> | ||
<p | ||
style={{ | ||
whiteSpace: 'nowrap', | ||
}} | ||
> | ||
The content here will not wrap at smaller screen sizes and will trigger the component to set the container as a | ||
region, label it, make it focusable, and make it scrollable. | ||
</p> | ||
</ScrollableRegion> | ||
) | ||
} | ||
|
||
export const Playground: StoryObj<typeof ScrollableRegion> = { | ||
render: args => { | ||
return ( | ||
<ScrollableRegion {...args}> | ||
<p>Example content that triggers overflow.</p> | ||
<p | ||
style={{ | ||
whiteSpace: 'nowrap', | ||
}} | ||
> | ||
The content here will not wrap at smaller screen sizes and will trigger the component to set the container as | ||
a region, label it, make it focusable, and make it scrollable. | ||
</p> | ||
</ScrollableRegion> | ||
) | ||
}, | ||
args: { | ||
'aria-label': 'Example scrollable region', | ||
}, | ||
argTypes: { | ||
'aria-label': { | ||
control: 'text', | ||
}, | ||
'aria-labelledby': { | ||
control: 'text', | ||
}, | ||
className: { | ||
control: 'text', | ||
}, | ||
}, | ||
} |
88 changes: 88 additions & 0 deletions
88
packages/react/src/ScrollableRegion/ScrollableRegion.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import {render, screen} from '@testing-library/react' | ||
import React, {act} from 'react' | ||
import {ScrollableRegion} from '../ScrollableRegion' | ||
|
||
const originalResizeObserver = global.ResizeObserver | ||
|
||
describe('ScrollableRegion', () => { | ||
let mockResizeCallback: (entries: Array<ResizeObserverEntry>) => void | ||
|
||
beforeEach(() => { | ||
global.ResizeObserver = class ResizeObserver { | ||
constructor(callback: ResizeObserverCallback) { | ||
mockResizeCallback = (entries: Array<ResizeObserverEntry>) => { | ||
return callback(entries, this) | ||
} | ||
} | ||
|
||
observe() {} | ||
disconnect() {} | ||
unobserve() {} | ||
} | ||
}) | ||
|
||
afterEach(() => { | ||
global.ResizeObserver = originalResizeObserver | ||
}) | ||
|
||
test('does not render with region props by default', () => { | ||
render( | ||
<ScrollableRegion aria-label="Example label" data-testid="container"> | ||
Example content | ||
</ScrollableRegion>, | ||
) | ||
|
||
expect(screen.getByTestId('container')).not.toHaveAttribute('role') | ||
expect(screen.getByTestId('container')).not.toHaveAttribute('tabindex') | ||
expect(screen.getByTestId('container')).not.toHaveAttribute('aria-labelledby') | ||
expect(screen.getByTestId('container')).not.toHaveAttribute('aria-label') | ||
|
||
expect(screen.getByTestId('container')).toHaveStyleRule('overflow', 'auto') | ||
expect(screen.getByTestId('container')).toHaveStyleRule('position', 'relative') | ||
}) | ||
|
||
test('does render with region props when overflow is present', () => { | ||
render( | ||
<ScrollableRegion aria-label="Example label" data-testid="container"> | ||
Example content | ||
</ScrollableRegion>, | ||
) | ||
|
||
act(() => { | ||
// Mock a resize occurring when the scroll height is greater than the | ||
// client height | ||
const target = document.createElement('div') | ||
mockResizeCallback([ | ||
{ | ||
target: { | ||
...target, | ||
scrollHeight: 500, | ||
clientHeight: 100, | ||
}, | ||
borderBoxSize: [], | ||
contentBoxSize: [], | ||
contentRect: { | ||
width: 0, | ||
height: 0, | ||
top: 0, | ||
right: 0, | ||
bottom: 0, | ||
left: 0, | ||
x: 0, | ||
y: 0, | ||
toJSON() { | ||
return {} | ||
}, | ||
}, | ||
devicePixelContentBoxSize: [], | ||
}, | ||
]) | ||
}) | ||
|
||
expect(screen.getByLabelText('Example label')).toBeVisible() | ||
|
||
expect(screen.getByLabelText('Example label')).toHaveAttribute('role', 'region') | ||
expect(screen.getByLabelText('Example label')).toHaveAttribute('tabindex', '0') | ||
expect(screen.getByLabelText('Example label')).toHaveAttribute('aria-label') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export {ScrollableRegion} from './ScrollableRegion' | ||
export type {ScrollableRegionProps} from './ScrollableRegion' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters