Skip to content

Commit

Permalink
List view: Refactor ARIA attributes (#48461)
Browse files Browse the repository at this point in the history
* Fix ARIA attributes.

* Add assertive for selection read.

* Remove useless useEffect. Refactor expanded to use isExpanded attribute.

* Add prop to disable aria-expanded on tree grid tr so I can override on the link.

* Try to fix failing e2e test

* Update list view tests

* Restore removed useEffect.

* Revert TreeGrid changes.

* Finish revert.

* Eliminate aria-expanded on main tr.

* Add data-expanded back to TreeGrid. Need to detect when to expand via keyboard.

* Fix expander.

* Move isExpanded undefined to tr directly.

* Fix E2E.

* Retain dark color for button when expanded

* Add title fallback.

* Save some verbosity on block options button.

* Fix E2E.

* Add components changelog entry.

---------

Co-authored-by: Andrew Serong <14988353+andrewserong@users.noreply.github.com>
  • Loading branch information
alexstine and andrewserong authored Apr 14, 2023
1 parent 1069f48 commit 6f96394
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ function ListViewBlockSelectButton(
onDragStart,
onDragEnd,
draggable,
isExpanded,
ariaLabel,
ariaDescribedBy,
},
ref
) {
Expand Down Expand Up @@ -76,7 +79,9 @@ function ListViewBlockSelectButton(
onDragEnd={ onDragEnd }
draggable={ draggable }
href={ `#block-${ clientId }` }
aria-hidden={ true }
aria-label={ ariaLabel }
aria-describedby={ ariaDescribedBy }
aria-expanded={ isExpanded }
>
<ListViewExpander onClick={ onToggleExpanded } />
<BlockIcon icon={ blockInformation?.icon } showColors />
Expand Down
43 changes: 15 additions & 28 deletions packages/block-editor/src/components/list-view/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ function ListViewBlock( {
path,
isExpanded,
selectedClientIds,
preventAnnouncement,
isSyncedBranch,
} ) {
const cellRef = useRef( null );
Expand Down Expand Up @@ -90,6 +89,7 @@ function ListViewBlock( {
const { toggleBlockHighlight } = useDispatch( blockEditorStore );

const blockInformation = useBlockDisplayInformation( clientId );
const blockTitle = blockInformation?.title || __( 'Untitled' );
const blockName = useSelect(
( select ) => select( blockEditorStore ).getBlockName( clientId ),
[ clientId ]
Expand All @@ -111,28 +111,19 @@ function ListViewBlock( {
level
);

let blockAriaLabel = __( 'Link' );
if ( blockInformation ) {
blockAriaLabel = isLocked
? sprintf(
// translators: %s: The title of the block. This string indicates a link to select the locked block.
__( '%s link (locked)' ),
blockInformation.title
)
: sprintf(
// translators: %s: The title of the block. This string indicates a link to select the block.
__( '%s link' ),
blockInformation.title
);
}

const settingsAriaLabel = blockInformation
const blockAriaLabel = isLocked
? sprintf(
// translators: %s: The title of the block.
__( 'Options for %s block' ),
blockInformation.title
// translators: %s: The title of the block. This string indicates a link to select the locked block.
__( '%s (locked)' ),
blockTitle
)
: __( 'Options' );
: blockTitle;

const settingsAriaLabel = sprintf(
// translators: %s: The title of the block.
__( 'Options for %s' ),
blockTitle
);

const { isTreeGridMounted, expand, collapse, BlockSettingsMenu } =
useListViewContext();
Expand Down Expand Up @@ -249,18 +240,13 @@ function ListViewBlock( {
id={ `list-view-block-${ clientId }` }
data-block={ clientId }
data-expanded={ canExpand ? isExpanded : undefined }
isExpanded={ canExpand ? isExpanded : undefined }
aria-selected={ !! isSelected || forceSelectionContentLock }
ref={ rowRef }
>
<TreeGridCell
className="block-editor-list-view-block__contents-cell"
colSpan={ colSpan }
ref={ cellRef }
aria-label={ blockAriaLabel }
aria-selected={ !! isSelected || forceSelectionContentLock }
aria-expanded={ canExpand ? isExpanded : undefined }
aria-describedby={ descriptionId }
>
{ ( { ref, tabIndex, onFocus } ) => (
<div className="block-editor-list-view-block__contents-container">
Expand All @@ -277,9 +263,10 @@ function ListViewBlock( {
currentlyEditingBlockInCanvas ? 0 : tabIndex
}
onFocus={ onFocus }
isExpanded={ isExpanded }
isExpanded={ canExpand ? isExpanded : undefined }
selectedClientIds={ selectedClientIds }
preventAnnouncement={ preventAnnouncement }
ariaLabel={ blockAriaLabel }
ariaDescribedBy={ descriptionId }
/>
<div
className="block-editor-list-view-block-select-button__description"
Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/src/components/list-view/leaf.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const ListViewLeaf = forwardRef(
level={ level }
positionInSet={ position }
setSize={ rowCount }
isExpanded={ undefined }
{ ...props }
>
{ children }
Expand Down
18 changes: 15 additions & 3 deletions packages/block-editor/src/components/list-view/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@
// Use position relative for row animation.
position: relative;

.components-button {
// When a row is expanded, retain the dark color.
&[aria-expanded="true"] {
color: $gray-900;
}

// Ensure that on hover, the admin color is still used.
&:hover {
color: var(--wp-admin-theme-color);
}
}

// The background has to be applied to the td, not tr, or border-radius won't work.
&.is-selected td {
background: var(--wp-admin-theme-color);
Expand Down Expand Up @@ -93,7 +105,7 @@
&.is-branch-selected.is-first-selected td:last-child {
border-top-right-radius: $radius-block-ui;
}
&[aria-expanded="false"] {
&[data-expanded="false"] {
&.is-branch-selected.is-first-selected td:first-child {
border-top-left-radius: $radius-block-ui;
}
Expand Down Expand Up @@ -380,15 +392,15 @@ $block-navigation-max-indent: 8;
}

// Point downwards when open.
.block-editor-list-view-leaf[aria-expanded="true"] .block-editor-list-view__expander svg {
.block-editor-list-view-leaf[data-expanded="true"] .block-editor-list-view__expander svg {
visibility: visible;
transition: transform 0.2s ease;
transform: rotate(90deg);
@include reduce-motion("transition");
}

// Point rightwards when closed
.block-editor-list-view-leaf[aria-expanded="false"] .block-editor-list-view__expander svg {
.block-editor-list-view-leaf[data-expanded="false"] .block-editor-list-view__expander svg {
visibility: visible;
transform: rotate(0deg);
transition: transform 0.2s ease;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export default function useBlockSelection() {
}

if ( label ) {
speak( label );
speak( label, 'assertive' );
}
},
[
Expand Down
4 changes: 4 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Enhancements

- `TreeGrid`: Modify keyboard navigation code to use a data-expanded attribute if aria-expanded is to be controlled outside of the TreeGrid component ([#48461](https://github.com/WordPress/gutenberg/pull/48461)).

### Documentation

- `Autocomplete`: Add heading and fix type for `onReplace` in README. ([#49798](https://github.com/WordPress/gutenberg/pull/49798)).
Expand Down
9 changes: 7 additions & 2 deletions packages/components/src/tree-grid/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ function UnforwardedTreeGrid(
const canExpandCollapse = 0 === currentColumnIndex;
const cannotFocusNextColumn =
canExpandCollapse &&
activeRow.getAttribute( 'aria-expanded' ) === 'false' &&
( activeRow.getAttribute( 'data-expanded' ) === 'false' ||
activeRow.getAttribute( 'aria-expanded' ) === 'false' ) &&
keyCode === RIGHT;

if ( ( [ LEFT, RIGHT ] as number[] ).includes( keyCode ) ) {
Expand All @@ -112,6 +113,8 @@ function UnforwardedTreeGrid(
// Left:
// If a row is focused, and it is expanded, collapses the current row.
if (
activeRow.getAttribute( 'data-expanded' ) ===
'true' ||
activeRow.getAttribute( 'aria-expanded' ) === 'true'
) {
onCollapseRow( activeRow );
Expand Down Expand Up @@ -151,8 +154,10 @@ function UnforwardedTreeGrid(
// Right:
// If a row is focused, and it is collapsed, expands the current row.
if (
activeRow.getAttribute( 'data-expanded' ) ===
'false' ||
activeRow.getAttribute( 'aria-expanded' ) ===
'false'
'false'
) {
onExpandRow( activeRow );
event.preventDefault();
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/specs/editor/blocks/columns.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test.describe( 'Columns', () => {
// block column add
await page
.locator(
'role=treegrid[name="Block navigation structure"i] >> role=gridcell[name="Column link"i]'
'role=treegrid[name="Block navigation structure"i] >> role=gridcell[name="Column"i]'
)
.first()
.click();
Expand Down
Loading

0 comments on commit 6f96394

Please sign in to comment.