Skip to content

Commit

Permalink
Add aria-orientation to Divider
Browse files Browse the repository at this point in the history
  • Loading branch information
aarongarciah committed Aug 9, 2024
1 parent 193bd6b commit cde7379
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function VerticalDividerMiddle() {
'& svg': {
m: 1,
},
'& hr': {
'& .MuiDivider-root': {
mx: 0.5,
},
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function VerticalDividerMiddle() {
'& svg': {
m: 1,
},
'& hr': {
'& .MuiDivider-root': {
mx: 0.5,
},
}}
Expand Down
2 changes: 1 addition & 1 deletion docs/data/material/components/dividers/VerticalDividers.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default function VerticalDividers() {
'& svg': {
m: 1,
},
'& hr': {
'& .MuiDivider-root': {
mx: 0.5,
},
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default function VerticalDividers() {
'& svg': {
m: 1,
},
'& hr': {
'& .MuiDivider-root': {
mx: 0.5,
},
}}
Expand Down
9 changes: 7 additions & 2 deletions packages/mui-material/src/Divider/Divider.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,10 @@ const Divider = React.forwardRef(function Divider(inProps, ref) {
absolute = false,
children,
className,
component = children ? 'div' : 'hr',
orientation = 'horizontal',
component = children || orientation === 'vertical' ? 'div' : 'hr',
flexItem = false,
light = false,
orientation = 'horizontal',
role = component !== 'hr' ? 'separator' : undefined,
textAlign = 'center',
variant = 'fullWidth',
Expand Down Expand Up @@ -255,6 +255,11 @@ const Divider = React.forwardRef(function Divider(inProps, ref) {
role={role}
ref={ref}
ownerState={ownerState}
aria-orientation={
role === 'separator' && (component !== 'hr' || orientation === 'vertical')
? orientation
: undefined
}
{...other}
>
{children ? (
Expand Down
24 changes: 17 additions & 7 deletions packages/mui-material/src/Divider/Divider.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import { expect } from 'chai';
import { createRenderer } from '@mui/internal-test-utils';
import { createRenderer, screen } from '@mui/internal-test-utils';
import { styled } from '@mui/material/styles';
import Divider, { dividerClasses as classes } from '@mui/material/Divider';
import describeConformance from '../../test/describeConformance';
Expand Down Expand Up @@ -165,19 +165,29 @@ describe('<Divider />', () => {

describe('role', () => {
it('avoids adding implicit aria semantics', () => {
const { container } = render(<Divider />);
expect(container.firstChild).not.to.have.attribute('role');
render(<Divider />);
expect(screen.getByRole('separator')).not.to.have.attribute('role');
expect(screen.getByRole('separator')).not.to.have.attribute('aria-orientation');
});

it('adds a proper role if none is specified', () => {
const { container } = render(<Divider component="div" />);
expect(container.firstChild).to.have.attribute('role', 'separator');
render(<Divider component="div" />);
expect(screen.getByRole('separator')).not.to.equal(null);
expect(screen.getByRole('separator')).to.have.attribute('aria-orientation');
});

it('adds a proper role with vertical orientation', () => {
render(<Divider orientation="vertical" />);
expect(screen.getByRole('separator')).not.to.equal(null);
expect(screen.getByRole('separator')).to.have.attribute('aria-orientation');
});

it('overrides the computed role with the provided one', () => {
// presentation is the only valid aria role
const { container } = render(<Divider role="presentation" />);
expect(container.firstChild).to.have.attribute('role', 'presentation');
render(<Divider role="presentation" data-testid="divider" />);
expect(screen.queryByRole('separator')).to.equal(null);
expect(screen.getByTestId('divider')).to.have.attribute('role', 'presentation');
expect(screen.getByTestId('divider')).not.to.have.attribute('aria-orientation');
});
});
});

0 comments on commit cde7379

Please sign in to comment.