Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List view: Refactor ARIA attributes #48461

Merged
merged 26 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
0034510
Fix ARIA attributes.
alexstine Feb 26, 2023
e28899c
Add assertive for selection read.
alexstine Feb 26, 2023
ebd1773
Merge branch 'trunk' of github.com:wordpress/gutenberg into update/li…
alexstine Feb 27, 2023
ba860a1
Merge branch 'trunk' of github.com:wordpress/gutenberg into update/li…
alexstine Mar 19, 2023
9749780
Merge branch 'trunk' of github.com:wordpress/gutenberg into update/li…
alexstine Mar 31, 2023
21fc852
Merge branch 'trunk' of github.com:wordpress/gutenberg into update/li…
alexstine Apr 3, 2023
55f43ac
Remove useless useEffect. Refactor expanded to use isExpanded attribute.
alexstine Apr 4, 2023
88788ba
Add prop to disable aria-expanded on tree grid tr so I can override o…
alexstine Apr 4, 2023
4c30383
Try to fix failing e2e test
andrewserong Apr 4, 2023
4c2b450
Update list view tests
andrewserong Apr 4, 2023
08967ae
Restore removed useEffect.
alexstine Apr 4, 2023
54797d5
Revert TreeGrid changes.
alexstine Apr 4, 2023
0833362
Finish revert.
alexstine Apr 4, 2023
115cfdc
Eliminate aria-expanded on main tr.
alexstine Apr 4, 2023
0630cf1
Add data-expanded back to TreeGrid. Need to detect when to expand via…
alexstine Apr 4, 2023
b56270b
Fix expander.
alexstine Apr 5, 2023
90f9425
Move isExpanded undefined to tr directly.
alexstine Apr 5, 2023
eed5466
Fix E2E.
alexstine Apr 5, 2023
17cdc5b
Retain dark color for button when expanded
andrewserong Apr 5, 2023
3c9068c
Add title fallback.
alexstine Apr 5, 2023
714f331
Save some verbosity on block options button.
alexstine Apr 5, 2023
87b8c8c
Fix E2E.
alexstine Apr 5, 2023
e7a577b
Add components changelog entry.
alexstine Apr 5, 2023
cffe7d1
Fix conflicts.
alexstine Apr 5, 2023
f2abb6a
Refresh.
alexstine Apr 14, 2023
7e5a6ba
Refresh, again.
alexstine Apr 14, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ function ListViewBlockSelectButton(
onDragStart,
onDragEnd,
draggable,
isExpanded,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why this component was no longer receiving this prop.

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 ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I notice that the condition to check if blockInformation is truthy has been removed. From a little digging, I think the check was added back in #38775 to deal with an issue with useSelect where it's possible that blockInformation will be null in some circumstances. So, I'm not too sure, but it might be necessary to keep the condition around. Just pinging @talldan who originally implemented the fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that is the case, please check the variable directly below this line. It was not included in this conditional and still used blockInformation to get the title for building the options aria-label.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the case of the below settingsAriaLabel it's doing a check for blockInformation within its ternary before attempting to access blockInformation.title, so it didn't need to be included in the if block. For blockAriaLabel both outcomes of the ternary are accessing properties of blockInformation, so if blockInformation were to be null then an error would be thrown.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not too sure if that check is still needed, though, but because #38775 introduced the check, it'd probaly be good to verify before removing the check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yeah, I totally missed that sneaky conditional on that line. NVM.

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 }
andrewserong marked this conversation as resolved.
Show resolved Hide resolved
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 }
andrewserong marked this conversation as resolved.
Show resolved Hide resolved
>
{ ( { 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 }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to match the others.

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' );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to this PR but this required a fix.

}
},
[
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' ||
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to add this attribute check here or else we'll never know if it is okay to expand or collapse with the keyboard. Should probably document an example in README. Likely need to update unit tests.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @alexstine , would you be able to work on a follow-up where you add info to the README, or (even better) add a Storybook example?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ciampo Storybook is tricky for me to use so doubt I would be much help making an example that might not be testable by me. I only use Storybook when stuff is not testable any other way because accessibility is not great and components used in-practice are much easier to test.

For the README example, let me open a follow-up. That should be easy enough.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds reasonable, thank you! I'll see if I can help and add the Storybook example myself

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