-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d989f56
commit 2e17725
Showing
11 changed files
with
192 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
packages/block-library/src/navigation-link-list/block.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"apiVersion": 2, | ||
"name": "core/navigation-link-list", | ||
"category": "design", | ||
"parent": [ "core/navigation" ], | ||
"supports": { | ||
"reusable": false, | ||
"html": false | ||
}, | ||
"editorStyle": "wp-block-navigation-link-list-editor", | ||
"style": "wp-block-navigation-link-list" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
InnerBlocks, | ||
BlockControls, | ||
InspectorControls, | ||
useBlockProps, | ||
__experimentalUseInnerBlocksProps as useInnerBlocksProps, | ||
store as blockEditorStore, | ||
} from '@wordpress/block-editor'; | ||
import { useSelect } from '@wordpress/data'; | ||
|
||
const ALLOWED_BLOCKS = [ 'core/navigation-link', 'core/spacer' ]; | ||
|
||
export default function NavigationLinksEdit( { clientId, isSelected } ) { | ||
const { | ||
isImmediateParentOfSelectedBlock, | ||
selectedBlockHasDescendants, | ||
} = useSelect( | ||
( select ) => { | ||
const { | ||
getClientIdsOfDescendants, | ||
hasSelectedInnerBlock, | ||
getSelectedBlockClientId, | ||
} = select( blockEditorStore ); | ||
const selectedBlockId = getSelectedBlockClientId(); | ||
return { | ||
isImmediateParentOfSelectedBlock: hasSelectedInnerBlock( | ||
clientId, | ||
false | ||
), | ||
selectedBlockHasDescendants: !! getClientIdsOfDescendants( [ | ||
selectedBlockId, | ||
] )?.length, | ||
}; | ||
}, | ||
[ clientId ] | ||
); | ||
|
||
const blockProps = useBlockProps( { | ||
className: 'wp-block-navigation__container', | ||
} ); | ||
|
||
const innerBlocksProps = useInnerBlocksProps( blockProps, { | ||
allowedBlocks: ALLOWED_BLOCKS, | ||
renderAppender: | ||
( isImmediateParentOfSelectedBlock && | ||
! selectedBlockHasDescendants ) || | ||
isSelected | ||
? InnerBlocks.DefaultAppender | ||
: false, | ||
__experimentalAppenderTagName: 'li', | ||
__experimentalCaptureToolbars: true, | ||
__experimentalLayout: { | ||
type: 'default', | ||
alignments: [], | ||
}, | ||
} ); | ||
|
||
return ( | ||
<> | ||
<BlockControls /> | ||
<InspectorControls /> | ||
<ul { ...innerBlocksProps } /> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __, _x } from '@wordpress/i18n'; | ||
import { customLink as linkIcon } from '@wordpress/icons'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import metadata from './block.json'; | ||
import edit from './edit'; | ||
import save from './save'; | ||
|
||
const { name } = metadata; | ||
|
||
export { metadata, name }; | ||
|
||
export const settings = { | ||
title: _x( 'List of links', 'block title' ), | ||
|
||
icon: linkIcon, | ||
|
||
description: __( 'Add a list of links to your navigation.' ), | ||
|
||
keywords: [ __( 'links' ) ], | ||
|
||
edit, | ||
|
||
save, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
/** | ||
* Renders the `core/navigation-link-list` block on server. | ||
* | ||
* @param array $block The parsed block. | ||
* | ||
* @return string Returns the block html. | ||
*/ | ||
function render_block_core_navigation_link_list( $attributes, $content, $block ) { | ||
|
||
if ( empty( $block->inner_blocks ) ) { | ||
return ''; | ||
} | ||
|
||
$inner_blocks_html = ''; | ||
foreach ( $block->inner_blocks as $inner_block ) { | ||
$inner_blocks_html .= $inner_block->render(); | ||
} | ||
|
||
return sprintf('<ul class="wp-block-navigation__container">%1$s</ul>', $inner_blocks_html); | ||
} | ||
|
||
/** | ||
* Register the navigation link list block. | ||
* | ||
* @uses render_block_core_navigation_link_list() | ||
* @throws WP_Error An WP_Error exception parsing the block definition. | ||
*/ | ||
function register_block_core_navigation_link_list() { | ||
register_block_type_from_metadata( | ||
__DIR__ . '/navigation-link-list', | ||
array( | ||
'render_callback' => 'render_block_core_navigation_link_list', | ||
) | ||
); | ||
} | ||
|
||
add_action( 'init', 'register_block_core_navigation_link_list' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { InnerBlocks } from '@wordpress/block-editor'; | ||
|
||
export default function save() { | ||
return <InnerBlocks.Content />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters