Skip to content

Commit

Permalink
Remove usage of aria-details from InputControl and BaseControl. (#61203)
Browse files Browse the repository at this point in the history
* Remove usage of aria-details from InputControl and BaseControl.

* Add changelog entry.

Co-authored-by: afercia <[email protected]>
Co-authored-by: Mamaduka <[email protected]>
  • Loading branch information
3 people authored May 10, 2024
1 parent f4cf64a commit df77d44
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 17 deletions.
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

### Bug Fix

- `BaseControl`, `InputControl`: Remove usage of aria-details from InputControl and BaseControl ([#61203](https://github.com/WordPress/gutenberg/pull/61203)).
- `SlotFill`: fixed missing `getServerSnapshot` parameter in slot map ([#60943](https://github.com/WordPress/gutenberg/pull/60943)).
- `Panel`: Fix issue with collapsing panel header ([#61319](https://github.com/WordPress/gutenberg/pull/61319)).

Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/base-control/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ If true, the label will only be visible to screen readers.

### help

Additional description for the control. It is preferable to use plain text for `help`, as it can be accessibly associated with the control using `aria-describedby`. When the `help` contains links, or otherwise non-plain text content, it will be associated with the control using `aria-details`.
Additional description for the control. The element containing the description will be programmatically associated to the BaseControl by the means of an `aria-describedby` attribute.

- Type: `ReactNode`
- Required: No
Expand Down
6 changes: 1 addition & 5 deletions packages/components/src/base-control/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ export function useBaseControlProps(
preferredId
);

// ARIA descriptions can only contain plain text, so fall back to aria-details if not.
const helpPropName =
typeof help === 'string' ? 'aria-describedby' : 'aria-details';

return {
baseControlProps: {
id: uniqueId,
Expand All @@ -39,7 +35,7 @@ export function useBaseControlProps(
},
controlProps: {
id: uniqueId,
...( !! help ? { [ helpPropName ]: `${ uniqueId }__help` } : {} ),
...( !! help ? { 'aria-describedby': `${ uniqueId }__help` } : {} ),
},
};
}
6 changes: 3 additions & 3 deletions packages/components/src/base-control/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe( 'BaseControl', () => {
).toBeInTheDocument();
} );

it( 'should render help as aria-details when not plain text', () => {
it( 'should still render help as aria-describedby when not plain text', () => {
render(
<MyBaseControl
label="Text"
Expand All @@ -44,10 +44,10 @@ describe( 'BaseControl', () => {
name: 'My help text',
} );

expect( textarea ).toHaveAttribute( 'aria-details' );
expect( textarea ).toHaveAttribute( 'aria-describedby' );
expect(
// eslint-disable-next-line testing-library/no-node-access
help.closest( `#${ textarea.getAttribute( 'aria-details' ) }` )
help.closest( `#${ textarea.getAttribute( 'aria-describedby' ) }` )
).toBeVisible();
} );
} );
3 changes: 1 addition & 2 deletions packages/components/src/base-control/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ export type BaseControlProps = {
/**
* Additional description for the control.
*
* It is preferable to use plain text for `help`, as it can be accessibly associated with the control using `aria-describedby`.
* When the `help` contains links, or otherwise non-plain text content, it will be associated with the control using `aria-details`.
* The element containing the description will be programmatically associated to the BaseControl by the means of an `aria-describedby` attribute.
*/
help?: ReactNode;
/**
Expand Down
5 changes: 1 addition & 4 deletions packages/components/src/input-control/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,7 @@ export function UnforwardedInputControl(
onChange,
} );

// ARIA descriptions can only contain plain text, so fall back to aria-details if not.
const helpPropName =
typeof help === 'string' ? 'aria-describedby' : 'aria-details';
const helpProp = !! help ? { [ helpPropName ]: `${ id }__help` } : {};
const helpProp = !! help ? { 'aria-describedby': `${ id }__help` } : {};

return (
<BaseControl
Expand Down
4 changes: 2 additions & 2 deletions packages/components/src/input-control/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ describe( 'InputControl', () => {
).toBeInTheDocument();
} );

it( 'should render help as aria-details when not plain text', () => {
it( 'should still render help as aria-describedby when not plain text', () => {
render( <InputControl help={ <a href="/foo">My help text</a> } /> );

const input = screen.getByRole( 'textbox' );
const help = screen.getByRole( 'link', { name: 'My help text' } );

expect(
// eslint-disable-next-line testing-library/no-node-access
help.closest( `#${ input.getAttribute( 'aria-details' ) }` )
help.closest( `#${ input.getAttribute( 'aria-describedby' ) }` )
).toBeVisible();
} );
} );
Expand Down

0 comments on commit df77d44

Please sign in to comment.