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

Add back old Navigation and File blocks JavaScript implementation when Gutenberg is not installed #52553

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/block-library/package.json
Expand Up @@ -68,6 +68,7 @@
"fast-average-color": "^9.1.1",
"fast-deep-equal": "^3.1.3",
"memize": "^2.1.0",
"micromodal": "^0.4.10",
"remove-accents": "^0.4.2",
"uuid": "^8.3.0"
},
Expand Down
17 changes: 17 additions & 0 deletions packages/block-library/src/file/index.php
Expand Up @@ -5,6 +5,23 @@
* @package WordPress
*/

if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ) {
/**
* Replaces view script for the File block with version using Interactivity API.
*
* @param array $metadata Block metadata as read in via block.json.
*
* @return array Filtered block type metadata.
*/
function gutenberg_block_core_file_update_interactive_view_script( $metadata ) {
if ( 'core/file' === $metadata['name'] ) {
$metadata['viewScript'] = array( 'file:./view-interactivity.min.js' );
}
return $metadata;
}
add_filter( 'block_type_metadata', 'gutenberg_block_core_file_update_interactive_view_script', 10, 1 );
}

/**
* When the `core/file` block is rendering, check if we need to enqueue the `'wp-block-file-view` script.
*
Expand Down
18 changes: 18 additions & 0 deletions packages/block-library/src/file/view-interactivity.js
@@ -0,0 +1,18 @@
/**
* WordPress dependencies
*/
import { store } from '@wordpress/interactivity';
/**
* Internal dependencies
*/
import { browserSupportsPdfs as hasPdfPreview } from './utils';

store( {
selectors: {
core: {
file: {
hasPdfPreview,
},
},
},
} );
19 changes: 5 additions & 14 deletions packages/block-library/src/file/view.js
@@ -1,18 +1,9 @@
/**
* WordPress dependencies
*/
import { store } from '@wordpress/interactivity';
/**
* Internal dependencies
*/
import { browserSupportsPdfs as hasPdfPreview } from './utils';
import { hidePdfEmbedsOnUnsupportedBrowsers } from './utils';

store( {
selectors: {
core: {
file: {
hasPdfPreview,
},
},
},
} );
document.addEventListener(
'DOMContentLoaded',
hidePdfEmbedsOnUnsupportedBrowsers
);
2 changes: 1 addition & 1 deletion packages/block-library/src/navigation/block.json
Expand Up @@ -134,7 +134,7 @@
},
"interactivity": true
},
"viewScript": "file:./view.min.js",
"viewScript": [ "file:./view.min.js", "file:./view-modal.min.js" ],
"editorStyle": "wp-block-navigation-editor",
"style": "wp-block-navigation"
}
21 changes: 18 additions & 3 deletions packages/block-library/src/navigation/index.php
Expand Up @@ -138,6 +138,21 @@ function block_core_navigation_add_directives_to_submenu( $w, $block_attributes
}
return $w->get_updated_html();
};

/**
* Replaces view script for the Navigation block with version using Interactivity API.
*
* @param array $metadata Block metadata as read in via block.json.
*
* @return array Filtered block type metadata.
*/
function gutenberg_block_core_navigation_update_interactive_view_script( $metadata ) {
if ( 'core/navigation' === $metadata['name'] ) {
$metadata['viewScript'] = array( 'file:./view-interactivity.min.js' );
}
return $metadata;
}
add_filter( 'block_type_metadata', 'gutenberg_block_core_navigation_update_interactive_view_script', 10, 1 );
}


Expand Down Expand Up @@ -746,11 +761,11 @@ function render_block_core_navigation( $attributes, $content, $block ) {
}

$responsive_container_markup = sprintf(
'<button aria-haspopup="true" %3$s class="%6$s" %11$s>%9$s</button>
'<button aria-haspopup="true" %3$s class="%6$s" data-micromodal-trigger="%1$s" %11$s>%9$s</button>
<div class="%5$s" style="%7$s" id="%1$s" %12$s>
<div class="wp-block-navigation__responsive-close" tabindex="-1">
<div class="wp-block-navigation__responsive-close" tabindex="-1" data-micromodal-close>
<div class="wp-block-navigation__responsive-dialog" aria-label="%8$s" %13$s>
<button %4$s class="wp-block-navigation__responsive-container-close" %14$s>%10$s</button>
<button %4$s data-micromodal-close class="wp-block-navigation__responsive-container-close" %14$s>%10$s</button>
<div class="wp-block-navigation__responsive-container-content" id="%1$s-content">
%2$s
</div>
Expand Down
196 changes: 196 additions & 0 deletions packages/block-library/src/navigation/view-interactivity.js
@@ -0,0 +1,196 @@
/**
* WordPress dependencies
*/
import { store as wpStore } from '@wordpress/interactivity';

const focusableSelectors = [
'a[href]',
'input:not([disabled]):not([type="hidden"]):not([aria-hidden])',
'select:not([disabled]):not([aria-hidden])',
'textarea:not([disabled]):not([aria-hidden])',
'button:not([disabled]):not([aria-hidden])',
'[contenteditable]',
'[tabindex]:not([tabindex^="-"])',
];

const openMenu = ( store, menuOpenedOn ) => {
const { context, ref, selectors } = store;
selectors.core.navigation.menuOpenedBy( store )[ menuOpenedOn ] = true;
context.core.navigation.previousFocus = ref;
if ( context.core.navigation.type === 'overlay' ) {
// Add a `has-modal-open` class to the <html> root.
document.documentElement.classList.add( 'has-modal-open' );
}
};

const closeMenu = ( store, menuClosedOn ) => {
const { context, selectors } = store;
selectors.core.navigation.menuOpenedBy( store )[ menuClosedOn ] = false;
// Check if the menu is still open or not.
if ( ! selectors.core.navigation.isMenuOpen( store ) ) {
if (
context.core.navigation.modal?.contains(
window.document.activeElement
)
) {
context.core.navigation.previousFocus.focus();
}
context.core.navigation.modal = null;
context.core.navigation.previousFocus = null;
if ( context.core.navigation.type === 'overlay' ) {
document.documentElement.classList.remove( 'has-modal-open' );
}
}
};

wpStore( {
effects: {
core: {
navigation: {
initMenu: ( store ) => {
const { context, selectors, ref } = store;
if ( selectors.core.navigation.isMenuOpen( store ) ) {
const focusableElements =
ref.querySelectorAll( focusableSelectors );
context.core.navigation.modal = ref;
context.core.navigation.firstFocusableElement =
focusableElements[ 0 ];
context.core.navigation.lastFocusableElement =
focusableElements[ focusableElements.length - 1 ];
}
},
focusFirstElement: ( store ) => {
const { selectors, ref } = store;
if ( selectors.core.navigation.isMenuOpen( store ) ) {
ref.querySelector(
'.wp-block-navigation-item > *:first-child'
).focus();
}
},
},
},
},
selectors: {
core: {
navigation: {
roleAttribute: ( store ) => {
const { context, selectors } = store;
return context.core.navigation.type === 'overlay' &&
selectors.core.navigation.isMenuOpen( store )
? 'dialog'
: '';
},
isMenuOpen: ( { context } ) =>
// The menu is opened if either `click`, `hover` or `focus` is true.
Object.values(
context.core.navigation[
context.core.navigation.type === 'overlay'
? 'overlayOpenedBy'
: 'submenuOpenedBy'
]
).filter( Boolean ).length > 0,
menuOpenedBy: ( { context } ) =>
context.core.navigation[
context.core.navigation.type === 'overlay'
? 'overlayOpenedBy'
: 'submenuOpenedBy'
],
},
},
},
actions: {
core: {
navigation: {
openMenuOnHover( store ) {
const { navigation } = store.context.core;
if (
navigation.type === 'submenu' &&
// Only open on hover if the overlay is closed.
Object.values(
navigation.overlayOpenedBy || {}
).filter( Boolean ).length === 0
)
openMenu( store, 'hover' );
},
closeMenuOnHover( store ) {
closeMenu( store, 'hover' );
},
openMenuOnClick( store ) {
openMenu( store, 'click' );
},
closeMenuOnClick( store ) {
closeMenu( store, 'click' );
closeMenu( store, 'focus' );
},
openMenuOnFocus( store ) {
openMenu( store, 'focus' );
},
toggleMenuOnClick: ( store ) => {
const { selectors } = store;
const menuOpenedBy =
selectors.core.navigation.menuOpenedBy( store );
if ( menuOpenedBy.click || menuOpenedBy.focus ) {
closeMenu( store, 'click' );
closeMenu( store, 'focus' );
} else {
openMenu( store, 'click' );
}
},
handleMenuKeydown: ( store ) => {
const { context, selectors, event } = store;
if (
selectors.core.navigation.menuOpenedBy( store ).click
) {
// If Escape close the menu.
if ( event?.key === 'Escape' ) {
closeMenu( store, 'click' );
closeMenu( store, 'focus' );
return;
}

// Trap focus if it is an overlay (main menu).
if (
context.core.navigation.type === 'overlay' &&
event.key === 'Tab'
) {
// If shift + tab it change the direction.
if (
event.shiftKey &&
window.document.activeElement ===
context.core.navigation
.firstFocusableElement
) {
event.preventDefault();
context.core.navigation.lastFocusableElement.focus();
} else if (
! event.shiftKey &&
window.document.activeElement ===
context.core.navigation.lastFocusableElement
) {
event.preventDefault();
context.core.navigation.firstFocusableElement.focus();
}
}
}
},
handleMenuFocusout: ( store ) => {
const { context, event } = store;
// If focus is outside modal, and in the document, close menu
// event.target === The element losing focus
// event.relatedTarget === The element receiving focus (if any)
// When focusout is outsite the document,
// `window.document.activeElement` doesn't change.
if (
! context.core.navigation.modal?.contains(
event.relatedTarget
) &&
event.target !== window.document.activeElement
) {
closeMenu( store, 'click' );
closeMenu( store, 'focus' );
}
},
},
},
},
} );