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 a Submenu block for use in Navigation #33775

Merged
merged 40 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
40 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
530e32a
Pointer cursor on button toggles.
tellthemachines Sep 10, 2021
fdde85d
Fix max nesting and top level link logic.
tellthemachines Sep 10, 2021
82d7ad7
Show in inserter.
tellthemachines Sep 13, 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
2 changes: 2 additions & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function gutenberg_reregister_core_block_types() {
'missing',
'more',
'navigation-link',
'navigation-submenu',
'nextpage',
'paragraph',
'preformatted',
Expand All @@ -59,6 +60,7 @@ function gutenberg_reregister_core_block_types() {
'loginout.php' => 'core/loginout',
'navigation.php' => 'core/navigation',
'navigation-link.php' => 'core/navigation-link',
'navigation-submenu.php' => 'core/navigation-submenu',
'home-link.php' => 'core/home-link',
'rss.php' => 'core/rss',
'search.php' => 'core/search',
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
@import "./more/editor.scss";
@import "./navigation/editor.scss";
@import "./navigation-link/editor.scss";
@import "./navigation-submenu/editor.scss";
@import "./nextpage/editor.scss";
@import "./page-list/editor.scss";
@import "./paragraph/editor.scss";
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import * as html from './html';
import * as mediaText from './media-text';
import * as navigation from './navigation';
import * as navigationLink from './navigation-link';
import * as navigationSubmenu from './navigation-submenu';
import * as homeLink from './home-link';
import * as latestComments from './latest-comments';
import * as latestPosts from './latest-posts';
Expand Down Expand Up @@ -231,6 +232,7 @@ export const __experimentalRegisterExperimentalCoreBlocks =
navigation,
navigationLink,
homeLink,
navigationSubmenu,

// Register Full Site Editing Blocks.
...( enableFSEBlocks
Expand Down
28 changes: 18 additions & 10 deletions packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ export default function NavigationLinkEdit( {
const { showSubmenuIcon } = context;
const { saveEntityRecord } = useDispatch( coreStore );
const {
insertBlock,
replaceBlock,
__unstableMarkNextChangeAsNotPersistent,
} = useDispatch( blockEditorStore );
const [ isLinkOpen, setIsLinkOpen ] = useState( false );
Expand All @@ -302,18 +302,19 @@ export default function NavigationLinkEdit( {
const ref = useRef();

const {
innerBlocks,
isAtMaxNesting,
isTopLevelLink,
isParentOfSelectedBlock,
isImmediateParentOfSelectedBlock,
hasDescendants,
selectedBlockHasDescendants,
numberOfDescendants,
userCanCreatePages,
userCanCreatePosts,
} = useSelect(
( select ) => {
const {
getBlocks,
getClientIdsOfDescendants,
hasSelectedInnerBlock,
getSelectedBlockClientId,
Expand All @@ -326,11 +327,16 @@ export default function NavigationLinkEdit( {
.length;

return {
innerBlocks: getBlocks( clientId ),
isAtMaxNesting:
getBlockParentsByBlockName( clientId, name ).length >=
Copy link
Contributor

Choose a reason for hiding this comment

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

Should name be expanded to take into account the submenu block the same way isTopLevelLink does?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes! Thanks, totally missed that 😅

MAX_NESTING,
// Temporary fix until navigation link submenus are properly deprecated.
isTopLevelLink:
getBlockParentsByBlockName( clientId, name ).length === 0,
getBlockParentsByBlockName( clientId, [
name,
'core/navigation-submenu',
] ).length === 0,
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know that it's possible to deprecate from one block type to another. 🤔

It might also be more flexible to make this check if the parent is a core/navigation block. something like getBlockName( getRootClientId( clientId ) ) === 'core/navigation'.

isParentOfSelectedBlock: hasSelectedInnerBlock(
clientId,
true
Expand All @@ -343,7 +349,6 @@ export default function NavigationLinkEdit( {
selectedBlockHasDescendants: !! getClientIdsOfDescendants( [
selectedBlockId,
] )?.length,
numberOfDescendants: descendants,
userCanCreatePages: select( coreStore ).canUser(
'create',
'pages'
Expand All @@ -367,12 +372,15 @@ export default function NavigationLinkEdit( {
}, [ isTopLevelLink ] );

/**
* Insert a link block when submenu is added.
* Transform to submenu block.
*/
function insertLinkBlock() {
const insertionPoint = numberOfDescendants;
const blockToInsert = createBlock( 'core/navigation-link' );
insertBlock( blockToInsert, insertionPoint, clientId );
function transformToSubmenu() {
const newSubmenu = createBlock(
'core/navigation-submenu',
attributes,
innerBlocks
);
replaceBlock( clientId, newSubmenu );
}

// Show the LinkControl on mount if the URL is empty
Expand Down Expand Up @@ -596,7 +604,7 @@ export default function NavigationLinkEdit( {
name="submenu"
icon={ addSubmenu }
title={ __( 'Add submenu' ) }
onClick={ insertLinkBlock }
onClick={ transformToSubmenu }
/>
) }
</ToolbarGroup>
Expand Down
15 changes: 15 additions & 0 deletions packages/block-library/src/navigation-link/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { _x } from '@wordpress/i18n';
import { customLink as linkIcon } from '@wordpress/icons';
import { InnerBlocks } from '@wordpress/block-editor';
import { addFilter } from '@wordpress/hooks';
import { createBlock } from '@wordpress/blocks';

/**
* Internal dependencies
Expand Down Expand Up @@ -84,6 +85,20 @@ export const settings = {
},
},
],
transforms: {
to: [
{
type: 'block',
blocks: [ 'core/navigation-submenu' ],
transform: ( attributes, innerBlocks ) =>
createBlock(
'core/navigation-submenu',
attributes,
innerBlocks
),
},
],
},
};

// importing this file includes side effects. This is whitelisted in block-library/package.json under sideEffects
Expand Down
67 changes: 67 additions & 0 deletions packages/block-library/src/navigation-submenu/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"apiVersion": 2,
"name": "core/navigation-submenu",
"title": "Submenu",
"category": "design",
"parent": [
"core/navigation"
],
"description": "Add a submenu to your navigation.",
"textdomain": "default",
"attributes": {
"label": {
"type": "string"
},
"type": {
"type": "string"
},
"description": {
"type": "string"
},
"rel": {
"type": "string"
},
"id": {
"type": "number"
},
"opensInNewTab": {
"type": "boolean",
"default": false
},
"url": {
"type": "string"
},
"title": {
"type": "string"
},
"kind": {
"type": "string"
},
"isTopLevelItem": {
"type": "boolean"
}
},
"usesContext": [
"textColor",
"customTextColor",
"backgroundColor",
"customBackgroundColor",
"overlayTextColor",
"customOverlayTextColor",
"overlayBackgroundColor",
"customOverlayBackgroundColor",
"fontSize",
"customFontSize",
"showSubmenuIcon",
"openSubmenusOnClick",
"style"
],
"supports": {
"reusable": false,
"html": false,
"inserter": false
},
"viewScript": "file:./view.min.js",
"editorStyle": "wp-block-navigation-submenu-editor",
"style": "wp-block-navigation-submenu"
}
Loading