Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Prevent block selection on Page List Item blocks
  • Loading branch information
scruffian committed Feb 3, 2023
commit 936cc34dc9f4901cff2efc06d8370ace52e2efff
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ Displays a page inside a list of all pages. ([Source](https://github.com/WordPre
- **Name:** core/page-list-item
- **Category:** widgets
- **Supports:** ~~html~~, ~~inserter~~, ~~lock~~, ~~reusable~~
- **Attributes:** hasChildren, id, label, link, title
- **Attributes:** hasChildren, id, label, link, onSelect, title

## Paragraph

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,16 @@ function OffCanvasEditor(
} );
const selectEditorBlock = useCallback(
( event, blockClientId ) => {
updateBlockSelection( event, blockClientId );
setSelectedTreeId( blockClientId );
let blockSelectionResult;
if ( onSelect ) {
onSelect( getBlock( blockClientId ) );
blockSelectionResult = onSelect(
getBlock( blockClientId ),
event
);
}
if ( blockSelectionResult !== false ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is needed to allow blocks to prevent their selection.

updateBlockSelection( event, blockClientId );
setSelectedTreeId( blockClientId );
}
},
[ setSelectedTreeId, updateBlockSelection, onSelect, getBlock ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ const MainContent = ( {
isExpanded={ true }
LeafMoreMenu={ LeafMoreMenu }
description={ description }
onSelect={ ( block ) => {
const onSelect = block.attributes?.onSelect;
if ( onSelect ) {
// If this returns fasle, then the block won't be selected.
return onSelect( block );
}
} }
/>
);
};
Expand Down
3 changes: 3 additions & 0 deletions packages/block-library/src/page-list-item/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
},
"hasChildren": {
"type": "boolean"
},
"onSelect": {
"type": "function"
}
},
"usesContext": [
Expand Down
4 changes: 4 additions & 0 deletions packages/block-library/src/page-list/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ export default function PageListEdit( {
title: page.title?.rendered,
link: page.url,
hasChildren,
onSelect: () => {
openModal();
return false;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

returning false stops the block being selected in the canvas.

},
};
let item = null;
const children = getBlockList( page.id );
Expand Down