Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

- `ColorPalette`: Apply the computed contrast color to the selected checkmark now that the icon is stroke-based. ([#78812](https://github.com/WordPress/gutenberg/pull/78812))
- `CheckboxControl`: Color the checked and indeterminate icons with `color` rather than `fill`, so they stay visible now that those icons are stroke-based. ([#78812](https://github.com/WordPress/gutenberg/pull/78812))
- `BorderBoxControl`: Give the group of border controls an accessible name from the `label` prop, by rendering the wrapper as a `role="group"` associated with the label through `aria-labelledby`. A consumer-supplied `aria-labelledby` or `aria-label` takes precedence over `label` and names the group instead; the wrapper is only given the `group` role when one of the three provides a name. A hidden label (`hideLabelFromVision`) now renders as a `span` rather than a `label` element, matching the visible one ([#82279](https://github.com/WordPress/gutenberg/pull/82279)).
- `Snackbar`: Restart the auto-dismiss timer when a notice is recreated with the same ID during removal ([#81764](https://github.com/WordPress/gutenberg/pull/81764)).
- `ToolsPanel`: Stop a panel item whose `panelId` doesn't match the panel from writing its value into that panel's menu, which could leave an orphaned entry in the dropdown ([#82127](https://github.com/WordPress/gutenberg/pull/82127)).
- `Icon`: Merge a consumer-supplied `style` prop with the icon's intrinsic styles instead of replacing them, so styles like `fill: none` on stroke-based icons survive unless the consumer overrides the same property explicitly. ([#78808](https://github.com/WordPress/gutenberg/pull/78808))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { __ } from '@wordpress/i18n';
import { useMemo, useState } from '@wordpress/element';
import { useMergeRefs } from '@wordpress/compose';
import { useInstanceId, useMergeRefs } from '@wordpress/compose';
import BorderBoxControlLinkedButton from '../border-box-control-linked-button';
import BorderBoxControlSplitControls from '../border-box-control-split-controls';
import { BorderControl } from '../../border-control';
Expand All @@ -17,8 +17,8 @@ import type {
BorderControlProps,
} from '../../border-control/types';

const BorderLabel = ( props: LabelProps ) => {
const { label, hideLabelFromVision } = props;
const BorderLabel = ( props: LabelProps & { id?: string } ) => {
const { id, label, hideLabelFromVision } = props;

if ( ! label ) {
return null;
Expand All @@ -28,9 +28,11 @@ const BorderLabel = ( props: LabelProps ) => {
// the stable `.components-base-control__label` className consumers style
// against; `StyledLabel` is an emotion component with a generated one.
return hideLabelFromVision ? (
<VisuallyHidden as="label">{ label }</VisuallyHidden>
<VisuallyHidden as="span" id={ id }>
{ label }
</VisuallyHidden>
) : (
<BaseControl.VisualLabel>{ label }</BaseControl.VisualLabel>
<BaseControl.VisualLabel id={ id }>{ label }</BaseControl.VisualLabel>
);
};

Expand Down Expand Up @@ -61,9 +63,27 @@ const UnconnectedBorderBoxControl = (
toggleLinked,
wrapperClassName,
__experimentalIsRenderedInSidebar,
'aria-label': ariaLabel,
'aria-labelledby': ariaLabelledBy,
...otherProps
} = useBorderBoxControl( props );

// The label names the group of border controls rather than a single input,
// so it is associated via `aria-labelledby` instead of `htmlFor`.
const generatedLabelId = useInstanceId(
BorderBoxControl,
'border-box-control-label'
);

// A consumer-provided accessible name takes precedence, so the generated
// relationship is only used as a fallback. Where an external name wins,
// the unused ID is left off the internal label. Empty values are treated
// as absent, matching the accessible name computation, which skips an
// empty `aria-label` or `aria-labelledby` rather than resolving a name
// from it.
const labelId =
label && ! ariaLabel && ! ariaLabelledBy ? generatedLabelId : undefined;

// Use internal state instead of a ref to make sure that the component
// re-renders when the popover's anchor updates.
const [ popoverAnchor, setPopoverAnchor ] = useState< Element | null >(
Expand All @@ -88,7 +108,17 @@ const UnconnectedBorderBoxControl = (
const mergedRef = useMergeRefs( [ setPopoverAnchor, forwardedRef ] );

return (
<View className={ className } { ...otherProps } ref={ mergedRef }>
<View
className={ className }
// Whichever naming source wins, it describes the border controls
// as a whole, so the wrapper needs a role for that accessible
// name to attach to. Without a name there is no group to expose.
role={ label || ariaLabel || ariaLabelledBy ? 'group' : undefined }
aria-label={ ariaLabel }
aria-labelledby={ ariaLabelledBy ?? labelId }
{ ...otherProps }
ref={ mergedRef }
>
{ hasVisibleLabel ? (
// The toggle shares the label's row so that it lines up with
// the equivalent toggle on sibling controls, e.g. the border
Expand All @@ -100,6 +130,7 @@ const UnconnectedBorderBoxControl = (
alignment="center"
>
<BorderLabel
id={ labelId }
label={ label }
hideLabelFromVision={ hideLabelFromVision }
/>
Expand All @@ -110,6 +141,7 @@ const UnconnectedBorderBoxControl = (
</Grid>
) : (
<BorderLabel
id={ labelId }
label={ label }
hideLabelFromVision={ hideLabelFromVision }
/>
Expand Down Expand Up @@ -168,6 +200,10 @@ const UnconnectedBorderBoxControl = (
* An input control for the color, style, and width of the border of a box. The
* border can be customized as a whole, or individually for each side of the box.
*
* The controls are exposed as a group named by the `label` prop. Passing
* `aria-labelledby` or `aria-label` names the group instead, taking precedence
* over `label`; with none of the three, the wrapper is not exposed as a group.
*
* ```jsx
* import { BorderBoxControl } from '@wordpress/components';
* import { __ } from '@wordpress/i18n';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ComponentProps } from 'react';
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { useId } from '@wordpress/element';
import { BorderBoxControl } from '..';

const colors = [
Expand Down Expand Up @@ -50,6 +51,102 @@ function TestBorderBoxControl(
}

describe( 'BorderBoxControl', () => {
describe( 'Labelling', () => {
it( 'should give the group of controls an accessible name from the label', () => {
render( <TestBorderBoxControl { ...props } /> );

expect(
screen.getByRole( 'group', { name: props.label } )
).toBeInTheDocument();
} );

it( 'should name the group of controls even when the label is hidden', () => {
render( <TestBorderBoxControl { ...props } hideLabelFromVision /> );

expect(
screen.getByRole( 'group', { name: props.label } )
).toBeInTheDocument();
} );

it( 'should let a consumer-provided `aria-label` take precedence over the label', () => {
render(
<TestBorderBoxControl { ...props } aria-label="Outer borders" />
);

expect(
screen.getByRole( 'group', { name: 'Outer borders' } )
).toBeInTheDocument();
} );

it( 'should let a consumer-provided `aria-labelledby` take precedence over the label', () => {
const ExternallyLabelledControl = () => {
const id = useId();
return (
<>
<span id={ id }>Outer borders</span>
<TestBorderBoxControl
{ ...props }
aria-labelledby={ id }
/>
</>
);
};

render( <ExternallyLabelledControl /> );

expect(
screen.getByRole( 'group', { name: 'Outer borders' } )
).toBeInTheDocument();

// With an external name in play, the internal label is no longer
// referenced, so it shouldn't carry a generated ID either.
expect( screen.getByText( props.label ) ).not.toHaveAttribute(
'id'
);
} );

it( 'should form a group when named only by `aria-label`', () => {
render(
<TestBorderBoxControl
{ ...props }
label={ undefined }
aria-label="Outer borders"
/>
);

expect(
screen.getByRole( 'group', { name: 'Outer borders' } )
).toBeInTheDocument();
} );

it( 'should fall back to the label when an ARIA naming prop is empty', () => {
// An empty `aria-label` is skipped by the accessible name
// computation, so it shouldn't suppress the label association and
// leave the group unnamed.
render( <TestBorderBoxControl { ...props } aria-label="" /> );

expect(
screen.getByRole( 'group', { name: props.label } )
).toBeInTheDocument();
} );

it( 'should give each instance its own label association', () => {
render(
<>
<TestBorderBoxControl { ...props } label="First" />
<TestBorderBoxControl { ...props } label="Second" />
</>
);

expect(
screen.getByRole( 'group', { name: 'First' } )
).toBeInTheDocument();
expect(
screen.getByRole( 'group', { name: 'Second' } )
).toBeInTheDocument();
} );
} );

describe( 'Linked view rendering', () => {
it( 'should render correctly when no value provided', () => {
render( <TestBorderBoxControl { ...props } /> );
Expand Down
Loading