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

Enable open on click for Page List inside Navigation. #34675

Merged
merged 48 commits into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
2d1f90e
Add a Dropdown block for use in Navigation
tellthemachines Jul 30, 2021
7821084
display dropdowns below buttons in editor
tellthemachines Jul 30, 2021
ab180c9
Add open on click functionality.
tellthemachines Aug 5, 2021
7ce9bca
Adjustments to click behaviour.
tellthemachines Aug 13, 2021
e48c0b7
Fix submenu logic
tellthemachines Aug 18, 2021
95a6f3f
Transform nav links to submenus
tellthemachines Aug 20, 2021
6e6c908
Change block name to Submenu and update classnames
tellthemachines Aug 20, 2021
89f33e5
Add submenu transform to navigation link.
tellthemachines Aug 24, 2021
4fb0716
Remove button default styling
tellthemachines Aug 24, 2021
8ed3e1d
Don't split at end.
tellthemachines Aug 24, 2021
fedffa3
Add block fixture.
tellthemachines Aug 24, 2021
451b70f
Fix php lint errors.
tellthemachines Aug 24, 2021
0cb9fe6
Update submenu icon classname.
tellthemachines Aug 25, 2021
520dac7
Use Submenu in Page List conversion to blocks.
tellthemachines Aug 25, 2021
c1f8bac
Don't lose children when transforming to Submenu.
tellthemachines Aug 27, 2021
f0e00aa
Fix link conversion in Page List block.
tellthemachines Aug 30, 2021
7112200
Add link to empty submenu and default navigation class.
tellthemachines Aug 30, 2021
3e32cd4
Introduce hover/click toggle on Navigation.
tellthemachines Aug 31, 2021
beb977f
Don't add link in submenu straightaway.
tellthemachines Sep 2, 2021
b14c119
Open link control on parent element when hover is set.
tellthemachines Sep 2, 2021
0fee1ca
Always show icons when opening on click.
tellthemachines Sep 2, 2021
1e39756
Open submenus on click in the editor.
tellthemachines Sep 2, 2021
f1c9696
Fix button styles and change toggle label.
tellthemachines Sep 3, 2021
313f757
Update navigation fixture and fix php lint errors.
tellthemachines Sep 3, 2021
1764eba
Fix php lint error.
tellthemachines Sep 3, 2021
c6f531e
Fix php lint error.
tellthemachines Sep 3, 2021
19f9901
Try hiding submenu in block inserter.
tellthemachines Sep 6, 2021
e8e7f04
Update menu items to blocks conversion.
tellthemachines Sep 6, 2021
ea847ad
Update menu items to blocks test.
tellthemachines Sep 7, 2021
71bbd83
Fix navigation link top level item status.
tellthemachines Sep 7, 2021
0738c48
Update and add new e2e test for open on click.
tellthemachines Sep 7, 2021
aec69a2
Fix undo/redo trap.
tellthemachines Sep 8, 2021
04f50f7
Fix styling for nested submenus in the editor.
tellthemachines Sep 8, 2021
55f7a06
Remove obsolete lint ignore
tellthemachines Sep 8, 2021
2886cb7
Display block label in list view.
tellthemachines Sep 8, 2021
2d30be1
Fix dropdown z-index
tellthemachines Sep 8, 2021
c414417
Switch toggle position
tellthemachines Sep 8, 2021
14eabf8
Enable open on click for Page List inside Nav.
tellthemachines Sep 9, 2021
f4185df
Add doc comments.
tellthemachines Sep 9, 2021
cc441be
Update e2e snapshot.
tellthemachines Sep 10, 2021
484e329
Simulate open on click in the editor.
tellthemachines Sep 10, 2021
5ad5e19
Merge branch 'trunk' into add/page-list-click-open
tellthemachines Sep 13, 2021
7b25bc7
Fix toggle dropdown styles.
tellthemachines Sep 13, 2021
14c7506
Target direct parent in toggle script.
tellthemachines Sep 13, 2021
e72add6
Update effect when context changes.
tellthemachines Sep 13, 2021
3ca15be
Update e2e snapshot.
tellthemachines Sep 13, 2021
68cd9eb
Fix button styles.
tellthemachines Sep 13, 2021
a757ebc
Merge branch 'trunk' into add/page-list-click-open
tellthemachines Sep 14, 2021
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: 0 additions & 1 deletion packages/block-library/src/navigation-submenu/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
"reusable": false,
"html": false
},
"viewScript": "file:./view.min.js",
"editorStyle": "wp-block-navigation-submenu-editor",
"style": "wp-block-navigation-submenu"
}
3 changes: 0 additions & 3 deletions packages/block-library/src/navigation-submenu/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,6 @@ function block_core_navigation_submenu_render_submenu_icon() {
* @return string Returns the post content with the legacy widget added.
*/
function render_block_core_navigation_submenu( $attributes, $content, $block ) {
if ( ! wp_script_is( 'wp-block-navigation-submenu-view' ) ) {
wp_enqueue_script( 'wp-block-navigation-submenu-view' );
}

$navigation_link_has_id = isset( $attributes['id'] ) && is_numeric( $attributes['id'] );
$is_post_type = isset( $attributes['kind'] ) && 'post-type' === $attributes['kind'];
Expand Down
16 changes: 0 additions & 16 deletions packages/block-library/src/navigation-submenu/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,9 @@
position: relative;
display: flex;

button.wp-block-navigation__submenu-icon.wp-block-navigation__submenu-icon.wp-block-navigation__submenu-icon.wp-block-navigation__submenu-icon {
padding: $grid-unit $grid-unit-20;
background-color: inherit;
color: currentColor;
border: none;
}

.wp-block-navigation__submenu-icon svg {
stroke: currentColor;
}

// Show submenus on click.
.wp-block-navigation-submenu__toggle[aria-expanded="true"] + .wp-block-navigation__submenu-container {
visibility: visible;
opacity: 1;
width: auto;
height: auto;
min-width: 200px;
}
}

button.wp-block-navigation-item__content {
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ function render_block_core_navigation( $attributes, $content, $block ) {

unset( $attributes['rgbTextColor'], $attributes['rgbBackgroundColor'] );

$should_load_view_script = ! empty( $attributes['isResponsive'] ) && ! wp_script_is( 'wp-block-navigation-view' );
$should_load_view_script = ! wp_script_is( 'wp-block-navigation-view' ) && ( ! empty( $attributes['isResponsive'] ) || $attributes['openSubmenusOnClick'] || $attributes['showSubmenuIcon'] );
if ( $should_load_view_script ) {
wp_enqueue_script( 'wp-block-navigation-view' );
}
Expand Down
17 changes: 17 additions & 0 deletions packages/block-library/src/navigation/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@
stroke: currentColor;
}
}

// Styles for button toggles.
button.wp-block-navigation__submenu-icon.wp-block-navigation__submenu-icon.wp-block-navigation__submenu-icon.wp-block-navigation__submenu-icon {
padding: $grid-unit $grid-unit-20;
Copy link
Contributor

Choose a reason for hiding this comment

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

In my testing, this specificity wasn't necessary, and the added padding makes the chevron look very different from editor and frontend. I have a followup in #35030.

background-color: inherit;
color: currentColor;
border: none;
}
}

// Styles for submenu flyout.
Expand Down Expand Up @@ -187,6 +195,15 @@
height: auto;
min-width: 200px;
}

// Show submenus on click.
.wp-block-navigation-submenu__toggle[aria-expanded="true"] + .wp-block-navigation__submenu-container {
visibility: visible;
opacity: 1;
width: auto;
height: auto;
min-width: 200px;
}
}

// Submenu indentation when there's a background.
Expand Down
67 changes: 67 additions & 0 deletions packages/block-library/src/navigation/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import MicroModal from 'micromodal';

// Responsive navigation toggle.
function navigationToggleModal( modal ) {
const triggerButton = document.querySelector(
`button[data-micromodal-trigger="${ modal.id }"]`
Expand All @@ -25,3 +26,69 @@ MicroModal.init( {
onClose: navigationToggleModal,
openClass: 'is-menu-open',
} );

// Open on click functionality.
function closeSubmenus( element ) {
element
.querySelectorAll( '[aria-expanded="true"]' )
.forEach( function ( toggle ) {
toggle.setAttribute( 'aria-expanded', 'false' );
} );
}

function toggleSubmenuOnClick( event ) {
const buttonToggle = event.target.closest( '[aria-expanded]' );
const isSubmenuOpen = buttonToggle.getAttribute( 'aria-expanded' );

if ( isSubmenuOpen === 'true' ) {
closeSubmenus( buttonToggle.closest( '.wp-block-navigation-item' ) );
} else {
// Close all sibling submenus.
const parentElement = buttonToggle.closest(
'.wp-block-navigation-item'
);
const navigationParent = buttonToggle.closest(
'.wp-block-navigation__submenu-container, .wp-block-navigation__container, .wp-block-page-list'
);
navigationParent
.querySelectorAll( '.wp-block-navigation-item' )
.forEach( function ( child ) {
if ( child !== parentElement ) {
closeSubmenus( child );
}
} );
// Open submenu.
buttonToggle.setAttribute( 'aria-expanded', 'true' );
}
}

const submenuButtons = document.querySelectorAll(
'.wp-block-navigation-submenu__toggle'
);

submenuButtons.forEach( function ( button ) {
button.addEventListener( 'click', toggleSubmenuOnClick );
} );

// Close on click outside.
document.addEventListener( 'click', function ( event ) {
const navigationBlocks = document.querySelectorAll(
'.wp-block-navigation'
);
navigationBlocks.forEach( function ( block ) {
if ( ! block.contains( event.target ) ) {
closeSubmenus( block );
}
} );
} );
// Close on focus outside.
document.addEventListener( 'keyup', function ( event ) {
const submenuBlocks = document.querySelectorAll(
'.wp-block-navigation-item.has-child'
);
submenuBlocks.forEach( function ( block ) {
if ( ! block.contains( event.target ) ) {
closeSubmenus( block );
}
} );
} );
9 changes: 8 additions & 1 deletion packages/block-library/src/page-list/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
},
"isNavigationChild": {
"type": "boolean"
},
"showSubmenuIcon": {
"type": "boolean"
},
"openSubmenusOnClick" : {
"type": "boolean"
}
},
"usesContext": [
Expand All @@ -47,7 +53,8 @@
"fontSize",
"customFontSize",
"showSubmenuIcon",
"style"
"style",
"openSubmenusOnClick"
],
"supports": {
"reusable": false,
Expand Down
14 changes: 9 additions & 5 deletions packages/block-library/src/page-list/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ export default function PageListEdit( {
context.customOverlayBackgroundColor,
] );

const { textColor, backgroundColor, showSubmenuIcon, style } =
context || {};
const { textColor, backgroundColor, style } = context || {};

const [ allowConvertToLinks, setAllowConvertToLinks ] = useState( false );

Expand All @@ -83,7 +82,6 @@ export default function PageListEdit( {
'background-color',
backgroundColor
) ]: !! backgroundColor,
'show-submenu-icons': !! showSubmenuIcon,
} ),
style: { ...style?.color },
} );
Expand All @@ -100,8 +98,12 @@ export default function PageListEdit( {
);

useEffect( () => {
setAttributes( { isNavigationChild: isParentNavigation } );
}, [] );
setAttributes( {
isNavigationChild: isParentNavigation,
openSubmenusOnClick: !! context.openSubmenusOnClick,
showSubmenuIcon: !! context.showSubmenuIcon,
} );
}, [ context.openSubmenusOnClick, context.showSubmenuIcon ] );

useEffect( () => {
if ( isParentNavigation ) {
Expand Down Expand Up @@ -129,6 +131,8 @@ export default function PageListEdit( {
const attributesWithParentStatus = {
...attributes,
isNavigationChild: isParentNavigation,
openSubmenusOnClick: !! context.openSubmenusOnClick,
showSubmenuIcon: !! context.showSubmenuIcon,
};

return (
Expand Down
14 changes: 14 additions & 0 deletions packages/block-library/src/page-list/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,17 @@
margin-left: $grid-unit-15;
}
}

// Simulate open on click behaviour in the editor by opening on focus instead.

.wp-block-page-list {
.open-on-click:focus-within {
> .wp-block-navigation__submenu-container {
visibility: visible;
opacity: 1;
width: auto;
height: auto;
min-width: 200px;
}
}
}
44 changes: 32 additions & 12 deletions packages/block-library/src/page-list/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ function block_core_page_list_build_css_font_sizes( $context ) {
/**
* Outputs Page list markup from an array of pages with nested children.
*
* @param boolean $open_submenus_on_click Whether to open submenus on click instead of hover.
* @param boolean $show_submenu_icons Whether to show submenu indicator icons.
* @param boolean $is_navigation_child If block is a child of Navigation block.
* @param array $nested_pages The array of nested pages.
* @param array $active_page_ancestor_ids An array of ancestor ids for active page.
Expand All @@ -136,7 +138,7 @@ function block_core_page_list_build_css_font_sizes( $context ) {
*
* @return string List markup.
*/
function block_core_page_list_render_nested_page_list( $is_navigation_child, $nested_pages, $active_page_ancestor_ids = array(), $colors = array(), $depth = 0 ) {
function block_core_page_list_render_nested_page_list( $open_submenus_on_click, $show_submenu_icons, $is_navigation_child, $nested_pages, $active_page_ancestor_ids = array(), $colors = array(), $depth = 0 ) {
if ( empty( $nested_pages ) ) {
return;
}
Expand All @@ -152,6 +154,12 @@ function block_core_page_list_render_nested_page_list( $is_navigation_child, $ne

if ( $is_navigation_child ) {
$css_class .= ' wp-block-navigation-item';

if ( $open_submenus_on_click ) {
$css_class .= ' open-on-click';
} elseif ( $show_submenu_icons ) {
$css_class .= ' open-on-hover-click';
}
}

$navigation_child_content_class = $is_navigation_child ? ' wp-block-navigation-item__content' : '';
Expand All @@ -165,20 +173,32 @@ function block_core_page_list_render_nested_page_list( $is_navigation_child, $ne
}

$markup .= '<li class="wp-block-pages-list__item' . $css_class . '"' . $style_attribute . '>';
$markup .= '<a class="wp-block-pages-list__item__link' . $navigation_child_content_class . ' "href="' . esc_url( $page['link'] ) . '">' . wp_kses(
$page['title'],
wp_kses_allowed_html( 'post' )
) . '</a>';

if ( isset( $page['children'] ) && $is_navigation_child && $open_submenus_on_click ) {
$markup .= '<button class="' . $navigation_child_content_class . ' wp-block-navigation-submenu__toggle" aria-expanded="false">' . wp_kses(
$page['title'],
wp_kses_allowed_html( 'post' )
) . '<span class="wp-block-page-list__submenu-icon wp-block-navigation__submenu-icon"><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 12 12" fill="none" role="img" aria-hidden="true" focusable="false"><path d="M1.50002 4L6.00002 8L10.5 4" stroke-width="1.5"></path></svg></span>' .
'</button>';
} else {
$markup .= '<a class="wp-block-pages-list__item__link' . $navigation_child_content_class . ' "href="' . esc_url( $page['link'] ) . '">' . wp_kses(
$page['title'],
wp_kses_allowed_html( 'post' )
) . '</a>';
}

if ( isset( $page['children'] ) ) {
if ( $is_navigation_child ) {
if ( $is_navigation_child && $show_submenu_icons && ! $open_submenus_on_click ) {
$markup .= '<button class="wp-block-navigation__submenu-icon wp-block-navigation-submenu__toggle" aria-expanded="false">';
$markup .= '<span class="wp-block-page-list__submenu-icon wp-block-navigation__submenu-icon"><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 12 12" fill="none" role="img" aria-hidden="true" focusable="false"><path d="M1.50002 4L6.00002 8L10.5 4" stroke-width="1.5"></path></svg></span>';
$markup .= '</button>';
}
$markup .= '<ul class="submenu-container';
// Extra classname is added when the block is a child of Navigation.
if ( $is_navigation_child ) {
$markup .= ' wp-block-navigation__submenu-container';
}
$markup .= '">' . block_core_page_list_render_nested_page_list( $is_navigation_child, $page['children'], $active_page_ancestor_ids, $colors, $depth + 1 ) . '</ul>';
$markup .= '">' . block_core_page_list_render_nested_page_list( $open_submenus_on_click, $show_submenu_icons, $is_navigation_child, $page['children'], $active_page_ancestor_ids, $colors, $depth + 1 ) . '</ul>';
}
$markup .= '</li>';
}
Expand Down Expand Up @@ -274,13 +294,13 @@ function render_block_core_page_list( $attributes, $content, $block ) {

$is_navigation_child = array_key_exists( 'isNavigationChild', $attributes ) ? $attributes['isNavigationChild'] : ! empty( $block->context );

$wrapper_markup = '<ul %1$s>%2$s</ul>';
$open_submenus_on_click = array_key_exists( 'openSubmenusOnClick', $attributes ) ? $attributes['openSubmenusOnClick'] : false;

$items_markup = block_core_page_list_render_nested_page_list( $is_navigation_child, $nested_pages, $active_page_ancestor_ids, $colors );
$show_submenu_icons = array_key_exists( 'showSubmenuIcon', $attributes ) ? $attributes['showSubmenuIcon'] : false;

if ( $block->context && $block->context['showSubmenuIcon'] ) {
$css_classes .= ' show-submenu-icons';
}
$wrapper_markup = '<ul %1$s>%2$s</ul>';

$items_markup = block_core_page_list_render_nested_page_list( $open_submenus_on_click, $show_submenu_icons, $is_navigation_child, $nested_pages, $active_page_ancestor_ids, $colors );

$wrapper_attributes = get_block_wrapper_attributes(
array(
Expand Down
12 changes: 0 additions & 12 deletions packages/block-library/src/page-list/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,6 @@
.wp-block-navigation-item {
background-color: inherit;
}

// Submenu icon indicator.
// The specific styles for the submenu indicator are inherited from the navigation block style.
.wp-block-page-list__submenu-icon {
display: none;
}

.show-submenu-icons {
.wp-block-page-list__submenu-icon {
display: inline-block;
}
}
}

.is-vertical .wp-block-navigation__container,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ exports[`Navigation Creating from existing Menus creates an empty navigation blo

exports[`Navigation Creating from existing Pages allows a navigation block to be created using existing pages 1`] = `
"<!-- wp:navigation -->
<!-- wp:page-list {\\"isNavigationChild\\":true} /-->
<!-- wp:page-list {\\"isNavigationChild\\":true,\\"showSubmenuIcon\\":true,\\"openSubmenusOnClick\\":false} /-->
<!-- /wp:navigation -->"
`;

Expand Down