Skip to content

Commit

Permalink
fix(ButtonGroup): add toolbar interactions for role toolbar (#5200)
Browse files Browse the repository at this point in the history
* fix(ButtonGroup): add toolbar interactions for role toolbar

* Create slow-news-love.md

* fix(ButtonGroup): remove unusued import

* test(vrt): update snapshots

* fix(ButtonGroup): remove focusZoneSettings prop

---------

Co-authored-by: francinelucca <francinelucca@users.noreply.github.com>
  • Loading branch information
francinelucca and francinelucca authored Nov 8, 2024
1 parent 706d272 commit b28e6b2
Show file tree
Hide file tree
Showing 14 changed files with 116 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/slow-news-love.md
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions e2e/components/ButtonGroup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,32 @@ test.describe('ButtonGroup', () => {
})
}
})

test.describe('As Toolbar', () => {
for (const theme of themes) {
test.describe(theme, () => {
test('default @vrt', async ({page}) => {
await visit(page, {
id: 'components-buttongroup-features--as-toolbar',
globals: {
colorScheme: theme,
},
})

// Default state
expect(await page.screenshot()).toMatchSnapshot(`ButtonGroup.As Toolbar.${theme}.png`)
})

test('axe @aat', async ({page}) => {
await visit(page, {
id: 'components-buttongroup-features--as-toolbar',
globals: {
colorScheme: theme,
},
})
await expect(page).toHaveNoViolations()
})
})
}
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,11 @@ export const DropdownSplit = () => {
</ButtonGroup>
)
}

export const AsToolbar = () => (
<ButtonGroup role="toolbar">
<Button>Button 1</Button>
<Button>Button 2</Button>
<Button>Button 3</Button>
</ButtonGroup>
)
58 changes: 58 additions & 0 deletions packages/react/src/ButtonGroup/ButtonGroup.test.tsx
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()
})
})
20 changes: 17 additions & 3 deletions packages/react/src/ButtonGroup/ButtonGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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>

ButtonGroup.displayName = 'ButtonGroup'

Expand Down

0 comments on commit b28e6b2

Please sign in to comment.