Skip to content

Commit

Permalink
Enable page list to expand in list view (#45776)
Browse files Browse the repository at this point in the history
* adds page list item to enable expansion in list view

* fixes the readme of the page list item block

* disable locking UI for page list item

* memoize the template and remove useless markup in page list

* page list item fixtures

* Removed copied over CSS, added better configuration to page list item json configuration.

Co-authored-by: Ben Dwyer <[email protected]>

* removed manual editing of core blocks docs

Co-authored-by: Ben Dwyer <[email protected]>
  • Loading branch information
draganescu and scruffian authored Nov 25, 2022
1 parent 9a4146f commit 9f2fe7d
Show file tree
Hide file tree
Showing 11 changed files with 250 additions and 117 deletions.
9 changes: 9 additions & 0 deletions docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,15 @@ Display a list of all pages. ([Source](https://github.com/WordPress/gutenberg/tr
- **Supports:** ~~html~~, ~~reusable~~
- **Attributes:** parentPageID

## Page List Item

Displays a page inside a list of all pages. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/page-list-item))

- **Name:** core/page-list-item
- **Category:** widgets
- **Supports:** ~~html~~, ~~inserter~~, ~~lock~~, ~~reusable~~
- **Attributes:** hasChildren, id, label, link, title

## Paragraph

Start with the basic building block of all narrative. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/paragraph))
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 @@ -67,6 +67,7 @@ import * as navigationSubmenu from './navigation-submenu';
import * as nextpage from './nextpage';
import * as pattern from './pattern';
import * as pageList from './page-list';
import * as pageListItem from './page-list-item';
import * as paragraph from './paragraph';
import * as postAuthor from './post-author';
import * as postAuthorName from './post-author-name';
Expand Down Expand Up @@ -155,6 +156,7 @@ const getAllBlocks = () =>
more,
nextpage,
pageList,
pageListItem,
pattern,
preformatted,
pullquote,
Expand Down
51 changes: 51 additions & 0 deletions packages/block-library/src/page-list-item/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "core/page-list-item",
"title": "Page List Item",
"category": "widgets",
"parent": [ "core/page-list" ],
"description": "Displays a page inside a list of all pages.",
"keywords": [ "page", "menu", "navigation" ],
"textdomain": "default",
"attributes": {
"id": {
"type": "number"
},
"label": {
"type": "string"
},
"title": {
"type": "string"
},
"link": {
"type": "string"
},
"hasChildren": {
"type": "boolean"
}
},
"usesContext": [
"textColor",
"customTextColor",
"backgroundColor",
"customBackgroundColor",
"overlayTextColor",
"customOverlayTextColor",
"overlayBackgroundColor",
"customOverlayBackgroundColor",
"fontSize",
"customFontSize",
"showSubmenuIcon",
"style",
"openSubmenusOnClick"
],
"supports": {
"reusable": false,
"html": false,
"lock": false,
"inserter": false
},
"editorStyle": "wp-block-page-list-editor",
"style": "wp-block-page-list"
}
94 changes: 94 additions & 0 deletions packages/block-library/src/page-list-item/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import { InnerBlocks } from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';

/**
* Internal dependencies
*/
import { ItemSubmenuIcon } from '../navigation-link/icons';

function useFrontPageId() {
return useSelect( ( select ) => {
const canReadSettings = select( coreStore ).canUser(
'read',
'settings'
);
if ( ! canReadSettings ) {
return undefined;
}

const site = select( coreStore ).getEntityRecord( 'root', 'site' );
return site?.show_on_front === 'page' && site?.page_on_front;
}, [] );
}

export default function PageListItemEdit( { context, attributes } ) {
const { id, label, link, hasChildren } = attributes;
const isNavigationChild = 'showSubmenuIcon' in context;
const frontPageId = useFrontPageId();
return (
<li
key={ id }
className={ classnames( 'wp-block-pages-list__item', {
'has-child': hasChildren,
'wp-block-navigation-item': isNavigationChild,
'open-on-click': context.openSubmenusOnClick,
'open-on-hover-click':
! context.openSubmenusOnClick && context.showSubmenuIcon,
'menu-item-home': id === frontPageId,
} ) }
>
{ hasChildren && context.openSubmenusOnClick ? (
<>
<button
className="wp-block-navigation-item__content wp-block-navigation-submenu__toggle"
aria-expanded="false"
>
{ label }
</button>
<span className="wp-block-page-list__submenu-icon wp-block-navigation__submenu-icon">
<ItemSubmenuIcon />
</span>
</>
) : (
<a
className={ classnames( 'wp-block-pages-list__item__link', {
'wp-block-navigation-item__content': isNavigationChild,
} ) }
href={ link }
>
{ label }
</a>
) }
{ hasChildren && (
<>
{ ! context.openSubmenusOnClick &&
context.showSubmenuIcon && (
<button
className="wp-block-navigation-item__content wp-block-navigation-submenu__toggle wp-block-page-list__submenu-icon wp-block-navigation__submenu-icon"
aria-expanded="false"
>
<ItemSubmenuIcon />
</button>
) }
<ul
className={ classnames( 'submenu-container', {
'wp-block-navigation__submenu-container':
isNavigationChild,
} ) }
>
<InnerBlocks />
</ul>
</>
) }
</li>
);
}
24 changes: 24 additions & 0 deletions packages/block-library/src/page-list-item/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* WordPress dependencies
*/
import { pages as icon } from '@wordpress/icons';

/**
* Internal dependencies
*/
import initBlock from '../utils/init-block';
import metadata from './block.json';
import edit from './edit.js';

const { name } = metadata;

export { metadata, name };

export const settings = {
__experimentalLabel: ( { label } ) => label,
icon,
example: {},
edit,
};

export const init = () => initBlock( { name, metadata, settings } );
6 changes: 6 additions & 0 deletions packages/block-library/src/page-list-item/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Internal dependencies
*/
import { init } from './';

export default init();
Loading

0 comments on commit 9f2fe7d

Please sign in to comment.