-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Page List: Invoke the convert to links modal on any interactions with the page list item block #47748
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
Page List: Invoke the convert to links modal on any interactions with the page list item block #47748
Changes from 3 commits
1e59424
bc2fe35
936cc34
6e159aa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,9 @@ | |
| }, | ||
| "hasChildren": { | ||
| "type": "boolean" | ||
| }, | ||
| "onSelect": { | ||
| "type": "function" | ||
| } | ||
| }, | ||
| "usesContext": [ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { Button, Modal } from '@wordpress/components'; | ||
| import { __ } from '@wordpress/i18n'; | ||
|
|
||
| export const convertDescription = __( | ||
| 'This menu is automatically kept in sync with pages on your site. You can manage the menu yourself by clicking "Edit" below.' | ||
| ); | ||
|
|
||
| export function ConvertToLinksModal( { onClick, disabled, closeModal } ) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had to convert this into a controlled component so it could be invoked from other places. |
||
| return ( | ||
| <Modal | ||
| onRequestClose={ closeModal } | ||
| title={ __( 'Edit this menu' ) } | ||
| className={ 'wp-block-page-list-modal' } | ||
| aria={ { | ||
| describedby: 'wp-block-page-list-modal__description', | ||
| } } | ||
| > | ||
| <p id={ 'wp-block-page-list-modal__description' }> | ||
| { convertDescription } | ||
| </p> | ||
| <div className="wp-block-page-list-modal-buttons"> | ||
| <Button variant="tertiary" onClick={ closeModal }> | ||
| { __( 'Cancel' ) } | ||
| </Button> | ||
| <Button | ||
| variant="primary" | ||
| disabled={ disabled } | ||
| onClick={ onClick } | ||
| > | ||
| { __( 'Edit' ) } | ||
| </Button> | ||
| </div> | ||
| </Modal> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,8 +8,8 @@ import classnames from 'classnames'; | |
| */ | ||
| import { createBlock } from '@wordpress/blocks'; | ||
| import { | ||
| InspectorControls, | ||
| BlockControls, | ||
| InspectorControls, | ||
| useBlockProps, | ||
| useInnerBlocksProps, | ||
| getColorClassName, | ||
|
|
@@ -18,12 +18,11 @@ import { | |
| } from '@wordpress/block-editor'; | ||
| import { | ||
| PanelBody, | ||
| ToolbarButton, | ||
| Spinner, | ||
| Notice, | ||
| ComboboxControl, | ||
| Button, | ||
| Modal, | ||
| ToolbarButton, | ||
| } from '@wordpress/components'; | ||
| import { __, sprintf } from '@wordpress/i18n'; | ||
| import { useMemo, useState, useEffect } from '@wordpress/element'; | ||
|
|
@@ -34,16 +33,15 @@ import { useSelect } from '@wordpress/data'; | |
| * Internal dependencies | ||
| */ | ||
| import { useConvertToNavigationLinks } from './use-convert-to-navigation-links'; | ||
| import { | ||
| convertDescription, | ||
| ConvertToLinksModal, | ||
| } from './convert-to-links-modal'; | ||
|
|
||
| // We only show the edit option when page count is <= MAX_PAGE_COUNT | ||
| // Performance of Navigation Links is not good past this value. | ||
| const MAX_PAGE_COUNT = 100; | ||
| const NOOP = () => {}; | ||
|
|
||
| const convertDescription = __( | ||
| 'This menu is automatically kept in sync with pages on your site. You can manage the menu yourself by clicking "Edit" below.' | ||
| ); | ||
|
|
||
| function BlockContent( { | ||
| blockProps, | ||
| innerBlocksProps, | ||
|
|
@@ -113,48 +111,6 @@ function BlockContent( { | |
| } | ||
| } | ||
|
|
||
| function ConvertToLinksModal( { onClick, disabled } ) { | ||
| const [ isOpen, setOpen ] = useState( false ); | ||
| const openModal = () => setOpen( true ); | ||
| const closeModal = () => setOpen( false ); | ||
|
|
||
| return ( | ||
| <> | ||
| <BlockControls group="other"> | ||
| <ToolbarButton title={ __( 'Edit' ) } onClick={ openModal }> | ||
| { __( 'Edit' ) } | ||
| </ToolbarButton> | ||
| </BlockControls> | ||
| { isOpen && ( | ||
| <Modal | ||
| onRequestClose={ closeModal } | ||
| title={ __( 'Edit this menu' ) } | ||
| className={ 'wp-block-page-list-modal' } | ||
| aria={ { | ||
| describedby: 'wp-block-page-list-modal__description', | ||
| } } | ||
| > | ||
| <p id={ 'wp-block-page-list-modal__description' }> | ||
| { convertDescription } | ||
| </p> | ||
| <div className="wp-block-page-list-modal-buttons"> | ||
| <Button variant="tertiary" onClick={ closeModal }> | ||
| { __( 'Cancel' ) } | ||
| </Button> | ||
| <Button | ||
| variant="primary" | ||
| disabled={ disabled } | ||
| onClick={ onClick } | ||
| > | ||
| { __( 'Edit' ) } | ||
| </Button> | ||
| </div> | ||
| </Modal> | ||
| ) } | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| export default function PageListEdit( { | ||
| context, | ||
| clientId, | ||
|
|
@@ -182,6 +138,10 @@ export default function PageListEdit( { | |
| pages?.length > 0 && | ||
| pages?.length <= MAX_PAGE_COUNT; | ||
|
|
||
| const [ isModalOpen, setModalOpen ] = useState( false ); | ||
| const openModal = () => setModalOpen( true ); | ||
| const closeModal = () => setModalOpen( false ); | ||
|
|
||
| const convertToNavigationLinks = useConvertToNavigationLinks( { | ||
| clientId, | ||
| pages, | ||
|
|
@@ -242,6 +202,10 @@ export default function PageListEdit( { | |
| title: page.title?.rendered, | ||
| link: page.url, | ||
| hasChildren, | ||
| onSelect: () => { | ||
| openModal(); | ||
| return false; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ); | ||
|
|
@@ -312,6 +276,11 @@ export default function PageListEdit( { | |
|
|
||
| return ( | ||
| <> | ||
| <BlockControls group="other"> | ||
| <ToolbarButton title={ __( 'Edit' ) } onClick={ openModal }> | ||
| { __( 'Edit' ) } | ||
| </ToolbarButton> | ||
| </BlockControls> | ||
| <InspectorControls> | ||
| { pagesTree.length > 0 && ( | ||
| <PanelBody> | ||
|
|
@@ -342,10 +311,11 @@ export default function PageListEdit( { | |
| </PanelBody> | ||
| ) } | ||
| </InspectorControls> | ||
| { allowConvertToLinks && ( | ||
| { allowConvertToLinks && isModalOpen && ( | ||
| <ConvertToLinksModal | ||
| disabled={ ! hasResolvedPages } | ||
| onClick={ convertToNavigationLinks } | ||
| closeModal={ closeModal } | ||
| /> | ||
| ) } | ||
| <BlockContent | ||
|
|
||
There was a problem hiding this comment.
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.