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

Remove role attribute when set to null in data-wp-bind #54608

Merged
merged 7 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 1 addition & 3 deletions packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -713,10 +713,8 @@ function render_block_core_navigation( $attributes, $content, $block ) {
tabindex="-1"
';
$responsive_dialog_directives = '
data-wp-bind--aria-modal="selectors.core.navigation.isMenuOpen"
aria-modal="false"
data-wp-bind--aria-modal="selectors.core.navigation.ariaModal"
data-wp-bind--role="selectors.core.navigation.roleAttribute"
role=""
data-wp-effect="effects.core.navigation.focusFirstElement"
';
$close_button_directives = '
Expand Down
9 changes: 8 additions & 1 deletion packages/block-library/src/navigation/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,14 @@ wpStore( {
return context.core.navigation.type === 'overlay' &&
selectors.core.navigation.isMenuOpen( store )
? 'dialog'
: '';
: null;
},
ariaModal: ( store ) => {
const { context, selectors } = store;
return context.core.navigation.type === 'overlay' &&
selectors.core.navigation.isMenuOpen( store )
? 'true'
: null;
},
isMenuOpen: ( { context } ) =>
// The menu is opened if either `click`, `hover` or `focus` is true.
Expand Down
20 changes: 19 additions & 1 deletion packages/interactivity/src/directives.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
/**
* External dependencies
*/
import { useContext, useMemo, useEffect, useRef } from 'preact/hooks';
import {
useContext,
useMemo,
useEffect,
useRef,
useLayoutEffect,
} from 'preact/hooks';
import { deepSignal, peek } from 'deepsignal';

/**
Expand Down Expand Up @@ -218,6 +224,17 @@ export default () => {
context: contextValue,
} );
element.props[ attribute ] = result;
// Preact doesn't handle the `role` attribute properly, as it doesn't remove it when `null`.
// We need this workaround until the following issue is solved:
// https://github.com/preactjs/preact/issues/4136
useLayoutEffect( () => {
if (
attribute === 'role' &&
( result === null || result === undefined )
) {
element.ref.current.removeAttribute( attribute );
}
}, [ attribute, result ] );

// This seems necessary because Preact doesn't change the attributes
// on the hydration, so we have to do it manually. It doesn't need
Expand All @@ -241,6 +258,7 @@ export default () => {
attribute !== 'download' &&
attribute !== 'rowSpan' &&
attribute !== 'colSpan' &&
attribute !== 'role' &&
attribute in el
) {
try {
Expand Down
Loading