-
Notifications
You must be signed in to change notification settings - Fork 536
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(ButtonGroup): add toolbar interactions for role toolbar #5200
Open
francinelucca
wants to merge
8
commits into
main
Choose a base branch
from
francinelucca/3423-prcbuttongroup-group-container-lacks-a-role--does-not-convey-its-purpose
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+116
−3
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f959845
fix(ButtonGroup): add toolbar interactions for role toolbar
francinelucca d904746
Create slow-news-love.md
francinelucca 2309a2d
fix(ButtonGroup): remove unusued import
francinelucca ccbe993
Merge branch 'francinelucca/3423-prcbuttongroup-group-container-lacks…
francinelucca b51c79a
test(vrt): update snapshots
francinelucca fb53032
Merge branch 'main' into francinelucca/3423-prcbuttongroup-group-cont…
francinelucca 7bd33b6
Merge branch 'main' of github.com:primer/react into francinelucca/342…
francinelucca e8782e2
fix(ButtonGroup): remove focusZoneSettings prop
francinelucca File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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": patch | ||
--- | ||
|
||
fix(ButtonGroup): add toolbar interactions for role toolbar |
Binary file added
BIN
+6.68 KB
.../ButtonGroup.test.ts-snapshots/ButtonGroup-As-Toolbar-dark-colorblind-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+6.73 KB
...ents/ButtonGroup.test.ts-snapshots/ButtonGroup-As-Toolbar-dark-dimmed-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+6.71 KB
...ttonGroup.test.ts-snapshots/ButtonGroup-As-Toolbar-dark-high-contrast-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+6.68 KB
.../components/ButtonGroup.test.ts-snapshots/ButtonGroup-As-Toolbar-dark-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+6.68 KB
.../ButtonGroup.test.ts-snapshots/ButtonGroup-As-Toolbar-dark-tritanopia-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+6.79 KB
...ButtonGroup.test.ts-snapshots/ButtonGroup-As-Toolbar-light-colorblind-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+6.91 KB
...tonGroup.test.ts-snapshots/ButtonGroup-As-Toolbar-light-high-contrast-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+6.79 KB
...components/ButtonGroup.test.ts-snapshots/ButtonGroup-As-Toolbar-light-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+6.79 KB
...ButtonGroup.test.ts-snapshots/ButtonGroup-As-Toolbar-light-tritanopia-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import {Button} from '../Button' | ||
import {render, screen} from '@testing-library/react' | ||
import axe from 'axe-core' | ||
import {FeatureFlags} from '../FeatureFlags' | ||
import {behavesAsComponent} from '../utils/testing' | ||
import type {ButtonGroupProps} from './ButtonGroup' | ||
import ButtonGroup from './ButtonGroup' | ||
import React from 'react' | ||
|
||
const TestButtonGroup = (props: ButtonGroupProps) => ( | ||
<ButtonGroup {...props}> | ||
<Button>Button 1</Button> | ||
<Button>Button 2</Button> | ||
<Button>Button 3</Button> | ||
</ButtonGroup> | ||
) | ||
|
||
describe('ButtonGroup', () => { | ||
behavesAsComponent({ | ||
Component: TestButtonGroup, | ||
options: {skipSx: true, skipAs: true}, | ||
}) | ||
|
||
it('should support `className` on the outermost element', () => { | ||
const Element = () => <ButtonGroup className={'test-class-name'} /> | ||
const FeatureFlagElement = () => { | ||
return ( | ||
<FeatureFlags | ||
flags={{ | ||
primer_react_css_modules_team: true, | ||
primer_react_css_modules_staff: true, | ||
primer_react_css_modules_ga: true, | ||
}} | ||
> | ||
<Element /> | ||
</FeatureFlags> | ||
) | ||
} | ||
expect(render(<Element />).container.firstChild).toHaveClass('test-class-name') | ||
expect(render(<FeatureFlagElement />).container.firstChild).toHaveClass('test-class-name') | ||
}) | ||
|
||
it('renders a <div>', () => { | ||
const container = render(<ButtonGroup data-testid="button-group" />) | ||
expect(container.getByTestId('button-group').tagName).toBe('DIV') | ||
}) | ||
|
||
it('should have no axe violations', async () => { | ||
const {container} = render(<TestButtonGroup />) | ||
const results = await axe.run(container) | ||
expect(results).toHaveNoViolations() | ||
}) | ||
|
||
it('should respect role prop', () => { | ||
render(<ButtonGroup role="toolbar" />) | ||
expect(screen.getByRole('toolbar')).toBeInTheDocument() | ||
}) | ||
}) |
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 |
---|---|---|
|
@@ -7,6 +7,9 @@ import classes from './ButtonGroup.module.css' | |
import {toggleStyledComponent} from '../internal/utils/toggleStyledComponent' | ||
import {clsx} from 'clsx' | ||
import {useFeatureFlag} from '../FeatureFlags' | ||
import {FocusKeys, useFocusZone} from '../hooks/useFocusZone' | ||
import {useProvidedRefOrCreate} from '../hooks' | ||
import type {ForwardRefComponent as PolymorphicForwardRefComponent} from '../utils/polymorphic' | ||
|
||
const StyledButtonGroup = toggleStyledComponent( | ||
'primer_react_css_modules_staff', | ||
|
@@ -74,23 +77,34 @@ const StyledButtonGroup = toggleStyledComponent( | |
) | ||
|
||
export type ButtonGroupProps = ComponentProps<typeof StyledButtonGroup> | ||
|
||
const ButtonGroup = React.forwardRef<HTMLElement, ButtonGroupProps>(function ButtonGroup( | ||
{children, className, ...rest}, | ||
{children, className, role, ...rest}, | ||
forwardRef, | ||
) { | ||
const enabled = useFeatureFlag('primer_react_css_modules_staff') | ||
const buttonRef = useProvidedRefOrCreate(forwardRef as React.RefObject<HTMLDivElement>) | ||
|
||
useFocusZone({ | ||
containerRef: buttonRef, | ||
disabled: role !== 'toolbar', | ||
bindKeys: FocusKeys.ArrowHorizontal, | ||
focusOutBehavior: 'wrap', | ||
}) | ||
|
||
return ( | ||
<StyledButtonGroup | ||
ref={forwardRef} | ||
ref={buttonRef} | ||
className={clsx(className, { | ||
[classes.ButtonGroup]: enabled, | ||
})} | ||
role={role} | ||
{...rest} | ||
> | ||
{children} | ||
</StyledButtonGroup> | ||
) | ||
}) | ||
}) as PolymorphicForwardRefComponent<'div', ButtonGroupProps> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Had to add this because the tests where complaining about types when trying to spread ( |
||
|
||
ButtonGroup.displayName = 'ButtonGroup' | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we add
focusOutBehavior: 'wrap'
to the object so that it wraps when you arrow the first/last item?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in e8782e2! Thanks so much for the suggestion 🙏🏼